abilities
3 days ago
admin
3 days ago
compatibility
3 days ago
extensions
3 days ago
foopluginbase
3 days ago
public
3 days ago
thumbs
3 days ago
asset-manifest.php
3 days ago
class-attachment-filters.php
3 days ago
class-command-palette.php
3 days ago
class-foogallery-animated-gif-support.php
3 days ago
class-foogallery-attachment-custom-class.php
3 days ago
class-foogallery-attachment-filename.php
3 days ago
class-foogallery-attachment-type.php
3 days ago
class-foogallery-attachment.php
3 days ago
class-foogallery-cache.php
3 days ago
class-foogallery-common-fields.php
3 days ago
class-foogallery-crop-position.php
3 days ago
class-foogallery-datasource-media_library.php
3 days ago
class-foogallery-debug.php
3 days ago
class-foogallery-delayed-runtime-loader.php
3 days ago
class-foogallery-extensions-compatibility.php
3 days ago
class-foogallery-force-https.php
3 days ago
class-foogallery-lazyload.php
3 days ago
class-foogallery-license-constant-handler.php
3 days ago
class-foogallery-lightbox.php
3 days ago
class-foogallery-no-javascript-fallback.php
3 days ago
class-foogallery-paging.php
3 days ago
class-foogallery-password-protect.php
3 days ago
class-foogallery-sitemaps.php
3 days ago
class-foogallery-widget.php
3 days ago
class-foogallery.php
3 days ago
class-gallery-advanced-settings.php
3 days ago
class-il8n.php
3 days ago
class-override-thumbnail.php
3 days ago
class-posttypes.php
3 days ago
class-previews.php
3 days ago
class-retina.php
3 days ago
class-thumbnail-dimensions.php
3 days ago
class-thumbnails.php
3 days ago
class-version-check.php
3 days ago
constants.php
3 days ago
functions.php
3 days ago
gallery-management-functions.php
3 days ago
includes.php
3 days ago
index.php
3 days ago
render-functions.php
3 days ago
class-foogallery-datasource-media_library.php
356 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * The default Gallery Datasource which pulls attachments from the WP media library |
| 9 | */ |
| 10 | if ( ! class_exists( 'FooGallery_Datasource_MediaLibrary' ) ) { |
| 11 | |
| 12 | class FooGallery_Datasource_MediaLibrary { |
| 13 | /** |
| 14 | * Tracks whether the add-actions wrapper has been opened. |
| 15 | * |
| 16 | * @var bool |
| 17 | */ |
| 18 | private $add_actions_wrapper_open = false; |
| 19 | |
| 20 | function __construct() { |
| 21 | add_filter( 'foogallery_datasource_media_library_item_count', array( $this, 'get_gallery_attachment_count' ), 10, 2 ); |
| 22 | add_filter( 'foogallery_datasource_media_library_featured_image', array( $this, 'get_gallery_featured_attachment' ), 10, 2 ); |
| 23 | add_filter( 'foogallery_datasource_media_library_attachments', array( $this, 'get_gallery_attachments' ), 10, 2 ); |
| 24 | |
| 25 | if ( is_admin() ) { |
| 26 | add_action('foogallery_gallery_metabox_items_add', array($this, 'output_add_button'), 8, 1); |
| 27 | add_action('foogallery_gallery_metabox_items_add', array($this, 'close_add_button_wrapper'), 1000, 1); |
| 28 | add_action('foogallery_gallery_metabox_items_list', array($this, 'output_attachment_items'), 10, 1); |
| 29 | |
| 30 | add_action('foogallery_before_save_gallery', array($this, 'save_gallery_attachments'), 10, 2); |
| 31 | |
| 32 | add_filter( 'foogallery_admin_il8n', array( $this, 'add_admin_il8n_strings' ) ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the number of attachments used from the media library |
| 38 | * |
| 39 | * @param int $count |
| 40 | * @param FooGallery $foogallery |
| 41 | * |
| 42 | * @return int |
| 43 | */ |
| 44 | public function get_gallery_attachment_count( $count, $foogallery ) { |
| 45 | return sizeof( $foogallery->attachment_ids ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns an array of FooGalleryAttachments from the datasource |
| 50 | * |
| 51 | * @param array $attachments |
| 52 | * @param FooGallery $foogallery |
| 53 | * |
| 54 | * @return array(FooGalleryAttachment) |
| 55 | */ |
| 56 | public function get_gallery_attachments( $attachments, $foogallery ) { |
| 57 | $attachments = array(); |
| 58 | |
| 59 | if ( ! empty( $foogallery->attachment_ids ) ) { |
| 60 | $helper = new FooGallery_Datasource_MediaLibrary_Query_Helper(); |
| 61 | $attachments = $helper->query_attachments( $foogallery, array( |
| 62 | 'post__in' => $foogallery->attachment_ids |
| 63 | ) ); |
| 64 | } |
| 65 | |
| 66 | return $attachments; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the featured FooGalleryAttachment from the datasource |
| 71 | * |
| 72 | * @param FooGalleryAttachment $default |
| 73 | * @param FooGallery $foogallery |
| 74 | * |
| 75 | * @return bool|FooGalleryAttachment |
| 76 | */ |
| 77 | public function get_gallery_featured_attachment( $default, $foogallery ) { |
| 78 | //if no featured image could be found then get the first image |
| 79 | if ( $foogallery->attachment_ids ) { |
| 80 | $attachment_id_values = array_values( $foogallery->attachment_ids ); |
| 81 | $attachment_id = array_shift( $attachment_id_values ); |
| 82 | |
| 83 | return FooGalleryAttachment::get_by_id( $attachment_id ); |
| 84 | } |
| 85 | return $default; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Output the Add Media button |
| 90 | * @param $foogallery |
| 91 | */ |
| 92 | public function output_add_button( $foogallery ) { |
| 93 | if ( ! $this->add_actions_wrapper_open ) { |
| 94 | $this->add_actions_wrapper_open = true; |
| 95 | ?> |
| 96 | <div class="foogallery-add-actions"> |
| 97 | <?php |
| 98 | } |
| 99 | ?> |
| 100 | <button type="button" class="button button-primary button-hero upload_image_button" |
| 101 | data-uploader-title="<?php esc_attr_e( 'Add Media To Gallery', 'foogallery' ); ?>" |
| 102 | data-uploader-button-text="<?php esc_attr_e( 'Add Media', 'foogallery' ); ?>" |
| 103 | data-post-id="<?php echo esc_attr( $foogallery->ID ); ?>"> |
| 104 | <span class="dashicons dashicons-admin-media"></span> |
| 105 | <span class="foogallery-add-button-label"><?php esc_attr_e( 'Add From Media Library', 'foogallery' ); ?></span> |
| 106 | </button> |
| 107 | <button type="button" class="button button-secondary button-hero foogallery-upload-direct" |
| 108 | data-post-id="<?php echo esc_attr( $foogallery->ID ); ?>"> |
| 109 | <span class="dashicons dashicons-cloud-upload"></span> |
| 110 | <span class="foogallery-add-button-label"><?php esc_attr_e( 'Upload From Computer', 'foogallery' ); ?></span> |
| 111 | </button> |
| 112 | <?php |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Closes the add-actions wrapper once all buttons have rendered. |
| 117 | * |
| 118 | * @param FooGallery $foogallery |
| 119 | */ |
| 120 | public function close_add_button_wrapper( $foogallery ) { |
| 121 | if ( $this->add_actions_wrapper_open ) { |
| 122 | ?> |
| 123 | </div> |
| 124 | <p><?php esc_html_e( 'hint : you can also drag and drop images here to upload them!', 'foogallery' ); ?></p> |
| 125 | <?php |
| 126 | $this->add_actions_wrapper_open = false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Adds datasource-related admin localisation strings. |
| 132 | * |
| 133 | * @param array $strings |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | public function add_admin_il8n_strings( $strings ) { |
| 138 | if ( ! is_array( $strings ) ) { |
| 139 | $strings = array(); |
| 140 | } |
| 141 | |
| 142 | $strings['dropzone_message'] = __( 'Drop images here to upload', 'foogallery' ); |
| 143 | |
| 144 | return $strings; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Outputs the attachments for a gallery |
| 149 | * |
| 150 | * @param FooGallery $foogallery |
| 151 | */ |
| 152 | public function output_attachment_items($foogallery) { |
| 153 | //make sure the media assets are enqueued |
| 154 | wp_enqueue_media(); |
| 155 | |
| 156 | //always output the ability to add via media library |
| 157 | $has_attachments = $foogallery->has_attachments(); |
| 158 | $show_attachments = isset( $foogallery->datasource_name ) && 'media_library' === $foogallery->datasource_name; |
| 159 | |
| 160 | $media_button_start = foogallery_get_setting('add_media_button_start', '' ) === 'on'; |
| 161 | $attachment_ids = ''; |
| 162 | if ( $has_attachments && $show_attachments ) { |
| 163 | $attachment_ids = $foogallery->attachment_id_csv(); |
| 164 | } |
| 165 | ?> |
| 166 | <input type="hidden" data-foogallery-preview="include" name='foogallery_attachments' id="foogallery_attachments" value="<?php echo esc_attr( $attachment_ids ); ?>"/> |
| 167 | <div class="foogallery-attachments-list-container <?php echo esc_attr( $show_attachments && $has_attachments ? '' : 'foogallery-hidden' ); ?>"> |
| 168 | <ul class="foogallery-attachments-list <?php echo esc_attr( $media_button_start ? 'foogallery-add-media-button-start' : '' ); ?>"> |
| 169 | <?php if ( $media_button_start ) { |
| 170 | $this->render_add_media_button( $foogallery->ID ); |
| 171 | } ?> |
| 172 | <?php |
| 173 | //render all attachments that have been added to the gallery from the media library |
| 174 | if ( $has_attachments && $show_attachments ) { |
| 175 | foreach ( $foogallery->attachments() as $attachment ) { |
| 176 | $this->render_attachment_item( $attachment ); |
| 177 | } |
| 178 | } ?> |
| 179 | <?php if ( !$media_button_start ) { |
| 180 | $this->render_add_media_button( $foogallery->ID ); |
| 181 | } ?> |
| 182 | </ul> |
| 183 | <div style="clear: both;"></div> |
| 184 | <textarea style="display: none" id="foogallery-attachment-template"><?php $this->render_attachment_item(); ?></textarea> |
| 185 | <div class="foogallery-attachments-list-bar"> |
| 186 | <?php do_action('foogallery_attachments_list_bar_buttons', $foogallery ); ?> |
| 187 | |
| 188 | <button type="button" class="button button-primary button-large alignright upload_image_button" |
| 189 | data-uploader-title="<?php esc_attr_e( 'Add Media To Gallery', 'foogallery' ); ?>" |
| 190 | data-uploader-button-text="<?php esc_attr_e( 'Add Media', 'foogallery' ); ?>" |
| 191 | data-post-id="<?php echo esc_attr( $foogallery->ID ); ?>"> |
| 192 | <?php esc_attr_e( 'Add Media', 'foogallery' ); ?> |
| 193 | </button> |
| 194 | |
| 195 | <button type="button" class="button button-primary button-large alignright remove_all_media"> |
| 196 | <?php esc_attr_e( 'Remove All Media', 'foogallery' ); ?> |
| 197 | </button> |
| 198 | |
| 199 | </div> |
| 200 | </div> |
| 201 | <?php |
| 202 | } |
| 203 | |
| 204 | private function render_add_media_button( $foogallery_id) { |
| 205 | ?> |
| 206 | <li class="add-attachment datasource-medialibrary"> |
| 207 | <a href="#" data-uploader-title="<?php esc_attr_e( 'Add Media To Gallery', 'foogallery' ); ?>" |
| 208 | data-uploader-button-text="<?php esc_attr_e( 'Add Media', 'foogallery' ); ?>" |
| 209 | data-post-id="<?php echo esc_attr( $foogallery_id ); ?>" class="upload_image_button" |
| 210 | title="<?php esc_attr_e( 'Add From Media Library', 'foogallery' ); ?>"> |
| 211 | <div class="dashicons dashicons-plus"></div> |
| 212 | </a> |
| 213 | </li> |
| 214 | <?php |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Render the output for an item added from the media library |
| 219 | * @param FooGalleryAttachment $attachment_post |
| 220 | */ |
| 221 | public function render_attachment_item( $attachment_post = false ) { |
| 222 | if ( $attachment_post != false ) { |
| 223 | $attachment_id = $attachment_post->ID; |
| 224 | $attachment = wp_get_attachment_image_src( $attachment_id ); |
| 225 | $extra_class = apply_filters( 'foogallery_admin_render_gallery_item_extra_classes' , '', $attachment_post ); |
| 226 | $attachment_title = $attachment_post->title; |
| 227 | } else { |
| 228 | $attachment_id = $attachment = $extra_class = $attachment_title = ''; |
| 229 | } |
| 230 | |
| 231 | $data_attribute = empty($attachment_id) ? '' : "data-attachment-id=\"{$attachment_id}\""; |
| 232 | $img_tag = empty($attachment) ? '<img width="150" height="150" alt="" />' : "<img width=\"150\" height=\"150\" data-src=\"{$attachment[0]}\" alt=\"\" />"; |
| 233 | ?> |
| 234 | <li aria-label="<?php esc_attr_e( $attachment_title ); ?>" class="attachment details" <?php echo $data_attribute; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attribute safely built above ?>> |
| 235 | <div class="attachment-preview type-image <?php echo esc_attr( $extra_class ); ?>"> |
| 236 | <div class="thumbnail"> |
| 237 | <div class="centered"> |
| 238 | <?php echo $img_tag; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Image Tag safely built above ?> |
| 239 | </div> |
| 240 | </div> |
| 241 | <a class="info" href="#" title="<?php esc_attr_e( 'Edit Info', 'foogallery' ); ?>"> |
| 242 | <span class="dashicons dashicons-info"></span> |
| 243 | </a> |
| 244 | <a class="remove" href="#" title="<?php esc_attr_e( 'Remove from gallery', 'foogallery' ); ?>"> |
| 245 | <span class="dashicons dashicons-dismiss"></span> |
| 246 | </a> |
| 247 | </div> |
| 248 | </li> |
| 249 | <?php |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Save the attachments for the gallery |
| 254 | * @param $post_id |
| 255 | * @param $form_post |
| 256 | */ |
| 257 | public function save_gallery_attachments($post_id, $form_post) { |
| 258 | $datasource = foogallery_default_datasource(); |
| 259 | if ( isset( $_POST[FOOGALLERY_META_DATASOURCE] ) ) { |
| 260 | $datasource = $_POST[FOOGALLERY_META_DATASOURCE]; |
| 261 | } |
| 262 | if ( $datasource === foogallery_default_datasource() ) { |
| 263 | $attachments = apply_filters( 'foogallery_save_gallery_attachments', explode( ',', $_POST[FOOGALLERY_META_ATTACHMENTS] ), $post_id, $_POST ); |
| 264 | update_post_meta( $post_id, FOOGALLERY_META_ATTACHMENTS, $attachments ); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if ( ! class_exists( 'FooGallery_Datasource_MediaLibrary_Query_Helper' ) ) { |
| 271 | class FooGallery_Datasource_MediaLibrary_Query_Helper { |
| 272 | /** |
| 273 | * Build up the WP query and fetch the attachments using 'get_posts' |
| 274 | * @param FooGallery $foogallery |
| 275 | * |
| 276 | * @return array(FooGalleryAttachment) |
| 277 | */ |
| 278 | public function query_attachments( $foogallery, $query_args ) { |
| 279 | $attachments = array(); |
| 280 | |
| 281 | global $foogallery_force_sort; |
| 282 | $foogallery_force_sort = foogallery_sorting_get_effective_sort( $foogallery ); |
| 283 | |
| 284 | $attachment_query_args = array( |
| 285 | 'post_type' => 'attachment', |
| 286 | 'posts_per_page' => -1, |
| 287 | 'orderby' => foogallery_sorting_get_posts_orderby_arg( $foogallery_force_sort ), |
| 288 | 'order' => foogallery_sorting_get_posts_order_arg( $foogallery_force_sort ) |
| 289 | ); |
| 290 | |
| 291 | if ( !empty( $query_args ) ) { |
| 292 | $attachment_query_args = array_merge( $attachment_query_args, $query_args ); |
| 293 | } |
| 294 | |
| 295 | //allow for others to override the query args |
| 296 | $attachment_query_args = apply_filters( 'foogallery_attachment_get_posts_args', $attachment_query_args, $foogallery ); |
| 297 | |
| 298 | global $current_foogallery_arguments; |
| 299 | |
| 300 | if ( isset( $current_foogallery_arguments ) ) { |
| 301 | |
| 302 | //check if a sorting override has been applied |
| 303 | if ( isset( $current_foogallery_arguments['sort'] ) ) { |
| 304 | $attachment_query_args['orderby'] = foogallery_sorting_get_posts_orderby_arg( $foogallery_force_sort ); |
| 305 | $attachment_query_args['order'] = foogallery_sorting_get_posts_order_arg( $foogallery_force_sort ); |
| 306 | } |
| 307 | |
| 308 | //check if a limit has been applied |
| 309 | if ( isset( $current_foogallery_arguments['limit'] ) ) { |
| 310 | $attachment_query_args['posts_per_page'] = intval( $current_foogallery_arguments['limit'] ); |
| 311 | } |
| 312 | |
| 313 | //check if an offset has been applied |
| 314 | if ( isset( $current_foogallery_arguments['offset'] ) ) { |
| 315 | $attachment_query_args['offset'] = intval( $current_foogallery_arguments['offset'] ); |
| 316 | |
| 317 | $posts_per_page = isset( $attachment_query_args['posts_per_page'] ) ? intval( $attachment_query_args['posts_per_page'] ) : 0; |
| 318 | if ( $posts_per_page <= 0 ) { |
| 319 | $attachment_query_args['posts_per_page'] = PHP_INT_MAX; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | $attachment_query_args = apply_filters( 'foogallery_attachment_get_posts_args_after_overrides', $attachment_query_args, $foogallery ); |
| 325 | $attachment_query_args = foogallery_sorting_defer_query_args( $attachment_query_args, $foogallery_force_sort ); |
| 326 | |
| 327 | //setup intercepting actions |
| 328 | add_action( 'pre_get_posts', array( $this, 'force_suppress_filters' ), PHP_INT_MAX ); |
| 329 | |
| 330 | $attachment_posts = get_posts( $attachment_query_args ); |
| 331 | |
| 332 | //remove intercepting actions |
| 333 | remove_action( 'pre_get_posts', array( $this, 'force_suppress_filters' ), PHP_INT_MAX ); |
| 334 | |
| 335 | foreach ( $attachment_posts as $attachment_post ) { |
| 336 | $attachments[] = apply_filters( 'foogallery_attachment_load', FooGalleryAttachment::get( $attachment_post ), $foogallery ); |
| 337 | } |
| 338 | |
| 339 | return $attachments; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * This forces the attachments to be fetched without any other filters. |
| 344 | * Some plugins override attachment queries, so this is a preventative measure to ensure attachments are fetched correctly |
| 345 | * @param $query WP_Query |
| 346 | */ |
| 347 | public function force_suppress_filters( $query ) { |
| 348 | //only care about attachments |
| 349 | if ( array_key_exists( 'post_type', $query->query ) && |
| 350 | 'attachment' === $query->query['post_type'] ) { |
| 351 | $query->set( 'suppress_filters', true ); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 |