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