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