PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.4.7
Responsive Lightbox & Gallery v2.4.7
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / class-frontend.php
responsive-lightbox / includes Last commit date
providers 2 years ago class-fast-image.php 2 years ago class-folders.php 2 years ago class-frontend.php 2 years ago class-galleries.php 2 years ago class-multilang.php 2 years ago class-remote-library-api.php 2 years ago class-remote-library.php 2 years ago class-settings.php 2 years ago class-tour.php 2 years ago class-welcome.php 2 years ago class-widgets.php 2 years ago functions.php 2 years ago
class-frontend.php
2495 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 new Responsive_Lightbox_Frontend();
7
8 /**
9 * Responsive Lightbox frontend class.
10 *
11 * @class Responsive_Lightbox_Frontend
12 */
13 class Responsive_Lightbox_Frontend {
14
15 public $gallery_no = 0;
16
17 /**
18 * Class constructor.
19 *
20 * @return void
21 */
22 public function __construct() {
23 // set instance
24 Responsive_Lightbox()->frontend = $this;
25
26 // actions
27 add_action( 'wp_enqueue_scripts', [ $this, 'wp_dequeue_scripts' ], 100 );
28 add_action( 'rl_before_gallery', [ $this, 'before_gallery' ], 10, 2 );
29 add_action( 'rl_after_gallery', [ $this, 'after_gallery' ], 10, 2 );
30 add_action( 'after_setup_theme', [ $this, 'woocommerce_gallery_init' ], 1000 );
31
32 // filters
33 add_filter( 'rl_gallery_container_class', [ $this, 'gallery_container_class' ], 10, 3 );
34 add_filter( 'the_content', [ $this, 'gallery_preview' ] );
35 add_filter( 'the_content', [ $this, 'add_lightbox' ], 11 );
36 add_filter( 'wp_get_attachment_link', [ $this, 'wp_get_attachment_link' ], 1000, 2 );
37 add_filter( 'get_comment_text', [ $this, 'get_comment_text' ] );
38 add_filter( 'dynamic_sidebar_params', [ $this, 'dynamic_sidebar_params' ] );
39 add_filter( 'rl_widget_output', [ $this, 'widget_output' ], 10, 3 );
40 add_filter( 'post_gallery', [ $this, 'gallery_attributes' ], 1000, 2 );
41 add_filter( 'post_gallery', [ $this, 'basic_grid_gallery_shortcode' ], 1001, 2 );
42 add_filter( 'post_gallery', [ $this, 'basic_slider_gallery_shortcode' ], 1001, 2 );
43 add_filter( 'post_gallery', [ $this, 'basic_masonry_gallery_shortcode' ], 1001, 2 );
44 add_filter( 'post_gallery', [ $this, 'force_custom_gallery_lightbox' ], 2000 );
45
46 // visual composer
47 add_filter( 'vc_shortcode_content_filter_after', [ $this, 'vc_shortcode_content_filter_after' ], 10, 2 );
48
49 // woocommerce
50 add_filter( 'woocommerce_single_product_image_html', [ $this, 'woocommerce_single_product_image_html' ], 100 );
51 add_filter( 'woocommerce_single_product_image_thumbnail_html', [ $this, 'woocommerce_single_product_image_thumbnail_html' ], 100, 2 );
52 }
53
54 /**
55 * Get class data.
56 *
57 * @param string $attr
58 * @return mixed
59 */
60 public function get_data( $attr ) {
61 return property_exists( $this, $attr ) ? $this->{$attr} : null;
62 }
63
64 /**
65 * Add lightbox to images, galleries and videos.
66 *
67 * @param string $content HTML content
68 * @return string
69 */
70 public function add_lightbox( $content ) {
71 // get main instance
72 $rl = Responsive_Lightbox();
73
74 // get current script
75 $script = $rl->get_data( 'current_script' );
76
77 // get scripts
78 $scripts = $rl->settings->get_data( 'scripts' );
79
80 // prepare arguments
81 $args = [
82 'selector' => $rl->options['settings']['selector'],
83 'script' => $script,
84 'settings' => [
85 'script' => $rl->options['configuration'][$script],
86 'plugin' => $rl->options['settings']
87 ],
88 'supports' => $scripts[$script]['supports']
89 ];
90
91 // workaround for builder galleries to bypass images_as_gallery option, applied only to rl_gallery posts
92 if ( is_singular( 'rl_gallery' ) )
93 $args['settings']['plugin']['images_as_gallery'] = true;
94
95 // search for links containing data-rl_content attribute
96 preg_match_all( '/<a.*?data-rl_content=(?:\'|")(.*?)(?:\'|").*?>/i', $content, $links );
97
98 // found any links?
99 if ( ! empty ( $links[0] ) ) {
100 foreach ( $links[0] as $link_number => $link ) {
101 // set content type
102 $args['content'] = $links[1][$link_number];
103
104 // set link number
105 $args['link_number'] = $link_number;
106
107 // update link
108 $content = str_replace( $link, $this->lightbox_content_link( $link, $args ), $content );
109 }
110 }
111
112 // images
113 if ( $args['settings']['plugin']['image_links'] || $args['settings']['plugin']['images_as_gallery'] || $args['settings']['plugin']['force_custom_gallery'] ) {
114 // search for image links
115 preg_match_all( '/<a([^>]*?)href=(?:\'|")([^>]*?)\.(bmp|gif|jpeg|jpg|png|webp)(?:\'|")(.*?)>(.*?)<\/a>/is', $content, $links );
116
117 // found any links?
118 if ( ! empty ( $links[0] ) ) {
119 // generate hash for single images gallery
120 if ( $args['settings']['plugin']['images_as_gallery'] )
121 $args['rel_hash'] = '-gallery-' . $this->generate_hash();
122 else
123 $args['rel_hash'] = '';
124
125 foreach ( $links[0] as $link_number => $link ) {
126 // get attachment id
127 $args['image_id'] = $this->get_attachment_id_by_url( $links[2][$link_number] . '.' . $links[3][$link_number] );
128
129 // set link number
130 $args['link_number'] = $link_number;
131
132 // link parts
133 $args['link_parts'] = [ $links[1][$link_number], $links[2][$link_number], $links[3][$link_number], $links[4][$link_number], $links[5][$link_number] ];
134
135 // get title type
136 $title_arg = $args['settings']['plugin']['force_custom_gallery'] ? $args['settings']['plugin']['gallery_image_title'] : $args['settings']['plugin']['image_title'];
137
138 // update title if needed
139 if ( $title_arg !== 'default' && $args['image_id'] )
140 $args['title'] = $this->get_attachment_title( $args['image_id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $args['image_id'], $links[2][$link_number] . '.' . $links[3][$link_number] ) );
141 else
142 $args['title'] = '';
143
144 // get caption type
145 $caption_arg = $args['settings']['plugin']['force_custom_gallery'] ? $args['settings']['plugin']['gallery_image_caption'] : $args['settings']['plugin']['image_caption'];
146
147 // update caption if needed
148 if ( $caption_arg !== 'default' && $args['image_id'] )
149 $args['caption'] = $this->get_attachment_title( $args['image_id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $caption_arg, $args['image_id'], $links[2][$link_number] . '.' . $links[3][$link_number] ) );
150 else
151 $args['caption'] = '';
152
153 // rl gallery link?
154 if ( preg_match( '/class="(?:.*?)rl-gallery-link[^"]*?"/i', $links[1][$link_number] ) === 1 || preg_match( '/class="(?:.*?)rl-gallery-link[^"]*?"/i', $links[4][$link_number] ) === 1 ) {
155 // update link allowing only filter to run, bypass default changes
156 $content = str_replace( $link, $this->lightbox_image_link( $link, $args, true ), $content );
157 } else {
158 // update link
159 $content = str_replace( $link, $this->lightbox_image_link( $link, $args ), $content );
160 }
161 }
162 }
163 }
164
165 // videos
166 if ( $args['settings']['plugin']['videos'] ) {
167 // search for video links
168 preg_match_all('/<a([^>]*?)href=(?:\'|")((http|https)(?::\/\/|)(?:(?:(?:youtu\.be\/|(?:www\.)?youtube\.com\/)(?:embed\/|v\/|watch\?v=)?([\w-]{11})(?:\?)?([a-z0-9;:@#&%=+\/\$_.-]*))|(?:(?:www\.)?vimeo\.com\/([0-9]+)(?:\?)?([a-z0-9;:@#&%=+\/\$_.-]*))))(?:\'|")(.*?)>(.*?)<\/a>/i', $content, $links );
169
170 // set empty video arguments
171 $args['video_id'] = $args['video_type'] = $args['video_query'] = $args['video_protocol'] = '';
172
173 // found any links?
174 if ( ! empty ( $links[0] ) ) {
175 foreach ( $links[0] as $link_number => $link ) {
176 // youtube?
177 if ( $links[4][$link_number] !== '' ) {
178 $args['video_id'] = $links[4][$link_number];
179 $args['video_type'] = 'youtube';
180 $args['video_query'] = $links[5][$link_number];
181 // vimeo?
182 } elseif ( $links[6][$link_number] !== '' ) {
183 $args['video_id'] = $links[6][$link_number];
184 $args['video_type'] = 'vimeo';
185 $args['video_query'] = $links[7][$link_number];
186 }
187
188 // set video protocol
189 $args['video_protocol'] = $links[3][$link_number];
190
191 // set link number
192 $args['link_number'] = $link_number;
193
194 // link parts
195 $args['link_parts'] = [ $links[1][$link_number], $links[2][$link_number], $links[8][$link_number], $links[9][$link_number] ];
196
197 // rl gallery link?
198 if ( preg_match( '/class="(?:.*?)rl-gallery-link[^"]*?"/i', $links[1][$link_number] ) === 1 || preg_match( '/class="(?:.*?)rl-gallery-link[^"]*?"/i', $links[8][$link_number] ) === 1 ) {
199 // update link allowing only filter to run, bypass default changes
200 $content = str_replace( $link, $this->lightbox_video_link( $link, $args, true ), $content );
201 } else {
202 // update link
203 $content = str_replace( $link, $this->lightbox_video_link( $link, $args ), $content );
204 }
205 }
206 }
207 }
208
209 return $content;
210 }
211
212 /**
213 * Add lightbox to video links.
214 *
215 * @param string $link Video link
216 * @param array $args Link arguments
217 * @param bool $only_filter Whether function should run only filter
218 * @return string
219 */
220 public function lightbox_video_link( $link, $args, $only_filter = false ) {
221 if ( ! $only_filter ) {
222 // link already contains data-rel attribute?
223 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/is', $link, $result ) === 1 ) {
224 // allow to modify link?
225 if ( $result[1] !== 'norl' ) {
226 // swipebox video fix
227 if ( $args['script'] === 'swipebox' && $args['video_type'] === 'vimeo' )
228 $link = str_replace( $args['link_parts'][1], add_query_arg( 'width', $args['settings']['script']['video_max_width'], $args['link_parts'][1] ), $link );
229
230 // replace data-rel
231 $link = preg_replace( '/data-rel=(\'|")(.*?)(\'|")/', 'data-rel="' . esc_attr( $args['selector'] ) . '-video-' . (int) $args['link_number'] . '"', $link );
232
233 if ( $args['script'] === 'magnific' )
234 $link = preg_replace( '/(<a.*?)>/is', '$1 data-magnific_type="video">', $link );
235 }
236 } else {
237 // swipebox video fix
238 if ( $args['script'] === 'swipebox' && $args['video_type'] === 'vimeo' )
239 $args['link_parts'][1] = add_query_arg( 'width', $args['settings']['script']['video_max_width'], $args['link_parts'][1] );
240
241 // add data-rel
242 $link = '<a' . $args['link_parts'][0] . 'href="' . $args['link_parts'][1] . '" data-rel="' . esc_attr( $args['selector'] ) . '-video-' . (int) $args['link_number'] . '"' . $args['link_parts'][2] . '>' . $args['link_parts'][3] . '</a>';
243
244 if ( $args['script'] === 'magnific' )
245 $link = preg_replace( '/(<a.*?)>/is', '$1 data-magnific_type="video">', $link );
246 }
247 }
248
249 return apply_filters( 'rl_lightbox_video_link', $link, $args );
250 }
251
252 /**
253 * Add lightbox to image links.
254 *
255 * @param string $link Image link
256 * @param array $args Link arguments
257 * @param bool $only_filter Whether function should run only filter
258 * @return string
259 */
260 public function lightbox_image_link( $link, $args, $only_filter = false ) {
261 if ( ! $only_filter ) {
262 if ( isset( $_GET['rl_gallery_no'], $_GET['rl_page'] ) )
263 $this->gallery_no = (int) $_GET['rl_gallery_no'];
264
265 // link already contains data-rel attribute?
266 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/is', $link, $result ) === 1 ) {
267 // allow to modify link?
268 if ( $result[1] !== 'norl' ) {
269 // gallery?
270 if ( $args['settings']['plugin']['images_as_gallery'] || $args['settings']['plugin']['force_custom_gallery'] )
271 $link = preg_replace( '/data-rel=(\'|")(.*?)(\'|")/s', 'data-rel="' . esc_attr( $args['selector'] ) . '-gallery-' . esc_attr( base64_encode( $result[1] ) ) . '" data-rl_title="__RL_IMAGE_TITLE__" data-rl_caption="__RL_IMAGE_CAPTION__"' . ( $args['script'] === 'magnific' ? ' data-magnific_type="gallery"' : '' ) . ( $args['script'] === 'imagelightbox' ? ' data-imagelightbox="' . (int) $args['link_number'] . '"' : '' ), $link );
272 // single image
273 else
274 $link = preg_replace( '/data-rel=(\'|")(.*?)(\'|")/s', 'data-rel="' . esc_attr( $args['selector'] ) . '-image-' . esc_attr( base64_encode( $result[1] ) ) . '"' . ( $args['script'] === 'magnific' ? ' data-magnific_type="image"' : '' ) . ( $args['script'] === 'imagelightbox' ? ' data-imagelightbox="' . (int) $args['link_number'] . '"' : '' ) . ' data-rl_title="__RL_IMAGE_TITLE__" data-rl_caption="__RL_IMAGE_CAPTION__"', $link );
275 }
276 // link without data-rel
277 } else {
278 // force images?
279 if ( $args['settings']['plugin']['force_custom_gallery'] ) {
280 // link already contains rel attribute?
281 if ( preg_match( '/<a.*?(?:rel)=(?:\'|")(.*?)(?:\'|").*?>/is', $link, $result ) === 1 ) {
282 // allow to modify link?
283 if ( $result[1] !== 'norl' )
284 $link = preg_replace( '/rel=(\'|")(.*?)(\'|")/', 'data-rel="' . esc_attr( $args['selector'] ) . '-gallery-' . (int) $this->gallery_no . '" data-rl_title="__RL_IMAGE_TITLE__" data-rl_caption="__RL_IMAGE_CAPTION__"' . ( $args['script'] === 'magnific' ? ' data-magnific_type="gallery"' : '' ) . ( $args['script'] === 'imagelightbox' ? ' data-imagelightbox="' . (int) $args['link_number'] . '"' : '' ), $link );
285 } else
286 $link = '<a' . $args['link_parts'][0] . ' href="' . $args['link_parts'][1] . '.' . $args['link_parts'][2] . '" data-rel="' . esc_attr( $args['selector'] ) . '-gallery-' . (int) $this->gallery_no . '" data-rl_title="__RL_IMAGE_TITLE__" data-rl_caption="__RL_IMAGE_CAPTION__"' . ( $args['script'] === 'magnific' ? ' data-magnific_type="gallery"' : '' ) . ( $args['script'] === 'imagelightbox' ? ' data-imagelightbox="' . (int) $args['link_number'] . '"' : '' ) . $args['link_parts'][3] . '>' . $args['link_parts'][4] . '</a>';
287 } else
288 $link = '<a' . $args['link_parts'][0] . 'href="' . $args['link_parts'][1] . '.' . $args['link_parts'][2] . '"' . $args['link_parts'][3] . ' data-rel="' . esc_attr( $args['selector'] ) . ( $args['settings']['plugin']['images_as_gallery'] ? esc_attr( $args['rel_hash'] ) : '-image-' . (int) $args['link_number'] ) . '"' . ( $args['script'] === 'magnific' ? ' data-magnific_type="image"' : '' ) . ( $args['script'] === 'imagelightbox' ? ' data-imagelightbox="' . (int) $args['link_number'] . '"' : '' ) . ' data-rl_title="__RL_IMAGE_TITLE__" data-rl_caption="__RL_IMAGE_CAPTION__">' . $args['link_parts'][4] . '</a>';
289 }
290
291 // prepare title and caption
292 $title = trim ( nl2br( $args['title'] ) );
293 $caption = trim( nl2br( $args['caption'] ) );
294
295 if ( ! rl_current_lightbox_supports( 'html_caption' ) ) {
296 $title = wp_strip_all_tags( $title, true );
297 $caption = wp_strip_all_tags( $caption, true );
298 }
299
300 // use safe replacement for data-rl_title and data-rl_caption
301 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), str_replace( '__RL_IMAGE_CAPTION__', esc_attr( $caption ), $link ) );
302
303 // title exists?
304 if ( preg_match( '/<a.*? title=(?:\'|").*?(?:\'|").*?>/is', $link ) === 1 ) {
305 $link = preg_replace( '/(<a.*? title=(?:\'|")).*?((?:\'|").*?>)/s', '${1}__RL_IMAGE_TITLE__$2', $link );
306 } else
307 $link = preg_replace( '/(<a.*?)>/s', '$1 title="__RL_IMAGE_TITLE__">', $link );
308
309 // last safe replacement for title
310 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), $link );
311 }
312
313 return apply_filters( 'rl_lightbox_image_link', $link, $args );
314 }
315
316 /**
317 * Add lightbox to gallery image links.
318 *
319 * @param string $link
320 * @param int|object $id
321 * @return string
322 */
323 public function wp_get_attachment_link( $link, $id ) {
324 // get main instance
325 $rl = Responsive_Lightbox();
326
327 if ( $rl->options['settings']['galleries'] && wp_attachment_is_image( $id ) ) {
328 // get current script
329 $script = $rl->get_data( 'current_script' );
330
331 // get scripts
332 $scripts = $rl->settings->get_data( 'scripts' );
333
334 // prepare arguments
335 $args = [
336 'selector' => $rl->options['settings']['selector'],
337 'script' => $script,
338 'settings' => [
339 'script' => $rl->options['configuration'][$script],
340 'plugin' => $rl->options['settings']
341 ],
342 'supports' => $scripts[$script]['supports'],
343 'image_id' => is_object( $id ) ? $id->id : $id,
344 'title' => '',
345 'caption' => '',
346 'src' => []
347 ];
348
349 $link = $this->lightbox_gallery_link( $link, $args );
350 }
351
352 return $link;
353 }
354
355 /**
356 * Add lightbox to gallery image links.
357 *
358 * @param string $link Gallery image link
359 * @param array $args Gallery link arguments
360 * @return string
361 */
362 public function lightbox_gallery_link( $link, $args ) {
363 // gallery image title
364 $title = ! empty( $args['title'] ) ? $args['title'] : '';
365
366 // get title type
367 $title_arg = $args['settings']['plugin']['gallery_image_title'];
368
369 // update title if needed
370 if ( ! empty( $args['image_id'] ) && $title_arg !== 'default' ) {
371 // original title
372 $args['title'] = $this->get_attachment_title( $args['image_id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $args['image_id'], $link ) );
373 }
374
375 // prepare title
376 $title = trim ( nl2br( $args['title'] ) );
377
378 if ( ! rl_current_lightbox_supports( 'html_caption' ) )
379 $title = wp_strip_all_tags( $title, true );
380
381 // use safe replacement for title and data-rl_title
382 if ( preg_match( '/<a.*? title=(?:\'|").*?(?:\'|").*?>/is', $link ) === 1 )
383 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), preg_replace( '/(<a.*? title=(?:\'|")).*?((?:\'|").*?>)/s', '$1__RL_IMAGE_TITLE__" data-rl_title="__RL_IMAGE_TITLE__$2', $link ) );
384 else
385 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), preg_replace( '/(<a.*?)>/s', '$1 title="__RL_IMAGE_TITLE__" data-rl_title="__RL_IMAGE_TITLE__">', $link ) );
386
387 // add class if needed
388 if ( preg_match( '/<a[^>]*? class=(?:\'|").*?(?:\'|").*?>/is', $link ) === 1 )
389 $link = preg_replace( '/(<a.*?) class=(?:\'|")(.*?)(?:\'|")(.*?>)/s', '$1 class="$2 rl-gallery-link" $3', $link );
390 else
391 $link = preg_replace( '/(<a.*?)>/s', '$1 class="rl-gallery-link">', $link );
392
393 // gallery image caption
394 $caption = ! empty( $args['caption'] ) ? $args['caption'] : '';
395
396 // get caption type
397 $caption_arg = $args['settings']['plugin']['gallery_image_caption'];
398
399 // update caption if needed
400 if ( ! empty( $args['image_id'] ) && $caption_arg !== 'default' ) {
401 // original caption
402 $args['caption'] = $this->get_attachment_title( $args['image_id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $caption_arg, $args['image_id'], $link ) );
403 }
404
405 // prepare caption
406 $caption = trim( nl2br( $args['caption'] ) );
407
408 if ( ! rl_current_lightbox_supports( 'html_caption' ) )
409 $caption = wp_strip_all_tags( $caption, true );
410
411 // use safe replacement for data-rl_caption
412 $link = str_replace( '__RL_IMAGE_CAPTION__', esc_attr( $caption ), preg_replace( '/(<a.*?)>/s', '$1 data-rl_caption="__RL_IMAGE_CAPTION__">', $link ) );
413
414 if ( isset( $_GET['rl_gallery_no'], $_GET['rl_page'] ) )
415 $this->gallery_no = (int) $_GET['rl_gallery_no'];
416
417 // link already contains data-rel attribute?
418 if ( preg_match( '/<a.*?data-rel=(\'|")(.*?)(\'|").*?>/is', $link, $result ) === 1 ) {
419 if ( $result[2] !== 'norl' )
420 $link = preg_replace( '/(<a.*?data-rel=(?:\'|").*?)((?:\'|").*?>)/s', '${1}' . esc_attr( $args['selector'] ) . '-gallery-' . (int) $this->gallery_no . '$2', $link );
421 } else
422 $link = preg_replace( '/(<a.*?)>/s', '$1 data-rel="' . esc_attr( $args['selector'] ) . '-gallery-' . (int) $this->gallery_no . '">', $link );
423
424 if ( ! ( isset( $args['link'] ) && $args['link'] !== 'file' ) ) {
425 // gallery image size
426 if ( ! empty( $args['image_id'] ) ) {
427 if ( empty( $args['src'] ) )
428 $args['src'] = wp_get_attachment_image_src( $args['image_id'], $args['settings']['plugin']['gallery_image_size'] );
429
430 // valid source?
431 if ( ! empty( $args['src'][0] ) ) {
432 if ( preg_match( '/<a.*? href=("|\').*?("|\').*?>/is', $link ) === 1 )
433 $link = preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $args['src'][0] . '$2', $link );
434 else
435 $link = preg_replace( '/(<a.*?)>/', '$1 href="' . $args['src'][0] . '">', $link );
436 }
437 }
438
439 if ( $args['script'] === 'magnific' )
440 $link = preg_replace( '/(<a.*?)>/is', '$1 data-magnific_type="gallery">', $link );
441 }
442
443 return apply_filters( 'rl_lightbox_gallery_link', $link, $args );
444 }
445
446 /**
447 * Add lightbox to content links.
448 *
449 * @param string $link Content link
450 * @param array $args Content arguments
451 * @return string
452 */
453 public function lightbox_content_link( $link, $args ) {
454 if ( in_array( $args['content'], $args['supports'], true ) ) {
455 // link already contains data-rel attribute?
456 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/is', $link, $result ) === 1 )
457 $link = preg_replace( '/data-rel=(\'|")(.*?)(\'|")/s', 'data-rel="' . esc_attr( $args['selector'] ) . '-content-' . esc_attr( base64_encode( $result[1] ) ) . '"', $link );
458 else
459 $link = preg_replace( '/(<a.*?)>/s', '$1 data-rel="' . esc_attr( $args['selector'] ) . '-content-' . (int) $args['link_number'] . '">', $link );
460
461 switch ( $args['script'] ) {
462 case 'nivo':
463 $link = preg_replace( '/(<a.*?)>/s', '$1 data-lightbox-type="' . esc_attr( $args['content'] ) . '">', $link );
464 break;
465
466 case 'featherlight':
467 $link = preg_replace( '/(<a.*?)>/s', '$1 data-featherlight="' . esc_attr( $args['content'] ) . '">', $link );
468 break;
469
470 case 'fancybox':
471 if ( $args['content'] === 'iframe' )
472 $link = preg_replace( '/(<a.*?href=(?:\'|"))(.*?)((?:\'|").*?>)/is', '$1' . add_query_arg( 'iframe', '', '$2' ) . '$3', $link );
473 break;
474
475 case 'prettyphoto':
476 if ( $args['content'] === 'iframe' )
477 $link = preg_replace( '/(<a.*?href=(?:\'|"))(.*?)((?:\'|").*?>)/is', '$1' . add_query_arg( [ 'iframe' => 'true', 'width' => (int) $args['settings']['width'], 'height' => (int) $args['settings']['height'] ], '$2' ) . '$3', $link );
478 }
479 }
480
481 return apply_filters( 'rl_lightbox_content_link', $link, $args );
482 }
483
484 /**
485 * Get gallery fields.
486 *
487 * @param string $type Gallery type
488 * @return array
489 */
490 public function get_gallery_fields( $type ) {
491 // get main instance
492 $rl = Responsive_Lightbox();
493
494 // get gallery fields
495 $gallery_fields = $rl->settings->settings[$type . '_gallery']['fields'];
496
497 // assign settings and defaults
498 $gallery_defaults = $rl->defaults[$type . '_gallery'];
499 $gallery_values = $rl->options[$type . '_gallery'];
500
501 // make a copy
502 $fields_copy = $gallery_fields;
503
504 foreach ( $fields_copy as $field_key => $field ) {
505 if ( $field['type'] === 'multiple' ) {
506 foreach ( $field['fields'] as $subfield_key => $subfield ) {
507 $gallery_fields[$field_key]['fields'][$subfield_key]['default'] = $gallery_defaults[$subfield_key];
508 $gallery_fields[$field_key]['fields'][$subfield_key]['value'] = array_key_exists( $subfield_key, $gallery_values ) ? $gallery_values[$subfield_key] : $gallery_defaults[$subfield_key];
509 }
510 } else {
511 $gallery_fields[$field_key]['default'] = $gallery_defaults[$field_key];
512 $gallery_fields[$field_key]['value'] = array_key_exists( $field_key, $gallery_values ) ? $gallery_values[$field_key] : $gallery_defaults[$field_key];
513 }
514 }
515
516 // get shortcode gallery fields combined with defaults
517 return apply_filters( 'rl_get_gallery_fields', $this->get_unique_fields( $this->get_default_gallery_fields(), $gallery_fields ) );
518 }
519
520 /**
521 * Get unique gallery fields.
522 *
523 * @param array $defaults Default gallery fields
524 * @param array $fields Custom gallery fields
525 * @return array
526 */
527 public function get_unique_fields( $defaults, $fields ) {
528 // check duplicated fields
529 $duplicates = array_intersect_key( $defaults, $fields );
530
531 // any duplicated fields?
532 if ( ! empty( $duplicates ) ) {
533 foreach ( $duplicates as $field_id => $field ) {
534 unset( $defaults[$field_id] );
535 }
536 }
537
538 // get default and custom fields all together
539 return $defaults + $fields;
540 }
541
542 /**
543 * Get gallery fields combined with shortcode attributes.
544 *
545 * @param array $fields Gallery fields
546 * @param array $shortcode_atts Gallery shortcode attributes
547 * @param bool $gallery Whether is it rl_gallery shortcode
548 * @return array
549 */
550 public function get_gallery_fields_atts( $fields, $shortcode_atts, $gallery = true ) {
551 // prepare default values
552 $field_atts = [];
553
554 // get all default field values
555 foreach ( $fields as $field_key => $field ) {
556 if ( $field['type'] === 'multiple' ) {
557 foreach ( $field['fields'] as $subfield_key => $subfield ) {
558 $field_atts[$subfield_key] = array_key_exists( 'value', $subfield ) ? $subfield['value'] : $subfield['default'];
559 }
560 } else
561 $field_atts[$field_key] = array_key_exists( 'value', $field ) ? $field['value'] : $field['default'];
562 }
563
564 // is it rl gallery?
565 if ( $gallery ) {
566 // get tabs
567 $tabs = Responsive_Lightbox()->galleries->get_data( 'tabs' );
568
569 if ( ! empty( $tabs ) ) {
570 foreach ( $tabs as $key => $args ) {
571 if ( in_array( $key, [ 'images', 'config' ] ) )
572 continue;
573
574 // get additional fields
575 $data = get_post_meta( $shortcode_atts['rl_gallery_id'], '_rl_' . $key, true );
576
577 // add those fields
578 if ( ! empty( $data['menu_item'] ) && is_array( $data[$data['menu_item']] ) ) {
579 $new_data = $data[$data['menu_item']];
580
581 if ( $key === 'design' ) {
582 // remove show_title to avoid shortcode attribute duplication
583 if ( isset( $new_data['show_title'] ) ) {
584 if ( ! isset( $new_data['design_show_title'] ) )
585 $new_data['design_show_title'] = $new_data['show_title'];
586
587 unset( $new_data['show_title'] );
588 }
589
590 // remove show_caption to avoid shortcode attribute duplication
591 if ( isset( $new_data['show_caption'] ) ) {
592 if ( ! isset( $new_data['design_show_caption'] ) )
593 $new_data['design_show_caption'] = $new_data['show_caption'];
594
595 unset( $new_data['show_caption'] );
596 }
597 }
598
599 $field_atts += $new_data;
600 }
601 }
602 }
603
604 if ( $field_atts['hover_effect'] !== '0' )
605 $field_atts['gallery_custom_class'] .= ' rl-hover-effect-' . $field_atts['hover_effect'];
606
607 if ( $field_atts['show_icon'] !== '0' )
608 $field_atts['gallery_custom_class'] .= ' rl-hover-icon-' . $field_atts['show_icon'];
609 }
610
611 return (array) apply_filters( 'rl_get_gallery_fields_atts', $field_atts );
612 }
613
614 /**
615 * Get default gallery fields.
616 *
617 * @return array
618 */
619 public function get_default_gallery_fields() {
620 $sizes = get_intermediate_image_sizes();
621 $sizes['full'] = 'full';
622
623 return [
624 'size' => [
625 'title' => __( 'Size', 'responsive-lightbox' ),
626 'type' => 'select',
627 'description' => __( 'Specify the image size to use for the thumbnail display.', 'responsive-lightbox' ),
628 'default' => 'medium',
629 'options' => array_combine( $sizes, $sizes )
630 ],
631 'link' => [
632 'title' => __( 'Link To', 'responsive-lightbox' ),
633 'type' => 'select',
634 'description' => __( 'Specify where you want the image to link.', 'responsive-lightbox' ),
635 'default' => 'file',
636 'options' => [
637 'post' => __( 'Attachment Page', 'responsive-lightbox' ),
638 'file' => __( 'Media File', 'responsive-lightbox' ),
639 'none' => __( 'None', 'responsive-lightbox' )
640 ]
641 ],
642 'orderby' => [
643 'title' => __( 'Orderby', 'responsive-lightbox' ),
644 'type' => 'select',
645 'description' => __( 'Specify how to sort the display thumbnails.', 'responsive-lightbox' ),
646 'default' => 'menu_order',
647 'options' => [
648 'id' => __( 'ID', 'responsive-lightbox' ),
649 'title' => __( 'Title', 'responsive-lightbox' ),
650 'post_date' => __( 'Date', 'responsive-lightbox' ),
651 'menu_order' => __( 'Menu Order', 'responsive-lightbox' ),
652 'rand' => __( 'Random', 'responsive-lightbox' )
653 ]
654 ],
655 'order' => [
656 'title' => __( 'Order', 'responsive-lightbox' ),
657 'type' => 'radio',
658 'description' => __( 'Specify the sort order.', 'responsive-lightbox' ),
659 'default' => 'asc',
660 'options' => [
661 'asc' => __( 'Ascending', 'responsive-lightbox' ),
662 'desc' => __( 'Descending', 'responsive-lightbox' )
663 ]
664 ],
665 'columns' => [
666 'title' => __( 'Columns', 'responsive-lightbox' ),
667 'type' => 'number',
668 'description' => __( 'Specify the number of columns.', 'responsive-lightbox' ),
669 'default' => 3,
670 'min' => 1,
671 'max' => 12
672 ]
673 ];
674 }
675
676 /**
677 * Sanitize shortcode gallery arguments.
678 *
679 * @param array $atts Shortcode arguments
680 * @param array $fields Gallery fields
681 * @return array
682 */
683 public function sanitize_shortcode_args( $atts, $fields ) {
684 // get main instance
685 $rl = Responsive_Lightbox();
686
687 // validate gallery fields
688 foreach ( $fields as $field_key => $field ) {
689 // checkbox field?
690 if ( $field['type'] === 'checkbox' ) {
691 // valid argument?
692 if ( array_key_exists( $field_key, $atts ) ) {
693 if ( is_array( $atts[$field_key] ) )
694 $array = $atts[$field_key];
695 elseif ( is_string( $atts[$field_key] ) ) {
696 if ( $atts[$field_key] === '' )
697 $array = [];
698 else
699 $array = explode( ',', $atts[$field_key] );
700 } else
701 $array = [];
702
703 $atts[$field_key] = $rl->galleries->sanitize_field( $field_key, array_flip( $array ), $field );
704 }
705 // boolean field?
706 } elseif ( $field['type'] === 'boolean' ) {
707 // multiple field?
708 if ( $field['type'] === 'multiple' ) {
709 foreach ( $field['fields'] as $subfield_key => $subfield ) {
710 // valid argument?
711 if ( array_key_exists( $subfield_key, $atts ) ) {
712 // true value?
713 if ( $atts[$subfield_key] === true || $atts[$subfield_key] === 'true' || $atts[$subfield_key] === '1' )
714 $atts[$subfield_key] = 1;
715 // false value?
716 elseif ( $atts[$subfield_key] === false || $atts[$subfield_key] === 'false' || $atts[$subfield_key] === '0' || $atts[$subfield_key] === '' )
717 $atts[$subfield_key] = 0;
718 // default value
719 else
720 $atts[$subfield_key] = (int) $field['default'];
721 }
722 }
723 } else {
724 // valid argument?
725 if ( array_key_exists( $field_key, $atts ) ) {
726 // true value?
727 if ( $atts[$field_key] === true || $atts[$field_key] === 'true' || $atts[$field_key] === '1' )
728 $atts[$field_key] = 1;
729 // false value?
730 elseif ( $atts[$field_key] === false || $atts[$field_key] === 'false' || $atts[$field_key] === '0' || $atts[$field_key] === '' )
731 $atts[$field_key] = 0;
732 // default value
733 else
734 $atts[$field_key] = (int) $field['default'];
735 }
736 }
737 // multiple field?
738 } elseif ( $field['type'] === 'multiple' ) {
739 foreach ( $field['fields'] as $subfield_key => $subfield ) {
740 // valid argument?
741 if ( array_key_exists( $subfield_key, $atts ) )
742 $atts[$subfield_key] = $rl->galleries->sanitize_field( $subfield_key, $atts[$subfield_key], $subfield );
743 }
744 // other field?
745 } else {
746 // valid argument?
747 if ( array_key_exists( $field_key, $atts ) )
748 $atts[$field_key] = $rl->galleries->sanitize_field( $field_key, $atts[$field_key], $field );
749 }
750 }
751
752 return (array) apply_filters( 'rl_sanitize_shortcode_args', $atts );
753 }
754
755 /**
756 * Get gallery images.
757 *
758 * @param array $shortcode_atts Gallery arguments
759 * @return array
760 */
761 public function get_gallery_shortcode_images( $shortcode_atts ) {
762 // get main instance
763 $rl = Responsive_Lightbox();
764
765 if ( ! isset( $shortcode_atts['design_show_title'] ) || $shortcode_atts['design_show_title'] === 'global' )
766 $shortcode_atts['design_show_title'] = $rl->options['settings']['gallery_image_title'];
767
768 if ( ! isset( $shortcode_atts['design_show_caption'] ) || $shortcode_atts['design_show_caption'] === 'global' )
769 $shortcode_atts['design_show_caption'] = $rl->options['settings']['gallery_image_caption'];
770
771 $images = [];
772
773 // get gallery id
774 $gallery_id = ! empty( $shortcode_atts['rl_gallery_id'] ) ? absint( $shortcode_atts['rl_gallery_id'] ) : 0;
775
776 // get images from gallery
777 if ( $gallery_id ) {
778 $images = $rl->galleries->get_gallery_images(
779 $gallery_id,
780 [
781 'exclude' => true,
782 'image_size' => $shortcode_atts['src_size'],
783 'thumbnail_size' => $shortcode_atts['size'],
784 'preview' => ( isset( $_GET['rl_gallery_revision_id'], $_GET['preview'] ) && $_GET['preview'] === 'true' ) || ( isset( $_POST['action'], $_POST['preview'] ) && $_POST['action'] === 'rl-get-gallery-page-content' && $_POST['preview'] === 'true' && wp_doing_ajax() )
785 ]
786 );
787 // get images from shortcode atts
788 } else {
789 $ids = [];
790
791 if ( ! empty( $shortcode_atts['include'] ) ) {
792 // filter attachment IDs
793 $include = array_unique( array_filter( array_map( 'intval', explode( ',', $shortcode_atts['include'] ) ) ) );
794
795 // any attachments?
796 if ( ! empty( $include ) ) {
797 // get attachments
798 $ids = get_posts(
799 [
800 'include' => implode( ',', $include ),
801 'post_status' => 'inherit',
802 'post_type' => 'attachment',
803 'post_mime_type' => 'image',
804 'order' => $shortcode_atts['order'],
805 'orderby' => ( $shortcode_atts['orderby'] === 'menu_order' || $shortcode_atts['orderby'] === '' ? 'post__in' : $shortcode_atts['orderby'] ),
806 'fields' => 'ids'
807 ]
808 );
809 }
810 } elseif ( ! empty( $exclude ) ) {
811 // filter attachment IDs
812 $exclude = array_unique( array_filter( array_map( 'intval', explode( ',', $shortcode_atts['exclude'] ) ) ) );
813
814 // any attachments?
815 if ( ! empty( $exclude ) ) {
816 // get attachments
817 $ids = get_children(
818 [
819 'post_parent' => $shortcode_atts['id'],
820 'exclude' => $exclude,
821 'post_status' => 'inherit',
822 'post_type' => 'attachment',
823 'post_mime_type' => 'image',
824 'order' => $shortcode_atts['order'],
825 'orderby' => $shortcode_atts['orderby'],
826 'fields' => 'ids'
827 ]
828 );
829 }
830 } else {
831 // get attachments
832 $ids = get_children(
833 [
834 'post_parent' => $shortcode_atts['id'],
835 'post_status' => 'inherit',
836 'post_type' => 'attachment',
837 'post_mime_type' => 'image',
838 'order' => $shortcode_atts['order'],
839 'orderby' => $shortcode_atts['orderby'],
840 'fields' => 'ids'
841 ]
842 );
843 }
844
845 // any attachments?
846 if ( ! empty( $ids ) ) {
847 foreach ( $ids as $attachment_id ) {
848 // get thumbnail image data
849 $images[] = $rl->galleries->get_gallery_image_src( $attachment_id, $shortcode_atts['src_size'], $shortcode_atts['size'] );
850 }
851 }
852 }
853
854 // apply adjustments, as per settings
855 if ( $images ) {
856 // get current script
857 $script = $rl->get_data( 'current_script' );
858
859 // get scripts
860 $scripts = $rl->settings->get_data( 'scripts' );
861
862 // prepare arguments
863 $args = [
864 'selector' => $rl->options['settings']['selector'],
865 'script' => $script,
866 'settings' => [
867 'script' => $rl->options['configuration'][$script],
868 'plugin' => $rl->options['settings']
869 ],
870 'supports' => $scripts[$script]['supports'],
871 'image_id' => 0,
872 'caption' => '',
873 'title' => '',
874 'src' => []
875 ];
876
877 // lightbox image title
878 $args['settings']['plugin']['gallery_image_title'] = ! empty( $shortcode_atts['lightbox_image_title'] ) ? ( $shortcode_atts['lightbox_image_title'] === 'global' ? $rl->options['settings']['gallery_image_title'] : $shortcode_atts['lightbox_image_title'] ) : $rl->options['settings']['gallery_image_title'];
879
880 // lightbox image caption
881 $args['settings']['plugin']['gallery_image_caption'] = ! empty( $shortcode_atts['lightbox_image_caption'] ) ? ( $shortcode_atts['lightbox_image_caption'] === 'global' ? $rl->options['settings']['gallery_image_caption'] : $shortcode_atts['lightbox_image_caption'] ) : $rl->options['settings']['gallery_image_caption'];
882
883 // get gallery image link
884 $args['link'] = isset( $shortcode_atts['link'] ) ? $shortcode_atts['link'] : '';
885
886 // copy images
887 $images_tmp = $images;
888
889 // apply adjustments, according to gallery settings
890 foreach ( $images_tmp as $index => $image ) {
891 // assign image
892 $new_image = $images[$index] = array_merge( $image, $rl->galleries->get_gallery_image_src( $image, $shortcode_atts['src_size'], $shortcode_atts['size'] ) );
893
894 // create image source data
895 $args['src'] = [ $new_image['url'], $new_image['width'], $new_image['height'], $new_image ];
896
897 // update image id
898 if ( ! empty( $new_image['id'] ) )
899 $args['image_id'] = $new_image['id'];
900
901 // set alt text
902 $images[$index]['alt'] = $shortcode_atts['alt'] = ! empty( $new_image['alt'] ) ? $new_image['alt'] : ( ! empty( $new_image['id'] ) ? get_post_meta( $new_image['id'], '_wp_attachment_image_alt', true ) : '' );
903
904 // set lightbox image title
905 if ( $args['settings']['plugin']['gallery_image_title'] === 'default' )
906 $images[$index]['title'] = $args['title'] = '';
907 else {
908 // embed element?
909 if ( preg_match( '/^e\d+$/', $new_image['id'] ) === 1 )
910 $shortcode_atts['title'] = $images[$index]['title'] = $args['title'] = $this->get_embed_title( $new_image['id'], apply_filters( 'rl_lightbox_embed_image_title_arg', $args['settings']['plugin']['gallery_image_title'], $images[$index]['link'] ), $new_image );
911 else
912 $images[$index]['title'] = $args['title'] = ! empty( $new_image['id'] ) ? $this->get_attachment_title( $new_image['id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $args['settings']['plugin']['gallery_image_title'], $new_image['id'], $images[$index]['link'] ) ) : $new_image['title'];
913 }
914
915 // set lightbox image caption
916 if ( $args['settings']['plugin']['gallery_image_caption'] === 'default' )
917 $images[$index]['caption'] = $args['caption'] = '';
918 else {
919 // embed element?
920 if ( preg_match( '/^e\d+$/', $new_image['id'] ) === 1 )
921 $shortcode_atts['caption'] = $images[$index]['caption'] = $args['caption'] = $this->get_embed_title( $new_image['id'], apply_filters( 'rl_lightbox_embed_image_title_arg', $args['settings']['plugin']['gallery_image_caption'], $images[$index]['link'] ), $new_image );
922 else
923 $images[$index]['caption'] = $args['caption'] = ! empty( $new_image['id'] ) ? $this->get_attachment_title( $new_image['id'], apply_filters( 'rl_lightbox_attachment_image_title_arg', $args['settings']['plugin']['gallery_image_caption'], $images[$index]['link'] ) ) : $new_image['caption'];
924 }
925
926 // set image gallery link
927 $images[$index]['link'] = $this->lightbox_gallery_link( $this->get_gallery_image_link( $new_image['id'], $args['src'], [ $new_image['thumbnail_url'], $new_image['thumbnail_width'], $new_image['thumbnail_height'] ], $shortcode_atts ), $args );
928
929 // is lightbox active?
930 if ( isset( $shortcode_atts['lightbox_enable'] ) && $shortcode_atts['lightbox_enable'] === 0 )
931 $images[$index]['link'] = preg_replace( '/data-rel=(\'|")(.*?)(\'|")/', 'data-rel="norl"', $images[$index]['link'] );
932 }
933 }
934
935 return (array) apply_filters( 'rl_get_gallery_shortcode_images', $images, $gallery_id, $shortcode_atts );
936 }
937
938 /**
939 * Get gallery image link.
940 *
941 * @param int $attachment_id Attachment ID
942 * @param array $image Source image data
943 * @param array $thumbnail Thumbnail image data
944 * @param array $args Arguments
945 * @return string
946 */
947 function get_gallery_image_link( $attachment_id, $image, $thumbnail, $args ) {
948 // link type
949 switch ( $args['link'] ) {
950 case 'post':
951 // embed element?
952 if ( preg_match( '/^e\d+$/', $attachment_id ) === 1 )
953 $attr = [ 'href' => $image[0] ];
954 else
955 $attr = [ 'href' => get_permalink( $attachment_id ) ];
956 break;
957
958 case 'none':
959 $attr = [ 'href' => 'javascript:void(0);', 'style' => 'cursor: default;' ];
960 break;
961
962 default:
963 case 'file':
964 $attr = [ 'href' => $image[0] ];
965 }
966
967 // filter attributes
968 $attr = apply_filters( 'rl_gallery_image_link_attributes', $attr, $attachment_id, $image, $args );
969
970 // start link
971 $link = '<a';
972
973 // escape attributes
974 foreach ( $attr as $name => $value ) {
975 $link .= ' ' . esc_attr( $name ) . '="' . ( $name === 'href' ? esc_url( $value ) : esc_attr( $value ) ) . '"';
976 }
977
978 $link .= '>';
979 $link .= apply_filters( 'rl_gallery_image_link_before', '', $attachment_id, $args );
980 $link .= '<img src="' . esc_url( $thumbnail[0] ) . '" width="' . (int) $thumbnail[1] . '" height="' . (int) $thumbnail[2] . '" alt="' . esc_attr( $args['alt'] ) . '"' . ( isset( $args['hide_image'] ) && $args['hide_image'] ? ' style="display: none;"' : '' ) . '/>';
981
982 // embed element?
983 if ( preg_match( '/^e\d+$/', $attachment_id ) === 1 ) {
984 $title = ! empty( $args['design_show_title'] ) ? $this->get_embed_title( $attachment_id, $args['design_show_title'], $args ) : '';
985 $caption = ! empty( $args['design_show_caption'] ) ? $this->get_embed_title( $attachment_id, $args['design_show_caption'], $args ) : '';
986 } else {
987 $title = ! empty( $args['design_show_title'] ) ? $this->get_attachment_title( $attachment_id, $args['design_show_title'] ) : '';
988 $caption = ! empty( $args['design_show_caption'] ) ? $this->get_attachment_title( $attachment_id, $args['design_show_caption'] ) : '';
989 }
990
991 if ( $title || $caption ) {
992 $link .= '<span class="rl-gallery-caption">';
993
994 if ( $title )
995 $link .= '<span class="rl-gallery-item-title">' . esc_html( $title ) . '</span>';
996
997 if ( $caption )
998 $link .= '<span class="rl-gallery-item-caption">' . esc_html( $caption ) . '</span>';
999
1000 $link .= '</span>';
1001 }
1002
1003 $link .= apply_filters( 'rl_gallery_image_link_after', '', $attachment_id, $args );
1004 $link .= '</a>';
1005
1006 return apply_filters( 'rl_gallery_image_link', $link, $attachment_id, $image, $thumbnail, $args );
1007 }
1008
1009 /**
1010 * Add lightbox to Jetpack tiled gallery.
1011 *
1012 * @param string $content
1013 * @return string
1014 */
1015 public function force_custom_gallery_lightbox( $content ) {
1016 // get main instance
1017 $rl = Responsive_Lightbox();
1018
1019 if ( $rl->options['settings']['force_custom_gallery'] ) {
1020 // search for image links
1021 preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?)\.(bmp|gif|jpeg|jpg|png|webp)(?:\'|")(.*?)>/i', $content, $links );
1022
1023 // found any links?
1024 if ( ! empty ( $links[0] ) ) {
1025 // get current script
1026 $script = $rl->get_data( 'current_script' );
1027
1028 foreach ( $links[0] as $link_number => $link ) {
1029 // get attachment id
1030 $image_id = $this->get_attachment_id_by_url( $links[2][$link_number] . '.' . $links[3][$link_number] );
1031
1032 // get title type
1033 $title_arg = $rl->options['settings']['gallery_image_title'];
1034
1035 // update title if needed
1036 if ( $title_arg !== 'default' && $image_id )
1037 $title = wp_strip_all_tags( $this->get_attachment_title( $image_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$link_number] . '.' . $links[3][$link_number] ) ), true );
1038 else
1039 $title = '';
1040
1041 // get caption type
1042 $caption_arg = $rl->options['settings']['gallery_image_caption'];
1043
1044 // update caption if needed
1045 if ( $caption_arg !== 'default' )
1046 $caption = wp_strip_all_tags( $this->get_attachment_title( $image_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $caption_arg, $image_id, $links[2][$link_number] . '.' . $links[3][$link_number] ) ), true );
1047 else
1048 $caption = '';
1049
1050 // link already contains data-rel attribute?
1051 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) {
1052 // do not modify this link
1053 if ( $result[1] === 'norl' )
1054 continue;
1055
1056 $content = str_replace( $link, preg_replace( '/data-rel=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . esc_attr( base64_encode( $result[1] ) ) . '" data-rl_title="' . esc_attr( $title ) . '" data-rl_caption="' . esc_attr( $caption ) . '"' . ( $script === 'imagelightbox' ? ' data-imagelightbox="' . (int) $link_number . '"' : '' ), $link ), $content );
1057 } elseif ( preg_match( '/<a.*?(?:rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) {
1058 // do not modify this link
1059 if ( $result[1] === 'norl' )
1060 continue;
1061
1062 $content = str_replace( $link, preg_replace( '/rel=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . esc_attr( base64_encode( $result[1] ) ) . '" data-rl_title="' . esc_attr( $title ) . '" data-rl_caption="' . esc_attr( $caption ) . '"' . ( $script === 'imagelightbox' ? ' data-imagelightbox="' . (int) $link_number . '"' : '' ), $link ), $content );
1063 } else
1064 $content = str_replace( $link, '<a' . $links[1][$link_number] . ' href="' . $links[2][$link_number] . '.' . $links[3][$link_number] . '" data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . esc_attr( base64_encode( $this->gallery_no ) ) . '" data-rl_title="' . esc_attr( $title ) . '" data-rl_caption="' . esc_attr( $caption ) . '"' . ( $script === 'imagelightbox' ? ' data-imagelightbox="' . (int) $link_number . '"' : '' ) . $links[4][$link_number] . '>', $content );
1065 }
1066 }
1067 }
1068
1069 return $content;
1070 }
1071
1072 /**
1073 * Remove specific styles and scripts.
1074 *
1075 * @global object $woocommerce
1076 *
1077 * @return void
1078 */
1079 public function wp_dequeue_scripts() {
1080 // woocommerce
1081 if ( class_exists( 'WooCommerce' ) ) {
1082 global $woocommerce;
1083
1084 // get main instance
1085 $rl = Responsive_Lightbox();
1086
1087 // specific woocommerce gallery?
1088 if ( ! empty( $rl->options['settings']['default_woocommerce_gallery'] ) && $rl->options['settings']['default_woocommerce_gallery'] !== 'default' ) {
1089 // replace default woocommerce lightbox?
1090 if ( $rl->options['settings']['woocommerce_gallery_lightbox'] === true ) {
1091 if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) {
1092 // dequeue scripts
1093 wp_dequeue_script( 'flexslider' );
1094 wp_dequeue_script( 'photoswipe' );
1095 wp_dequeue_script( 'photoswipe-ui-default' );
1096
1097 // dequeue styles
1098 wp_dequeue_style( 'photoswipe' );
1099 wp_dequeue_style( 'photoswipe-default-skin' );
1100
1101 // remove theme supports
1102 remove_theme_support( 'wc-product-gallery-lightbox' );
1103 remove_theme_support( 'wc-product-gallery-slider' );
1104 } else {
1105 // remove styles
1106 wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
1107
1108 // remove scripts
1109 wp_dequeue_script( 'prettyPhoto' );
1110 wp_dequeue_script( 'prettyPhoto-init' );
1111 wp_dequeue_script( 'fancybox' );
1112 wp_dequeue_script( 'enable-lightbox' );
1113 }
1114 } else {
1115 if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) {
1116 // dequeue scripts
1117 wp_dequeue_script( 'flexslider' );
1118 }
1119 }
1120 // default gallery?
1121 } else {
1122 // replace default woocommerce lightbox?
1123 if ( $rl->options['settings']['woocommerce_gallery_lightbox'] === true ) {
1124 if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) {
1125 // dequeue scripts
1126 wp_dequeue_script( 'photoswipe' );
1127 wp_dequeue_script( 'photoswipe-ui-default' );
1128
1129 // dequeue styles
1130 wp_dequeue_style( 'photoswipe' );
1131 wp_dequeue_style( 'photoswipe-default-skin' );
1132
1133 // remove theme supports
1134 remove_theme_support( 'wc-product-gallery-lightbox' );
1135 } else {
1136 // remove styles
1137 wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
1138
1139 // remove scripts
1140 wp_dequeue_script( 'prettyPhoto' );
1141 wp_dequeue_script( 'prettyPhoto-init' );
1142 wp_dequeue_script( 'fancybox' );
1143 wp_dequeue_script( 'enable-lightbox' );
1144 }
1145 }
1146 }
1147 }
1148
1149 // visual composer
1150 if ( class_exists( 'Vc_Manager' ) ) {
1151 wp_dequeue_script( 'prettyphoto' );
1152 wp_deregister_script( 'prettyphoto' );
1153 wp_dequeue_style( 'prettyphoto' );
1154 wp_deregister_style( 'prettyphoto' );
1155 }
1156 }
1157
1158 /**
1159 * Apply lightbox to WooCommerce product image.
1160 *
1161 * @param string $html
1162 * @return string
1163 */
1164 public function woocommerce_single_product_image_html( $html ) {
1165 // get main instance
1166 $rl = Responsive_Lightbox();
1167
1168 if ( $rl->options['settings']['woocommerce_gallery_lightbox'] )
1169 $html = preg_replace( '/data-rel=\"(.*?)\"/', 'data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . (int) $this->gallery_no . '"', $html );
1170
1171 return $html;
1172 }
1173
1174 /**
1175 * Apply lightbox to WooCommerce product gallery.
1176 *
1177 * @param string $html
1178 * @param int $attachment_id
1179 * @return string
1180 */
1181 public function woocommerce_single_product_image_thumbnail_html( $html, $attachment_id ) {
1182 // get main instance
1183 $rl = Responsive_Lightbox();
1184
1185 if ( $rl->options['settings']['woocommerce_gallery_lightbox'] ) {
1186 // make sure main product image has same gallery number
1187 $gallery_no = $this->gallery_no + 1;
1188
1189 $html = preg_replace( '/data-rel=\"(.*?)\"/', 'data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . (int) $gallery_no . '"', $html );
1190
1191 preg_match( '/<a(.*?)((?:data-rel)=(?:\'|").*?(?:\'|"))(.*?)>/i', $html, $result );
1192
1193 // no data-rel?
1194 if ( empty( $result ) ) {
1195 preg_match( '/^(.*?)<a(.*?)((?:href)=(?:\'|").*?(?:\'|"))(.*?)>(.*?)$/i', $html, $result );
1196
1197 // found valid link?
1198 if ( ! empty( $result ) )
1199 $html = $result[1] . '<a' . $result[2] . 'data-rel="' . esc_attr( $rl->options['settings']['selector'] ) . '-gallery-' . (int) $gallery_no . '" ' . $result[3] . $result[4] . '>' . $result[5];
1200 }
1201
1202 $html = $this->woocommerce_gallery_link( $html, $attachment_id );
1203 }
1204
1205 return $html;
1206 }
1207
1208 /**
1209 * Add title and caption to WooCommerce gallery image links.
1210 *
1211 * @param string $link
1212 * @param int $attachment_id
1213 * @return string
1214 */
1215 public function woocommerce_gallery_link( $link, $attachment_id ) {
1216 // get main instance
1217 $rl = Responsive_Lightbox();
1218
1219 // gallery image title
1220 $title = '';
1221
1222 // get title type
1223 $title_arg = $rl->options['settings']['gallery_image_title'];
1224
1225 // update title if needed
1226 if ( $title_arg !== 'default' ) {
1227 // original title
1228 $title = $this->get_attachment_title( $attachment_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $attachment_id, $link ) );
1229 }
1230
1231 if ( $title !== '' ) {
1232 // title
1233 $title = trim( nl2br( $title ) );
1234
1235 if ( ! rl_current_lightbox_supports( 'html_caption' ) )
1236 $title = wp_strip_all_tags( $title, true );
1237
1238 // add title and rl_title if needed
1239 if ( preg_match( '/<a[^>]*?title=(?:\'|")[^>]*?(?:\'|").*?>/is', $link ) === 1 )
1240 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), preg_replace( '/(<a[^>]*?title=(?:\'|"))[^>]*?((?:\'|").*?>)/is', '$1__RL_IMAGE_TITLE__" data-rl_title="__RL_IMAGE_TITLE__$2', $link ) );
1241 else
1242 $link = str_replace( '__RL_IMAGE_TITLE__', esc_attr( $title ), preg_replace( '/(<a[^>]*?)>/is', '$1 title="__RL_IMAGE_TITLE__" data-rl_title="__RL_IMAGE_TITLE__">', $link ) );
1243 }
1244
1245 // gallery image caption
1246 $caption = '';
1247
1248 // get caption type
1249 $caption_arg = $rl->options['settings']['gallery_image_caption'];
1250
1251 // update caption if needed
1252 if ( $caption_arg !== 'default' ) {
1253 // original caption
1254 $caption = $this->get_attachment_title( $attachment_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $caption_arg, $attachment_id, $link ) );
1255 }
1256
1257 if ( $caption !== '' ) {
1258 // caption
1259 $caption = trim( nl2br( $caption ) );
1260
1261 if ( ! rl_current_lightbox_supports( 'html_caption' ) )
1262 $caption = wp_strip_all_tags( $caption, true );
1263
1264 // add rl_caption
1265 $link = str_replace( '__RL_IMAGE_CAPTION__', esc_attr( $caption ), preg_replace( '/(<a[^>]*?)>/is', '$1 data-rl_caption="__RL_IMAGE_CAPTION__">', $link ) );
1266 }
1267
1268 if ( $rl->get_data( 'current_script' ) === 'magnific' )
1269 $link = preg_replace( '/(<a[^>]*?)>/is', '$1 data-magnific_type="gallery">', $link );
1270
1271 return $link;
1272 }
1273
1274 /**
1275 * WooCommerce gallery init.
1276 *
1277 * @return void
1278 */
1279 public function woocommerce_gallery_init() {
1280 // get main instance
1281 $rl = Responsive_Lightbox();
1282
1283 if ( ( $priority = has_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails' ) ) !== false && ! empty( $rl->options['settings']['default_woocommerce_gallery'] ) && $rl->options['settings']['default_woocommerce_gallery'] !== 'default' ) {
1284 // remove default gallery
1285 remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', $priority );
1286
1287 // handle product gallery
1288 add_action( 'woocommerce_product_thumbnails', [ $this, 'woocommerce_gallery' ], $priority );
1289 }
1290 }
1291
1292 /**
1293 * WooCommerce gallery support.
1294 *
1295 * @global object $product
1296 *
1297 * @return void
1298 */
1299 public function woocommerce_gallery() {
1300 global $product;
1301
1302 $attachment_ids = [];
1303
1304 // woocommerce 3.x
1305 if ( method_exists( $product, 'get_gallery_image_ids' ) )
1306 $attachment_ids = $product->get_gallery_image_ids();
1307 // woocommerce 2.x
1308 elseif ( method_exists( $product, 'get_gallery_attachment_ids' ) )
1309 $attachment_ids = $product->get_gallery_attachment_ids();
1310
1311 if ( ! empty( $attachment_ids ) && is_array( $attachment_ids ) )
1312 echo do_shortcode( '[gallery type="' . esc_attr( Responsive_Lightbox()->options['settings']['default_woocommerce_gallery'] ) . '" size="medium" ids="' . esc_attr( implode( ',', $attachment_ids ) ) . '"]' );
1313 }
1314
1315 /**
1316 * Get embed text.
1317 *
1318 * @param string $id
1319 * @param string $title_arg
1320 * @param array $embed
1321 * @return false|string
1322 */
1323 public function get_embed_title( $id, $title_arg, $embed ) {
1324 if ( empty( $title_arg ) || empty( $id ) )
1325 return false;
1326
1327 switch( $title_arg ) {
1328 case 'title':
1329 $text = $embed['title'];
1330 break;
1331
1332 // caption is always the same for these options
1333 case 'caption':
1334 case 'alt':
1335 case 'description':
1336 $text = $embed['caption'];
1337 break;
1338
1339 default:
1340 $text = '';
1341 }
1342
1343 return trim( apply_filters( 'rl_get_embed_title', $text, $id, $title_arg, $embed ) );
1344 }
1345
1346 /**
1347 * Get attachment text.
1348 *
1349 * @param int $id
1350 * @param string $title_arg
1351 * @return false|string
1352 */
1353 public function get_attachment_title( $id, $title_arg ) {
1354 if ( empty( $title_arg ) || empty( $id ) )
1355 return false;
1356
1357 switch( $title_arg ) {
1358 case 'title':
1359 $text = get_the_title( $id );
1360 break;
1361
1362 case 'caption':
1363 $text = get_post_field( 'post_excerpt', $id ) ;
1364 break;
1365
1366 case 'alt':
1367 $text = get_post_meta( $id, '_wp_attachment_image_alt', true );
1368 break;
1369
1370 case 'description':
1371 $text = get_post_field( 'post_content', $id ) ;
1372 break;
1373
1374 default:
1375 $text = '';
1376 }
1377
1378 return trim( apply_filters( 'rl_get_attachment_title', $text, $id, $title_arg ) );
1379 }
1380
1381 /**
1382 * Get attachment ID by url, adjusted to work for cropped and scaled images.
1383 *
1384 * @param string $url
1385 * @return int
1386 */
1387 public function get_attachment_id_by_url( $url ) {
1388 $org_url = $url;
1389
1390 // parse url
1391 $url = ! empty( $url ) ? esc_url_raw( $url ) : '';
1392
1393 // get url with both schemes
1394 $url_http = set_url_scheme( $url, 'http' );
1395 $url_https = set_url_scheme( $url, 'https' );
1396
1397 // https? set scheme order
1398 if ( is_ssl() )
1399 $urls = [ $url_https, $url_http ];
1400 else
1401 $urls = [ $url_http, $url_https ];
1402
1403 // get upload dir
1404 $dir = wp_get_upload_dir();
1405
1406 // set base url
1407 $base_url = $dir['baseurl'];
1408
1409 if ( is_ssl() )
1410 $base_url = set_url_scheme( $base_url, 'https' );
1411
1412 // set post id
1413 $post_id = 0;
1414
1415 // get cached data
1416 $post_ids = get_transient( 'rl-attachment_ids_by_url' );
1417
1418 // set default flag
1419 $update_transient = false;
1420
1421 // check url with forced scheme based on is_ssl(), then fallback to second one
1422 foreach ( $urls as $url_with_scheme ) {
1423 // set current url
1424 $url = $url_with_scheme;
1425
1426 // cached url not found?
1427 if ( $post_ids === false || ! in_array( $url_with_scheme, array_keys( $post_ids ), true ) ) {
1428 // try to get post id
1429 $post_id = $this->_get_attachment_id_by_url( $url_with_scheme, $base_url );
1430
1431 // set flag
1432 $update_transient = true;
1433 // cached url found
1434 } elseif ( ! empty( $post_ids[$url_with_scheme] ) )
1435 $post_id = (int) $post_ids[$url_with_scheme];
1436 // found post id but it is zero
1437 else {
1438 // try to refresh post id
1439 $post_id = $this->_get_attachment_id_by_url( $url_with_scheme, $base_url );
1440
1441 // set flag
1442 $update_transient = true;
1443 }
1444
1445 if ( $post_id )
1446 break;
1447
1448 // set default flag
1449 $update_transient = false;
1450 }
1451
1452 if ( $update_transient ) {
1453 // set the cache expiration, 24 hours by default
1454 $expire = (int) apply_filters( 'rl_object_cache_expire', DAY_IN_SECONDS );
1455
1456 if ( ! is_array( $post_ids ) )
1457 $post_ids = [];
1458
1459 // update post ids
1460 $post_ids[$url] = $post_id;
1461
1462 // set transient
1463 set_transient( 'rl-attachment_ids_by_url', $post_ids, $expire );
1464 }
1465
1466 return (int) apply_filters( 'rl_get_attachment_id_by_url', $post_id, $org_url );
1467 }
1468
1469 /**
1470 * Get attachment ID by url.
1471 *
1472 * @param string $url
1473 * @param string $base_url
1474 * @return int
1475 */
1476 private function _get_attachment_id_by_url( $url, $base_url ) {
1477 // try to get post id
1478 $post_id = (int) attachment_url_to_postid( $url );
1479
1480 // no post id?
1481 if ( ! $post_id ) {
1482 $path = $url;
1483
1484 if ( strpos( $path, $base_url . '/' ) === 0 )
1485 $path = substr( $path, strlen( $base_url . '/' ) );
1486
1487 // try to check full size image
1488 if ( preg_match( '/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches ) )
1489 $post_id = (int) attachment_url_to_postid( $base_url . '/' . $matches[1] . $matches[3] );
1490
1491 // try to check scaled size image
1492 if ( ! $post_id && ! empty( $matches[1] ) && ! empty( $matches[3] ) )
1493 $post_id = (int) attachment_url_to_postid( $base_url . '/' . $matches[1] . '-scaled' . $matches[3] );
1494 }
1495
1496 return $post_id;
1497 }
1498
1499 /**
1500 * Get image size by URL.
1501 *
1502 * @param string $url Image URL
1503 * @return array
1504 */
1505 public function get_image_size_by_url( $url ) {
1506 // parse url
1507 $url = ! empty( $url ) ? esc_url_raw( $url ) : '';
1508 $size = [ 0, 0 ];
1509
1510 if ( ! empty( $url ) ) {
1511 // get cached data
1512 $image_sizes = get_transient( 'rl-image_sizes_by_url' );
1513
1514 // cached url not found?
1515 if ( $image_sizes === false || ! in_array( $url, array_keys( $image_sizes ) ) || empty( $image_sizes[$url] ) ) {
1516 if ( class_exists( 'Responsive_Lightbox_Fast_Image' ) ) {
1517 // loading image
1518 $image = new Responsive_Lightbox_Fast_Image( $url );
1519
1520 // get size
1521 $size = $image->get_size();
1522 } else {
1523 // get size using php
1524 $size = getimagesize( $url );
1525 }
1526
1527 // set the cache expiration, 24 hours by default
1528 $expire = absint( apply_filters( 'rl_object_cache_expire', DAY_IN_SECONDS ) );
1529
1530 $image_sizes[$url] = $size;
1531
1532 set_transient( 'rl-image_sizes_by_url', $image_sizes, $expire );
1533 // cached url found
1534 } elseif ( ! empty( $image_sizes[$url] ) )
1535 $size = array_map( 'absint', $image_sizes[$url] );
1536 }
1537
1538 return (array) apply_filters( 'rl_get_image_size_by_url', $size, $url );
1539 }
1540
1541 /**
1542 * Add gallery shortcode to gallery post content.
1543 *
1544 * @param string $content
1545 * @return string
1546 */
1547 public function gallery_preview( $content ) {
1548 if ( get_post_type() === 'rl_gallery' && ! ( is_archive() && is_main_query() ) )
1549 $content .= do_shortcode( '[rl_gallery id="' . (int) get_the_ID() . '"]' );
1550
1551 return $content;
1552 }
1553
1554 /**
1555 * Helper: gallery number function.
1556 *
1557 * @param string $content
1558 * @param array $shortcode_atts
1559 * @return string
1560 */
1561 public function gallery_attributes( $content, $shortcode_atts ) {
1562 // private gallery?
1563 if ( isset( $shortcode_atts['rl_gallery_id'] ) && get_post_status( $shortcode_atts['rl_gallery_id'] ) === 'private' && ! current_user_can( 'read_private_posts' ) )
1564 return '';
1565
1566 // check forced gallery number
1567 if ( isset( $shortcode_atts['rl_gallery_no'] ) ) {
1568 $shortcode_atts['rl_gallery_no'] = (int) $shortcode_atts['rl_gallery_no'];
1569
1570 if ( $shortcode_atts['rl_gallery_no'] > 0 )
1571 $this->gallery_no = $shortcode_atts['rl_gallery_no'];
1572 } else
1573 ++$this->gallery_no;
1574
1575 // add inline style, to our galleries only
1576 if ( isset( $shortcode_atts['type'] ) ) {
1577 // get main instance
1578 $rl = Responsive_Lightbox();
1579
1580 // gallery style
1581 wp_enqueue_style( 'responsive-lightbox-gallery' );
1582
1583 // is there rl_gallery ID?
1584 $rl_gallery_id = ! empty( $shortcode_atts['rl_gallery_id'] ) ? (int) $shortcode_atts['rl_gallery_id'] : 0;
1585
1586 // is it rl gallery?
1587 $rl_gallery = $rl->options['builder']['gallery_builder'] && $rl_gallery_id && get_post_type( $rl_gallery_id ) === 'rl_gallery';
1588
1589 // is it rl gallery? add design options
1590 if ( $rl_gallery ) {
1591 // get fields
1592 $fields = $rl->galleries->get_data( 'fields' );
1593
1594 // get gallery fields attributes
1595 $field_atts = rl_get_gallery_fields_atts( $fields['design']['options'], $shortcode_atts, $rl_gallery );
1596
1597 // get only valid arguments
1598 $atts = shortcode_atts( $field_atts, array_merge( $field_atts, $shortcode_atts ), 'gallery' );
1599
1600 // sanitize gallery fields
1601 $atts = $this->sanitize_shortcode_args( $atts, $fields['design']['options'] );
1602
1603 // convert color
1604 $background_color = $rl->hex2rgb( $atts['background_color'] );
1605
1606 // invalid color?
1607 if ( ! $background_color )
1608 $background_color = '0,0,0';
1609 else
1610 $background_color = implode( ',', $background_color );
1611
1612 // get opacity
1613 $opacity = (string) round( $atts['background_opacity'] / 100, 2 );
1614
1615 // add inline style
1616 wp_add_inline_style(
1617 'responsive-lightbox-gallery',
1618 ':root {
1619 --rl-gallery-background_color: ' . esc_attr( $atts['background_color'] ) . ';
1620 --rl-gallery-background_opacity: ' . esc_attr( $opacity ) . ';
1621 --rl-gallery-border_color: ' . esc_attr( $atts['border_color'] ) . ';
1622 --rl-gallery-title_color: ' . esc_attr( $atts['title_color'] ) . ';
1623 --rl-gallery-caption_color: ' . esc_attr( $atts['caption_color'] ) . ';
1624 }
1625 .rl-gallery .rl-gallery-link {
1626 border: ' . (int) $atts['border_width'] . 'px solid ' . esc_attr( $atts['border_color'] ) . ';
1627 }
1628 .rl-gallery .rl-gallery-link .rl-gallery-item-title {
1629 color: ' . esc_attr( $atts['title_color'] ) . ';
1630 }
1631 .rl-gallery .rl-gallery-link .rl-gallery-item-caption {
1632 color: ' . esc_attr( $atts['caption_color'] ) . ';
1633 }
1634 .rl-gallery .rl-gallery-link .rl-gallery-caption,
1635 .rl-gallery .rl-gallery-link:after {
1636 background-color: rgba( ' . esc_attr( $background_color ) . ', ' . esc_attr( $opacity ) . ' );
1637 }
1638 [class^="rl-hover-icon-"] .rl-gallery-link:before,
1639 [class*=" rl-hover-icon-"] .rl-gallery-link:before {
1640 color: ' . esc_attr( $atts['title_color'] ) . ';
1641 background-color: rgba( ' . esc_attr( $background_color ) . ', ' . esc_attr( $opacity ) . ' );
1642 }'
1643 );
1644 }
1645 }
1646
1647 return $content;
1648 }
1649
1650 /**
1651 * Generate unique hash.
1652 *
1653 * @param int $length
1654 * @return string
1655 */
1656 private function generate_hash( $length = 8 ) {
1657 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
1658 $hash = '';
1659
1660 for( $i = 0; $i < $length; $i++ ) {
1661 $hash .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
1662 }
1663
1664 return $hash;
1665 }
1666
1667 /**
1668 * Replace widget callback function.
1669 *
1670 * @global array $wp_registered_widgets
1671 *
1672 * @param array $sidebar_params
1673 * @return array
1674 */
1675 public function dynamic_sidebar_params( $sidebar_params ) {
1676 if ( ( is_admin() && ! wp_doing_ajax() ) || Responsive_Lightbox()->options['settings']['widgets'] !== true )
1677 return $sidebar_params;
1678
1679 global $wp_registered_widgets;
1680
1681 $widget_id = $sidebar_params[0]['widget_id'];
1682 $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
1683 $wp_registered_widgets[ $widget_id ]['callback'] = [ $this, 'widget_callback_function' ];
1684
1685 return $sidebar_params;
1686 }
1687
1688 /**
1689 * Widget callback function.
1690 *
1691 * @global array $wp_registered_widgets
1692 *
1693 * @return void
1694 */
1695 public function widget_callback_function() {
1696 global $wp_registered_widgets;
1697
1698 $original_callback_params = func_get_args();
1699 $widget_id = $original_callback_params[0]['widget_id'];
1700 $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
1701 $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
1702 $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
1703
1704 if ( is_callable( $original_callback ) ) {
1705 ob_start();
1706
1707 call_user_func_array( $original_callback, $original_callback_params );
1708
1709 $widget_output = ob_get_clean();
1710
1711 echo apply_filters( 'rl_widget_output', $widget_output, $widget_id_base, $widget_id );
1712 }
1713 }
1714
1715 /**
1716 * Filter widget output.
1717 *
1718 * @param string $content
1719 * @param string $widget_id_base
1720 * @param id $widget_id
1721 * @return string
1722 */
1723 public function widget_output( $content, $widget_id_base, $widget_id ) {
1724 return $this->add_lightbox( $content );
1725 }
1726
1727 /**
1728 * Filter comment content.
1729 *
1730 * @param string $content
1731 * @return string
1732 */
1733 public function get_comment_text( $content ) {
1734 if ( ( is_admin() && ! wp_doing_ajax() ) || Responsive_Lightbox()->options['settings']['comments'] !== true )
1735 return $content;
1736
1737 return $this->add_lightbox( $content );
1738 }
1739
1740 /**
1741 * Modify gallery container class.
1742 *
1743 * @param string $class
1744 * @param array $args
1745 * @param int $gallery_id
1746 * @return string
1747 */
1748 public function gallery_container_class( $class, $args, $gallery_id ) {
1749 if ( $gallery_id ) {
1750 $class .= ' rl-loading';
1751
1752 if ( $args['pagination'] )
1753 $class .= ' rl-pagination-' . $args['pagination_type'];
1754 }
1755
1756 return $class;
1757 }
1758
1759 /**
1760 * Display content before the gallery.
1761 *
1762 * @param array $args
1763 * @param int $gallery_id
1764 * @return void
1765 */
1766 public function before_gallery( $args, $gallery_id ) {
1767 if ( $gallery_id ) {
1768 // get current post id
1769 if ( isset( $_POST['action'], $_POST['post_id'] ) && $_POST['action'] === 'rl-get-gallery-page-content' && wp_doing_ajax() )
1770 $current_id = (int) $_POST['post_id'];
1771 else
1772 $current_id = (int) get_the_ID();
1773
1774 if ( isset( $args['gallery_title_position'] ) && $args['gallery_title_position'] === 'top' && get_post_type( $current_id ) )
1775 echo '<h4 class="rl-gallery-title">' . esc_html( get_the_title( $gallery_id ) ) . '</h4>';
1776
1777 if ( isset( $args['gallery_description_position'] ) && $args['gallery_description_position'] === 'top' )
1778 echo '<div class="rl-gallery-description">' . nl2br( esc_html( $args['gallery_description'] ) ) . '</div>';
1779 }
1780 }
1781
1782 /**
1783 * Display content after the gallery.
1784 *
1785 * @param array $args
1786 * @param int $gallery_id
1787 * @return void
1788 */
1789 public function after_gallery( $args, $gallery_id ) {
1790 if ( $gallery_id ) {
1791 // get current post id
1792 if ( isset( $_POST['action'], $_POST['post_id'] ) && $_POST['action'] === 'rl-get-gallery-page-content' && wp_doing_ajax() )
1793 $current_id = (int) $_POST['post_id'];
1794 else
1795 $current_id = (int) get_the_ID();
1796
1797 if ( isset( $args['gallery_title_position'] ) && $args['gallery_title_position'] === 'bottom' )
1798 echo '<h4 class="rl-gallery-title">' . esc_html( get_the_title( $gallery_id ) ) . '</h4>';
1799
1800 if ( isset( $args['gallery_description_position'] ) && $args['gallery_description_position'] === 'bottom' )
1801 echo '<div class="rl-gallery-description">' . nl2br( esc_html( $args['gallery_description'] ) ) . '</div>';
1802 }
1803 }
1804
1805 /**
1806 * Add lightbox to Visual Composer shortcodes.
1807 *
1808 * @param string $content HTML content
1809 * @param string $shortcode Shortcode type
1810 * @return string
1811 */
1812 public function vc_shortcode_content_filter_after( $content, $shortcode ) {
1813 if ( in_array( $shortcode, apply_filters( 'rl_lightbox_vc_allowed_shortcode', [ 'vc_gallery', 'vc_single_image', 'vc_images_carousel' ] ), true ) )
1814 $content = $this->add_lightbox( $content );
1815
1816 return $content;
1817 }
1818
1819 /**
1820 * Render Basic Grid gallery shortcode.
1821 *
1822 * @global object $post
1823 *
1824 * @param string $output HTML output
1825 * @param array $shortcode_atts Shortcode attributes
1826 * @return string
1827 */
1828 public function basic_grid_gallery_shortcode( $output, $shortcode_atts ) {
1829 if ( ! empty( $output ) )
1830 return $output;
1831
1832 global $post;
1833
1834 $defaults = [
1835 'rl_gallery_id' => 0,
1836 'id' => isset( $post->ID ) ? (int) $post->ID : 0,
1837 'class' => '',
1838 'include' => '',
1839 'exclude' => '',
1840 'urls' => '',
1841 'type' => '',
1842 'order' => 'asc',
1843 'orderby' => 'menu_order',
1844 'size' => 'medium',
1845 'link' => 'file',
1846 'columns' => 3
1847 ];
1848
1849 // get main instance
1850 $rl = Responsive_Lightbox();
1851
1852 if ( ! is_array( $shortcode_atts ) )
1853 $shortcode_atts = wp_parse_args( $shortcode_atts, $defaults );
1854
1855 // is there rl_gallery ID?
1856 $rl_gallery_id = $defaults['rl_gallery_id'] = ! empty( $shortcode_atts['rl_gallery_id'] ) ? (int) $shortcode_atts['rl_gallery_id'] : 0;
1857
1858 // is it rl gallery?
1859 $rl_gallery = $rl->options['builder']['gallery_builder'] && $rl_gallery_id && get_post_type( $rl_gallery_id ) === 'rl_gallery';
1860
1861 // private gallery?
1862 if ( $rl_gallery && get_post_status( $rl_gallery_id ) === 'private' && ! current_user_can( 'read_private_posts' ) )
1863 return '';
1864
1865 if ( ! array_key_exists( 'type', $shortcode_atts ) )
1866 $shortcode_atts['type'] = '';
1867
1868 // break if it is not basic grid gallery - first check
1869 if ( ! ( $shortcode_atts['type'] === 'basicgrid' || ( $shortcode_atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicgrid' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicgrid' ) ) ) ) )
1870 return $output;
1871
1872 // get shortcode gallery fields combined with defaults
1873 $fields = rl_get_gallery_fields( 'basicgrid' );
1874
1875 // get gallery fields attributes
1876 $field_atts = rl_get_gallery_fields_atts( $fields, $shortcode_atts, $rl_gallery );
1877
1878 // is it rl gallery? add misc and lightbox fields
1879 if ( $rl_gallery ) {
1880 // get fields
1881 $fields_data = $rl->galleries->get_data( 'fields' );
1882
1883 $fields += $fields_data['lightbox']['options'] + $fields_data['misc']['options'];
1884 }
1885
1886 // get only valid arguments
1887 $atts = shortcode_atts( array_merge( $defaults, $field_atts ), $shortcode_atts, 'gallery' );
1888
1889 // sanitize gallery fields
1890 $atts = $this->sanitize_shortcode_args( $atts, $fields );
1891
1892 // break if it is not basic grid gallery
1893 if ( ! ( $atts['type'] === 'basicgrid' || ( $atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicgrid' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicgrid' ) ) ) ) )
1894 return $output;
1895
1896 // ID
1897 $atts['id'] = (int) $atts['id'];
1898
1899 // add custom classes if needed
1900 if ( $rl_gallery )
1901 $atts['class'] .= ' ' . $atts['gallery_custom_class'];
1902
1903 // any classes?
1904 if ( $atts['class'] !== '' ) {
1905 $atts['class'] = trim( $atts['class'] );
1906
1907 // more than 1 class?
1908 if ( strpos( $atts['class'], ' ' ) !== false ) {
1909 // get unique valid HTML classes
1910 $atts['class'] = array_unique( array_filter( array_map( 'sanitize_html_class', explode( ' ', $atts['class'] ) ) ) );
1911
1912 if ( ! empty( $atts['class'] ) )
1913 $atts['class'] = implode( ' ', $atts['class'] );
1914 else
1915 $atts['class'] = '';
1916 // single class
1917 } else
1918 $atts['class'] = sanitize_html_class( $atts['class'] );
1919 }
1920
1921 // orderby
1922 if ( empty( $atts['orderby'] ) ) {
1923 $atts['orderby'] = sanitize_sql_orderby( $atts['orderby'] );
1924
1925 if ( empty( $atts['orderby'] ) )
1926 $atts['orderby'] = $defaults['orderby'];
1927 }
1928
1929 // order
1930 if ( strtolower( $atts['order'] ) === 'rand' )
1931 $atts['orderby'] = 'rand';
1932
1933 // check columns
1934 if ( $atts['columns_lg'] === 0 )
1935 $atts['columns_lg'] = $atts['columns'];
1936
1937 if ( $atts['columns_md'] === 0 )
1938 $atts['columns_md'] = $atts['columns'];
1939
1940 if ( $atts['columns_sm'] === 0 )
1941 $atts['columns_sm'] = $atts['columns'];
1942
1943 if ( $atts['columns_xs'] === 0 )
1944 $atts['columns_xs'] = $atts['columns'];
1945
1946 // gallery lightbox source size
1947 if ( ! empty( $atts['lightbox_image_size'] ) ) {
1948 if ( $atts['lightbox_image_size'] === 'global' )
1949 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
1950 elseif ( $atts['lightbox_image_size'] === 'lightbox_custom_size' && isset( $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ) )
1951 $atts['src_size'] = [ $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ];
1952 else
1953 $atts['src_size'] = $atts['lightbox_image_size'];
1954 } else
1955 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
1956
1957 // filter all shortcode arguments
1958 $atts = apply_filters( 'rl_gallery_shortcode_atts', $atts, $rl_gallery_id );
1959
1960 // get gallery images
1961 $images = rl_get_gallery_shortcode_images( $atts );
1962
1963 if ( empty( $images ) || is_feed() || defined( 'IS_HTML_EMAIL' ) )
1964 return $output;
1965
1966 // make sure it is integer
1967 $gallery_no = (int) $this->gallery_no;
1968
1969 ob_start();
1970
1971 // $gallery_no and $rl_gallery_id are both integers ?>
1972 <div class="rl-gallery-container<?php echo esc_attr( apply_filters( 'rl_gallery_container_class', '', $atts, $rl_gallery_id ) ); ?>" id="rl-gallery-container-<?php echo (int) $gallery_no; ?>" data-gallery_id="<?php echo (int) $rl_gallery_id; ?>">
1973
1974 <?php do_action( 'rl_before_gallery', $atts, $rl_gallery_id ); ?>
1975
1976 <div class="rl-gallery rl-basicgrid-gallery <?php echo esc_attr( $atts['class'] ); ?>" id="rl-gallery-<?php echo (int) $gallery_no; ?>" data-gallery_no="<?php echo (int) $gallery_no; ?>">
1977
1978 <?php foreach ( $images as $image ) {
1979 // $image['link'] is already escaped
1980 echo '<div class="rl-gallery-item">' . $image['link'] . '</div>';
1981 } ?>
1982
1983 </div>
1984
1985 <?php do_action( 'rl_after_gallery', $atts, $rl_gallery_id ); ?>
1986
1987 </div>
1988
1989 <?php $gallery_html = ob_get_contents();
1990
1991 ob_end_clean();
1992
1993 // styles
1994 wp_enqueue_style( 'responsive-lightbox-basicgrid-gallery', plugins_url( 'css/gallery-basicgrid.css', dirname( __FILE__ ) ), [], $rl->defaults['version'] );
1995
1996 // add inline style
1997 $inline_css = '
1998 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
1999 width: calc(' . (string) round( 100 / (int) $atts['columns'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2000 margin: ' . (string) round( (int) $atts['gutter'] / 2, 2 ) . 'px;
2001 }
2002 @media all and (min-width: 1200px) {
2003 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
2004 width: calc(' . (string) round( 100 / (int) $atts['columns_lg'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2005 }
2006 }
2007 @media all and (min-width: 992px) and (max-width: 1200px) {
2008 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
2009 width: calc(' . (string) round( 100 / (int) $atts['columns_md'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2010 }
2011 }
2012 @media all and (min-width: 768px) and (max-width: 992px) {
2013 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
2014 width: calc(' . (string) round( 100 / (int) $atts['columns_sm'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2015 }
2016 }
2017 @media all and (max-width: 768px) {
2018 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
2019 width: calc(' . (string) round( 100 / (int) $atts['columns_xs'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2020 }
2021 }
2022 ';
2023
2024 if ( $atts['force_height'] ) {
2025 $inline_css .= '
2026 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item {
2027 height: ' . (int) $atts['row_height'] . 'px;
2028 }
2029 #rl-gallery-container-' . $gallery_no . ' .rl-basicgrid-gallery .rl-gallery-item img {
2030 height: ' . (int) $atts['row_height'] . 'px;
2031 object-fit: cover;
2032 max-width: 100%;
2033 min-width: 100%;
2034 }';
2035 }
2036
2037 wp_add_inline_style( 'responsive-lightbox-basicgrid-gallery', $inline_css );
2038
2039 // remove any new lines from the output so that the reader parses it better
2040 return apply_filters( 'rl_gallery_shortcode_html', trim( preg_replace( '/\s+/', ' ', $gallery_html ) ), $atts, $rl_gallery_id );
2041 }
2042
2043 /**
2044 * Render Basic Slider gallery shortcode.
2045 *
2046 * @global object $post
2047 *
2048 * @param string $output HTML output
2049 * @param array $shortcode_atts Shortcode attributes
2050 * @return string
2051 */
2052 public function basic_slider_gallery_shortcode( $output, $shortcode_atts ) {
2053 if ( ! empty( $output ) )
2054 return $output;
2055
2056 global $post;
2057
2058 $defaults = [
2059 'rl_gallery_id' => 0,
2060 'id' => isset( $post->ID ) ? (int) $post->ID : 0,
2061 'class' => '',
2062 'include' => '',
2063 'exclude' => '',
2064 'urls' => '',
2065 'type' => '',
2066 'order' => 'asc',
2067 'orderby' => 'menu_order',
2068 'size' => 'medium',
2069 'link' => 'file',
2070 'columns' => 3
2071 ];
2072
2073 // get main instance
2074 $rl = Responsive_Lightbox();
2075
2076 if ( ! is_array( $shortcode_atts ) )
2077 $shortcode_atts = wp_parse_args( $shortcode_atts, $defaults );
2078
2079 // is there rl_gallery ID?
2080 $rl_gallery_id = $defaults['rl_gallery_id'] = ! empty( $shortcode_atts['rl_gallery_id'] ) ? (int) $shortcode_atts['rl_gallery_id'] : 0;
2081
2082 // is it rl gallery?
2083 $rl_gallery = $rl->options['builder']['gallery_builder'] && $rl_gallery_id && get_post_type( $rl_gallery_id ) === 'rl_gallery';
2084
2085 // private gallery?
2086 if ( $rl_gallery && get_post_status( $rl_gallery_id ) === 'private' && ! current_user_can( 'read_private_posts' ) )
2087 return '';
2088
2089 if ( ! array_key_exists( 'type', $shortcode_atts ) )
2090 $shortcode_atts['type'] = '';
2091
2092 // break if it is not basic slider gallery - first check
2093 if ( ! ( $shortcode_atts['type'] === 'basicslider' || ( $shortcode_atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicslider' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicslider' ) ) ) ) )
2094 return $output;
2095
2096 // get shortcode gallery fields combined with defaults
2097 $fields = rl_get_gallery_fields( 'basicslider' );
2098
2099 // get gallery fields attributes
2100 $field_atts = rl_get_gallery_fields_atts( $fields, $shortcode_atts, $rl_gallery );
2101
2102 // is it rl gallery? add misc and lightbox fields
2103 if ( $rl_gallery ) {
2104 // get fields
2105 $fields_data = $rl->galleries->get_data( 'fields' );
2106
2107 $fields += $fields_data['lightbox']['options'] + $fields_data['misc']['options'];
2108 }
2109
2110 // get only valid arguments
2111 $atts = shortcode_atts( array_merge( $defaults, $field_atts ), $shortcode_atts, 'gallery' );
2112
2113 // sanitize gallery fields
2114 $atts = $this->sanitize_shortcode_args( $atts, $fields );
2115
2116 // break if it is not basic slider gallery
2117 if ( ! ( $atts['type'] === 'basicslider' || ( $atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicslider' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicslider' ) ) ) ) )
2118 return $output;
2119
2120 // ID
2121 $atts['id'] = (int) $atts['id'];
2122
2123 // add custom classes if needed
2124 if ( $rl_gallery )
2125 $atts['class'] .= ' ' . $atts['gallery_custom_class'];
2126
2127 // any classes?
2128 if ( $atts['class'] !== '' ) {
2129 $atts['class'] = trim( $atts['class'] );
2130
2131 // more than 1 class?
2132 if ( strpos( $atts['class'], ' ' ) !== false ) {
2133 // get unique valid HTML classes
2134 $atts['class'] = array_unique( array_filter( array_map( 'sanitize_html_class', explode( ' ', $atts['class'] ) ) ) );
2135
2136 if ( ! empty( $atts['class'] ) )
2137 $atts['class'] = implode( ' ', $atts['class'] );
2138 else
2139 $atts['class'] = '';
2140 // single class
2141 } else
2142 $atts['class'] = sanitize_html_class( $atts['class'] );
2143 }
2144
2145 // orderby
2146 if ( empty( $atts['orderby'] ) ) {
2147 $atts['orderby'] = sanitize_sql_orderby( $atts['orderby'] );
2148
2149 if ( empty( $atts['orderby'] ) )
2150 $atts['orderby'] = $defaults['orderby'];
2151 }
2152
2153 // order
2154 if ( strtolower( $atts['order'] ) === 'rand' )
2155 $atts['orderby'] = 'rand';
2156
2157 // gallery lightbox source size
2158 if ( ! empty( $atts['lightbox_image_size'] ) ) {
2159 if ( $atts['lightbox_image_size'] === 'global' )
2160 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
2161 elseif ( $atts['lightbox_image_size'] === 'lightbox_custom_size' && isset( $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ) )
2162 $atts['src_size'] = [ $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ];
2163 else
2164 $atts['src_size'] = $atts['lightbox_image_size'];
2165 } else
2166 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
2167
2168 // filter all shortcode arguments
2169 $atts = apply_filters( 'rl_gallery_shortcode_atts', $atts, $rl_gallery_id );
2170
2171 // get gallery images
2172 $images = rl_get_gallery_shortcode_images( $atts );
2173
2174 if ( empty( $images ) || is_feed() || defined( 'IS_HTML_EMAIL' ) )
2175 return $output;
2176
2177 // make sure it is integer
2178 $gallery_no = (int) $this->gallery_no;
2179
2180 ob_start();
2181
2182 // $gallery_no and $rl_gallery_id are both integers ?>
2183 <div class="rl-gallery-container<?php echo esc_attr( apply_filters( 'rl_gallery_container_class', '', $atts, $rl_gallery_id ) ); ?>" id="rl-gallery-container-<?php echo (int) $gallery_no; ?>" data-gallery_id="<?php echo (int) $rl_gallery_id; ?>">
2184
2185 <?php do_action( 'rl_before_gallery', $atts, $rl_gallery_id ); ?>
2186
2187 <ul class="rl-gallery rl-basicslider-gallery <?php echo esc_attr( $atts['class'] ); ?>" id="rl-gallery-<?php echo (int) $gallery_no; ?>" data-gallery_no="<?php echo (int) $gallery_no; ?>">
2188
2189 <?php foreach ( $images as $image ) {
2190 // $image['link'] is already escaped
2191 echo '<li class="rl-gallery-item">' . $image['link'] . '</li>';
2192 } ?>
2193
2194 </ul>
2195
2196 <?php do_action( 'rl_after_gallery', $atts, $rl_gallery_id ); ?>
2197
2198 </div>
2199
2200 <?php $gallery_html = ob_get_contents();
2201
2202 ob_end_clean();
2203
2204 // scripts
2205 wp_register_script( 'responsive-lightbox-basicslider-gallery-js', plugins_url( 'assets/slippry/slippry' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.js', dirname( __FILE__ ) ), [ 'jquery' ], $rl->defaults['version'], ( $rl->options['settings']['loading_place'] === 'footer' ) );
2206 wp_enqueue_script( 'responsive-lightbox-basicslider-gallery', plugins_url( 'js/front-basicslider.js', dirname( __FILE__ ) ), [ 'jquery', 'responsive-lightbox-basicslider-gallery-js' ], $rl->defaults['version'], ( $rl->options['settings']['loading_place'] === 'footer' ) );
2207
2208 // styles
2209 wp_enqueue_style( 'responsive-lightbox-basicslider-gallery', plugins_url( 'assets/slippry/slippry' . ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : '' ) . '.css', dirname( __FILE__ ) ), [], $rl->defaults['version'] );
2210
2211 // prepare script data
2212 $script_data = [
2213 'adaptive_height' => $atts['adaptive_height'],
2214 'loop' => $atts['loop'],
2215 'captions' => $atts['captions'],
2216 'init_single' => $atts['init_single'],
2217 'responsive' => $atts['responsive'],
2218 'preload' => $atts['preload'],
2219 'pager' => $atts['pager'],
2220 'controls' => $atts['controls'],
2221 'hide_on_end' => $atts['hide_on_end'],
2222 'slide_margin' => $atts['slide_margin'],
2223 'transition' => $atts['transition'],
2224 'kenburns_zoom' => $atts['kenburns_zoom'],
2225 'speed' => $atts['speed'],
2226 'easing' => $atts['easing'],
2227 'continuous' => $atts['continuous'],
2228 'use_css' => $atts['use_css'],
2229 'slideshow' => $atts['slideshow'],
2230 'slideshow_direction' => $atts['slideshow_direction'],
2231 'slideshow_hover' => $atts['slideshow_hover'],
2232 'slideshow_hover_delay' => $atts['slideshow_hover_delay'],
2233 'slideshow_delay' => $atts['slideshow_delay'],
2234 'slideshow_pause' => $atts['slideshow_pause']
2235 ];
2236
2237 wp_add_inline_script( 'responsive-lightbox-basicslider-gallery', 'var rlArgsBasicSliderGallery' . ( $gallery_no + 1 ) . ' = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
2238
2239 // remove any new lines from the output so that the reader parses it better
2240 return apply_filters( 'rl_gallery_shortcode_html', trim( preg_replace( '/\s+/', ' ', $gallery_html ) ), $atts, $rl_gallery_id );
2241 }
2242
2243 /**
2244 * Render Basic Masonry gallery shortcode.
2245 *
2246 * @global object $post
2247 *
2248 * @param string $output HTML output
2249 * @param array $shortcode_atts Shortcode attributes
2250 * @return string
2251 */
2252 public function basic_masonry_gallery_shortcode( $output, $shortcode_atts ) {
2253 if ( ! empty( $output ) )
2254 return $output;
2255
2256 global $post;
2257
2258 $defaults = [
2259 'rl_gallery_id' => 0,
2260 'id' => isset( $post->ID ) ? (int) $post->ID : 0,
2261 'class' => '',
2262 'include' => '',
2263 'exclude' => '',
2264 'urls' => '',
2265 'type' => '',
2266 'order' => 'asc',
2267 'orderby' => 'menu_order',
2268 'size' => 'medium',
2269 'link' => 'file',
2270 'columns' => 3
2271 ];
2272
2273 // get main instance
2274 $rl = Responsive_Lightbox();
2275
2276 if ( ! is_array( $shortcode_atts ) )
2277 $shortcode_atts = wp_parse_args( $shortcode_atts, $defaults );
2278
2279 // is there rl_gallery ID?
2280 $rl_gallery_id = $defaults['rl_gallery_id'] = ! empty( $shortcode_atts['rl_gallery_id'] ) ? (int) $shortcode_atts['rl_gallery_id'] : 0;
2281
2282 // is it rl gallery?
2283 $rl_gallery = $rl->options['builder']['gallery_builder'] && $rl_gallery_id && get_post_type( $rl_gallery_id ) === 'rl_gallery';
2284
2285 // private gallery?
2286 if ( $rl_gallery && get_post_status( $rl_gallery_id ) === 'private' && ! current_user_can( 'read_private_posts' ) )
2287 return '';
2288
2289 if ( ! array_key_exists( 'type', $shortcode_atts ) )
2290 $shortcode_atts['type'] = '';
2291
2292 // break if it is not basic masonry gallery - first check
2293 if ( ! ( $shortcode_atts['type'] === 'basicmasonry' || ( $shortcode_atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicmasonry' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicmasonry' ) ) ) ) )
2294 return $output;
2295
2296 // get shortcode gallery fields combined with defaults
2297 $fields = rl_get_gallery_fields( 'basicmasonry' );
2298
2299 // get gallery fields attributes
2300 $field_atts = rl_get_gallery_fields_atts( $fields, $shortcode_atts, $rl_gallery );
2301
2302 // is it rl gallery? add misc and lightbox fields
2303 if ( $rl_gallery ) {
2304 // get fields
2305 $fields_data = $rl->galleries->get_data( 'fields' );
2306
2307 $fields += $fields_data['lightbox']['options'] + $fields_data['misc']['options'];
2308 }
2309
2310 // get only valid arguments
2311 $atts = shortcode_atts( array_merge( $defaults, $field_atts ), $shortcode_atts, 'gallery' );
2312
2313 // sanitize gallery fields
2314 $atts = $this->sanitize_shortcode_args( $atts, $fields );
2315
2316 // break if it is not basic masonry gallery
2317 if ( ! ( $atts['type'] === 'basicmasonry' || ( $atts['type'] === '' && ( ( $rl_gallery && $rl->options['settings']['builder_gallery'] === 'basicmasonry' ) || ( ! $rl_gallery && $rl->options['settings']['default_gallery'] === 'basicmasonry' ) ) ) ) )
2318 return $output;
2319
2320 // ID
2321 $atts['id'] = (int) $atts['id'];
2322
2323 // add custom classes if needed
2324 if ( $rl_gallery )
2325 $atts['class'] .= ' ' . $atts['gallery_custom_class'];
2326
2327 // any classes?
2328 if ( $atts['class'] !== '' ) {
2329 $atts['class'] = trim( $atts['class'] );
2330
2331 // more than 1 class?
2332 if ( strpos( $atts['class'], ' ' ) !== false ) {
2333 // get unique valid HTML classes
2334 $atts['class'] = array_unique( array_filter( array_map( 'sanitize_html_class', explode( ' ', $atts['class'] ) ) ) );
2335
2336 if ( ! empty( $atts['class'] ) )
2337 $atts['class'] = implode( ' ', $atts['class'] );
2338 else
2339 $atts['class'] = '';
2340 // single class
2341 } else
2342 $atts['class'] = sanitize_html_class( $atts['class'] );
2343 }
2344
2345 // orderby
2346 if ( empty( $atts['orderby'] ) ) {
2347 $atts['orderby'] = sanitize_sql_orderby( $atts['orderby'] );
2348
2349 if ( empty( $atts['orderby'] ) )
2350 $atts['orderby'] = $defaults['orderby'];
2351 }
2352
2353 // order
2354 if ( strtolower( $atts['order'] ) === 'rand' )
2355 $atts['orderby'] = 'rand';
2356
2357 // check columns
2358 if ( $atts['columns_lg'] === 0 )
2359 $atts['columns_lg'] = $atts['columns'];
2360
2361 if ( $atts['columns_md'] === 0 )
2362 $atts['columns_md'] = $atts['columns'];
2363
2364 if ( $atts['columns_sm'] === 0 )
2365 $atts['columns_sm'] = $atts['columns'];
2366
2367 if ( $atts['columns_xs'] === 0 )
2368 $atts['columns_xs'] = $atts['columns'];
2369
2370 // gallery lightbox source size
2371 if ( ! empty( $atts['lightbox_image_size'] ) ) {
2372 if ( $atts['lightbox_image_size'] === 'global' )
2373 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
2374 elseif ( $atts['lightbox_image_size'] === 'lightbox_custom_size' && isset( $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ) )
2375 $atts['src_size'] = [ $atts['lightbox_custom_size_width'], $atts['lightbox_custom_size_height'] ];
2376 else
2377 $atts['src_size'] = $atts['lightbox_image_size'];
2378 } else
2379 $atts['src_size'] = $rl->options['settings']['gallery_image_size'];
2380
2381 // filter all shortcode arguments
2382 $atts = apply_filters( 'rl_gallery_shortcode_atts', $atts, $rl_gallery_id );
2383
2384 // get gallery images
2385 $images = rl_get_gallery_shortcode_images( $atts );
2386
2387 if ( empty( $images ) || is_feed() || defined( 'IS_HTML_EMAIL' ) )
2388 return $output;
2389
2390 // make sure it is integer
2391 $gallery_no = (int) $this->gallery_no;
2392
2393 ob_start();
2394
2395 // $gallery_no and $rl_gallery_id are both integers ?>
2396 <div class="rl-gallery-container<?php echo esc_attr( apply_filters( 'rl_gallery_container_class', '', $atts, $rl_gallery_id ) ); ?>" id="rl-gallery-container-<?php echo (int) $gallery_no; ?>" data-gallery_id="<?php echo (int) $rl_gallery_id; ?>">
2397
2398 <?php do_action( 'rl_before_gallery', $atts, $rl_gallery_id ); ?>
2399
2400 <div class="rl-gallery rl-basicmasonry-gallery <?php echo esc_attr( $atts['class'] ); ?>" id="rl-gallery-<?php echo (int) $gallery_no; ?>" data-gallery_no="<?php echo (int) $gallery_no; ?>">
2401
2402 <?php
2403 $count = 0;
2404
2405 if ( $count === 0 )
2406 echo '<div class="rl-gutter-sizer"></div><div class="rl-grid-sizer"></div>';
2407
2408 foreach ( $images as $image ) {
2409 // $image['link'] is already escaped
2410 echo '
2411 <div class="rl-gallery-item' . ( $count === 0 ? ' rl-gallery-item-width-4' : '' ) . '" ' . implode( ' ', apply_filters( 'rl_gallery_item_extra_args', [], $atts, $image ) ) . '>
2412 <div class="rl-gallery-item-content">
2413 ' . $image['link'] . '
2414 </div>
2415 </div>';
2416
2417 $count++;
2418 } ?>
2419
2420 </div>
2421
2422 <?php do_action( 'rl_after_gallery', $atts, $rl_gallery_id ); ?>
2423
2424 </div>
2425
2426 <?php $gallery_html = ob_get_contents();
2427
2428 ob_clean();
2429
2430 // scripts
2431 wp_enqueue_script( 'responsive-lightbox-basicmasonry-gallery', plugins_url( 'js/front-basicmasonry.js', dirname( __FILE__ ) ), [ 'jquery', 'responsive-lightbox-masonry', 'responsive-lightbox-images-loaded' ], $rl->defaults['version'], ( $rl->options['settings']['loading_place'] === 'footer' ) );
2432
2433 // styles
2434 wp_enqueue_style( 'responsive-lightbox-basicmasonry-gallery', plugins_url( 'css/gallery-basicmasonry.css', dirname( __FILE__ ) ), [], $rl->defaults['version'] );
2435
2436 // add inline style
2437 wp_add_inline_style( 'responsive-lightbox-basicmasonry-gallery', '
2438 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery {
2439 margin: ' . -(string) round( (int) $atts['margin'] / 2, 1 ) . 'px ' . -(string) round( (int) $atts['gutter'] / 2, 1 ) . 'px;
2440 padding: ' . (int) $atts['margin'] . 'px 0;
2441 }
2442 #rl-gallery-container-' . $gallery_no . ' .rl-pagination-bottom {
2443 margin-top: ' . ( (int) $atts['margin'] / 2 ) . 'px
2444 }
2445 #rl-gallery-container-' . $gallery_no . ' .rl-pagination-top {
2446 margin-bottom: ' . ( (int) $atts['margin'] / 2 ) . 'px
2447 }
2448 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-gallery-item,
2449 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-grid-sizer {
2450 width: calc(' . (string) round( 100 / (int) $atts['columns'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2451 margin: ' . ( (int) $atts['margin'] / 2 ) . 'px ' . ( (int) $atts['gutter'] / 2 ) . 'px;
2452 }
2453 @media all and (min-width: 1200px) {
2454 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-gallery-item,
2455 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-grid-sizer {
2456 width: calc(' . (string) round( 100 / (int) $atts['columns_lg'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2457 margin: ' . ( (int) $atts['margin'] / 2 ) . 'px ' . ( (int) $atts['gutter'] / 2 ) . 'px;
2458 }
2459 }
2460 @media all and (min-width: 992px) and (max-width: 1200px) {
2461 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-gallery-item,
2462 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-grid-sizer {
2463 width: calc(' . (string) round( 100 / (int) $atts['columns_md'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2464 margin: ' . ( (int) $atts['margin'] / 2 ) . 'px ' . ( (int) $atts['gutter'] / 2 ) . 'px;
2465 }
2466 }
2467 @media all and (min-width: 768px) and (max-width: 992px) {
2468 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-gallery-item,
2469 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-grid-sizer {
2470 width: calc(' . (string) round( 100 / (int) $atts['columns_sm'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2471 margin: ' . ( (int) $atts['margin'] / 2 ) . 'px ' . ( (int) $atts['gutter'] / 2 ) . 'px;
2472 }
2473 }
2474 @media all and (max-width: 768px) {
2475 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-gallery-item,
2476 #rl-gallery-container-' . $gallery_no . ' .rl-basicmasonry-gallery .rl-grid-sizer {
2477 width: calc(' . (string) round( 100 / (int) $atts['columns_xs'], 2 ) . '% - ' . (int) $atts['gutter'] . 'px);
2478 margin: ' . ( (int) $atts['margin'] / 2 ) . 'px ' . ( (int) $atts['gutter'] / 2 ) . 'px;
2479 }
2480 }'
2481 );
2482
2483 // prepare script data
2484 $script_data = [
2485 'originLeft' => $atts['origin_left'],
2486 'originTop' => $atts['origin_top']
2487 ];
2488
2489 wp_add_inline_script( 'responsive-lightbox-basicmasonry-gallery', 'var rlArgsBasicMasonryGallery' . ( $gallery_no + 1 ) . ' = ' . wp_json_encode( $script_data ) . ";\n", 'before' );
2490
2491 // remove any new lines from the output so that the reader parses it better
2492 return apply_filters( 'rl_gallery_shortcode_html', trim( preg_replace( '/\s+/', ' ', $gallery_html ) ), $atts, $rl_gallery_id );
2493 }
2494 }
2495