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