class-frontend.php
551 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) |
| 3 | exit; |
| 4 | |
| 5 | new Responsive_Lightbox_Frontend(); |
| 6 | |
| 7 | /** |
| 8 | * Responsive Lightbox frontend class. |
| 9 | * |
| 10 | * @class Responsive_Lightbox_Frontend |
| 11 | */ |
| 12 | class Responsive_Lightbox_Frontend { |
| 13 | |
| 14 | public $gallery_no = 0; |
| 15 | |
| 16 | public function __construct() { |
| 17 | // set instance |
| 18 | Responsive_Lightbox()->frontend = $this; |
| 19 | |
| 20 | // filters |
| 21 | add_filter( 'post_gallery', array( $this, 'gallery_attributes' ), 1000 ); |
| 22 | add_filter( 'the_content', array( $this, 'add_links_lightbox_selector' ) ); |
| 23 | add_filter( 'the_content', array( $this, 'add_videos_lightbox_selector' ) ); |
| 24 | add_filter( 'the_content', array( $this, 'add_custom_gallery_lightbox_selector' ), 2000, 2 ); |
| 25 | add_filter( 'wp_get_attachment_link', array( $this, 'add_gallery_lightbox_selector' ), 1000, 6 ); |
| 26 | add_filter( 'woocommerce_single_product_image_html', array( $this, 'woocommerce_single_product_image_html' ), 100 ); |
| 27 | add_filter( 'woocommerce_single_product_image_thumbnail_html', array( $this, 'woocommerce_single_product_image_thumbnail_html' ), 100 ); |
| 28 | add_filter( 'get_comment_text', array( $this, 'get_comment_text' ) ); |
| 29 | add_filter( 'dynamic_sidebar_params', array( $this, 'dynamic_sidebar_params' ) ); |
| 30 | add_filter( 'rl_widget_output', array( $this, 'widget_output' ), 10, 3 ); |
| 31 | |
| 32 | // actions |
| 33 | add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ), 100 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add lightbox to to image links |
| 38 | * |
| 39 | * @param mixed $content |
| 40 | * @return mixed |
| 41 | */ |
| 42 | public function add_links_lightbox_selector( $content ) { |
| 43 | if ( Responsive_Lightbox()->options['settings']['image_links'] || Responsive_Lightbox()->options['settings']['images_as_gallery'] ) { |
| 44 | |
| 45 | // search for image-links |
| 46 | preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/is', $content, $links ); |
| 47 | |
| 48 | // found? |
| 49 | if ( ! empty ( $links[0] ) ) { |
| 50 | // generate hash for single images gallery |
| 51 | if ( Responsive_Lightbox()->options['settings']['images_as_gallery'] ) |
| 52 | $rel_hash = '-gallery-' . $this->generate_password( 4 ); |
| 53 | |
| 54 | foreach ( $links[0] as $id => $link ) { |
| 55 | // single image title |
| 56 | $title = ''; |
| 57 | |
| 58 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['image_title'] ) !== 'default' ) { |
| 59 | // get attachment id |
| 60 | $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] ); |
| 61 | |
| 62 | if ( $image_id ) |
| 63 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ) ) ) ); |
| 64 | } |
| 65 | |
| 66 | // update title if needed |
| 67 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['image_title'] ) !== 'default' && $image_id ) |
| 68 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ) ) ) ); |
| 69 | |
| 70 | // link contains data-rel attribute |
| 71 | if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/s', $link, $result ) === 1 ) { |
| 72 | |
| 73 | // do not modify this link |
| 74 | if ( $result[1] === 'norl' ) |
| 75 | continue; |
| 76 | |
| 77 | // single images gallery |
| 78 | if ( Responsive_Lightbox()->options['settings']['images_as_gallery'] ) |
| 79 | $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(?:.*?)(?:\'|")/s', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . base64_encode( $result[1] ) . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . esc_attr( $title ) . '"', $link ), $content ); |
| 80 | // single image |
| 81 | else { |
| 82 | $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(?:.*?)(?:\'|")/s', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-' . base64_encode( $result[1] ) . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . esc_attr( $title ) . '"', $link ), $content ); |
| 83 | } |
| 84 | // link without data-rel |
| 85 | } else { |
| 86 | $content = str_replace( $link, '<a' . $links[1][$id] . 'href="' . $links[2][$id] . '.' . $links[3][$id] . '"' . $links[4][$id] . ' data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . ( Responsive_Lightbox()->options['settings']['images_as_gallery'] ? $rel_hash : '-' . $id ) . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . esc_attr( $title ) . '">', $content ); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $content; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add lightbox to gallery |
| 97 | */ |
| 98 | public function add_gallery_lightbox_selector( $link, $id, $size, $permalink, $icon, $text ) { |
| 99 | |
| 100 | if ( Responsive_Lightbox()->options['settings']['galleries'] && wp_attachment_is_image( $id ) ) { |
| 101 | // gallery link target image |
| 102 | $src = array(); |
| 103 | |
| 104 | // gallery image title |
| 105 | $title = ''; |
| 106 | |
| 107 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) { |
| 108 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $link, $id ); |
| 109 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $id, $title_arg ) ) ); |
| 110 | } |
| 111 | |
| 112 | if ( $title ) |
| 113 | $link = str_replace( '<a href', '<a title="'. esc_attr( $title ) .'" href', $link ); |
| 114 | |
| 115 | $link = ( preg_match( '/<a.*? (?:data-rel)=("|\').*?("|\')>/s', $link ) === 1 ? preg_replace( '/(<a.*? data-rel=(?:"|\').*?)((?:"|\').*?>)/s', '$1 ' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '$2', $link ) : preg_replace( '/(<a.*?)>/s', '$1 data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '">', $link ) ); |
| 116 | |
| 117 | // gallery image size |
| 118 | if ( Responsive_Lightbox()->options['settings']['gallery_image_size'] != 'full' ) { |
| 119 | $src = wp_get_attachment_image_src( $id, Responsive_Lightbox()->options['settings']['gallery_image_size'] ); |
| 120 | |
| 121 | $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) ); |
| 122 | } else { |
| 123 | $src = wp_get_attachment_image_src( $id, 'full' ); |
| 124 | |
| 125 | $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) ); |
| 126 | } |
| 127 | |
| 128 | return apply_filters( 'rl_lightbox_attachment_link', $link, $id, $size, $permalink, $icon, $text, $src ); |
| 129 | } |
| 130 | |
| 131 | return $link; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Add lightbox to Jetpack tiled gallery |
| 136 | * |
| 137 | * @param mixed $content |
| 138 | * @param array $attr |
| 139 | * @return mixed |
| 140 | */ |
| 141 | public function add_custom_gallery_lightbox_selector( $content ) { |
| 142 | if ( Responsive_Lightbox()->options['settings']['force_custom_gallery'] ) { |
| 143 | |
| 144 | preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links ); |
| 145 | |
| 146 | if ( isset( $links[0] ) ) { |
| 147 | |
| 148 | foreach ( $links[0] as $id => $link ) { |
| 149 | // gallery image title |
| 150 | $title = ''; |
| 151 | |
| 152 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) { |
| 153 | // get attachment id |
| 154 | $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] ); |
| 155 | |
| 156 | if ( $image_id ) { |
| 157 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ); |
| 158 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, $title_arg ) ) ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // get attachment id |
| 163 | $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] ); |
| 164 | |
| 165 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' && $image_id ) { |
| 166 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ); |
| 167 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, $title_arg ) ) ); |
| 168 | } |
| 169 | |
| 170 | if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 171 | // do not modify this link |
| 172 | if ( $result[1] === 'norl' ) |
| 173 | continue; |
| 174 | |
| 175 | $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . base64_encode( $result[1] ) . '"' . ( ! empty ( $title ) ? ' title="' . esc_attr( $title ) . '"' : '' ) . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ), $link ), $content ); |
| 176 | } elseif ( preg_match( '/<a.*?(?:rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 177 | // do not modify this link |
| 178 | if ( $result[1] === 'norl' ) |
| 179 | continue; |
| 180 | |
| 181 | $content = str_replace( $link, preg_replace( '/(?:rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . base64_encode( $result[1] ) . '"' . ( ! empty ( $title ) ? ' title="' . esc_attr( $title ) . '"' : '' ) . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ), $link ), $content ); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | return $content; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Add lightbox to videos |
| 193 | * |
| 194 | * @param mixed $content |
| 195 | * @return mixed |
| 196 | */ |
| 197 | public function add_videos_lightbox_selector( $content ) { |
| 198 | if ( Responsive_Lightbox()->options['settings']['videos'] ) { |
| 199 | // search for video-links |
| 200 | preg_match_all('/<a(.*?)href=(?:\'|")((?:http|https)(?::\/\/|)(?:www\.)?(?:(?:(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=))(?:[\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*)|(?:vimeo\.com\/[0-9]+[a-z0-9;:@#?&%=+\/\$_.-]*)))(?:\'|")(.*?)>/i', $content, $links ); |
| 201 | |
| 202 | // found? |
| 203 | if ( ! empty ( $links[0] ) ) { |
| 204 | foreach ( $links[0] as $id => $link ) { |
| 205 | if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 206 | |
| 207 | // do not modify this link |
| 208 | if ( $result[1] === 'norl' ) |
| 209 | continue; |
| 210 | |
| 211 | // swipebox video fix |
| 212 | if ( Responsive_Lightbox()->options['settings']['script'] === 'swipebox' && strpos( $links[2][$id], 'vimeo.com') !== false ) |
| 213 | $content = str_replace( $link, str_replace( $links[2][$id], $links[2][$id] . '?width=' . Responsive_Lightbox()->options['configuration']['swipebox']['video_max_width'], $link ), $content ); |
| 214 | |
| 215 | // replace data-rel |
| 216 | $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '"', $link ), $content ); |
| 217 | } else { |
| 218 | // swipebox video fix |
| 219 | if ( Responsive_Lightbox()->options['settings']['script'] === 'swipebox' && strpos( $links[2][$id], 'vimeo.com') !== false ) |
| 220 | $links[2][$id] = $links[2][$id] . '?width=' . Responsive_Lightbox()->options['configuration']['swipebox']['video_max_width']; |
| 221 | |
| 222 | // replace data-rel |
| 223 | $content = str_replace( $link, '<a' . $links[1][$id] . 'href="' . $links[2][$id] . '" data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '"' . $links[3][$id] . '>', $content ); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return $content; |
| 230 | } |
| 231 | |
| 232 | // zaktualizuj photo swipe |
| 233 | |
| 234 | /** |
| 235 | * Remove WooCommerce prettyPhoto lightbox styles and scripts. |
| 236 | */ |
| 237 | public function wp_enqueue_scripts() { |
| 238 | if ( class_exists( 'WooCommerce' ) ) { |
| 239 | global $woocommerce; |
| 240 | |
| 241 | // specific WooCommerce gallery? |
| 242 | if ( ! empty( Responsive_Lightbox()->options['settings']['default_woocommerce_gallery'] ) && Responsive_Lightbox()->options['settings']['default_woocommerce_gallery'] !== 'default' ) { |
| 243 | // replace default WooCommerce lightbox? |
| 244 | if ( Responsive_Lightbox()->options['settings']['woocommerce_gallery_lightbox'] === true ) { |
| 245 | if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) { |
| 246 | // dequeue scripts |
| 247 | wp_dequeue_script( 'flexslider' ); |
| 248 | wp_dequeue_script( 'photoswipe' ); |
| 249 | wp_dequeue_script( 'photoswipe-ui-default' ); |
| 250 | |
| 251 | // dequeue styles |
| 252 | wp_dequeue_style( 'photoswipe' ); |
| 253 | wp_dequeue_style( 'photoswipe-default-skin' ); |
| 254 | |
| 255 | // remove theme supports |
| 256 | remove_theme_support( 'wc-product-gallery-lightbox' ); |
| 257 | // remove_theme_support( 'wc-product-gallery-zoom' ); |
| 258 | remove_theme_support( 'wc-product-gallery-slider' ); |
| 259 | } else { |
| 260 | // remove styles |
| 261 | wp_dequeue_style( 'woocommerce_prettyPhoto_css' ); |
| 262 | |
| 263 | // remove scripts |
| 264 | wp_dequeue_script( 'prettyPhoto' ); |
| 265 | wp_dequeue_script( 'prettyPhoto-init' ); |
| 266 | wp_dequeue_script( 'fancybox' ); |
| 267 | wp_dequeue_script( 'enable-lightbox' ); |
| 268 | } |
| 269 | } else { |
| 270 | if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) { |
| 271 | // dequeue scripts |
| 272 | wp_dequeue_script( 'flexslider' ); |
| 273 | } |
| 274 | } |
| 275 | // default gallery? |
| 276 | } else { |
| 277 | // replace default WooCommerce lightbox? |
| 278 | if ( Responsive_Lightbox()->options['settings']['woocommerce_gallery_lightbox'] === true ) { |
| 279 | if ( version_compare( $woocommerce->version, '3.0', ">=" ) ) { |
| 280 | // dequeue scripts |
| 281 | wp_dequeue_script( 'photoswipe' ); |
| 282 | wp_dequeue_script( 'photoswipe-ui-default' ); |
| 283 | |
| 284 | // dequeue styles |
| 285 | wp_dequeue_style( 'photoswipe' ); |
| 286 | wp_dequeue_style( 'photoswipe-default-skin' ); |
| 287 | |
| 288 | // remove theme supports |
| 289 | remove_theme_support( 'wc-product-gallery-lightbox' ); |
| 290 | } else { |
| 291 | // remove styles |
| 292 | wp_dequeue_style( 'woocommerce_prettyPhoto_css' ); |
| 293 | |
| 294 | // remove scripts |
| 295 | wp_dequeue_script( 'prettyPhoto' ); |
| 296 | wp_dequeue_script( 'prettyPhoto-init' ); |
| 297 | wp_dequeue_script( 'fancybox' ); |
| 298 | wp_dequeue_script( 'enable-lightbox' ); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Visual Composer lightbox |
| 305 | if ( class_exists( 'Vc_Manager' ) ) { |
| 306 | wp_dequeue_script( 'prettyphoto' ); |
| 307 | wp_deregister_script( 'prettyphoto' ); |
| 308 | wp_dequeue_style( 'prettyphoto' ); |
| 309 | wp_deregister_style( 'prettyphoto' ); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Apply lightbox to WooCommerce product image. |
| 315 | * |
| 316 | * @param mixed $html |
| 317 | * @return mixed |
| 318 | */ |
| 319 | public function woocommerce_single_product_image_html( $html ) { |
| 320 | if ( Responsive_Lightbox()->options['settings']['woocommerce_gallery_lightbox'] === true ) { |
| 321 | $html = preg_replace( '/data-rel=\"(.*?)\"/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"', $html ); |
| 322 | } |
| 323 | |
| 324 | return $html; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Apply lightbox to WooCommerce product gallery. |
| 329 | * |
| 330 | * @param mixed $html |
| 331 | * @return mixed |
| 332 | */ |
| 333 | public function woocommerce_single_product_image_thumbnail_html( $html ) { |
| 334 | if ( Responsive_Lightbox()->options['settings']['woocommerce_gallery_lightbox'] === true ) { |
| 335 | $html = preg_replace( '/data-rel=\"(.*?)\"/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"', $html ); |
| 336 | |
| 337 | preg_match( '/<a(.*?)((?:data-rel)=(?:\'|").*?(?:\'|"))(.*?)>/', $html, $result ); |
| 338 | |
| 339 | // no data-rel? |
| 340 | if ( empty( $result ) ) { |
| 341 | preg_match( '/^(.*?)<a(.*?)((?:href)=(?:\'|").*?(?:\'|"))(.*?)>(.*?)$/', $html, $result ); |
| 342 | |
| 343 | // found valid link? |
| 344 | if ( ! empty( $result ) ) |
| 345 | $html = $result[1] . '<a' . $result[2] . ' data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '" ' . $result[3] . $result[4] . '>' . $result[5]; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return $html; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Get attachment title function |
| 354 | * |
| 355 | * @param int $id |
| 356 | * @param string $title_arg |
| 357 | * @return string |
| 358 | */ |
| 359 | public function get_attachment_title( $id, $title_arg ) { |
| 360 | |
| 361 | if ( empty( $title_arg ) || empty( $id ) ) { |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | switch( $title_arg ) { |
| 366 | case 'title': |
| 367 | $title = get_the_title( $id ); |
| 368 | break; |
| 369 | case 'caption': |
| 370 | $title = get_post_field( 'post_excerpt', $id ) ; |
| 371 | break; |
| 372 | case 'alt': |
| 373 | $title = get_post_meta( $id, '_wp_attachment_image_alt', true ); |
| 374 | break; |
| 375 | case 'description': |
| 376 | $title = get_post_field( 'post_content', $id ) ; |
| 377 | break; |
| 378 | default: |
| 379 | $title = ''; |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | return apply_filters( 'rl_get_attachment_title', $title, $id, $title_arg ); |
| 384 | |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get attachment id by url function, adjusted to work for cropped images |
| 389 | * |
| 390 | * @param string $url |
| 391 | * @return int |
| 392 | */ |
| 393 | public function get_attachment_id_by_url( $url ) { |
| 394 | $url = ! empty( $url ) ? esc_url( $url ) : ''; |
| 395 | |
| 396 | // get cached data |
| 397 | // $post_id = wp_cache_get( md5( $url ), 'rl-attachment_id_by_url' ); |
| 398 | $post_ids = get_transient( 'rl-attachment_ids' ); |
| 399 | $post_id = 0; |
| 400 | |
| 401 | // cached url not found? |
| 402 | if ( $post_ids === false || ! in_array( $url, array_keys( $post_ids ) ) ) { |
| 403 | $post_id = attachment_url_to_postid( $url ); |
| 404 | |
| 405 | if ( ! $post_id ) { |
| 406 | $dir = wp_upload_dir(); |
| 407 | $path = $url; |
| 408 | |
| 409 | if ( strpos( $path, $dir['baseurl'] . '/' ) === 0 ) |
| 410 | $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
| 411 | |
| 412 | if ( preg_match( '/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches ) ) |
| 413 | $post_id = attachment_url_to_postid( $dir['baseurl'] . '/' . $matches[1] . $matches[3] ); |
| 414 | } |
| 415 | |
| 416 | // set the cache expiration, 24 hours by default |
| 417 | $expire = absint( apply_filters( 'rl_object_cache_expire', DAY_IN_SECONDS ) ); |
| 418 | |
| 419 | // wp_cache_add( md5( $url ), $post_id, 'rl-attachment_id_by_url', $expire ); |
| 420 | |
| 421 | $post_ids[$url] = $post_id; |
| 422 | |
| 423 | set_transient( 'rl-attachment_ids', $post_ids, $expire ); |
| 424 | // cached url found |
| 425 | } elseif ( ! empty( $post_ids[$url] ) ) { |
| 426 | $post_id = absint( $post_ids[$url] ); |
| 427 | } |
| 428 | |
| 429 | return (int) $post_id; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Helper: generate password without wp_rand() and DB call it uses |
| 434 | * |
| 435 | * @param int $length |
| 436 | * @return string |
| 437 | */ |
| 438 | private function generate_password( $length = 64 ) { |
| 439 | $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
| 440 | $password = ''; |
| 441 | |
| 442 | for( $i = 0; $i < $length; $i++ ) { |
| 443 | $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 ); |
| 444 | } |
| 445 | |
| 446 | return $password; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Helper: gallery number function |
| 451 | * |
| 452 | * @param mixed $content |
| 453 | * @return mixed |
| 454 | */ |
| 455 | public function gallery_attributes( $content ) { |
| 456 | ++$this->gallery_no; |
| 457 | |
| 458 | return $content; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Replace widget callback function. |
| 463 | * |
| 464 | * @global array $wp_registered_widgets |
| 465 | * @param array $sidebar_params |
| 466 | * @return type |
| 467 | */ |
| 468 | public function dynamic_sidebar_params( $sidebar_params ) { |
| 469 | if ( ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) || Responsive_Lightbox()->options['settings']['widgets'] != true ) { |
| 470 | return $sidebar_params; |
| 471 | } |
| 472 | |
| 473 | global $wp_registered_widgets; |
| 474 | |
| 475 | $widget_id = $sidebar_params[0]['widget_id']; |
| 476 | |
| 477 | $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback']; |
| 478 | $wp_registered_widgets[ $widget_id ]['callback'] = array( $this, 'widget_callback_function' ); |
| 479 | |
| 480 | return $sidebar_params; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Widget callback function. |
| 485 | * |
| 486 | * @global array $wp_registered_widgets |
| 487 | */ |
| 488 | public function widget_callback_function() { |
| 489 | |
| 490 | global $wp_registered_widgets; |
| 491 | $original_callback_params = func_get_args(); |
| 492 | $widget_id = $original_callback_params[0]['widget_id']; |
| 493 | |
| 494 | $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback']; |
| 495 | $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback; |
| 496 | |
| 497 | $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base; |
| 498 | |
| 499 | if ( is_callable( $original_callback ) ) { |
| 500 | |
| 501 | ob_start(); |
| 502 | call_user_func_array( $original_callback, $original_callback_params ); |
| 503 | $widget_output = ob_get_clean(); |
| 504 | |
| 505 | echo apply_filters( 'rl_widget_output', $widget_output, $widget_id_base, $widget_id ); |
| 506 | |
| 507 | } |
| 508 | |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Filter widget output. |
| 513 | * |
| 514 | * @param mixed $widget_output |
| 515 | * @param string $widget_id_base |
| 516 | * @param id $widget_id |
| 517 | * @return mixed |
| 518 | */ |
| 519 | public function widget_output( $widget_output, $widget_id_base, $widget_id ) { |
| 520 | // filter galleries |
| 521 | $widget_output = $this->add_custom_gallery_lightbox_selector( $widget_output ); |
| 522 | // filter image links |
| 523 | $widget_output = $this->add_links_lightbox_selector( $widget_output ); |
| 524 | // filter videos |
| 525 | $widget_output = $this->add_videos_lightbox_selector( $widget_output ); |
| 526 | |
| 527 | return $widget_output; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Filter comment content. |
| 532 | * |
| 533 | * @param mixed $comment_content |
| 534 | * @return mixed |
| 535 | */ |
| 536 | public function get_comment_text( $comment_content ) { |
| 537 | if ( ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) || Responsive_Lightbox()->options['settings']['comments'] != true ) { |
| 538 | return $comment_content; |
| 539 | } |
| 540 | |
| 541 | // filter galleries |
| 542 | $comment_content = $this->add_custom_gallery_lightbox_selector( $comment_content ); |
| 543 | // filter image links |
| 544 | $comment_content = $this->add_links_lightbox_selector( $comment_content ); |
| 545 | // filter videos |
| 546 | $comment_content = $this->add_videos_lightbox_selector( $comment_content ); |
| 547 | |
| 548 | return $comment_content; |
| 549 | } |
| 550 | |
| 551 | } |