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