1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class FooGalleryAttachment |
4
|
|
|
* |
5
|
|
|
* An easy to use wrapper class for a FooGallery Attachment |
6
|
|
|
*/ |
7
|
|
|
if ( ! class_exists( 'FooGalleryAttachment' ) ) { |
8
|
|
|
|
9
|
|
|
class FooGalleryAttachment extends stdClass { |
10
|
|
|
/** |
11
|
|
|
* public constructor |
12
|
|
|
* |
13
|
|
|
* @param null $post |
14
|
|
|
*/ |
15
|
|
|
public 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->title = ''; |
30
|
|
|
$this->caption = ''; |
31
|
|
|
$this->description = ''; |
32
|
|
|
$this->alt = ''; |
33
|
|
|
$this->url = ''; |
34
|
|
|
$this->width = 0; |
35
|
|
|
$this->height = 0; |
36
|
|
|
$this->custom_url = ''; |
37
|
|
|
$this->custom_target = ''; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* private attachment load function |
42
|
|
|
* @param $post |
43
|
|
|
*/ |
44
|
|
|
private function load( $post ) { |
45
|
|
|
$this->_post = $post; |
46
|
|
|
$this->ID = $post->ID; |
47
|
|
|
$this->title = trim( $post->post_title ); |
48
|
|
|
$this->caption = foogallery_get_caption_title_for_attachment( $post ); |
49
|
|
|
$this->description = foogallery_get_caption_desc_for_attachment( $post ); |
50
|
|
|
$this->alt = trim( get_post_meta( $this->ID, '_wp_attachment_image_alt', true ) ); |
51
|
|
|
$this->custom_url = get_post_meta( $this->ID, '_foogallery_custom_url', true ); |
52
|
|
|
$this->custom_target = get_post_meta( $this->ID, '_foogallery_custom_target', true ); |
53
|
|
|
$image_attributes = wp_get_attachment_image_src( $this->ID, 'full' ); |
54
|
|
|
if ( $image_attributes ) { |
55
|
|
|
$this->url = $image_attributes[0]; |
56
|
|
|
$this->width = $image_attributes[1]; |
57
|
|
|
$this->height = $image_attributes[2]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Static function to load a FooGalleryAttachment instance by passing in a post object |
63
|
|
|
* @static |
64
|
|
|
* |
65
|
|
|
* @param $post |
66
|
|
|
* |
67
|
|
|
* @return FooGalleryAttachment |
68
|
|
|
*/ |
69
|
|
|
public static function get( $post ) { |
70
|
|
|
return new self( $post ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Static function to load a FooGalleryAttachment instance by passing in an attachment_id |
75
|
|
|
* @static |
76
|
|
|
* |
77
|
|
|
* @param $attachment_id |
78
|
|
|
* |
79
|
|
|
* @return FooGalleryAttachment |
80
|
|
|
*/ |
81
|
|
|
public static function get_by_id( $attachment_id ) { |
82
|
|
|
$post = get_post( $attachment_id ); |
83
|
|
|
return new self( $post ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the image source only |
88
|
|
|
* |
89
|
|
|
* @param array $args |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function html_img_src( $args = array() ) { |
93
|
|
|
return apply_filters( 'foogallery_attachment_resize_thumbnail', $this->url, $args, $this ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the HTML img tag for the attachment |
98
|
|
|
* @param array $args |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function html_img( $args = array() ) { |
103
|
|
|
$attr['src'] = $this->html_img_src( $args ); |
|
|
|
|
104
|
|
|
|
105
|
|
|
if ( ! empty( $this->alt ) ) { |
106
|
|
|
$attr['alt'] = $this->alt; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//pull any custom attributes out the args |
110
|
|
|
if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) { |
111
|
|
|
$attr = array_merge( $attr, $args['image_attributes'] ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//check for width and height args and add those to the image |
115
|
|
|
if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) { |
116
|
|
|
$attr['width'] = $args['width']; |
117
|
|
|
} |
118
|
|
|
if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) { |
119
|
|
|
$attr['height'] = $args['height']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $this ); |
123
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
124
|
|
|
$html = '<img '; |
125
|
|
|
foreach ( $attr as $name => $value ) { |
126
|
|
|
$html .= " $name=" . '"' . $value . '"'; |
127
|
|
|
} |
128
|
|
|
$html .= ' />'; |
129
|
|
|
|
130
|
|
|
return apply_filters( 'foogallery_attachment_html_image', $html, $args, $this ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns HTML for the attachment |
135
|
|
|
* @param array $args |
136
|
|
|
* @param bool $output_image |
137
|
|
|
* @param bool $output_closing_tag |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function html( $args = array(), $output_image = true, $output_closing_tag = true ) { |
142
|
|
|
if ( empty ( $this->url ) ) { |
143
|
|
|
return ''; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$arg_defaults = array( |
147
|
|
|
'link' => 'image', |
148
|
|
|
'custom_link' => $this->custom_url |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
$args = wp_parse_args( $args, $arg_defaults ); |
152
|
|
|
|
153
|
|
|
$link = $args['link']; |
154
|
|
|
|
155
|
|
|
$img = $this->html_img( $args ); |
156
|
|
|
|
157
|
|
|
/* 12 Apr 2016 - PLEASE NOTE |
158
|
|
|
We no longer just return the image html when "no link" option is chosen. |
159
|
|
|
It was decided that it is better to return an anchor link with no href or target attributes. |
160
|
|
|
This results in more standardized HTML output for better CSS and JS code |
161
|
|
|
*/ |
162
|
|
|
|
163
|
|
|
if ( 'page' === $link ) { |
164
|
|
|
//get the URL to the attachment page |
165
|
|
|
$url = get_attachment_link( $this->ID ); |
166
|
|
|
} else if ( 'custom' === $link ) { |
167
|
|
|
$url = $args['custom_link']; |
168
|
|
|
} else { |
169
|
|
|
$url = $this->url; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
//fallback for images that might not have a custom url |
173
|
|
|
if ( empty( $url ) ) { |
174
|
|
|
$url = $this->url; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$attr = array(); |
178
|
|
|
|
179
|
|
|
//only add href and target attributes to the anchor if the link is NOT set to 'none' |
180
|
|
|
if ( $link !== 'none' ){ |
181
|
|
|
$attr['href'] = $url; |
182
|
|
|
if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) { |
183
|
|
|
$attr['target'] = $this->custom_target; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ( ! empty( $this->caption ) ) { |
188
|
|
|
$attr['data-caption-title'] = $this->caption; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ( !empty( $this->description ) ) { |
192
|
|
|
$attr['data-caption-desc'] = $this->description; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$attr['data-attachment-id'] = $this->ID; |
196
|
|
|
|
197
|
|
|
//pull any custom attributes out the args |
198
|
|
|
if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
199
|
|
|
$attr = array_merge( $attr, $args['link_attributes'] ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this ); |
203
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
204
|
|
|
$html = '<a '; |
205
|
|
|
foreach ( $attr as $name => $value ) { |
206
|
|
|
$html .= " $name=" . '"' . $value . '"'; |
207
|
|
|
} |
208
|
|
|
$html .= '>'; |
209
|
|
|
if ( $output_image ) { |
210
|
|
|
$html .= $img; |
211
|
|
|
} |
212
|
|
|
if ( $output_closing_tag ) { |
213
|
|
|
$html .= '</a>'; |
214
|
|
|
}; |
215
|
|
|
|
216
|
|
|
return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns generic html for captions |
221
|
|
|
* |
222
|
|
|
* @param $caption_content string Include title, desc, or both |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function html_caption( $caption_content ) { |
227
|
|
|
$html = ''; |
228
|
|
|
$caption_html = array(); |
229
|
|
|
if ( $this->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) { |
230
|
|
|
$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>'; |
231
|
|
|
} |
232
|
|
|
if ( $this->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) { |
233
|
|
|
$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>'; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ( count($caption_html) > 0 ) { |
237
|
|
|
$html = '<div class="foogallery-caption"><div class="foogallery-caption-inner">'; |
238
|
|
|
$html .= implode( $caption_html ); |
239
|
|
|
$html .= '</div></div>'; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this ); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
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.