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