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['data-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
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
68
|
|
|
$html = '<img '; |
69
|
|
|
foreach ( $attr as $name => $value ) { |
70
|
|
|
$html .= " $name=" . '"' . $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
|
|
|
$caption_title = foogallery_gallery_template_setting( 'hover_effect_title', '' ); |
200
|
|
|
$caption_desc = foogallery_gallery_template_setting( 'hover_effect_desc', '' ); |
201
|
|
|
|
202
|
|
|
if ( 'fg-custom' === $preset ) { |
203
|
|
|
|
204
|
|
|
$show_caption_title = $caption_title !== 'none'; |
205
|
|
|
$show_caption_desc = $caption_desc !== 'none'; |
206
|
|
|
|
207
|
|
|
} else { |
208
|
|
|
//always show both title and desc for the presets |
209
|
|
|
$show_caption_title = true; |
210
|
|
|
$show_caption_desc = true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ( $foogallery_attachment->caption && $show_caption_title ) { |
214
|
|
|
$caption_html[] = '<div class="fg-caption-title">' . $foogallery_attachment->caption . '</div>'; |
215
|
|
|
} |
216
|
|
|
if ( $foogallery_attachment->description && $show_caption_desc ) { |
217
|
|
|
$caption_html[] = '<div class="fg-caption-desc">' . $foogallery_attachment->description . '</div>'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$html = '<figcaption class="fg-caption"><div class="fg-caption-inner">'; |
221
|
|
|
if ( count( $caption_html ) > 0 ) { |
222
|
|
|
$html .= implode( $caption_html ); |
223
|
|
|
} |
224
|
|
|
$html .= '</div></figcaption>'; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args ); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns generic html for an attachment |
232
|
|
|
* |
233
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
234
|
|
|
* @param array $args |
235
|
|
|
* @param $caption_content string Include title, desc, or both |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
function foogallery_attachment_html( $foogallery_attachment, $args = array() ) { |
240
|
|
|
$html = '<div class="fg-item"><figure class="fg-item-inner">'; |
241
|
|
|
$html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args ); |
242
|
|
|
$html .= foogallery_attachment_html_image( $foogallery_attachment, $args ); |
243
|
|
|
$html .= '</a>'; |
244
|
|
|
$html .= foogallery_attachment_html_caption( $foogallery_attachment, $args ); |
245
|
|
|
$html .= '</figure></div>'; |
246
|
|
|
return $html; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.