|
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 HTML img tag for the attachment |
|
88
|
|
|
* @param array $args |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function html_img( $args = array() ) { |
|
93
|
|
|
$attr['src'] = apply_filters( 'foogallery_attachment_resize_thumbnail', $this->url, $args, $this ); |
|
94
|
|
|
|
|
95
|
|
|
if ( ! empty( $this->alt ) ) { |
|
96
|
|
|
$attr['alt'] = $this->alt; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//pull any custom attributes out the args |
|
100
|
|
|
if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) { |
|
101
|
|
|
$attr = array_merge( $attr, $args['image_attributes'] ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
//check for width and height args and add those to the image |
|
105
|
|
|
if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) { |
|
106
|
|
|
$attr['width'] = $args['width']; |
|
107
|
|
|
} |
|
108
|
|
|
if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) { |
|
109
|
|
|
$attr['height'] = $args['height']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $this ); |
|
113
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
|
114
|
|
|
$html = '<img '; |
|
115
|
|
|
foreach ( $attr as $name => $value ) { |
|
116
|
|
|
$html .= " $name=" . '"' . $value . '"'; |
|
117
|
|
|
} |
|
118
|
|
|
$html .= ' />'; |
|
119
|
|
|
|
|
120
|
|
|
return apply_filters( 'foogallery_attachment_html_image', $html, $args, $this ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns HTML for the attachment |
|
125
|
|
|
* @param array $args |
|
126
|
|
|
* @param bool $output_image |
|
127
|
|
|
* @param bool $output_closing_tag |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function html( $args = array(), $output_image = true, $output_closing_tag = true ) { |
|
132
|
|
|
if ( empty ( $this->url ) ) { |
|
133
|
|
|
return ''; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$arg_defaults = array( |
|
137
|
|
|
'link' => 'image', |
|
138
|
|
|
'custom_link' => $this->custom_url |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$args = wp_parse_args( $args, $arg_defaults ); |
|
142
|
|
|
|
|
143
|
|
|
$link = $args['link']; |
|
144
|
|
|
|
|
145
|
|
|
$img = $this->html_img( $args ); |
|
146
|
|
|
|
|
147
|
|
|
/* 12 Apr 2016 - PLEASE NOTE |
|
148
|
|
|
We no longer just return the image html when "no link" option is chosen. |
|
149
|
|
|
It was decided that it is better to return an anchor link with no href or target attributes. |
|
150
|
|
|
This results in more standardized HTML output for better CSS and JS code |
|
151
|
|
|
*/ |
|
152
|
|
|
|
|
153
|
|
|
if ( 'page' === $link ) { |
|
154
|
|
|
//get the URL to the attachment page |
|
155
|
|
|
$url = get_attachment_link( $this->ID ); |
|
156
|
|
|
} else if ( 'custom' === $link ) { |
|
157
|
|
|
$url = $args['custom_link']; |
|
158
|
|
|
} else { |
|
159
|
|
|
$url = $this->url; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
//fallback for images that might not have a custom url |
|
163
|
|
|
if ( empty( $url ) ) { |
|
164
|
|
|
$url = $this->url; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$attr = array(); |
|
168
|
|
|
|
|
169
|
|
|
//only add href and target attributes to the anchor if the link is NOT set to 'none' |
|
170
|
|
|
if ( $link !== 'none' ){ |
|
171
|
|
|
$attr['href'] = $url; |
|
172
|
|
|
if ( ! empty( $this->custom_target ) && 'default' !== $this->custom_target ) { |
|
173
|
|
|
$attr['target'] = $this->custom_target; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if ( ! empty( $this->caption ) ) { |
|
178
|
|
|
$attr['data-caption-title'] = $this->caption; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
if ( !empty( $this->description ) ) { |
|
182
|
|
|
$attr['data-caption-desc'] = $this->description; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$attr['data-attachment-id'] = $this->ID; |
|
186
|
|
|
|
|
187
|
|
|
//pull any custom attributes out the args |
|
188
|
|
|
if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) { |
|
189
|
|
|
$attr = array_merge( $attr, $args['link_attributes'] ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $this ); |
|
193
|
|
|
$attr = array_map( 'esc_attr', $attr ); |
|
194
|
|
|
$html = '<a '; |
|
195
|
|
|
foreach ( $attr as $name => $value ) { |
|
196
|
|
|
$html .= " $name=" . '"' . $value . '"'; |
|
197
|
|
|
} |
|
198
|
|
|
$html .= '>'; |
|
199
|
|
|
if ( $output_image ) { |
|
200
|
|
|
$html .= $img; |
|
201
|
|
|
} |
|
202
|
|
|
if ( $output_closing_tag ) { |
|
203
|
|
|
$html .= '</a>'; |
|
204
|
|
|
}; |
|
205
|
|
|
|
|
206
|
|
|
return apply_filters( 'foogallery_attachment_html_link', $html, $args, $this ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns generic html for captions |
|
211
|
|
|
* |
|
212
|
|
|
* @param $caption_content string Include title, desc, or both |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
public function html_caption( $caption_content ) { |
|
217
|
|
|
$html = ''; |
|
218
|
|
|
$caption_html = array(); |
|
219
|
|
|
if ( $this->caption && ( 'title' === $caption_content || 'both' === $caption_content ) ) { |
|
220
|
|
|
$caption_html[] = '<div class="foogallery-caption-title">' . $this->caption . '</div>'; |
|
221
|
|
|
} |
|
222
|
|
|
if ( $this->description && ( 'desc' === $caption_content || 'both' === $caption_content ) ) { |
|
223
|
|
|
$caption_html[] = '<div class="foogallery-caption-desc">' . $this->description . '</div>'; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ( count($caption_html) > 0 ) { |
|
227
|
|
|
$html = '<div class="foogallery-caption"><div class="foogallery-caption-inner">'; |
|
228
|
|
|
$html .= implode( $caption_html ); |
|
229
|
|
|
$html .= '</div></div>'; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return apply_filters( 'foogallery_attachment_html_caption', $html, $caption_content, $this ); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Returns responsive image html |
|
237
|
|
|
* |
|
238
|
|
|
* @param $size string Set size, defaults to full |
|
239
|
|
|
* |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
public function responsive($size = 'full'){ |
|
243
|
|
|
return wp_get_attachment_image( $this->ID, $size ); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|