PluginProbe ʕ •ᴥ•ʔ
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel / trunk
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel vtrunk
trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-foogallery.php
foogallery / includes Last commit date
admin 7 months ago compatibility 7 months ago extensions 7 months ago foopluginbase 7 months ago public 7 months ago thumbs 7 months ago .DS_Store 7 months ago class-attachment-filters.php 7 months ago class-foogallery-animated-gif-support.php 7 months ago class-foogallery-attachment-custom-class.php 7 months ago class-foogallery-attachment-type.php 7 months ago class-foogallery-attachment.php 7 months ago class-foogallery-cache.php 7 months ago class-foogallery-common-fields.php 7 months ago class-foogallery-crop-position.php 7 months ago class-foogallery-datasource-media_library.php 7 months ago class-foogallery-debug.php 7 months ago class-foogallery-extensions-compatibility.php 7 months ago class-foogallery-force-https.php 7 months ago class-foogallery-lazyload.php 7 months ago class-foogallery-lightbox.php 7 months ago class-foogallery-paging.php 7 months ago class-foogallery-password-protect.php 7 months ago class-foogallery-sitemaps.php 7 months ago class-foogallery-widget.php 7 months ago class-foogallery.php 7 months ago class-gallery-advanced-settings.php 7 months ago class-il8n.php 7 months ago class-override-thumbnail.php 7 months ago class-posttypes.php 7 months ago class-retina.php 7 months ago class-thumbnail-dimensions.php 7 months ago class-thumbnails.php 7 months ago class-version-check.php 7 months ago constants.php 7 months ago functions.php 7 months ago includes.php 7 months ago index.php 11 years ago render-functions.php 7 months ago
class-foogallery.php
608 lines
1 <?php
2
3 /**
4 * Class FooGallery
5 *
6 * An easy to use wrapper class for a FooGallery gallery post
7 */
8 class FooGallery extends stdClass {
9
10 /**
11 * private constructor
12 *
13 * @param null $post
14 */
15 private function __construct( $post = null ) {
16 $this->set_defaults();
17
18 if ( $post !== null ) {
19 $this->load( $post );
20 }
21 }
22
23 /**
24 * Sets the default when a new gallery is instantiated
25 */
26 private function set_defaults() {
27 $this->_post = null;
28 $this->ID = 0;
29 $this->attachment_ids = array();
30 $this->_attachments = false;
31 $this->datasource_name = foogallery_default_datasource();
32 $this->settings = array();
33 $this->sorting = '';
34 $this->force_use_original_thumbs = false;
35 $this->retina = array();
36 }
37
38 /**
39 * private gallery load function
40 *
41 * @param $post
42 */
43 private function load( $post ) {
44 if ( $post->post_type !== FOOGALLERY_CPT_GALLERY ) {
45 return;
46 }
47
48 $this->_post = $post;
49 $this->ID = $post->ID;
50 $this->slug = $post->post_name;
51 $this->name = $post->post_title;
52 $this->author = $post->post_author;
53 $this->post_status = $post->post_status;
54
55 // Load attachment metadata.
56 $attachment_meta = get_post_meta( $post->ID, FOOGALLERY_META_ATTACHMENTS, true );
57 $this->attachment_ids = is_array( $attachment_meta ) ? array_filter( $attachment_meta ) : array();
58
59 // Load datasource metadata.
60 $this->datasource_name = get_post_meta( $post->ID, FOOGALLERY_META_DATASOURCE, true );
61 if ( empty( $this->datasource_name ) ) {
62 $this->datasource_name = foogallery_default_datasource();
63 } else {
64 $this->datasource_value = get_post_meta( $post->ID, FOOGALLERY_META_DATASOURCE_VALUE, true );
65 }
66
67 $gallery_id = apply_filters( 'foogallery_load_gallery_settings_id', $post->ID, $post );
68
69 $this->load_meta( $gallery_id );
70
71 do_action( 'foogallery_instance_after_load', $this, $post );
72 }
73
74 /**
75 * private meta data load function
76 *
77 * @param $post_id int
78 */
79 private function load_meta( $post_id ) {
80 $this->gallery_template = get_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, true );
81 $this->settings = $this->load_settings( $post_id );
82 $this->custom_css = get_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, true );
83 $this->sorting = get_post_meta( $post_id, FOOGALLERY_META_SORT, true );
84 $this->retina = get_post_meta( $post_id, FOOGALLERY_META_RETINA, true );
85 $this->force_use_original_thumbs = 'true' === get_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
86 }
87
88 private function load_settings( $post_id ) {
89 $settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
90
91 //the gallery is considered new if the template has not been set
92 $is_new = empty( $this->gallery_template );
93
94 //if we have no settings, and the gallery is not new, then allow for an upgrade
95 if ( empty( $settings ) && ! $is_new ) {
96 $settings = apply_filters( 'foogallery_settings_upgrade', $settings, $this );
97 }
98
99 //if we still have no settings, then get default settings for the gallery template
100 if ( empty( $settings ) && ! $is_new ) {
101 $settings = foogallery_build_default_settings_for_gallery_template( $this->gallery_template );
102
103 $settings = apply_filters( 'foogallery_default_settings-' . $this->gallery_template, $settings, $this );
104 }
105
106 //allow the settings to be overridden
107 return apply_filters( 'foogallery_settings_override', $settings, $this->gallery_template, $this );
108 }
109
110 /**
111 * private function to load a gallery by an id
112 *
113 * @param $post_id
114 */
115 private function load_by_id( $post_id ) {
116 $post = get_post( $post_id );
117 if ( $post ) {
118 $this->load( $post );
119 }
120 }
121
122 /**
123 * private function to load a gallery by the slug.
124 * Will be used when loading gallery shortcodes
125 *
126 * @param $slug
127 */
128 private function load_by_slug( $slug ) {
129 if ( ! empty( $slug ) ) {
130 $args = array(
131 'name' => $slug,
132 'numberposts' => 1,
133 'post_type' => FOOGALLERY_CPT_GALLERY,
134 );
135
136 $galleries = get_posts( $args );
137
138 if ( $galleries ) {
139 $this->load( $galleries[0] );
140 }
141 }
142 }
143
144 /**
145 * Static function to build a dynamic gallery that does not exist in the database
146 *
147 * @param $template
148 * @param $attachment_ids
149 *
150 * @return FooGallery
151 */
152 public static function dynamic( $template, $attachment_ids ) {
153 $gallery = new self( null );
154
155 $gallery->gallery_template = $template;
156 $gallery->attachment_ids = $attachment_ids;
157
158 //loads all meta data from the default gallery
159 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
160 if ( $default_gallery_id > 0 ) {
161 $gallery->load_meta( $default_gallery_id );
162 }
163
164 return $gallery;
165 }
166
167 /**
168 * Static function to build a dynamic gallery that does not exist in the database for a specific datasource
169 *
170 * @return FooGallery
171 */
172 public static function dynamic_for_datasource( $datasource ) {
173 $gallery = new self( null );
174
175 //loads all meta data from the default gallery
176 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
177 if ( $default_gallery_id > 0 ) {
178 $gallery->load_meta( $default_gallery_id );
179 }
180
181 $gallery->datasource_name = $datasource;
182
183 //set the datasource_value from a filter
184
185 return $gallery;
186 }
187
188 /**
189 * Static function to load a Gallery instance by passing in a post object
190 *
191 * @static
192 *
193 * @param $post
194 *
195 * @return FooGallery
196 */
197 public static function get( $post ) {
198 return new self( $post );
199 }
200
201 /**
202 * Static function to load a Gallery instance by post id
203 *
204 * @param $post_id
205 *
206 * @return FooGallery | boolean
207 */
208 public static function get_by_id( $post_id ) {
209 $gallery = new self();
210 $gallery->load_by_id( $post_id );
211 if ( ! $gallery->does_exist() ) {
212 return false;
213 }
214
215 return $gallery;
216 }
217
218 /**
219 * Static function to load a gallery instance by passing in a gallery slug
220 *
221 * @param string $slug
222 *
223 * @return FooGallery | boolean
224 */
225 public static function get_by_slug( $slug ) {
226 $gallery = new self();
227 $gallery->load_by_slug( $slug );
228 if ( ! $gallery->does_exist() ) {
229 return false;
230 }
231
232 return $gallery;
233 }
234
235 /**
236 * Get a setting using the current template and meta key
237 *
238 * @param $key
239 * @param $default
240 *
241 * @return mixed|null
242 */
243 function get_setting( $key, $default ) {
244 return $this->get_meta( "{$this->gallery_template}_$key", $default );
245 }
246
247 /**
248 * Get a meta value using a full key
249 *
250 * @param $key
251 * @param $default
252 *
253 * @return mixed|null
254 */
255 function get_meta( $key, $default ) {
256 if ( ! is_array( $this->settings ) ) {
257 return $default;
258 }
259
260 $value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
261
262 if ( $value === null ) {
263 return $default;
264 }
265
266 return $value;
267 }
268
269 function is_checked( $key, $default = false ) {
270 if ( ! is_array( $this->settings ) ) {
271 return $default;
272 }
273
274 return array_key_exists( $key, $this->settings );
275 }
276
277 /**
278 * Returns the number of attachments in the current gallery, that were added from the media library
279 *
280 * @return int
281 */
282 public function attachment_count() {
283 if ( is_array( $this->attachment_ids ) ) {
284 return count( $this->attachment_ids );
285 }
286
287 return 0;
288 }
289
290 /**
291 * Checks if the gallery has attachments. There will only be attachments if they were added from the media library.
292 *
293 * @return bool
294 */
295 public function has_attachments() {
296 return $this->attachment_count() > 0;
297 }
298
299 /**
300 * Checks if the gallery exists
301 *
302 * @return bool
303 */
304 public function does_exist() {
305 return $this->ID > 0;
306 }
307
308 /**
309 * Returns true if the gallery is published
310 *
311 * @return bool
312 */
313 public function is_published() {
314 return $this->post_status === 'publish';
315 }
316
317 /**
318 * Returns true if the gallery is newly created and not yet saved
319 */
320 public function is_new() {
321 $template = get_post_meta( $this->ID, FOOGALLERY_META_TEMPLATE, true );
322
323 return empty( $template );
324 }
325
326 /**
327 * Get a comma separated list of attachment ids
328 *
329 * @return string
330 */
331 public function attachment_id_csv() {
332 if ( is_array( $this->attachment_ids ) ) {
333 // Filter the array to ensure all values are strings or numbers.
334 $filtered_ids = array_filter( $this->attachment_ids, function( $id ) {
335 return is_string( $id ) || is_numeric( $id );
336 });
337
338 return implode( ',', $filtered_ids );
339 }
340
341 return '';
342 }
343
344 /**
345 * Lazy load the attachments for the gallery
346 *
347 * @return array
348 */
349 public function attachments() {
350 //lazy load the attachments for performance
351 if ( $this->_attachments === false ) {
352 $attachments = $this->apply_datasource_filter( 'attachments', array() );
353 $attachments = apply_filters( 'foogallery_attachments_pre_sort', $attachments, $this );
354 if ( ! empty( $attachments ) && $this->apply_datasource_filter( 'must_sort', true ) ) {
355 $orderby = foogallery_sorting_get_posts_orderby_arg( $this->sorting );
356 $order = foogallery_sorting_get_posts_order_arg( $this->sorting );
357 $attachments = foogallery_sort_attachments( $attachments, $orderby, $order );
358 $attachments = apply_filters( 'foogallery_attachments', $attachments, $this );
359 }
360 $this->_attachments = $attachments;
361 }
362
363 return $this->_attachments;
364 }
365
366 /**
367 * Output the shortcode for the gallery
368 *
369 * @return string
370 */
371 public function shortcode() {
372 return foogallery_build_gallery_shortcode( $this->ID );
373 }
374
375 /**
376 * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image
377 * in the gallery
378 *
379 * @return bool|FooGalleryAttachment
380 */
381 public function featured_attachment() {
382 //first try get back the featured image for the gallery
383 $attachment_id = get_post_thumbnail_id( $this->ID );
384 if ( $attachment_id ) {
385 return FooGalleryAttachment::get_by_id( $attachment_id );
386 }
387
388 //then get the featured image from the datasource
389 $default_placeholder_attachment = new FooGalleryAttachment();
390 $default_placeholder_attachment->url = foogallery_image_placeholder_src();
391
392 return $this->apply_datasource_filter( 'featured_image', $default_placeholder_attachment );
393 }
394
395 /**
396 * Returns the string representation of the number of items in the gallery
397 *
398 * @return string
399 */
400 public function image_count() {
401 $no_images_text = esc_html( foogallery_get_setting( 'language_images_count_none_text', __( 'No images', 'foogallery' ) ) );
402 $singular_text = esc_html( foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) ) );
403 $plural_text = esc_html( foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) ) );
404
405 $count = $this->item_count();
406
407 switch ( $count ) {
408 case 0:
409 $count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text;
410 break;
411 case 1:
412 $count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text;
413 break;
414 default:
415 $count_text = sprintf( $plural_text === false ? __( '%s images', 'foogallery' ) : $plural_text, $count );
416 }
417
418 return esc_html( apply_filters( 'foogallery_image_count', $count_text, $this, $count ) );
419 }
420
421 /**
422 * Returns a safe name for the gallery, in case there has been no title set
423 *
424 * @return string
425 */
426 public function safe_name() {
427 return empty( $this->name ) ? sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) : $this->name;
428 }
429
430 /**
431 * Finds usages of the FooGallery
432 *
433 * @return WP_Post[]
434 */
435 public function find_usages() {
436 return get_posts( array(
437 'post_type' => foogallery_allowed_post_types_for_usage(),
438 'post_status' => array( 'draft', 'publish', ),
439 'posts_per_page' => -1,
440 'orderby' => 'post_type',
441 'meta_query' => array(
442 array(
443 'key' => FOOGALLERY_META_POST_USAGE,
444 'value' => $this->ID,
445 'compare' => 'IN',
446 ),
447 ),
448 ) );
449 }
450
451 /**
452 * Returns the current gallery template details
453 *
454 * @return array|bool
455 */
456 public function gallery_template_details() {
457 if ( ! empty( $this->gallery_template ) ) {
458
459 foreach ( foogallery_gallery_templates() as $template ) {
460 if ( $this->gallery_template == $template['slug'] ) {
461 return $template;
462 }
463 }
464 }
465
466 return false;
467 }
468
469 /**
470 * Returns the name of the gallery template
471 *
472 * @return string
473 */
474 public function gallery_template_name() {
475 $template = $this->gallery_template_details();
476 if ( false !== $template ) {
477 return $template['name'];
478 }
479
480 return __( 'Unknown', 'foogallery' );
481 }
482
483 /**
484 * Returns true if the gallery template has a field of a certain type
485 *
486 * @param $field_type
487 *
488 * @return bool
489 */
490 public function gallery_template_has_field_of_type( $field_type ) {
491 $gallery_template_details = $this->gallery_template_details();
492
493 if ( false != $gallery_template_details ) {
494 if ( array_key_exists( 'fields', $gallery_template_details ) ) {
495 foreach ( $gallery_template_details['fields'] as $field ) {
496 if ( $field_type == $field['type'] ) {
497 return true;
498 }
499 }
500 }
501 }
502
503 return false;
504 }
505
506 /**
507 * Loads default settings from another gallery if it is set on the settings page
508 */
509 public function load_default_settings_if_new() {
510 if ( $this->is_new() ) {
511 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
512 //loads all meta data from the default gallery
513 $this->load_meta( $default_gallery_id );
514 }
515 }
516
517
518 /**
519 * Small helper function to apply the datasource-specific filters in a consistent way
520 *
521 * @param $filter_name
522 * @param $filter_default_value
523 *
524 * @return mixed
525 * @since 1.8.0
526 */
527 private function apply_datasource_filter( $filter_name, $filter_default_value ) {
528 return apply_filters( "foogallery_datasource_{$this->datasource_name}_{$filter_name}", $filter_default_value, $this );
529 }
530
531 /**
532 * Does the current gallery contain any items
533 *
534 * @return bool
535 * @since 1.8.0
536 */
537 public function has_items() {
538 return $this->item_count() > 0;
539 }
540
541 /**
542 * Return the number of items in the gallery. This is determined by querying the gallery datasource
543 *
544 * @return bool
545 * @since 1.8.0
546 */
547 public function item_count() {
548 return $this->apply_datasource_filter( 'item_count', 0 );
549 }
550
551 /**
552 * Returns an array of the attachment ID's within the gallery
553 *
554 * @return array
555 */
556 public function item_attachment_ids() {
557 return $this->apply_datasource_filter( 'attachment_ids', $this->attachment_ids );
558 }
559
560 public function is_empty() {
561 if ( foogallery_default_datasource() === $this->datasource_name ) {
562 return $this->attachment_count() === 0;
563 }
564
565 return empty( $this->datasource_value );
566 }
567
568 /**
569 * Returns true if the datasource is not media_library
570 */
571 public function is_dynamic() {
572 return $this->datasource_name !== foogallery_default_datasource();
573 }
574
575 private $container_id;
576
577 /**
578 * Returns the ID that is rendered on the container div of the gallery
579 *
580 * @return string
581 */
582 public function container_id() {
583 if ( isset( $this->container_id ) ) {
584 return $this->container_id;
585 }
586
587 global $foogallery_container_ids;
588
589 if ( !isset( $foogallery_container_ids ) ) {
590 $foogallery_container_ids = array();
591 }
592 $foogallery_container_id = 'foogallery-gallery-' . $this->ID;
593
594 if ( array_key_exists( $this->ID, $foogallery_container_ids ) ) {
595 //The FooGallery has already been added to the page, so we need to generate a new container_id
596
597 $count = count( $foogallery_container_ids[$this->ID] );
598 $foogallery_container_id .= '_' . $count;
599 }
600
601 $foogallery_container_ids[$this->ID][] = $foogallery_container_id;
602
603 $this->container_id = $foogallery_container_id;
604
605 return $foogallery_container_id;
606 }
607 }
608