1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
<?php
/** * Class FooGallery * * An easy to use wrapper class for a FooGallery gallery post */ class FooGallery extends stdClass {
/** * private constructor * * @param null $post */ private function __construct( $post = null ) { $this->set_defaults();
if ( $post !== null ) { $this->load( $post ); } }
/** * Sets the default when a new gallery is instantiated */ private function set_defaults() { $this->_post = null; $this->ID = 0; $this->attachment_ids = array(); $this->_attachments = false; $this->datasource_name = foogallery_default_datasource(); $this->_datasource = false; $this->settings = array(); $this->sorting = ''; $this->force_use_original_thumbs = false; $this->retina = array(); }
/** * private gallery load function * @param $post */ private function load( $post ) { $this->_post = $post; $this->ID = $post->ID; $this->slug = $post->post_name; $this->name = $post->post_title; $this->author = $post->post_author; $this->post_status = $post->post_status;
$attachment_meta = get_post_meta( $post->ID, FOOGALLERY_META_ATTACHMENTS, true ); $this->attachment_ids = is_array( $attachment_meta ) ? array_filter( $attachment_meta ) : array();
$this->load_meta( $post->ID );
do_action( 'foogallery_instance_after_load', $this, $post ); }
/** * private meta data load function * @param $post_id int */ private function load_meta( $post_id ) { $this->gallery_template = get_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, true ); $this->settings = $this->load_settings( $post_id ); $this->custom_css = get_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, true ); $this->sorting = get_post_meta( $post_id, FOOGALLERY_META_SORT, true ); $this->datasource_name = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true ); if ( empty( $this->datasource_name ) ) { $this->datasource_name = foogallery_default_datasource(); } $this->retina = get_post_meta( $post_id, FOOGALLERY_META_RETINA, true ); $this->force_use_original_thumbs = 'true' === get_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true ); }
private function load_settings( $post_id ) { $settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
//the gallery is considered new if the template has not been set $is_new = empty( $this->gallery_template );
//if we have no settings, and the gallery is not new, then allow for an upgrade if ( empty( $settings ) && !$is_new ) { $settings = apply_filters( 'foogallery_settings_upgrade', $settings, $this ); }
//if we still have no settings, then get default settings for the gallery template if ( empty( $settings ) && !$is_new ) { $settings = foogallery_build_default_settings_for_gallery_template( $this->gallery_template );
$settings = apply_filters('foogallery_default_settings-' . $this->gallery_template, $settings, $this); }
return $settings; }
/** * private function to load a gallery by an id * @param $post_id */ private function load_by_id( $post_id ) { $post = get_post( $post_id ); if ( $post ) { $this->load( $post ); } }
/** * private function to load a gallery by the slug. * Will be used when loading gallery shortcodes * @param $slug */ private function load_by_slug( $slug ) { if ( ! empty( $slug ) ) { $args = array( 'name' => $slug, 'numberposts' => 1, 'post_type' => FOOGALLERY_CPT_GALLERY, );
$galleries = get_posts( $args );
if ( $galleries ) { $this->load( $galleries[0] ); } } }
/** * Static function to build a dynamic gallery that does not exist in the database * @param $template * @param $attachment_ids * * @return FooGallery */ public static function dynamic( $template, $attachment_ids ) { $gallery = new self( null );
$gallery->gallery_template = $template; $gallery->attachment_ids = $attachment_ids;
//loads all meta data from the default gallery $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' ); if ( $default_gallery_id > 0 ) { $gallery->load_meta( $default_gallery_id ); }
return $gallery; }
/** * Static function to load a Gallery instance by passing in a post object * @static * * @param $post * * @return FooGallery */ public static function get( $post ) { return new self( $post ); }
/** * Static function to load a Gallery instance by post id * * @param $post_id * * @return FooGallery | boolean */ public static function get_by_id( $post_id ) { $gallery = new self(); $gallery->load_by_id( $post_id ); if ( ! $gallery->does_exist() ) { return false; } return $gallery; }
/** * Static function to load a gallery instance by passing in a gallery slug * * @param string $slug * * @return FooGallery | boolean */ public static function get_by_slug( $slug ) { $gallery = new self(); $gallery->load_by_slug( $slug ); if ( ! $gallery->does_exist() ) { return false; } return $gallery; }
/** * Get a setting using the current template and meta key * @param $key * @param $default * * @return mixed|null */ function get_setting( $key, $default ) { return $this->get_meta( "{$this->gallery_template}_$key", $default ); }
/** * Get a meta value using a full key * @param $key * @param $default * * @return mixed|null */ function get_meta( $key, $default ) { if ( ! is_array( $this->settings ) ) { return $default; }
$value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
if ( $value === null ) { return $default; }
return $value; }
function is_checked( $key, $default = false ) { if ( ! is_array( $this->settings ) ) { return $default; }
return array_key_exists( $key, $this->settings ); }
/** * Returns the number of attachments in the current gallery * @return int */ public function attachment_count() { return $this->datasource()->getCount(); }
/** * Checks if the gallery has attachments * @return bool */ public function has_attachments() { return $this->attachment_count() > 0; }
/** * Checks if the gallery exists * @return bool */ public function does_exist() { return $this->ID > 0; }
/** * Returns true if the gallery is published * @return bool */ public function is_published() { return $this->post_status === 'publish'; }
/** * Returns true if the gallery is newly created and not yet saved */ public function is_new() { $template = get_post_meta( $this->ID, FOOGALLERY_META_TEMPLATE, true ); return empty( $template ); }
/** * Get a comma separated list of attachment ids * @return string */ public function attachment_id_csv() { return $this->datasource()->getSerializedData(); }
/** * Lazy load the attachments for the gallery * * @return array */ public function attachments() { //lazy load the attachments for performance if ( $this->_attachments === false ) { $this->_attachments = $this->datasource()->getAttachments(); }
return $this->_attachments; }
/** * @deprecated 1.3.0 This is now moved into the datasource implementation * * This forces the attachments to be fetched using the correct ordering. * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct * @param $query WP_Query */ public function force_gallery_ordering( $query ) { _deprecated_function( __FUNCTION__, '1.3.0' ); }
/** * Output the shortcode for the gallery * * @return string */ public function shortcode() { return foogallery_build_gallery_shortcode( $this->ID ); }
/** * @deprecated 1.3.0 This is now moved into the datasource implementation * * @return int|mixed|string */ public function find_featured_attachment_id() { _deprecated_function( __FUNCTION__, '1.3.0' );
return 0; }
/** * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery * * @return bool|FooGalleryAttachment */ public function featured_attachment() { return $this->datasource()->getFeaturedAttachment(); }
/** * @deprecated 1.3.0 This is now moved into the datasource implementation * * @param string $size * @param bool $icon * * @return bool */ public function featured_image_src( $size = 'thumbnail', $icon = false ) { _deprecated_function( __FUNCTION__, '1.3.0' );
return false; }
/** * @deprecated 1.3.0 This is now moved into the datasource implementation * * Get an HTML img element representing the featured image for the gallery * * @param string $size Optional, default is 'thumbnail'. * @param bool $icon Optional, default is false. Whether it is an icon. * * @return string HTML img element or empty string on failure. */ public function featured_image_html( $size = 'thumbnail', $icon = false ) { _deprecated_function( __FUNCTION__, '1.3.0' );
return ''; }
public function image_count() { $no_images_text = foogallery_get_setting( 'language_images_count_none_text', __( 'No images', 'foogallery' ) ); $singular_text = foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) ); $plural_text = foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) );
$count = $this->attachment_count();
switch ( $count ) { case 0: $count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text; break; case 1: $count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text; break; default: $count_text = sprintf( $plural_text === false ? __( '%s images', 'foogallery' ) : $plural_text, $count ); }
return esc_html( apply_filters( 'foogallery_image_count', $count_text, $this ) ); }
/** * Returns a safe name for the gallery, in case there has been no title set * * @return string */ public function safe_name() { return empty( $this->name ) ? sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) : $this->name; }
public function find_usages() { return get_posts( array( 'post_type' => foogallery_allowed_post_types_for_usage(), 'post_status' => array( 'draft', 'publish', ), 'posts_per_page' => -1, 'orderby' => 'post_type', 'meta_query' => array( array( 'key' => FOOGALLERY_META_POST_USAGE, 'value' => $this->ID, 'compare' => 'IN', ), ), ) ); }
public function gallery_template_details() { if ( ! empty( $this->gallery_template ) ) {
foreach ( foogallery_gallery_templates() as $template ) { if ( $this->gallery_template == $template['slug'] ) { return $template; } } }
return false; }
/** * Returns the name of the gallery template * @return string|void */ public function gallery_template_name() { $template = $this->gallery_template_details(); if ( false !== $template ) { return $template['name']; } return __( 'Unknown', 'foogallery' ); }
public function gallery_template_has_field_of_type( $field_type ) { $gallery_template_details = $this->gallery_template_details();
if ( false != $gallery_template_details ) { if ( array_key_exists( 'fields', $gallery_template_details ) ) { foreach ( $gallery_template_details['fields'] as $field ) { if ( $field_type == $field['type'] ) { return true; } } } } return false; }
/** * Loads default settings from another gallery if it is set on the settings page */ public function load_default_settings_if_new() { if ( $this->is_new() ) { $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' ); //loads all meta data from the default gallery $this->load_meta( $default_gallery_id ); } }
/** * Returns the current gallery datasource object * * @returns IFooGalleryDatasource */ public function datasource() { //lazy load the datasource only when needed if ( $this->_datasource === false ) { $this->_datasource = foogallery_instantiate_datasource( $this->datasource_name ); $this->_datasource->setGallery( $this ); }
return $this->_datasource; } }
|