class-gallery-api.php
4 months ago
class-gallery-base.php
4 months ago
class-gallery-config.php
4 months ago
class-gallery-design.php
4 months ago
class-gallery-field-provider.php
4 months ago
class-gallery-images.php
4 months ago
class-gallery-lightbox.php
4 months ago
class-gallery-misc.php
4 months ago
class-gallery-paging.php
4 months ago
trait-gallery-ajax.php
4 months ago
trait-gallery-duplicate.php
4 months ago
trait-gallery-image-methods.php
4 months ago
trait-gallery-preview.php
4 months ago
trait-gallery-sanitize.php
4 months ago
trait-gallery-image-methods.php
1323 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Responsive Lightbox Galleries Image Methods Trait. |
| 8 | * |
| 9 | * Handles all image query, source resolution, and pagination operations |
| 10 | * for the Responsive_Lightbox_Galleries class. |
| 11 | * |
| 12 | * @trait Responsive_Lightbox_Galleries_Image_Methods |
| 13 | */ |
| 14 | trait Responsive_Lightbox_Galleries_Image_Methods { |
| 15 | |
| 16 | /** |
| 17 | * Get gallery images. |
| 18 | * |
| 19 | * @param int $gallery_id Gallery ID |
| 20 | * @param array $args Additional arguments |
| 21 | * @return array |
| 22 | */ |
| 23 | public function get_gallery_images( $gallery_id = 0, $args = [] ) { |
| 24 | $images = []; |
| 25 | $excluded = []; |
| 26 | |
| 27 | // get main instance |
| 28 | $rl = Responsive_Lightbox(); |
| 29 | |
| 30 | // get args |
| 31 | $defaults = array( |
| 32 | 'count_images' => false, |
| 33 | 'exclude' => false, |
| 34 | 'posts_per_page' => -1, |
| 35 | 'images_per_page' => 0, |
| 36 | 'page' => 1, |
| 37 | 'limit' => 0, |
| 38 | 'nopaging' => true, |
| 39 | 'image_size' => 'large', |
| 40 | 'thumbnail_size' => 'thumbnail', |
| 41 | 'pagination_type' => 'paged', |
| 42 | 'pagination_position' => 'bottom', |
| 43 | 'orderby' => 'menu_order', |
| 44 | 'order' => 'asc', |
| 45 | 'preview' => is_admin(), |
| 46 | 'preview_type' => 'update', |
| 47 | 'preview_page' => 1, |
| 48 | 'preview_per_page' => 20, |
| 49 | 'taxonomy' => $rl->folders->get_active_taxonomy(), |
| 50 | 'folder' => array( |
| 51 | 'id' => 0, |
| 52 | 'children' => null // do not change! |
| 53 | ) |
| 54 | ); |
| 55 | |
| 56 | // parse arguments |
| 57 | $args = wp_parse_args( apply_filters( 'rl_get_gallery_images_args', $args, $gallery_id ), $defaults ); |
| 58 | |
| 59 | // disable counting mode |
| 60 | if ( $args['preview'] ) |
| 61 | $args['count_images'] = false; |
| 62 | |
| 63 | // sanitize args |
| 64 | $args['exclude'] = (bool) ! empty( $args['exclude'] ); |
| 65 | $args['posts_per_page'] = ! empty( $args['posts_per_page'] ) ? (int) $args['posts_per_page'] : -1; |
| 66 | $args['nopaging'] = (bool) ! empty( $args['nopaging'] ); |
| 67 | |
| 68 | // check gallery post type |
| 69 | $valid_gallery_type = ( get_post_type( $gallery_id ) === 'rl_gallery' ); |
| 70 | |
| 71 | // is it rl_gallery? skip when counting mode is enabled |
| 72 | if ( $valid_gallery_type && ! $args['count_images'] ) { |
| 73 | $paging = get_post_meta( $gallery_id, '_rl_paging', true ); |
| 74 | |
| 75 | if ( isset( $paging['menu_item'] ) ) { |
| 76 | $pagination = $paging[$paging['menu_item']]; |
| 77 | |
| 78 | if ( $pagination['pagination'] ) { |
| 79 | $args['nopaging'] = false; |
| 80 | $args['images_per_page'] = $pagination['images_per_page']; |
| 81 | $args['pagination_type'] = $pagination['pagination_type']; |
| 82 | |
| 83 | // infinite type? |
| 84 | if ( $args['pagination_type'] === 'infinite' ) |
| 85 | $args['pagination_position'] = 'bottom'; |
| 86 | else |
| 87 | $args['pagination_position'] = $pagination['pagination_position']; |
| 88 | } else |
| 89 | $args['nopaging'] = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | global $pagenow; |
| 94 | |
| 95 | // is it preview? |
| 96 | if ( ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) && $gallery_id ) || ( isset( $_POST['action'] ) && $_POST['action'] === 'rl-get-preview-content' ) || ( wp_doing_ajax() && isset( $_POST['action'] ) && ( $_POST['action'] === 'rl-post-gallery-preview' || $_POST['action'] === 'rl-get-menu-content' ) ) ) |
| 97 | $args['images_per_page'] = 0; |
| 98 | |
| 99 | if ( isset( $_GET['rl_page'] ) ) |
| 100 | $args['page'] = (int) $_GET['rl_page']; |
| 101 | else |
| 102 | $args['page'] = (int) $args['page']; |
| 103 | |
| 104 | // is it rl_gallery? |
| 105 | if ( $valid_gallery_type ) { |
| 106 | // no need order in counting mode |
| 107 | if ( ! $args['count_images'] ) { |
| 108 | // get config metadata |
| 109 | $config_meta = get_post_meta( $gallery_id, '_rl_config', true ); |
| 110 | |
| 111 | // config order |
| 112 | if ( isset( $config_meta['menu_item'] ) ) { |
| 113 | $config = $config_meta[$config_meta['menu_item']]; |
| 114 | |
| 115 | $args['orderby'] = $config['orderby']; |
| 116 | $args['order'] = $config['order']; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // get images metadata |
| 121 | $data = get_post_meta( $gallery_id, '_rl_images', true ); |
| 122 | |
| 123 | // array? |
| 124 | if ( ! is_array( $data ) ) |
| 125 | $data = []; |
| 126 | |
| 127 | // get menu item |
| 128 | if ( ! empty( $this->menu_item ) ) |
| 129 | $menu_item = $this->menu_item; |
| 130 | elseif ( array_key_exists( 'menu_item', $data ) ) |
| 131 | $menu_item = $data['menu_item']; |
| 132 | else |
| 133 | $menu_item = 'media'; |
| 134 | |
| 135 | // Normalize stale/unavailable sources (e.g. saved "folders" while folders are disabled). |
| 136 | $image_fields = ( isset( $this->fields['images'] ) && is_array( $this->fields['images'] ) ) ? $this->fields['images'] : []; |
| 137 | if ( ! isset( $image_fields[$menu_item] ) || ! is_array( $image_fields[$menu_item] ) ) { |
| 138 | if ( isset( $image_fields['media'] ) ) |
| 139 | $menu_item = 'media'; |
| 140 | else { |
| 141 | $fallback_menu_item = key( $image_fields ); |
| 142 | $menu_item = is_string( $fallback_menu_item ) ? $fallback_menu_item : ''; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( $menu_item === '' ) |
| 147 | return []; |
| 148 | |
| 149 | // valid data? |
| 150 | if ( ! array_key_exists( $menu_item, $data ) ) |
| 151 | $data[$menu_item] = []; |
| 152 | |
| 153 | $has_preview_pagination = ! empty( $image_fields[$menu_item]['attachments']['preview']['pagination'] ); |
| 154 | if ( $args['preview'] && $has_preview_pagination ) { |
| 155 | if ( isset( $args['preview_page'] ) ) |
| 156 | $args['preview_page'] = (int) $args['preview_page']; |
| 157 | else |
| 158 | $args['preview_page'] = 1; |
| 159 | |
| 160 | $args['preview_per_page'] = (int) $args['preview_per_page']; |
| 161 | } |
| 162 | |
| 163 | switch ( $menu_item ) { |
| 164 | case 'media': |
| 165 | // check embed data |
| 166 | if ( ! empty( $data[$menu_item]['attachments']['embed'] ) ) { |
| 167 | $atts_args = [ |
| 168 | 'embed_keys' => array_keys( $data[$menu_item]['attachments']['embed'] ), |
| 169 | 'providers' => [ 'youtube', 'vimeo' ] |
| 170 | ]; |
| 171 | } else |
| 172 | $atts_args = []; |
| 173 | |
| 174 | // get attachment ids |
| 175 | $attachments = ! empty( $data[$menu_item]['attachments']['ids'] ) ? $this->check_attachments( array_unique( array_filter( $data[$menu_item]['attachments']['ids'] ) ), $atts_args ) : []; |
| 176 | |
| 177 | // filter attachments |
| 178 | $attachments = apply_filters( 'rl_get_gallery_images_attachments', $attachments, $atts_args ); |
| 179 | |
| 180 | // exclude any attachments? |
| 181 | if ( $args['exclude'] && ! empty( $data[$menu_item]['attachments']['exclude'] ) ) |
| 182 | $attachments = array_diff( $attachments, $data[$menu_item]['attachments']['exclude'] ); |
| 183 | |
| 184 | // check filtered attachments |
| 185 | $attachments = $this->check_attachments( $attachments, $atts_args ); |
| 186 | |
| 187 | // any attachments? |
| 188 | if ( $attachments ) { |
| 189 | if ( $args['limit'] ) |
| 190 | $counter = 0; |
| 191 | |
| 192 | foreach ( $attachments as $attachment_id ) { |
| 193 | // for counting mode get attachment id only |
| 194 | if ( $args['count_images'] ) |
| 195 | $images[] = $attachment_id; |
| 196 | else { |
| 197 | // embed? |
| 198 | if ( preg_match( '/^e\d+$/', $attachment_id ) === 1 ) { |
| 199 | $attachment_data = $data[$menu_item]['attachments']['embed'][$attachment_id]; |
| 200 | $attachment_data['type'] = 'embed'; |
| 201 | } else |
| 202 | $attachment_data = $attachment_id; |
| 203 | |
| 204 | // get attachment image data |
| 205 | $images[] = $this->get_gallery_image_src( $attachment_data, $args['image_size'], $args['thumbnail_size'] ); |
| 206 | |
| 207 | // limit attachments? |
| 208 | if ( $args['limit'] ) { |
| 209 | $counter++; |
| 210 | |
| 211 | // limit reached? |
| 212 | if ( $counter === $args['limit'] ) |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | case 'featured': |
| 221 | // only for featured frontend galleries |
| 222 | if ( ! is_admin() || wp_doing_ajax() ) { |
| 223 | // prepare featured fields |
| 224 | $this->fields['images']['featured'] = $this->prepare_featured_fields( $this->fields['images']['featured'] ); |
| 225 | } |
| 226 | |
| 227 | // copy arguments |
| 228 | $query_args = $args; |
| 229 | |
| 230 | // skip order for counting mode |
| 231 | if ( ! $args['count_images'] ) { |
| 232 | // prevent duplicating images order (config tab) with posts order (images tab), query will handle empty strings |
| 233 | if ( array_key_exists( 'post_orderby', $args ) ) |
| 234 | $query_args['orderby'] = $args['post_orderby']; |
| 235 | elseif ( array_key_exists( 'orderby', $data[$menu_item] ) ) |
| 236 | $query_args['orderby'] = $data[$menu_item]['orderby']; |
| 237 | else |
| 238 | $query_args['orderby'] = ''; |
| 239 | |
| 240 | if ( array_key_exists( 'post_order', $args ) ) |
| 241 | $query_args['order'] = $args['post_order']; |
| 242 | elseif ( array_key_exists( 'order', $data[$menu_item] ) ) |
| 243 | $query_args['order'] = $data[$menu_item]['order']; |
| 244 | else |
| 245 | $query_args['order'] = ''; |
| 246 | } |
| 247 | |
| 248 | // get attachment ids |
| 249 | $attachments = $this->gallery_query( array_merge( $data[$menu_item], $query_args ) ); |
| 250 | |
| 251 | // filter attachments |
| 252 | $attachments = apply_filters( 'rl_get_gallery_images_attachments', $attachments ); |
| 253 | |
| 254 | // exclude any attachments? |
| 255 | if ( $args['exclude'] && ! empty( $data[$menu_item]['attachments']['exclude'] ) ) |
| 256 | $attachments = array_diff( $attachments, $data[$menu_item]['attachments']['exclude'] ); |
| 257 | |
| 258 | // any attachments? |
| 259 | if ( $attachments ) { |
| 260 | if ( $args['limit'] ) |
| 261 | $counter = 0; |
| 262 | |
| 263 | foreach ( $attachments as $attachment_id ) { |
| 264 | // real attachment? |
| 265 | if ( ! wp_attachment_is_image( $attachment_id ) ) |
| 266 | continue; |
| 267 | |
| 268 | // for counting mode get attachment id only |
| 269 | if ( $args['count_images'] ) |
| 270 | $images[] = $attachment_id; |
| 271 | else { |
| 272 | // get attachment image data |
| 273 | $images[] = $this->get_gallery_image_src( $attachment_id, $args['image_size'], $args['thumbnail_size'] ); |
| 274 | |
| 275 | // limit attachments? |
| 276 | if ( $args['limit'] ) { |
| 277 | $counter++; |
| 278 | |
| 279 | // limit reached? |
| 280 | if ( $counter === $args['limit'] ) |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | break; |
| 287 | |
| 288 | case 'folders': |
| 289 | // is folders active? |
| 290 | if ( ! $rl->options['folders']['active'] ) |
| 291 | break; |
| 292 | |
| 293 | if ( ! array_key_exists( 'folder', $data[$menu_item] ) ) |
| 294 | $data[$menu_item]['folder'] = $defaults['folder']; |
| 295 | |
| 296 | // ajax requests |
| 297 | if ( is_string( $args['folder']['id'] ) ) |
| 298 | $args['folder']['id'] = (int) $args['folder']['id']; |
| 299 | |
| 300 | // not empty folder term id? |
| 301 | if ( ! empty( $args['folder']['id'] ) ) { |
| 302 | // get term |
| 303 | $term = get_term( $args['folder']['id'], $args['taxonomy'] ); |
| 304 | |
| 305 | // valid term? |
| 306 | if ( is_a( $term, 'WP_Term' ) ) |
| 307 | $folder_id = (int) $term->term_id; |
| 308 | else |
| 309 | $folder_id = (int) $data[$menu_item]['folder']['id']; |
| 310 | } else { |
| 311 | if ( isset( $_POST['action'] ) && $_POST['action'] === 'rl-get-preview-content' ) |
| 312 | $folder_id = $args['folder']['id']; |
| 313 | else |
| 314 | $folder_id = (int) $data[$menu_item]['folder']['id']; |
| 315 | } |
| 316 | |
| 317 | if ( $folder_id >= 0 ) { |
| 318 | $include_children = false; |
| 319 | |
| 320 | // null means folder was not changed |
| 321 | if ( $args['folder']['children'] === null ) { |
| 322 | if ( array_key_exists( 'children', $data[$menu_item]['folder'] ) && $data[$menu_item]['folder']['children'] === true ) |
| 323 | $include_children = true; |
| 324 | // overwritten by args |
| 325 | } else { |
| 326 | if ( is_string( $args['folder']['children'] ) ) { |
| 327 | if ( $args['folder']['children'] === 'true' ) |
| 328 | $include_children = true; |
| 329 | } elseif ( is_bool( $args['folder']['children'] ) ) { |
| 330 | if ( $args['folder']['children'] ) |
| 331 | $include_children = true; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if ( $folder_id === 0 ) { |
| 336 | if ( $include_children ) { |
| 337 | $all_folders = get_terms( |
| 338 | array( |
| 339 | 'taxonomy' => $args['taxonomy'], |
| 340 | 'hide_empty' => false, |
| 341 | 'fields' => 'ids', |
| 342 | 'hierarchical' => false, |
| 343 | 'number' => 0 |
| 344 | ) |
| 345 | ); |
| 346 | |
| 347 | $tax_query = array( |
| 348 | array( |
| 349 | 'relation' => 'OR', |
| 350 | array( |
| 351 | 'taxonomy' => $args['taxonomy'], |
| 352 | 'field' => 'term_id', |
| 353 | 'terms' => ( ! is_wp_error( $all_folders ) ) ? $all_folders : $folder_id, |
| 354 | 'include_children' => $include_children, |
| 355 | 'operator' => 'IN' |
| 356 | ), |
| 357 | array( |
| 358 | 'taxonomy' => $args['taxonomy'], |
| 359 | 'field' => 'term_id', |
| 360 | 'terms' => $folder_id, |
| 361 | 'include_children' => $include_children, |
| 362 | 'operator' => 'NOT EXISTS' |
| 363 | ) |
| 364 | ) |
| 365 | ); |
| 366 | } else { |
| 367 | $tax_query = array( |
| 368 | array( |
| 369 | 'taxonomy' => $args['taxonomy'], |
| 370 | 'field' => 'term_id', |
| 371 | 'terms' => $folder_id, |
| 372 | 'include_children' => $include_children, |
| 373 | 'operator' => 'NOT EXISTS' |
| 374 | ) |
| 375 | ); |
| 376 | } |
| 377 | } else { |
| 378 | $tax_query = array( |
| 379 | array( |
| 380 | 'taxonomy' => $args['taxonomy'], |
| 381 | 'field' => 'term_id', |
| 382 | 'terms' => $folder_id, |
| 383 | 'include_children' => $include_children, |
| 384 | 'operator' => 'IN' |
| 385 | ) |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | // prepare query arguments |
| 390 | $wp_query_args = array( |
| 391 | 'post_type' => 'attachment', |
| 392 | 'post_status' => 'inherit', |
| 393 | 'post_mime_type' => array( 'image/jpeg', 'image/gif', 'image/png' ), |
| 394 | 'nopaging' => true, |
| 395 | 'posts_per_page' => -1, |
| 396 | 'fields' => 'ids', |
| 397 | 'tax_query' => $tax_query |
| 398 | ); |
| 399 | |
| 400 | // is it preview? |
| 401 | if ( $args['preview'] ) { |
| 402 | $wp_query_args['posts_per_page'] = $args['preview_per_page']; |
| 403 | $wp_query_args['offset'] = ( $args['preview_page'] - 1 ) * $args['preview_per_page']; |
| 404 | $wp_query_args['nopaging'] = false; |
| 405 | } |
| 406 | |
| 407 | // run query |
| 408 | $query = new WP_Query( apply_filters( 'rl_folders_query_args', $wp_query_args ) ); |
| 409 | |
| 410 | // get attachment ids |
| 411 | $attachments = $query->get_posts(); |
| 412 | |
| 413 | // valid attachments? |
| 414 | if ( ! is_wp_error( $attachments ) ) { |
| 415 | // cast ids to int |
| 416 | $attachments = array_map( 'intval', $attachments ); |
| 417 | |
| 418 | // make sure to skip duplicates |
| 419 | $attachments = array_unique( $attachments ); |
| 420 | |
| 421 | // filter attachments |
| 422 | $attachments = apply_filters( 'rl_get_gallery_images_attachments', $attachments ); |
| 423 | |
| 424 | // exclude any attachments? |
| 425 | if ( $args['exclude'] && ! empty( $data[$menu_item]['attachments']['exclude'] ) ) |
| 426 | $attachments = array_diff( $attachments, $data[$menu_item]['attachments']['exclude'] ); |
| 427 | |
| 428 | // any attachments? |
| 429 | if ( $attachments ) { |
| 430 | if ( $args['limit'] ) |
| 431 | $counter = 0; |
| 432 | |
| 433 | foreach ( $attachments as $attachment_id ) { |
| 434 | // real attachment? |
| 435 | if ( ! wp_attachment_is_image( $attachment_id ) ) |
| 436 | continue; |
| 437 | |
| 438 | // for counting mode get attachment id only |
| 439 | if ( $args['count_images'] ) |
| 440 | $images[] = $attachment_id; |
| 441 | else { |
| 442 | // get attachment image data |
| 443 | $images[] = $this->get_gallery_image_src( $attachment_id, $args['image_size'], $args['thumbnail_size'] ); |
| 444 | |
| 445 | // limit attachments? |
| 446 | if ( $args['limit'] ) { |
| 447 | $counter++; |
| 448 | |
| 449 | // limit reached? |
| 450 | if ( $counter === $args['limit'] ) |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | break; |
| 459 | |
| 460 | case 'remote_library': |
| 461 | // is remote library active? |
| 462 | if ( ! $rl->options['remote_library']['active'] ) |
| 463 | break; |
| 464 | |
| 465 | // no media search phrase? |
| 466 | if ( ! isset( $args['media_search'] ) ) |
| 467 | $args['media_search'] = isset( $data[$menu_item]['media_search'] ) ? $data[$menu_item]['media_search'] : ''; |
| 468 | |
| 469 | // no media provider? |
| 470 | if ( ! isset( $args['media_provider'] ) ) |
| 471 | $args['media_provider'] = isset( $data[$menu_item]['media_provider'] ) ? $data[$menu_item]['media_provider'] : 'all'; |
| 472 | |
| 473 | // get remote images |
| 474 | $images = $rl->remote_library->get_remote_library_images( $args ); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // skip order for counting mode |
| 480 | if ( ! $args['count_images'] ) { |
| 481 | // config sort order |
| 482 | switch ( $args['orderby'] ) { |
| 483 | case 'id': |
| 484 | $sort = []; |
| 485 | |
| 486 | foreach ( $images as $key => $image ) { |
| 487 | // set sorting value |
| 488 | $sort[$key] = $image['id']; |
| 489 | } |
| 490 | |
| 491 | // sort |
| 492 | array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, SORT_NUMERIC, $images ); |
| 493 | break; |
| 494 | |
| 495 | case 'title': |
| 496 | $sort = []; |
| 497 | |
| 498 | if ( $valid_gallery_type ) { |
| 499 | // get lightbox data |
| 500 | $lightbox_meta = get_post_meta( $gallery_id, '_rl_lightbox', true ); |
| 501 | |
| 502 | // valid data? |
| 503 | if ( isset( $lightbox_meta['menu_item'] ) ) |
| 504 | $title_arg = $lightbox_meta[$lightbox_meta['menu_item']]['lightbox_image_title']; |
| 505 | else |
| 506 | $title_arg = $rl->options['settings']['gallery_image_title']; |
| 507 | } else |
| 508 | $title_arg = $rl->options['settings']['gallery_image_title']; |
| 509 | |
| 510 | $images_copy = $images; |
| 511 | |
| 512 | foreach ( $images_copy as $key => $image ) { |
| 513 | if ( $title_arg === 'global' ) |
| 514 | $images[$key]['title'] = $rl->frontend->get_attachment_title( $image['id'], $rl->options['settings']['gallery_image_title'] ); |
| 515 | elseif ( $title_arg === 'default' ) |
| 516 | $images[$key]['title'] = ''; |
| 517 | else |
| 518 | $images[$key]['title'] = $rl->frontend->get_attachment_title( $image['id'], $title_arg ); |
| 519 | |
| 520 | // set sorting value |
| 521 | $sort[$key] = function_exists( 'mb_strtolower' ) ? mb_strtolower( $images[$key]['title'] ) : strtolower( $images[$key]['title'] ); |
| 522 | } |
| 523 | |
| 524 | // sort |
| 525 | array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, SORT_STRING, $images ); |
| 526 | break; |
| 527 | |
| 528 | case 'post_date': |
| 529 | $sort = []; |
| 530 | |
| 531 | foreach ( $images as $key => $image ) { |
| 532 | // set sorting value |
| 533 | $sort[$key] = $image['date']; |
| 534 | } |
| 535 | |
| 536 | // sort |
| 537 | array_multisort( $sort, $args['order'] === 'asc' ? SORT_ASC : SORT_DESC, $images ); |
| 538 | break; |
| 539 | |
| 540 | case 'menu_order': |
| 541 | // do nothing |
| 542 | break; |
| 543 | |
| 544 | case 'rand': |
| 545 | shuffle( $images ); |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // filter images |
| 551 | $images = apply_filters( 'rl_get_gallery_images_array', $images, $gallery_id, $args ); |
| 552 | |
| 553 | // count number of images |
| 554 | $images_count = count( $images ); |
| 555 | |
| 556 | // no preview? |
| 557 | if ( ! $args['preview'] && ! $args['count_images'] && $args['limit'] === 0 ) |
| 558 | update_post_meta( $gallery_id, '_rl_images_count', $images_count ); |
| 559 | |
| 560 | // images pagination? |
| 561 | if ( $images && ! $args['nopaging'] && $args['images_per_page'] > 0 && ! $args['count_images'] ) { |
| 562 | // get part of images |
| 563 | $images = array_slice( $images, ( $args['page'] - 1 ) * $args['images_per_page'], $args['images_per_page'], true ); |
| 564 | |
| 565 | // pass gallery args |
| 566 | $this->gallery_args = $args; |
| 567 | $this->gallery_args['total'] = (int) ceil( $images_count / $args['images_per_page'] ); |
| 568 | |
| 569 | // remove actions to avoid issues with multiple galleries on single page |
| 570 | remove_action( 'rl_before_gallery', [ $this, 'do_pagination' ], 10 ); |
| 571 | remove_action( 'rl_after_gallery', [ $this, 'do_pagination' ], 10 ); |
| 572 | |
| 573 | // pagination position |
| 574 | if ( $args['pagination_position'] === 'top' ) |
| 575 | add_action( 'rl_before_gallery', [ $this, 'do_pagination' ], 10, 2 ); |
| 576 | elseif ( $args['pagination_position'] === 'bottom' ) |
| 577 | add_action( 'rl_after_gallery', [ $this, 'do_pagination' ], 10, 2 ); |
| 578 | else { |
| 579 | add_action( 'rl_before_gallery', [ $this, 'do_pagination' ], 10, 2 ); |
| 580 | add_action( 'rl_after_gallery', [ $this, 'do_pagination' ], 10, 2 ); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return apply_filters( 'rl_get_gallery_images', array_values( $images ), $gallery_id, $args ); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Create gallery pagination. |
| 589 | * |
| 590 | * @global object $wp |
| 591 | * |
| 592 | * @param array $args |
| 593 | * @param int $gallery_id |
| 594 | * @return void |
| 595 | */ |
| 596 | public function do_pagination( $args, $gallery_id ) { |
| 597 | global $wp; |
| 598 | |
| 599 | // get main instance |
| 600 | $rl = Responsive_Lightbox(); |
| 601 | |
| 602 | // get current action |
| 603 | $current_action = current_action(); |
| 604 | |
| 605 | if ( $current_action === 'rl_before_gallery' ) |
| 606 | $class = 'rl-pagination-top'; |
| 607 | elseif ( $current_action === 'rl_after_gallery' ) |
| 608 | $class = 'rl-pagination-bottom'; |
| 609 | else |
| 610 | $class = ''; |
| 611 | |
| 612 | // set base arguments |
| 613 | $base_args = [ 'rl_gallery_no' => $rl->frontend->get_data( 'gallery_no' ), 'rl_page' => '%#%' ]; |
| 614 | |
| 615 | if ( empty( $args['pagination_type'] ) ) |
| 616 | $args['pagination_type'] = 'paged'; |
| 617 | |
| 618 | // infinite scroll? |
| 619 | if ( $args['pagination_type'] === 'infinite' ) |
| 620 | $base_args['rl_lightbox_script'] = $rl->get_data( 'current_script' ); |
| 621 | |
| 622 | echo |
| 623 | '<div class="rl-pagination ' . esc_attr( $class ) . '"' . ( $args['pagination_type'] === 'infinite' ? ' data-button="' . esc_attr( $args['load_more'] ) . '"' : '' ) .'>' . |
| 624 | paginate_links( |
| 625 | [ |
| 626 | 'format' => '?rl_page=%#%', |
| 627 | 'base' => add_query_arg( $base_args, $args['pagination_type'] !== 'paged' ? get_permalink( $gallery_id ) : home_url( $wp->request ) ), |
| 628 | 'total' => $this->gallery_args['total'], |
| 629 | 'current' => $this->gallery_args['page'], |
| 630 | 'show_all' => false, |
| 631 | 'end_size' => 1, |
| 632 | 'mid_size' => 2, |
| 633 | 'prev_next' => true, |
| 634 | 'prev_text' => esc_html__( '« Previous', 'responsive-lightbox' ), |
| 635 | 'next_text' => esc_html__( 'Next »', 'responsive-lightbox' ), |
| 636 | 'type' => 'plain', |
| 637 | 'add_args' => '', |
| 638 | 'add_fragment' => '', |
| 639 | 'before_page_number' => '', |
| 640 | 'after_page_number' => '' |
| 641 | ] |
| 642 | ) . |
| 643 | '</div>' . ( $args['pagination_type'] === 'infinite' && $args['load_more'] === 'manually' ? '<div class="rl-gallery-button"><button class="rl-button rl-load-more">' . esc_html__( 'Load more', 'responsive-lightbox' ) . '</button></div>' : '' ); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Get gallery image link. |
| 648 | * |
| 649 | * @param array $image Image data |
| 650 | * @param string $size Image size |
| 651 | * @param array $attr Image attributes |
| 652 | * @return string |
| 653 | */ |
| 654 | public function get_gallery_image_link( $image, $size = 'thumbnail', $attr = [] ) { |
| 655 | $link = ''; |
| 656 | |
| 657 | if ( $size === 'thumbnail' ) { |
| 658 | $url = $image['thumbnail_url']; |
| 659 | $width = $image['thumbnail_width']; |
| 660 | $height = $image['thumbnail_height']; |
| 661 | } else { |
| 662 | $url = $image['url']; |
| 663 | $width = $image['width']; |
| 664 | $height = $image['height']; |
| 665 | } |
| 666 | |
| 667 | if ( ! empty( $image['url'] ) ) { |
| 668 | $size_class = $size; |
| 669 | |
| 670 | if ( is_array( $size_class ) ) |
| 671 | $size_class = join( 'x', $size_class ); |
| 672 | |
| 673 | // combine attributes |
| 674 | $attr = wp_parse_args( |
| 675 | $attr, |
| 676 | array( |
| 677 | 'src' => $url, |
| 678 | 'class' => 'attachment-' . $size_class . ' size-' . $size_class . ' format-' . ( $height > $width ? 'portrait' : 'landscape' ), |
| 679 | 'alt' => $image['alt'] |
| 680 | ) |
| 681 | ); |
| 682 | |
| 683 | // apply filters if any |
| 684 | $attr = apply_filters( 'rl_get_gallery_image_attributes', $attr, $image, $size ); |
| 685 | |
| 686 | // start link output |
| 687 | $link = rtrim( '<img ' . image_hwstring( $width, $height ) ); |
| 688 | |
| 689 | // add attributes |
| 690 | foreach ( $attr as $name => $value ) { |
| 691 | $link .= ' ' . esc_attr( $name ) . '="' . ( $name === 'src' ? esc_url( $value ) : esc_attr( $value ) ) . '"'; |
| 692 | } |
| 693 | |
| 694 | // end link output |
| 695 | $link .= ' />'; |
| 696 | } |
| 697 | |
| 698 | return apply_filters( 'rl_get_gallery_image_link', $link, $image, $size ); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Get attachment image source. |
| 703 | * |
| 704 | * @param int|string|array $image Attachment ID, image URL or array of image data |
| 705 | * @param string $image_size Image size |
| 706 | * @param string $thumbnail_size Thumbnail size |
| 707 | * @return array |
| 708 | */ |
| 709 | public function get_gallery_image_src( $image, $image_size = 'large', $thumbnail_size = 'thumbnail' ) { |
| 710 | $imagedata = []; |
| 711 | |
| 712 | // check difference in size between image and thumbnail |
| 713 | $diff_sizes = $thumbnail_size !== $image_size; |
| 714 | |
| 715 | // attachment id? |
| 716 | if ( is_int( $image ) ) { |
| 717 | if ( $image ) { |
| 718 | $type = 'image'; |
| 719 | $width = 0; |
| 720 | $height = 0; |
| 721 | |
| 722 | // image src |
| 723 | if ( wp_attachment_is_image( $image ) ) { |
| 724 | $image_src = wp_get_attachment_image_src( $image, $image_size, false ); |
| 725 | |
| 726 | // different image and thumbnail sizes? |
| 727 | if ( $diff_sizes ) |
| 728 | $thumbnail_src = wp_get_attachment_image_src( $image, $thumbnail_size, false ); |
| 729 | else |
| 730 | $thumbnail_src = $image_src; |
| 731 | |
| 732 | $file_url = $image_src[0]; |
| 733 | $width = $image_src[1]; |
| 734 | $height = $image_src[2]; |
| 735 | $thumbnail_url = $thumbnail_src[0]; |
| 736 | $thumbnail_width = $thumbnail_src[1]; |
| 737 | $thumbnail_height = $thumbnail_src[2]; |
| 738 | // video, blank thumbnail src |
| 739 | } elseif ( rl_current_lightbox_supports( 'video' ) && wp_attachment_is( 'video', $image ) ) { |
| 740 | $type = 'video'; |
| 741 | $thumbnail_id = $this->get_video_thumbnail_id( $image ); |
| 742 | $thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, $image_size, false ); |
| 743 | |
| 744 | // get video metadata |
| 745 | $meta = wp_get_attachment_metadata( $image ); |
| 746 | |
| 747 | if ( $meta ) { |
| 748 | $width = $meta['width']; |
| 749 | $height = $meta['height']; |
| 750 | } else { |
| 751 | $width = $thumbnail_src[1]; |
| 752 | $height = $thumbnail_src[2]; |
| 753 | } |
| 754 | |
| 755 | // different image and thumbnail sizes? |
| 756 | if ( $diff_sizes ) |
| 757 | $thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, $thumbnail_size, false ); |
| 758 | |
| 759 | // file url |
| 760 | $file_url = wp_get_attachment_url( $image ); |
| 761 | $thumbnail_url = $thumbnail_src[0]; |
| 762 | $thumbnail_width = $thumbnail_src[1]; |
| 763 | $thumbnail_height = $thumbnail_src[2]; |
| 764 | } |
| 765 | |
| 766 | // get alternative text |
| 767 | $alt = get_post_meta( $image, '_wp_attachment_image_alt', true ); |
| 768 | |
| 769 | // allow only strings |
| 770 | if ( ! is_string( $alt ) ) |
| 771 | $alt = ''; |
| 772 | |
| 773 | $imagedata = array( |
| 774 | 'id' => $image, |
| 775 | 'title' => get_the_title( $image ), |
| 776 | 'date' => get_the_date( 'Y-m-d H:i:s', $image ), |
| 777 | 'caption' => '', |
| 778 | 'alt' => $alt, |
| 779 | 'url' => $file_url, // $image_src[0], |
| 780 | 'width' => $width, |
| 781 | 'height' => $height, |
| 782 | 'orientation' => $height > $width ? 'portrait' : 'landscape', |
| 783 | 'thumbnail_url' => $thumbnail_url, |
| 784 | 'thumbnail_width' => $thumbnail_width, |
| 785 | 'thumbnail_height' => $thumbnail_height, |
| 786 | 'type' => $type |
| 787 | ); |
| 788 | |
| 789 | if ( $diff_sizes ) |
| 790 | $imagedata['thumbnail_orientation'] = $thumbnail_src[2] > $thumbnail_src[1] ? 'portrait' : 'landscape'; |
| 791 | else |
| 792 | $imagedata['thumbnail_orientation'] = $imagedata['orientation']; |
| 793 | } |
| 794 | // image url |
| 795 | } elseif ( is_string( $image ) ) { |
| 796 | $imagedata['url'] = $image; |
| 797 | |
| 798 | @list( $imagedata['width'], $imagedata['height'] ) = rl_get_image_size_by_url( $imagedata['url'] ); |
| 799 | |
| 800 | $imagedata = array( |
| 801 | 'id' => 0, |
| 802 | 'title' => '', |
| 803 | 'date' => '', |
| 804 | 'caption' => '', |
| 805 | 'alt' => '', |
| 806 | 'url' => $imagedata['url'], |
| 807 | 'width' => $imagedata['width'], |
| 808 | 'height' => $imagedata['height'], |
| 809 | 'orientation' => $imagedata['height'] > $imagedata['width'] ? 'portrait' : 'landscape', |
| 810 | 'thumbnail_url' => $imagedata['url'], |
| 811 | 'thumbnail_width' => $imagedata['width'], |
| 812 | 'thumbnail_height' => $imagedata['height'], |
| 813 | 'type' => 'image' |
| 814 | ); |
| 815 | |
| 816 | $imagedata['thumbnail_orientation'] = $imagedata['orientation']; |
| 817 | // full image array |
| 818 | } elseif ( is_array( $image ) ) { |
| 819 | // set width and height from url, if not available |
| 820 | if ( empty( $image['width'] ) || empty( $image['height'] ) ) |
| 821 | @list( $image['width'], $image['height'] ) = rl_get_image_size_by_url( $image['url'] ); |
| 822 | |
| 823 | // set thumbnail data, if not available |
| 824 | if ( empty( $image['thumbnail_url'] ) ) { |
| 825 | $image['thumbnail_url'] = $image['url']; |
| 826 | $image['thumbnail_width'] = $image['width']; |
| 827 | $image['thumbnail_height'] = $image['height']; |
| 828 | } else { |
| 829 | // set thumbnail width and height from url, if not available |
| 830 | if ( empty( $image['thumbnail_width'] ) || empty( $image['thumbnail_height'] ) ) |
| 831 | @list( $image['thumbnail_width'], $image['thumbnail_height'] ) = rl_get_image_size_by_url( $image['thumbnail_url'] ); |
| 832 | } |
| 833 | |
| 834 | $imagedata = array( |
| 835 | 'id' => ! empty( $image['id'] ) ? ( preg_match( '/^e\d+$/', $image['id'] ) === 1 ? $image['id'] : (int) $image['id'] ) : 0, |
| 836 | 'title' => ! empty( $image['title'] ) ? ( $image['title'] ) : '', |
| 837 | 'date' => ! empty( $image['date'] ) ? ( $image['date'] ) : '', |
| 838 | 'caption' => ! empty( $image['caption'] ) ? ( $image['caption'] ) : '', |
| 839 | 'alt' => ! empty( $image['alt'] ) ? ( $image['alt'] ) : '', |
| 840 | 'url' => ! empty( $image['url'] ) ? esc_url_raw( $image['url'] ) : '', |
| 841 | 'width' => ! empty( $image['width'] ) ? (int) $image['width'] : 0, |
| 842 | 'height' => ! empty( $image['height'] ) ? (int) $image['height'] : 0, |
| 843 | 'thumbnail_url' => ! empty( $image['thumbnail_url'] ) ? esc_url_raw( $image['thumbnail_url'] ) : '', |
| 844 | 'thumbnail_width' => ! empty( $image['thumbnail_width'] ) ? (int) $image['thumbnail_width'] : 0, |
| 845 | 'thumbnail_height' => ! empty( $image['thumbnail_height'] ) ? (int) $image['thumbnail_height'] : 0, |
| 846 | 'link' => ! empty( $image['link'] ) ? esc_url_raw( $image['link'] ) : '', |
| 847 | 'thumbnail_link' => ! empty( $image['thumbnail_link'] ) ? esc_url_raw( $image['thumbnail_link'] ) : '', |
| 848 | 'type' => ! empty( $image['type'] ) ? ( $image['type'] ) : 'image' |
| 849 | ); |
| 850 | |
| 851 | $imagedata['orientation'] = $imagedata['height'] > $imagedata['width'] ? 'portrait' : 'landscape'; |
| 852 | $imagedata['thumbnail_orientation'] = $imagedata['thumbnail_height'] > $imagedata['thumbnail_width'] ? 'portrait' : 'landscape'; |
| 853 | } |
| 854 | |
| 855 | if ( ! empty( $imagedata ) ) { |
| 856 | // link does not exist? |
| 857 | if ( empty( $imagedata['link'] ) ) |
| 858 | $imagedata['link'] = $this->get_gallery_image_link( $imagedata, $image_size ); |
| 859 | |
| 860 | // thumbnail link does not exist? |
| 861 | if ( empty( $imagedata['thumbnail_link'] ) ) { |
| 862 | // different image and thumbnail sizes? |
| 863 | if ( $diff_sizes ) |
| 864 | $imagedata['thumbnail_link'] = $this->get_gallery_image_link( $imagedata, $thumbnail_size ); |
| 865 | else |
| 866 | $imagedata['thumbnail_link'] = $imagedata['link']; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return apply_filters( 'rl_get_gallery_image_src', $imagedata, $image, $image_size, $thumbnail_size ); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Get gallery featured image. |
| 875 | * |
| 876 | * @param int $gallery_id |
| 877 | * @param string $size Image size |
| 878 | * @param array $attr Image attributes |
| 879 | * @return string |
| 880 | */ |
| 881 | public function get_featured_image( $gallery_id, $size = 'thumbnail', $attr = [] ) { |
| 882 | $image = $this->get_featured_image_src( $gallery_id ); |
| 883 | $html = ''; |
| 884 | |
| 885 | if ( $image ) |
| 886 | $html = $this->get_gallery_image_link( $this->get_gallery_image_src( $image, 'large', $size ), $size, $attr ); |
| 887 | |
| 888 | return apply_filters( 'rl_get_featured_image', $html, $gallery_id, $size ); |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * Get gallery featured image data. |
| 893 | * |
| 894 | * @param int $gallery_id |
| 895 | * @return array |
| 896 | */ |
| 897 | public function get_featured_image_src( $gallery_id ) { |
| 898 | // get featured image data |
| 899 | $featured_image_type = get_post_meta( $gallery_id, '_rl_featured_image_type', true ); |
| 900 | $featured_image = get_post_meta( $gallery_id, '_rl_featured_image', true ); |
| 901 | |
| 902 | switch ( $featured_image_type ) { |
| 903 | // custom url |
| 904 | case 'url': |
| 905 | $frontend = function_exists( 'Responsive_Lightbox' ) ? Responsive_Lightbox()->frontend : null; |
| 906 | if ( $frontend && method_exists( $frontend, 'sanitize_remote_image_url' ) ) |
| 907 | $featured_image = $frontend->sanitize_remote_image_url( $featured_image ); |
| 908 | else |
| 909 | $featured_image = ''; |
| 910 | |
| 911 | if ( $featured_image !== '' ) { |
| 912 | $image = esc_url( $featured_image ); |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | $image = $this->get_first_gallery_featured_image( $gallery_id ); |
| 917 | break; |
| 918 | |
| 919 | // attachment id |
| 920 | case 'id': |
| 921 | $featured_image = (int) $featured_image; |
| 922 | $image = wp_attachment_is_image( $featured_image ) ? $featured_image : $this->maybe_generate_thumbnail(); |
| 923 | break; |
| 924 | |
| 925 | // first image |
| 926 | case 'image': |
| 927 | default: |
| 928 | $image = $this->get_first_gallery_featured_image( $gallery_id ); |
| 929 | } |
| 930 | |
| 931 | return apply_filters( 'rl_get_featured_image_src', $image, $gallery_id, $featured_image_type, $featured_image ); |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Helper to fetch the first gallery image data. |
| 936 | * |
| 937 | * @param int $gallery_id |
| 938 | * @return array|int |
| 939 | */ |
| 940 | protected function get_first_gallery_featured_image( $gallery_id ) { |
| 941 | $images = $this->get_gallery_images( |
| 942 | $gallery_id, |
| 943 | [ |
| 944 | 'exclude' => true, |
| 945 | 'limit' => 1 |
| 946 | ] |
| 947 | ); |
| 948 | |
| 949 | if ( $images ) |
| 950 | return reset( $images ); |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * Get featured gallery attachments. |
| 957 | * |
| 958 | * @param array $args |
| 959 | * @return array |
| 960 | */ |
| 961 | public function gallery_query( $args ) { |
| 962 | $attachments = []; |
| 963 | |
| 964 | // get fields |
| 965 | $fields = $this->fields['images']['featured']; |
| 966 | |
| 967 | // force these settings |
| 968 | $args['fields'] = 'ids'; |
| 969 | $args['tax_query'] = []; |
| 970 | $args['meta_query'] = []; |
| 971 | $args['author__in'] = []; |
| 972 | $args['post_parent__in'] = []; |
| 973 | |
| 974 | // get image source |
| 975 | $args['image_source'] = isset( $args['image_source'] ) && array_key_exists( $args['image_source'], $fields['image_source']['options'] ) ? $args['image_source'] : $fields['image_source']['default']; |
| 976 | |
| 977 | // get images per post |
| 978 | $args['images_per_post'] = isset( $args['images_per_post'] ) ? absint( $args['images_per_post'] ) : $fields['images_per_post']['default']; |
| 979 | |
| 980 | // get number of posts |
| 981 | $args['number_of_posts'] = isset( $args['number_of_posts'] ) ? (int) $args['number_of_posts'] : $fields['number_of_posts']['default']; |
| 982 | |
| 983 | // get all posts? |
| 984 | if ( $args['number_of_posts'] <= 0 ) |
| 985 | $args['number_of_posts'] = -1; |
| 986 | |
| 987 | // convert to wp query arg |
| 988 | $args['posts_per_page'] = $args['number_of_posts']; |
| 989 | |
| 990 | $args['order'] = isset( $args['order'] ) && array_key_exists( $args['order'], $fields['order']['options'] ) ? $args['order'] : $fields['order']['default']; |
| 991 | $args['orderby'] = isset( $args['orderby'] ) && array_key_exists( $args['orderby'], $fields['orderby']['options'] ) ? $args['orderby'] : $fields['orderby']['default']; |
| 992 | $args['offset'] = isset( $args['offset'] ) ? absint( $args['offset'] ) : 0; |
| 993 | |
| 994 | $tax_queries = array( |
| 995 | 'post_format' => [], |
| 996 | 'post_term' => [] |
| 997 | ); |
| 998 | |
| 999 | $meta_queries = array( |
| 1000 | 'page_template' => [], |
| 1001 | 'image_source' => [] |
| 1002 | ); |
| 1003 | |
| 1004 | // post type |
| 1005 | if ( ! empty( $args['post_type'] ) ) { |
| 1006 | // assign post types |
| 1007 | $post_types = $args['post_type']; |
| 1008 | |
| 1009 | // clear post types |
| 1010 | $args['post_type'] = []; |
| 1011 | |
| 1012 | foreach ( $post_types as $post_type ) { |
| 1013 | if ( array_key_exists( $post_type, $fields['post_type']['options'] ) ) |
| 1014 | $args['post_type'][] = $post_type; |
| 1015 | } |
| 1016 | } else |
| 1017 | $args['post_type'] = $this->get_post_types( true ); |
| 1018 | |
| 1019 | // post status |
| 1020 | if ( ! empty( $args['post_status'] ) ) { |
| 1021 | // assign post statuses |
| 1022 | $post_statuses = $args['post_status']; |
| 1023 | |
| 1024 | // clear post statuses |
| 1025 | $args['post_status'] = []; |
| 1026 | |
| 1027 | foreach ( $post_statuses as $post_status ) { |
| 1028 | if ( array_key_exists( $post_status, $fields['post_status']['options'] ) ) |
| 1029 | $args['post_status'][] = $post_status; |
| 1030 | } |
| 1031 | } else { |
| 1032 | // Keep legacy behavior: empty selection means no post-status filtering in UI terms. |
| 1033 | $args['post_status'] = array_keys( $fields['post_status']['options'] ); |
| 1034 | } |
| 1035 | |
| 1036 | // Defensive fallback if options could not be prepared yet. |
| 1037 | if ( empty( $args['post_status'] ) ) { |
| 1038 | $args['post_status'] = [ 'publish' ]; |
| 1039 | } |
| 1040 | |
| 1041 | // post format |
| 1042 | if ( ! empty( $args['post_format'] ) ) { |
| 1043 | // assign post formats |
| 1044 | $post_formats = $args['post_format']; |
| 1045 | |
| 1046 | foreach ( $post_formats as $post_format ) { |
| 1047 | if ( array_key_exists( $post_format, $fields['post_format']['options'] ) ) { |
| 1048 | // standard format? |
| 1049 | if ( $post_format === 'standard' ) { |
| 1050 | $tax_queries['post_format'][] = array( |
| 1051 | 'relation' => 'OR', |
| 1052 | array( |
| 1053 | 'taxonomy' => 'post_format', |
| 1054 | 'field' => 'slug', |
| 1055 | 'terms' => array( 'post-format-standard' ) |
| 1056 | ), |
| 1057 | array( |
| 1058 | 'taxonomy' => 'post_format', |
| 1059 | 'field' => 'slug', |
| 1060 | 'operator' => 'NOT EXISTS' |
| 1061 | ) |
| 1062 | ); |
| 1063 | } else { |
| 1064 | $tax_queries['post_format'][] = array( |
| 1065 | 'taxonomy' => 'post_format', |
| 1066 | 'field' => 'slug', |
| 1067 | 'terms' => array( 'post-format-' . $post_format ) |
| 1068 | ); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | unset( $args['post_format'] ); |
| 1074 | } |
| 1075 | |
| 1076 | // page template |
| 1077 | if ( ! empty( $args['page_template'] ) ) { |
| 1078 | foreach ( $args['page_template'] as $page_template ) { |
| 1079 | if ( array_key_exists( $page_template, $fields['page_template']['options'] ) ) { |
| 1080 | if ( $page_template === 'default' ) { |
| 1081 | $meta_queries['page_template'][] = array( |
| 1082 | 'relation' => 'OR', |
| 1083 | array( |
| 1084 | 'key' => '_wp_page_template', |
| 1085 | 'value' => 'default' |
| 1086 | ), |
| 1087 | array( |
| 1088 | 'key' => '_wp_page_template', |
| 1089 | 'value' => '' |
| 1090 | ), |
| 1091 | array( |
| 1092 | 'key' => '_wp_page_template', |
| 1093 | 'compare' => 'NOT EXISTS' |
| 1094 | ) |
| 1095 | ); |
| 1096 | } else { |
| 1097 | $meta_queries['page_template'][] = array( |
| 1098 | 'key' => '_wp_page_template', |
| 1099 | 'value' => $page_template |
| 1100 | ); |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | // post author |
| 1107 | if ( ! empty( $args['post_author'] ) ) { |
| 1108 | foreach ( $args['post_author'] as $post_author ) { |
| 1109 | if ( array_key_exists( $post_author, $fields['post_author']['options'] ) ) |
| 1110 | $args['author__in'][] = $post_author; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | // page parent |
| 1115 | if ( ! empty( $args['page_parent'] ) ) { |
| 1116 | foreach ( $args['page_parent'] as $page_parent ) { |
| 1117 | if ( array_key_exists( $page_parent, $fields['page_parent']['options'] ) ) |
| 1118 | $args['post_parent__in'][] = $page_parent; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | // post term |
| 1123 | if ( ! empty( $args['post_term'] ) ) { |
| 1124 | $terms = []; |
| 1125 | |
| 1126 | // get all terms |
| 1127 | if ( ! empty( $fields['post_term']['options'] ) ) { |
| 1128 | foreach ( $fields['post_term']['options'] as $tax => $data ) { |
| 1129 | $terms = array_merge( $terms, array_map( 'intval', array_keys( $data['terms'] ) ) ); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | foreach ( $args['post_term'] as $post_term ) { |
| 1134 | if ( in_array( $post_term, $terms ) ) { |
| 1135 | $term = get_term( $post_term ); |
| 1136 | |
| 1137 | $tax_queries['post_term'][] = array( |
| 1138 | 'taxonomy' => $term->taxonomy, |
| 1139 | 'field' => 'term_id', |
| 1140 | 'terms' => (int) $post_term |
| 1141 | ); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | switch ( $args['image_source'] ) { |
| 1147 | case 'thumbnails': |
| 1148 | $meta_queries['image_source'][] = array( |
| 1149 | 'relation' => 'OR', |
| 1150 | array( |
| 1151 | 'key' => '_thumbnail_id', |
| 1152 | 'compare' => 'EXISTS' |
| 1153 | ) |
| 1154 | ); |
| 1155 | } |
| 1156 | |
| 1157 | // any tax queries? |
| 1158 | if ( ! empty( $tax_queries['post_term'] ) || ! empty( $tax_queries['post_format'] ) ) { |
| 1159 | $args['tax_query'] = array( 'relation' => 'AND' ); |
| 1160 | |
| 1161 | if ( ! empty( $tax_queries['post_term'] ) ) |
| 1162 | $args['tax_query'][] = array( 'relation' => 'OR' ) + $tax_queries['post_term']; |
| 1163 | |
| 1164 | if ( ! empty( $tax_queries['post_format'] ) ) |
| 1165 | $args['tax_query'][] = array( 'relation' => 'OR' ) + $tax_queries['post_format']; |
| 1166 | } |
| 1167 | |
| 1168 | // any tax queries? |
| 1169 | if ( ! empty( $meta_queries['page_template'] ) || ! empty( $meta_queries['image_source'] ) ) { |
| 1170 | $args['meta_query'] = array( 'relation' => 'AND' ); |
| 1171 | |
| 1172 | if ( ! empty( $meta_queries['page_template'] ) ) |
| 1173 | $args['meta_query'][] = array( 'relation' => 'OR', $meta_queries['page_template'] ); |
| 1174 | |
| 1175 | if ( ! empty( $meta_queries['image_source'] ) ) |
| 1176 | $args['meta_query'][] = array( 'relation' => 'OR', $meta_queries['image_source'] ); |
| 1177 | } |
| 1178 | |
| 1179 | // get posts |
| 1180 | $query = new WP_Query( apply_filters( 'rl_gallery_query_args', $args ) ); |
| 1181 | |
| 1182 | // get attachments |
| 1183 | if ( $query->have_posts() ) |
| 1184 | $attachments = $this->get_gallery_query_attachments( $query->posts, $args ); |
| 1185 | |
| 1186 | return $attachments; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * Get query attachments. |
| 1191 | * |
| 1192 | * @param array $posts Post IDs, array or objects |
| 1193 | * @param array $args Additional arguments |
| 1194 | * @return array |
| 1195 | */ |
| 1196 | public function get_gallery_query_attachments( $posts, $args ) { |
| 1197 | $attachments = []; |
| 1198 | |
| 1199 | // any posts? |
| 1200 | if ( ! empty( $posts ) ) { |
| 1201 | switch ( $args['image_source'] ) { |
| 1202 | case 'thumbnails': |
| 1203 | $nop = count( $posts ) - 1; |
| 1204 | |
| 1205 | foreach ( $posts as $number => $post_id ) { |
| 1206 | $attachment_id = (int) get_post_thumbnail_id( $post_id ); |
| 1207 | |
| 1208 | // real attachment? |
| 1209 | if ( wp_attachment_is_image( $attachment_id ) ) |
| 1210 | $attachments[] = $attachment_id; |
| 1211 | else |
| 1212 | continue; |
| 1213 | |
| 1214 | if ( $args['preview'] ) { |
| 1215 | $attachments = array_unique( $attachments ); |
| 1216 | $noa = count( $attachments ); |
| 1217 | |
| 1218 | if ( ( $noa >= ( $args['preview_per_page'] * $args['preview_page'] ) ) || $nop === $number ) { |
| 1219 | $attachments = array_slice( $attachments, ( $args['preview_page'] - 1 ) * $args['preview_per_page'], $args['preview_per_page'], false ); |
| 1220 | |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | break; |
| 1226 | |
| 1227 | case 'attached_images': |
| 1228 | $nop = count( $posts ) - 1; |
| 1229 | |
| 1230 | foreach ( $posts as $number => $post_id ) { |
| 1231 | // get attached images, do not use get_attached_media here! |
| 1232 | $attachment_ids = (array) get_children( |
| 1233 | array( |
| 1234 | 'post_parent' => $post_id, |
| 1235 | 'post_status' => 'inherit', |
| 1236 | 'post_type' => 'attachment', |
| 1237 | 'post_mime_type' => 'image', |
| 1238 | 'posts_per_page' => $args['images_per_post'], |
| 1239 | 'order' => 'ASC', |
| 1240 | 'orderby' => 'menu_order', |
| 1241 | 'nopaging' => false, |
| 1242 | 'page' => 1, |
| 1243 | 'fields' => 'ids' |
| 1244 | ) |
| 1245 | ); |
| 1246 | |
| 1247 | if ( $attachment_ids ) { |
| 1248 | foreach ( $attachment_ids as $attachment_id ) { |
| 1249 | if ( ! empty( $attachment_id ) ) { |
| 1250 | $attachments[] = $attachment_id; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | if ( $args['preview'] ) { |
| 1256 | $attachments = array_unique( $attachments ); |
| 1257 | $noa = count( $attachments ); |
| 1258 | |
| 1259 | if ( ( $noa >= ( $args['preview_per_page'] * $args['preview_page'] ) ) || $nop === $number ) { |
| 1260 | $attachments = array_slice( $attachments, ( $args['preview_page'] - 1 ) * $args['preview_per_page'], $args['preview_per_page'], false ); |
| 1261 | |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | return apply_filters( 'rl_get_gallery_query_attachments', array_unique( $attachments ), $posts, $args ); |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * Check attachments. |
| 1274 | * |
| 1275 | * @param array $attachments Attachment IDs |
| 1276 | * @param array $args Additional arguments |
| 1277 | * @return array |
| 1278 | */ |
| 1279 | public function check_attachments( $attachments, $args = [] ) { |
| 1280 | // no attachments? |
| 1281 | if ( empty( $attachments ) || ! is_array( $attachments ) ) |
| 1282 | return []; |
| 1283 | |
| 1284 | // check providers support |
| 1285 | if ( ! empty( $args['providers'] ) ) |
| 1286 | $embed = rl_current_lightbox_supports( $args['providers'], 'OR' ); |
| 1287 | else |
| 1288 | $embed = false; |
| 1289 | |
| 1290 | // no embed data? |
| 1291 | if ( ! $embed ) |
| 1292 | $copy = array_map( 'intval', $attachments ); |
| 1293 | else |
| 1294 | $copy = $attachments; |
| 1295 | |
| 1296 | // check attachments |
| 1297 | foreach ( $attachments as $key => $attachment_id ) { |
| 1298 | // embed? |
| 1299 | if ( $embed && preg_match( '/^e\d+$/', $attachment_id ) === 1 ) { |
| 1300 | if ( ! in_array( $attachment_id, $args['embed_keys'], true ) ) |
| 1301 | unset( $copy[$key] ); |
| 1302 | // video support? |
| 1303 | } elseif ( rl_current_lightbox_supports( 'video' ) ) { |
| 1304 | // is it an image or video? |
| 1305 | if ( ! wp_attachment_is( 'video', $attachment_id ) && ! wp_attachment_is( 'image', $attachment_id ) ) |
| 1306 | unset( $copy[$key] ); |
| 1307 | // make sure it's integer |
| 1308 | elseif ( $embed ) |
| 1309 | $copy[$key] = (int) $copy[$key]; |
| 1310 | } else { |
| 1311 | // is it an image? |
| 1312 | if ( ! wp_attachment_is_image( $attachment_id ) ) |
| 1313 | unset( $copy[$key] ); |
| 1314 | // make sure it's integer |
| 1315 | elseif ( $embed ) |
| 1316 | $copy[$key] = (int) $copy[$key] ; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | return array_values( $copy ); |
| 1321 | } |
| 1322 | } |
| 1323 |