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