|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* FooGallery helper functions for rendering HTML |
|
5
|
|
|
* Created by Brad Vincent |
|
6
|
|
|
* Date: 11/07/2017 |
|
7
|
|
|
* |
|
8
|
|
|
* @since 2.0.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Returns the attachment image source only |
|
13
|
|
|
* |
|
14
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
15
|
|
|
* @param array $args |
|
16
|
|
|
* |
|
17
|
|
|
* @since 2.0.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 2.0.0 |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
function foogallery_attachment_html_image( $foogallery_attachment, $args = array() ) { |
|
36
|
|
|
$attr['src'] = foogallery_attachment_html_image_src( $foogallery_attachment, $args ); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
if ( ! empty( $foogallery_attachment->alt ) ) { |
|
39
|
|
|
$attr['alt'] = $foogallery_attachment->alt; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ( ! empty( $foogallery_attachment->caption ) ) { |
|
43
|
|
|
$attr['title'] = $foogallery_attachment->caption; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
//pull any custom attributes out the args |
|
47
|
|
|
if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) { |
|
48
|
|
|
$attr = array_merge( $attr, $args['image_attributes'] ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
//check for width and height args and add those to the image |
|
52
|
|
|
if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) { |
|
53
|
|
|
$attr['width'] = $args['width']; |
|
54
|
|
|
} |
|
55
|
|
|
if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) { |
|
56
|
|
|
$attr['height'] = $args['height']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $foogallery_attachment ); |
|
60
|
|
|
|
|
61
|
|
|
if ( array_key_exists( 'class', $attr ) ) { |
|
62
|
|
|
$attr['class'] .= ' fg-image'; |
|
63
|
|
|
} else { |
|
64
|
|
|
$attr['class'] = 'fg-image'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$html = '<img '; |
|
68
|
|
|
foreach ( $attr as $name => $value ) { |
|
69
|
|
|
$name = str_replace(' ', '', $name); //ensure we have no spaces! |
|
70
|
|
|
$html .= " $name=" . '"' . esc_attr($value) . '"'; |
|
71
|
|
|
} |
|
72
|
|
|
$html .= ' />'; |
|
73
|
|
|
|
|
74
|
|
|
return apply_filters( 'foogallery_attachment_html_image', $html, $args, $foogallery_attachment ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the attachment anchor HTML opening tag |
|
79
|
|
|
* |
|
80
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
81
|
|
|
* @param array $args |
|
82
|
|
|
* @param bool $output_image |
|
|
|
|
|
|
83
|
|
|
* @param bool $output_closing_tag |
|
|
|
|
|
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
function foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args = array() ) { |
|
88
|
|
|
$arg_defaults = array( |
|
89
|
|
|
'link' => 'image', |
|
90
|
|
|
'custom_link' => $foogallery_attachment->custom_url |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$args = wp_parse_args( $args, $arg_defaults ); |
|
94
|
|
|
|
|
95
|
|
|
$link = $args['link']; |
|
96
|
|
|
|
|
97
|
|
|
if ( 'page' === $link ) { |
|
98
|
|
|
//get the URL to the attachment page |
|
99
|
|
|
$url = get_attachment_link( $foogallery_attachment->ID ); |
|
100
|
|
|
} else if ( 'custom' === $link ) { |
|
101
|
|
|
$url = $args['custom_link']; |
|
102
|
|
|
} else { |
|
103
|
|
|
$url = $foogallery_attachment->url; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
//fallback for images that might not have a custom url |
|
107
|
|
|
if ( empty( $url ) ) { |
|
108
|
|
|
$url = $foogallery_attachment->url; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$attr = array(); |
|
112
|
|
|
|
|
113
|
|
|
//only add href and target attributes to the anchor if the link is NOT set to 'none' |
|
114
|
|
|
if ( $link !== 'none' ){ |
|
115
|
|
|
$attr['href'] = $url; |
|
116
|
|
|
if ( ! empty( $foogallery_attachment->custom_target ) && 'default' !== $foogallery_attachment->custom_target ) { |
|
117
|
|
|
$attr['target'] = $foogallery_attachment->custom_target; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ( ! empty( $foogallery_attachment->caption ) ) { |
|
122
|
|
|
$attr['data-caption-title'] = $foogallery_attachment->caption; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ( !empty( $foogallery_attachment->description ) ) { |
|
126
|
|
|
$attr['data-caption-desc'] = $foogallery_attachment->description; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$attr['data-attachment-id'] = $foogallery_attachment->ID; |
|
130
|
|
|
|
|
131
|
|
|
//pull any custom attributes out the args |
|
132
|
|
|
if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
|
133
|
|
|
$attr = array_merge( $attr, $args['link_attributes'] ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment ); |
|
137
|
|
|
|
|
138
|
|
|
if ( array_key_exists( 'class', $attr ) ) { |
|
139
|
|
|
$attr['class'] .= ' fg-thumb'; |
|
140
|
|
|
} else { |
|
141
|
|
|
$attr['class'] = 'fg-thumb'; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
|
145
|
|
|
$html = '<a '; |
|
146
|
|
|
foreach ( $attr as $name => $value ) { |
|
147
|
|
|
$html .= " $name=" . '"' . $value . '"'; |
|
148
|
|
|
} |
|
149
|
|
|
$html .= '>'; |
|
150
|
|
|
|
|
151
|
|
|
return apply_filters( 'foogallery_attachment_html_anchor_opening', $html, $args, $foogallery_attachment ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns the attachment anchor HTML |
|
156
|
|
|
* |
|
157
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
158
|
|
|
* @param array $args |
|
159
|
|
|
* @param bool $output_image |
|
160
|
|
|
* @param bool $output_closing_tag |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) { |
|
165
|
|
|
if ( empty ( $foogallery_attachment->url ) ) { |
|
166
|
|
|
return ''; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$html = foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args ); |
|
170
|
|
|
|
|
171
|
|
|
if ( $output_image ) { |
|
172
|
|
|
$html .= foogallery_attachment_html_image( $foogallery_attachment, $args );; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ( $output_closing_tag ) { |
|
176
|
|
|
$html .= '</a>'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return apply_filters( 'foogallery_attachment_html_anchor', $html, $args, $foogallery_attachment ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Returns generic html for captions |
|
184
|
|
|
* |
|
185
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
186
|
|
|
* @param array $args |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
function foogallery_attachment_html_caption( $foogallery_attachment, $args = array() ) { |
|
191
|
|
|
|
|
192
|
|
|
$preset = foogallery_gallery_template_setting( 'caption_preset', 'fg-custom' ); |
|
193
|
|
|
|
|
194
|
|
|
$html = ''; |
|
195
|
|
|
|
|
196
|
|
|
if ( 'none' !== $preset ) { |
|
197
|
|
|
$caption_html = array(); |
|
198
|
|
|
|
|
199
|
|
|
$show_caption_title = false; |
|
|
|
|
|
|
200
|
|
|
$show_caption_desc = false; |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
//check if we have provided overrides for the caption title |
|
203
|
|
|
if ( isset( $args['override_caption_title'] ) ) { |
|
204
|
|
|
$caption_title = $args['override_caption_title']; |
|
205
|
|
|
$show_caption_title = true; |
|
206
|
|
|
} else { |
|
207
|
|
|
$caption_title_source = foogallery_gallery_template_setting( 'caption_title_source', '' ); |
|
208
|
|
|
|
|
209
|
|
|
//if we need to use the settings, then make sure our source is false |
|
210
|
|
|
if ( empty( $caption_title_source ) ) { $caption_title_source = false; } |
|
211
|
|
|
|
|
212
|
|
|
if ( 'fg-custom' === $preset ) { |
|
213
|
|
|
$show_caption_title = $caption_title_source !== 'none'; |
|
214
|
|
|
} else { |
|
215
|
|
|
//always show both title and desc for the presets |
|
216
|
|
|
$show_caption_title = true; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
//get the correct captions |
|
220
|
|
|
$caption_title = foogallery_get_caption_title_for_attachment( $foogallery_attachment->_post, $caption_title_source ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
//check if we have provided overrides for the caption description |
|
224
|
|
|
if ( isset( $args['override_caption_desc'] ) ) { |
|
225
|
|
|
$caption_desc = $args['override_caption_desc']; |
|
226
|
|
|
$show_caption_desc = true; |
|
227
|
|
|
} else { |
|
228
|
|
|
|
|
229
|
|
|
$caption_desc_source = foogallery_gallery_template_setting( 'caption_desc_source', '' ); |
|
230
|
|
|
|
|
231
|
|
|
//if we need to use the settings, then make sure our source is false |
|
232
|
|
|
if ( empty( $caption_desc_source ) ) { $caption_desc_source = false; } |
|
233
|
|
|
|
|
234
|
|
|
if ( 'fg-custom' === $preset ) { |
|
235
|
|
|
$show_caption_desc = $caption_desc_source !== 'none'; |
|
236
|
|
|
} else { |
|
237
|
|
|
//always show both title and desc for the presets |
|
238
|
|
|
$show_caption_desc = true; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$caption_desc = foogallery_get_caption_desc_for_attachment( $foogallery_attachment->_post, $caption_desc_source ); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
if ( $caption_title && $show_caption_title ) { |
|
245
|
|
|
$caption_html[] = '<div class="fg-caption-title">' . $caption_title . '</div>'; |
|
246
|
|
|
} |
|
247
|
|
|
if ( $caption_desc && $show_caption_desc ) { |
|
248
|
|
|
$caption_html[] = '<div class="fg-caption-desc">' . $caption_desc . '</div>'; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$html = '<figcaption class="fg-caption"><div class="fg-caption-inner">'; |
|
252
|
|
|
if ( count( $caption_html ) > 0 ) { |
|
253
|
|
|
$html .= implode( $caption_html ); |
|
254
|
|
|
} |
|
255
|
|
|
$html .= '</div></figcaption>'; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args ); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
function foogallery_attachment_html_item_opening($foogallery_attachment, $args = array() ) { |
|
262
|
|
|
|
|
263
|
|
|
$classes[] = 'fg-item'; |
|
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
$classes = apply_filters( 'foogallery_attachment_html_item_classes', $classes, $foogallery_attachment, $args ); |
|
266
|
|
|
|
|
267
|
|
|
$class_list = ''; |
|
268
|
|
|
if ( is_array( $classes ) ) { |
|
269
|
|
|
$class_list = implode( ' ', $classes ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$html = '<div class="' . $class_list . '"><figure class="fg-item-inner">'; |
|
273
|
|
|
return apply_filters( 'foogallery_attachment_html_item_opening', $html, $foogallery_attachment, $args ); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Returns generic html for an attachment |
|
278
|
|
|
* |
|
279
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
280
|
|
|
* @param array $args |
|
281
|
|
|
* @param $caption_content string Include title, desc, or both |
|
282
|
|
|
* |
|
283
|
|
|
* @return string |
|
284
|
|
|
*/ |
|
285
|
|
|
function foogallery_attachment_html( $foogallery_attachment, $args = array() ) { |
|
286
|
|
|
$html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args ); |
|
287
|
|
|
$html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args ); |
|
288
|
|
|
$html .= foogallery_attachment_html_image( $foogallery_attachment, $args ); |
|
289
|
|
|
$html .= '</a>'; |
|
290
|
|
|
$html .= foogallery_attachment_html_caption( $foogallery_attachment, $args ); |
|
291
|
|
|
$html .= '</figure></div>'; |
|
292
|
|
|
return $html; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
|
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.