|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* FooGallery helper functions for rendering HTML |
|
5
|
|
|
* Created by Brad Vincent |
|
6
|
|
|
* Date: 11/07/2017 |
|
7
|
|
|
* |
|
8
|
|
|
* @since 1.4.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Returns the attachment image source only |
|
13
|
|
|
* |
|
14
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
15
|
|
|
* @param array $args |
|
16
|
|
|
* |
|
17
|
|
|
* @since 1.4.0 |
|
18
|
|
|
* |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
function foogallery_attachment_html_image_src( $foogallery_attachment, $args = array() ) { |
|
22
|
|
|
return apply_filters( 'foogallery_attachment_resize_thumbnail', $foogallery_attachment->url, $args, $foogallery_attachment ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Returns the attachment img HTML |
|
27
|
|
|
* |
|
28
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
29
|
|
|
* @param array $args |
|
30
|
|
|
* |
|
31
|
|
|
* @since 1.4.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
function foogallery_attachment_html_image( $foogallery_attachment, $args = array() ) { |
|
36
|
|
|
$attr = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args ); |
|
37
|
|
|
|
|
38
|
|
|
$html = '<img '; |
|
39
|
|
|
foreach ( $attr as $name => $value ) { |
|
40
|
|
|
$name = str_replace(' ', '', $name); //ensure we have no spaces! |
|
41
|
|
|
$html .= " $name=" . '"' . foogallery_esc_attr($value) . '"'; |
|
42
|
|
|
} |
|
43
|
|
|
$html .= ' />'; |
|
44
|
|
|
|
|
45
|
|
|
return apply_filters( 'foogallery_attachment_html_image', $html, $args, $foogallery_attachment ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the attachment img HTML |
|
50
|
|
|
* |
|
51
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
52
|
|
|
* @param array $args |
|
53
|
|
|
* |
|
54
|
|
|
* @since 1.4.9 |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
function foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args = array() ) { |
|
59
|
|
|
$attr['src'] = foogallery_attachment_html_image_src( $foogallery_attachment, $args ); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if ( ! empty( $foogallery_attachment->alt ) ) { |
|
62
|
|
|
$attr['alt'] = $foogallery_attachment->alt; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ( ! empty( $foogallery_attachment->caption ) ) { |
|
66
|
|
|
$attr['title'] = $foogallery_attachment->caption; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
//pull any custom attributes out the args |
|
70
|
|
|
if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) { |
|
71
|
|
|
$attr = array_merge( $attr, $args['image_attributes'] ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
//check for width and height args and add those to the image |
|
75
|
|
|
if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) { |
|
76
|
|
|
$attr['width'] = $args['width']; |
|
77
|
|
|
} |
|
78
|
|
|
if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) { |
|
79
|
|
|
$attr['height'] = $args['height']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $foogallery_attachment ); |
|
83
|
|
|
|
|
84
|
|
|
if ( array_key_exists( 'class', $attr ) ) { |
|
85
|
|
|
$attr['class'] .= ' fg-image'; |
|
86
|
|
|
} else { |
|
87
|
|
|
$attr['class'] = 'fg-image'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $attr; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the attachment anchor HTML opening tag |
|
95
|
|
|
* |
|
96
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
97
|
|
|
* @param array $args |
|
98
|
|
|
* |
|
99
|
|
|
* @since 1.4.0 |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
function foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args = array() ) { |
|
104
|
|
|
$attr = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args ); |
|
105
|
|
|
|
|
106
|
|
|
$html = '<a '; |
|
107
|
|
|
foreach ( $attr as $name => $value ) { |
|
108
|
|
|
$name = str_replace(' ', '', $name); //ensure we have no spaces! |
|
109
|
|
|
$html .= " $name=" . '"' . foogallery_esc_attr($value) . '"'; |
|
110
|
|
|
} |
|
111
|
|
|
$html .= '>'; |
|
112
|
|
|
|
|
113
|
|
|
return apply_filters( 'foogallery_attachment_html_anchor_opening', $html, $args, $foogallery_attachment ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the array of attributes that will be used on the anchor for a FooGalleryAttachment |
|
118
|
|
|
* |
|
119
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
120
|
|
|
* @param array $args |
|
121
|
|
|
* |
|
122
|
|
|
* @since 1.4.9 |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
function foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args = array() ) { |
|
127
|
|
|
$arg_defaults = array( |
|
128
|
|
|
'link' => 'image', |
|
129
|
|
|
'custom_link' => $foogallery_attachment->custom_url |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
$args = wp_parse_args( $args, $arg_defaults ); |
|
133
|
|
|
|
|
134
|
|
|
$link = $args['link']; |
|
135
|
|
|
|
|
136
|
|
|
if ( 'page' === $link ) { |
|
137
|
|
|
//get the URL to the attachment page |
|
138
|
|
|
$url = get_attachment_link( $foogallery_attachment->ID ); |
|
139
|
|
|
} else if ( 'custom' === $link ) { |
|
140
|
|
|
$url = $args['custom_link']; |
|
141
|
|
|
} else { |
|
142
|
|
|
$url = $foogallery_attachment->url; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
//fallback for images that might not have a custom url |
|
146
|
|
|
if ( empty( $url ) ) { |
|
147
|
|
|
$url = $foogallery_attachment->url; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$attr = array(); |
|
151
|
|
|
|
|
152
|
|
|
//only add href and target attributes to the anchor if the link is NOT set to 'none' |
|
153
|
|
|
if ( $link !== 'none' ){ |
|
154
|
|
|
$attr['href'] = $url; |
|
155
|
|
|
if ( ! empty( $foogallery_attachment->custom_target ) && 'default' !== $foogallery_attachment->custom_target ) { |
|
156
|
|
|
$attr['target'] = $foogallery_attachment->custom_target; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ( ! empty( $foogallery_attachment->caption ) ) { |
|
161
|
|
|
$attr['data-caption-title'] = $foogallery_attachment->caption; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ( !empty( $foogallery_attachment->description ) ) { |
|
165
|
|
|
$attr['data-caption-desc'] = $foogallery_attachment->description; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$attr['data-attachment-id'] = $foogallery_attachment->ID; |
|
169
|
|
|
|
|
170
|
|
|
//pull any custom attributes out the args |
|
171
|
|
|
if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
|
172
|
|
|
$attr = array_merge( $attr, $args['link_attributes'] ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment ); |
|
176
|
|
|
|
|
177
|
|
|
//always add the fg-thumb class |
|
178
|
|
|
if ( array_key_exists( 'class', $attr ) ) { |
|
179
|
|
|
$attr['class'] .= ' fg-thumb'; |
|
180
|
|
|
} else { |
|
181
|
|
|
$attr['class'] = 'fg-thumb'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $attr; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Returns the attachment anchor HTML |
|
189
|
|
|
* |
|
190
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
191
|
|
|
* @param array $args |
|
192
|
|
|
* @param bool $output_image |
|
193
|
|
|
* @param bool $output_closing_tag |
|
194
|
|
|
* |
|
195
|
|
|
* @since 1.4.0 |
|
196
|
|
|
* |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) { |
|
200
|
|
|
if ( empty ( $foogallery_attachment->url ) ) { |
|
201
|
|
|
return ''; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$html = foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args ); |
|
205
|
|
|
|
|
206
|
|
|
if ( $output_image ) { |
|
207
|
|
|
$html .= foogallery_attachment_html_image( $foogallery_attachment, $args );; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if ( $output_closing_tag ) { |
|
211
|
|
|
$html .= '</a>'; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return apply_filters( 'foogallery_attachment_html_anchor', $html, $args, $foogallery_attachment ); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Builds up the captions for an attachment |
|
219
|
|
|
* |
|
220
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
221
|
|
|
* @param array $args |
|
222
|
|
|
* |
|
223
|
|
|
* @since 1.4.9 |
|
224
|
|
|
* |
|
225
|
|
|
* @return array|bool |
|
226
|
|
|
*/ |
|
227
|
|
|
function foogallery_build_attachment_html_caption( $foogallery_attachment, $args = array() ) { |
|
228
|
|
|
$captions = array(); |
|
229
|
|
|
|
|
230
|
|
|
$preset = foogallery_gallery_template_setting( 'caption_preset', 'fg-custom' ); |
|
231
|
|
|
|
|
232
|
|
|
if ( 'none' !== $preset ) { |
|
233
|
|
|
$caption_html = array(); |
|
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
$show_caption_title = false; |
|
|
|
|
|
|
236
|
|
|
$show_caption_desc = false; |
|
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
//check if we have provided overrides for the caption title |
|
239
|
|
|
if ( isset( $args['override_caption_title'] ) ) { |
|
240
|
|
|
$caption_title = $args['override_caption_title']; |
|
241
|
|
|
$show_caption_title = true; |
|
242
|
|
|
} else { |
|
243
|
|
|
$caption_title_source = foogallery_gallery_template_setting( 'caption_title_source', '' ); |
|
244
|
|
|
|
|
245
|
|
|
//if we need to use the settings, then make sure our source is false |
|
246
|
|
|
if ( empty( $caption_title_source ) ) { $caption_title_source = false; } |
|
247
|
|
|
|
|
248
|
|
|
if ( 'fg-custom' === $preset ) { |
|
249
|
|
|
$show_caption_title = $caption_title_source !== 'none'; |
|
250
|
|
|
} else { |
|
251
|
|
|
//always show both title and desc for the presets |
|
252
|
|
|
$show_caption_title = true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
//get the correct captions |
|
256
|
|
|
$caption_title = foogallery_get_caption_title_for_attachment( $foogallery_attachment->_post, $caption_title_source ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
//check if we have provided overrides for the caption description |
|
260
|
|
|
if ( isset( $args['override_caption_desc'] ) ) { |
|
261
|
|
|
$caption_desc = $args['override_caption_desc']; |
|
262
|
|
|
$show_caption_desc = true; |
|
263
|
|
|
} else { |
|
264
|
|
|
|
|
265
|
|
|
$caption_desc_source = foogallery_gallery_template_setting( 'caption_desc_source', '' ); |
|
266
|
|
|
|
|
267
|
|
|
//if we need to use the settings, then make sure our source is false |
|
268
|
|
|
if ( empty( $caption_desc_source ) ) { $caption_desc_source = false; } |
|
269
|
|
|
|
|
270
|
|
|
if ( 'fg-custom' === $preset ) { |
|
271
|
|
|
$show_caption_desc = $caption_desc_source !== 'none'; |
|
272
|
|
|
} else { |
|
273
|
|
|
//always show both title and desc for the presets |
|
274
|
|
|
$show_caption_desc = true; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$caption_desc = foogallery_get_caption_desc_for_attachment( $foogallery_attachment->_post, $caption_desc_source ); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if ( $caption_title && $show_caption_title ) { |
|
281
|
|
|
$captions['title'] = $caption_title; |
|
282
|
|
|
} |
|
283
|
|
|
if ( $caption_desc && $show_caption_desc ) { |
|
284
|
|
|
$captions['desc'] = $caption_desc; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
} else { |
|
288
|
|
|
$captions = false; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return apply_filters( 'foogallery_build_attachment_html_caption', $captions, $foogallery_attachment, $args ); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Returns generic html for captions |
|
296
|
|
|
* |
|
297
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
298
|
|
|
* @param array $args |
|
299
|
|
|
* |
|
300
|
|
|
* @since 1.4.0 |
|
301
|
|
|
* |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
function foogallery_attachment_html_caption( $foogallery_attachment, $args = array() ) { |
|
305
|
|
|
$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args ); |
|
306
|
|
|
$html = ''; |
|
307
|
|
|
|
|
308
|
|
|
if ( $captions !== false ) { |
|
309
|
|
|
$html = '<figcaption class="fg-caption"><div class="fg-caption-inner">'; |
|
310
|
|
|
if ( array_key_exists( 'title', $captions ) ) { |
|
311
|
|
|
$html .= '<div class="fg-caption-title">' . $captions['title'] . '</div>'; |
|
312
|
|
|
} |
|
313
|
|
|
if ( array_key_exists( 'desc', $captions ) ) { |
|
314
|
|
|
$html .= '<div class="fg-caption-desc">' . $captions['desc'] . '</div>'; |
|
315
|
|
|
} |
|
316
|
|
|
$html .= '</div></figcaption>'; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args ); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Returns the attachment item opening HTML |
|
324
|
|
|
* |
|
325
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
326
|
|
|
* @param array $args |
|
327
|
|
|
* |
|
328
|
|
|
* @since 1.4.0 |
|
329
|
|
|
* |
|
330
|
|
|
* @return string |
|
331
|
|
|
*/ |
|
332
|
|
|
function foogallery_attachment_html_item_opening($foogallery_attachment, $args = array() ) { |
|
333
|
|
|
|
|
334
|
|
|
$classes[] = 'fg-item'; |
|
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
$classes = apply_filters( 'foogallery_attachment_html_item_classes', $classes, $foogallery_attachment, $args ); |
|
337
|
|
|
|
|
338
|
|
|
$class_list = ''; |
|
339
|
|
|
if ( is_array( $classes ) ) { |
|
340
|
|
|
$class_list = implode( ' ', $classes ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$html = '<div class="' . $class_list . '"><figure class="fg-item-inner">'; |
|
344
|
|
|
return apply_filters( 'foogallery_attachment_html_item_opening', $html, $foogallery_attachment, $args ); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Returns generic html for an attachment |
|
349
|
|
|
* |
|
350
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
351
|
|
|
* @param array $args |
|
352
|
|
|
* |
|
353
|
|
|
* @since 1.4.0 |
|
354
|
|
|
* |
|
355
|
|
|
* @return string |
|
356
|
|
|
*/ |
|
357
|
|
|
function foogallery_attachment_html( $foogallery_attachment, $args = array() ) { |
|
358
|
|
|
|
|
359
|
|
|
//check if no arguments were passed in, and build them up if so |
|
360
|
|
|
if ( empty( $args ) ) { |
|
361
|
|
|
$args = foogallery_gallery_template_arguments(); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
$html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args ); |
|
365
|
|
|
$html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args ); |
|
366
|
|
|
$html .= foogallery_attachment_html_image( $foogallery_attachment, $args ); |
|
367
|
|
|
$html .= '</a>'; |
|
368
|
|
|
$html .= foogallery_attachment_html_caption( $foogallery_attachment, $args ); |
|
369
|
|
|
$html .= '</figure></div>'; |
|
370
|
|
|
return $html; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Get the foogallery template arguments for the current foogallery that is being output to the frontend |
|
375
|
|
|
* |
|
376
|
|
|
* @return array |
|
377
|
|
|
*/ |
|
378
|
|
|
function foogallery_gallery_template_arguments() { |
|
379
|
|
|
global $current_foogallery_template; |
|
|
|
|
|
|
380
|
|
|
|
|
381
|
|
|
return apply_filters( 'foogallery_gallery_template_arguments-' . $current_foogallery_template, array() ); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* Build up an object that will be encoded to JSON for a FooGallery Attachment |
|
386
|
|
|
* |
|
387
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
388
|
|
|
* @param array $args |
|
389
|
|
|
* |
|
390
|
|
|
* @since 1.6.0 |
|
391
|
|
|
* |
|
392
|
|
|
* @returns string |
|
393
|
|
|
* @return string |
|
394
|
|
|
*/ |
|
395
|
|
|
function foogallery_build_json_object_from_attachment( $foogallery_attachment, $args = array() ) { |
|
396
|
|
|
if ( isset( $foogallery_attachment ) ) { |
|
397
|
|
|
|
|
398
|
|
|
//check if no arguments were passed in, and build them up if so |
|
399
|
|
|
if ( empty( $args ) ) { |
|
400
|
|
|
$args = foogallery_gallery_template_arguments(); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
$anchor_attributes = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args ); |
|
404
|
|
|
$image_attributes = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args ); |
|
405
|
|
|
$captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args ); |
|
406
|
|
|
|
|
407
|
|
|
if ( array_key_exists( 'src', $image_attributes ) ) { |
|
408
|
|
|
$src = $image_attributes['src']; |
|
409
|
|
|
} else if ( array_key_exists( 'data-src-fg', $image_attributes ) ) { |
|
410
|
|
|
$src = $image_attributes['data-src-fg']; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
if ( array_key_exists( 'srcset', $image_attributes ) ) { |
|
414
|
|
|
$srcset = $image_attributes['srcset']; |
|
415
|
|
|
} else if ( array_key_exists( 'data-srcset-fg', $image_attributes ) ) { |
|
416
|
|
|
$srcset = $image_attributes['data-srcset-fg']; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
$json_object = new stdClass(); |
|
420
|
|
|
$json_object->href = $anchor_attributes['href']; |
|
421
|
|
|
if ( isset( $src ) ) { |
|
422
|
|
|
$json_object->src = $src; |
|
423
|
|
|
} |
|
424
|
|
|
if ( isset( $srcset ) ) { |
|
425
|
|
|
$json_object->srcset = $srcset; |
|
426
|
|
|
} |
|
427
|
|
|
if ( array_key_exists( 'width', $image_attributes ) ) { |
|
428
|
|
|
$json_object->width = $image_attributes['width']; |
|
429
|
|
|
} |
|
430
|
|
|
if ( array_key_exists( 'height', $image_attributes ) ) { |
|
431
|
|
|
$json_object->height = $image_attributes['height']; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
$json_object->alt = $foogallery_attachment->alt; |
|
435
|
|
|
|
|
436
|
|
|
$json_object_attr_anchor = new stdClass(); |
|
437
|
|
|
$json_object_attr_anchor->{'data-attachment-id'} = $foogallery_attachment->ID; |
|
438
|
|
|
|
|
439
|
|
|
if ( $captions !== false ) { |
|
440
|
|
|
if ( array_key_exists( 'title', $captions ) ) { |
|
441
|
|
|
$json_object->caption = $json_object->title = $json_object_attr_anchor->{'data-caption-title'} = $captions['title']; |
|
442
|
|
|
} |
|
443
|
|
|
if ( array_key_exists( 'desc', $captions ) ) { |
|
444
|
|
|
$json_object->description = $json_object_attr_anchor->{'data-caption-desc'} = $captions['desc']; |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
$json_object->attr = new stdClass(); |
|
449
|
|
|
$json_object->attr->anchor = $json_object_attr_anchor; |
|
450
|
|
|
|
|
451
|
|
|
$json_object = apply_filters( 'foogallery_build_attachment_json', $json_object, $foogallery_attachment, $args, $anchor_attributes, $image_attributes, $captions ); |
|
452
|
|
|
|
|
453
|
|
|
return $json_object; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
return false; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Build up a JSON string for a FooGallery Attachment |
|
461
|
|
|
* |
|
462
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
463
|
|
|
* @param array $args |
|
464
|
|
|
* |
|
465
|
|
|
* @since 1.4.9 |
|
466
|
|
|
* |
|
467
|
|
|
* @returns string |
|
468
|
|
|
* @return string |
|
469
|
|
|
*/ |
|
470
|
|
|
function foogallery_build_json_from_attachment( $foogallery_attachment, $args = array() ) { |
|
471
|
|
|
if ( isset( $foogallery_attachment ) ) { |
|
472
|
|
|
|
|
473
|
|
|
$json_object = foogallery_build_json_object_from_attachment( $foogallery_attachment, $args ); |
|
474
|
|
|
|
|
475
|
|
|
if ( defined( 'JSON_UNESCAPED_UNICODE' ) ) { |
|
476
|
|
|
return json_encode( $json_object, JSON_UNESCAPED_UNICODE ); |
|
477
|
|
|
} else { |
|
478
|
|
|
return json_encode( $json_object ); |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
return ''; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.