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