PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.0
Gallery : FooGallery v3.3.0
3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 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
abilities 1 week ago admin 1 week ago compatibility 1 week ago extensions 1 week ago foopluginbase 1 week ago public 1 week ago thumbs 1 week ago asset-manifest.php 1 week ago class-attachment-filters.php 1 week ago class-command-palette.php 1 week ago class-foogallery-animated-gif-support.php 1 week ago class-foogallery-attachment-custom-class.php 1 week ago class-foogallery-attachment-filename.php 1 week ago class-foogallery-attachment-type.php 1 week ago class-foogallery-attachment.php 1 week ago class-foogallery-cache.php 1 week ago class-foogallery-common-fields.php 1 week ago class-foogallery-crop-position.php 1 week ago class-foogallery-datasource-media_library.php 1 week ago class-foogallery-debug.php 1 week ago class-foogallery-delayed-runtime-loader.php 1 week ago class-foogallery-extensions-compatibility.php 1 week ago class-foogallery-force-https.php 1 week ago class-foogallery-lazyload.php 1 week ago class-foogallery-license-constant-handler.php 1 week ago class-foogallery-lightbox.php 1 week ago class-foogallery-no-javascript-fallback.php 1 week ago class-foogallery-paging.php 1 week ago class-foogallery-password-protect.php 1 week ago class-foogallery-sitemaps.php 1 week ago class-foogallery-widget.php 1 week ago class-foogallery.php 1 week ago class-gallery-advanced-settings.php 1 week ago class-il8n.php 1 week ago class-override-thumbnail.php 1 week ago class-posttypes.php 1 week ago class-previews.php 1 week ago class-retina.php 1 week ago class-thumbnail-dimensions.php 1 week ago class-thumbnails.php 1 week ago class-version-check.php 1 week ago constants.php 1 week ago functions.php 1 week ago gallery-management-functions.php 1 week ago includes.php 1 week ago index.php 1 week ago render-functions.php 1 week ago
class-foogallery.php
636 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 * Apply template field defaults when no saved settings are available.
112 *
113 * Dynamic galleries do not have persisted post meta, so they need to
114 * synthesize their initial settings from the template field definitions.
115 */
116 private function apply_default_settings_from_template() {
117 if ( empty( $this->gallery_template ) || ! empty( $this->settings ) ) {
118 return;
119 }
120
121 $this->settings = foogallery_build_default_settings_for_gallery_template( $this->gallery_template );
122 $this->settings = apply_filters( 'foogallery_default_settings-' . $this->gallery_template, $this->settings, $this );
123 $this->settings = apply_filters( 'foogallery_settings_override', $this->settings, $this->gallery_template, $this );
124 }
125
126 /**
127 * private function to load a gallery by an id
128 *
129 * @param $post_id
130 */
131 private function load_by_id( $post_id ) {
132 $post = get_post( $post_id );
133 if ( $post ) {
134 $this->load( $post );
135 }
136 }
137
138 /**
139 * private function to load a gallery by the slug.
140 * Will be used when loading gallery shortcodes
141 *
142 * @param $slug
143 */
144 private function load_by_slug( $slug ) {
145 if ( ! empty( $slug ) ) {
146 $args = array(
147 'name' => $slug,
148 'numberposts' => 1,
149 'post_type' => FOOGALLERY_CPT_GALLERY,
150 );
151
152 $galleries = get_posts( $args );
153
154 if ( $galleries ) {
155 $this->load( $galleries[0] );
156 }
157 }
158 }
159
160 /**
161 * Static function to build a dynamic gallery that does not exist in the database
162 *
163 * @param $template
164 * @param $attachment_ids
165 *
166 * @return FooGallery
167 */
168 public static function dynamic( $template, $attachment_ids ) {
169 $gallery = new self( null );
170
171 $gallery->gallery_template = $template;
172 $gallery->attachment_ids = $attachment_ids;
173
174 //loads all meta data from the default gallery
175 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
176 if ( $default_gallery_id > 0 ) {
177 $gallery->load_meta( $default_gallery_id );
178 }
179
180 $gallery->apply_default_settings_from_template();
181
182 return $gallery;
183 }
184
185 /**
186 * Static function to build a dynamic gallery that does not exist in the database for a specific datasource
187 *
188 * @return FooGallery
189 */
190 public static function dynamic_for_datasource( $datasource ) {
191 $gallery = new self( null );
192
193 //loads all meta data from the default gallery
194 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
195 if ( $default_gallery_id > 0 ) {
196 $gallery->load_meta( $default_gallery_id );
197 }
198
199 $gallery->datasource_name = $datasource;
200
201 //set the datasource_value from a filter
202
203 return $gallery;
204 }
205
206 /**
207 * Static function to load a Gallery instance by passing in a post object
208 *
209 * @static
210 *
211 * @param $post
212 *
213 * @return FooGallery
214 */
215 public static function get( $post ) {
216 return new self( $post );
217 }
218
219 /**
220 * Static function to load a Gallery instance by post id
221 *
222 * @param $post_id
223 *
224 * @return FooGallery | boolean
225 */
226 public static function get_by_id( $post_id ) {
227 $gallery = new self();
228 $gallery->load_by_id( $post_id );
229 if ( ! $gallery->does_exist() ) {
230 return false;
231 }
232
233 return $gallery;
234 }
235
236 /**
237 * Static function to load a gallery instance by passing in a gallery slug
238 *
239 * @param string $slug
240 *
241 * @return FooGallery | boolean
242 */
243 public static function get_by_slug( $slug ) {
244 $gallery = new self();
245 $gallery->load_by_slug( $slug );
246 if ( ! $gallery->does_exist() ) {
247 return false;
248 }
249
250 return $gallery;
251 }
252
253 /**
254 * Get a setting using the current template and meta key
255 *
256 * @param $key
257 * @param $default
258 *
259 * @return mixed|null
260 */
261 function get_setting( $key, $default ) {
262 $override_setting = apply_filters( 'foogallery_instance_get_setting', null, $key, $default, $this );
263 if ( null !== $override_setting ) {
264 return $override_setting;
265 }
266
267 return $this->get_meta( "{$this->gallery_template}_$key", $default );
268 }
269
270 /**
271 * Get a meta value using a full key
272 *
273 * @param $key
274 * @param $default
275 *
276 * @return mixed|null
277 */
278 function get_meta( $key, $default ) {
279 if ( ! is_array( $this->settings ) ) {
280 return $default;
281 }
282
283 $value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
284
285 if ( $value === null ) {
286 return $default;
287 }
288
289 return $value;
290 }
291
292 function is_checked( $key, $default = false ) {
293 if ( ! is_array( $this->settings ) ) {
294 return $default;
295 }
296
297 return array_key_exists( $key, $this->settings );
298 }
299
300 /**
301 * Returns the number of attachments in the current gallery, that were added from the media library
302 *
303 * @return int
304 */
305 public function attachment_count() {
306 if ( is_array( $this->attachment_ids ) ) {
307 return count( $this->attachment_ids );
308 }
309
310 return 0;
311 }
312
313 /**
314 * Checks if the gallery has attachments. There will only be attachments if they were added from the media library.
315 *
316 * @return bool
317 */
318 public function has_attachments() {
319 return $this->attachment_count() > 0;
320 }
321
322 /**
323 * Checks if the gallery exists
324 *
325 * @return bool
326 */
327 public function does_exist() {
328 return $this->ID > 0;
329 }
330
331 /**
332 * Returns true if the gallery is published
333 *
334 * @return bool
335 */
336 public function is_published() {
337 return $this->post_status === 'publish';
338 }
339
340 /**
341 * Returns true if the gallery is newly created and not yet saved
342 */
343 public function is_new() {
344 $template = get_post_meta( $this->ID, FOOGALLERY_META_TEMPLATE, true );
345
346 return empty( $template );
347 }
348
349 /**
350 * Get a comma separated list of attachment ids
351 *
352 * @return string
353 */
354 public function attachment_id_csv() {
355 if ( is_array( $this->attachment_ids ) ) {
356 // Filter the array to ensure all values are strings or numbers.
357 $filtered_ids = array_filter( $this->attachment_ids, function( $id ) {
358 return is_string( $id ) || is_numeric( $id );
359 });
360
361 return implode( ',', $filtered_ids );
362 }
363
364 return '';
365 }
366
367 /**
368 * Lazy load the attachments for the gallery
369 *
370 * @return array
371 */
372 public function attachments() {
373 //lazy load the attachments for performance
374 if ( $this->_attachments === false ) {
375 $attachments = $this->apply_datasource_filter( 'attachments', array() );
376 $attachments = apply_filters( 'foogallery_attachments_pre_sort', $attachments, $this );
377 if ( ! empty( $attachments ) && $this->apply_datasource_filter( 'must_sort', true ) ) {
378 $sort = foogallery_sorting_get_effective_sort( $this );
379 $orderby = foogallery_sorting_get_posts_orderby_arg( $sort );
380 $order = foogallery_sorting_get_posts_order_arg( $sort );
381 $attachments = foogallery_sort_attachments( $attachments, $orderby, $order, $sort );
382 $attachments = foogallery_sorting_apply_deferred_query_args( $attachments, $sort );
383 $attachments = apply_filters( 'foogallery_attachments', $attachments, $this );
384 }
385 $this->_attachments = $attachments;
386 }
387
388 return $this->_attachments;
389 }
390
391 /**
392 * Output the shortcode for the gallery
393 *
394 * @return string
395 */
396 public function shortcode() {
397 return foogallery_build_gallery_shortcode( $this->ID );
398 }
399
400 /**
401 * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image
402 * in the gallery
403 *
404 * @return bool|FooGalleryAttachment
405 */
406 public function featured_attachment() {
407 //first try get back the featured image for the gallery
408 $attachment_id = get_post_thumbnail_id( $this->ID );
409 if ( $attachment_id ) {
410 return FooGalleryAttachment::get_by_id( $attachment_id );
411 }
412
413 //then get the featured image from the datasource
414 $default_placeholder_attachment = new FooGalleryAttachment();
415 $default_placeholder_attachment->url = foogallery_image_placeholder_src();
416
417 return $this->apply_datasource_filter( 'featured_image', $default_placeholder_attachment );
418 }
419
420 /**
421 * Returns the string representation of the number of items in the gallery
422 *
423 * @return string
424 */
425 public function image_count() {
426 $no_images_text = esc_html( foogallery_get_setting( 'language_images_count_none_text', __( 'No images', 'foogallery' ) ) );
427 $singular_text = esc_html( foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) ) );
428 /* translators: %s: Value inserted at runtime. */
429 $plural_text = esc_html( foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) ) );
430
431 $count = $this->item_count();
432
433 switch ( $count ) {
434 case 0:
435 $count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text;
436 break;
437 case 1:
438 $count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text;
439 break;
440 default:
441 /* translators: %s: Value inserted at runtime. */
442 $count_text = sprintf( $plural_text === false ? __( '%s images', 'foogallery' ) : $plural_text, $count );
443 }
444
445 return esc_html( apply_filters( 'foogallery_image_count', $count_text, $this, $count ) );
446 }
447
448 /**
449 * Returns a safe name for the gallery, in case there has been no title set
450 *
451 * @return string
452 */
453 public function safe_name() {
454 /* translators: %s: Value inserted at runtime. */
455 return empty( $this->name ) ? sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) : $this->name;
456 }
457
458 /**
459 * Finds usages of the FooGallery
460 *
461 * @return WP_Post[]
462 */
463 public function find_usages() {
464 return get_posts( array(
465 'post_type' => foogallery_allowed_post_types_for_usage(),
466 'post_status' => array( 'draft', 'publish', ),
467 'posts_per_page' => -1,
468 'orderby' => 'post_type',
469 'meta_query' => array(
470 array(
471 'key' => FOOGALLERY_META_POST_USAGE,
472 'value' => $this->ID,
473 'compare' => 'IN',
474 ),
475 ),
476 ) );
477 }
478
479 /**
480 * Returns the current gallery template details
481 *
482 * @return array|bool
483 */
484 public function gallery_template_details() {
485 if ( ! empty( $this->gallery_template ) ) {
486
487 foreach ( foogallery_gallery_templates() as $template ) {
488 if ( $this->gallery_template == $template['slug'] ) {
489 return $template;
490 }
491 }
492 }
493
494 return false;
495 }
496
497 /**
498 * Returns the name of the gallery template
499 *
500 * @return string
501 */
502 public function gallery_template_name() {
503 $template = $this->gallery_template_details();
504 if ( false !== $template ) {
505 return $template['name'];
506 }
507
508 return __( 'Unknown', 'foogallery' );
509 }
510
511 /**
512 * Returns true if the gallery template has a field of a certain type
513 *
514 * @param $field_type
515 *
516 * @return bool
517 */
518 public function gallery_template_has_field_of_type( $field_type ) {
519 $gallery_template_details = $this->gallery_template_details();
520
521 if ( false != $gallery_template_details ) {
522 if ( array_key_exists( 'fields', $gallery_template_details ) ) {
523 foreach ( $gallery_template_details['fields'] as $field ) {
524 if ( $field_type == $field['type'] ) {
525 return true;
526 }
527 }
528 }
529 }
530
531 return false;
532 }
533
534 /**
535 * Loads default settings from another gallery if it is set on the settings page
536 */
537 public function load_default_settings_if_new() {
538 if ( $this->is_new() ) {
539 $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
540 //loads all meta data from the default gallery
541 $this->load_meta( $default_gallery_id );
542 }
543 }
544
545
546 /**
547 * Small helper function to apply the datasource-specific filters in a consistent way
548 *
549 * @param $filter_name
550 * @param $filter_default_value
551 *
552 * @return mixed
553 * @since 1.8.0
554 */
555 private function apply_datasource_filter( $filter_name, $filter_default_value ) {
556 return apply_filters( "foogallery_datasource_{$this->datasource_name}_{$filter_name}", $filter_default_value, $this );
557 }
558
559 /**
560 * Does the current gallery contain any items
561 *
562 * @return bool
563 * @since 1.8.0
564 */
565 public function has_items() {
566 return $this->item_count() > 0;
567 }
568
569 /**
570 * Return the number of items in the gallery. This is determined by querying the gallery datasource
571 *
572 * @return bool
573 * @since 1.8.0
574 */
575 public function item_count() {
576 return $this->apply_datasource_filter( 'item_count', 0 );
577 }
578
579 /**
580 * Returns an array of the attachment ID's within the gallery
581 *
582 * @return array
583 */
584 public function item_attachment_ids() {
585 return $this->apply_datasource_filter( 'attachment_ids', $this->attachment_ids );
586 }
587
588 public function is_empty() {
589 if ( foogallery_default_datasource() === $this->datasource_name ) {
590 return $this->attachment_count() === 0;
591 }
592
593 return empty( $this->datasource_value );
594 }
595
596 /**
597 * Returns true if the datasource is not media_library
598 */
599 public function is_dynamic() {
600 return $this->datasource_name !== foogallery_default_datasource();
601 }
602
603 private $container_id;
604
605 /**
606 * Returns the ID that is rendered on the container div of the gallery
607 *
608 * @return string
609 */
610 public function container_id() {
611 if ( isset( $this->container_id ) ) {
612 return $this->container_id;
613 }
614
615 global $foogallery_container_ids;
616
617 if ( !isset( $foogallery_container_ids ) ) {
618 $foogallery_container_ids = array();
619 }
620 $foogallery_container_id = 'foogallery-gallery-' . $this->ID;
621
622 if ( array_key_exists( $this->ID, $foogallery_container_ids ) ) {
623 //The FooGallery has already been added to the page, so we need to generate a new container_id
624
625 $count = count( $foogallery_container_ids[$this->ID] );
626 $foogallery_container_id .= '_' . $count;
627 }
628
629 $foogallery_container_ids[$this->ID][] = $foogallery_container_id;
630
631 $this->container_id = $foogallery_container_id;
632
633 return $foogallery_container_id;
634 }
635 }
636