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