class-frontend.php
317 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, 10, 2 ); |
| 22 | add_filter( 'wp_get_attachment_link', array( &$this, 'add_gallery_lightbox_selector' ), 1000, 6 ); |
| 23 | add_filter( 'the_content', array( &$this, 'add_videos_lightbox_selector' ) ); |
| 24 | add_filter( 'the_content', array( &$this, 'add_links_lightbox_selector' ) ); |
| 25 | add_filter( 'post_gallery', array( &$this, 'add_custom_gallery_lightbox_selector' ), 2000, 10, 2 ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Add lightbox to videos |
| 30 | * |
| 31 | * @param mixed $content |
| 32 | * @return mixed |
| 33 | */ |
| 34 | public function add_videos_lightbox_selector( $content ) { |
| 35 | |
| 36 | if ( Responsive_Lightbox()->options['settings']['videos'] === true ) { |
| 37 | |
| 38 | preg_match_all( '/<a(.*?)href=(?:\'|")((?:http|https|)(?::\/\/|)(?:www.|)((?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*)|((?:http|https|)(?::\/\/|)(?:www.|)(?:vimeo\.com\/[0-9]*(?:.+))))(?:\'|")(.*?)>/i', $content, $links ); |
| 39 | |
| 40 | if ( isset( $links[0] ) ) { |
| 41 | foreach ( $links[0] as $id => $link ) { |
| 42 | if ( preg_match( '/<a.*?(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 43 | if ( isset( $result[1] ) ) { |
| 44 | $new_rels = array(); |
| 45 | $rels = explode( ' ', $result[1] ); |
| 46 | |
| 47 | if ( in_array( Responsive_Lightbox()->options['settings']['selector'], $rels, true ) ) { |
| 48 | foreach ( $rels as $no => $rel ) { |
| 49 | if ( $rel !== Responsive_Lightbox()->options['settings']['selector'] ) |
| 50 | $new_rels[] = $rel; |
| 51 | } |
| 52 | |
| 53 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . ( ! empty( $new_rel ) ? simplode( ' ', $new_rels ) . ' ' : '') . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '"', $link ), $content ); |
| 54 | } else |
| 55 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . ( $result[1] !== '' ? $result[1] . ' ' : '' ) . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '"', $link ), $content ); |
| 56 | } |
| 57 | } else { |
| 58 | // swipebox video fix |
| 59 | if ( Responsive_Lightbox()->options['settings']['script'] === 'swipebox' && strpos( $links[2][$id], 'vimeo') !== false ) { |
| 60 | $links[2][$id] = $links[2][$id] . '?width=' . Responsive_Lightbox()->options['configuration']['swipebox']['video_max_width']; |
| 61 | } |
| 62 | $content = str_replace( $link, '<a' . $links[1][$id] . 'href="' . $links[2][$id] . '" data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '">', $content ); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | |
| 69 | return $content; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add lightbox to to image links |
| 74 | * |
| 75 | * @param mixed $content |
| 76 | * @return mixed |
| 77 | */ |
| 78 | public function add_links_lightbox_selector( $content ) { |
| 79 | |
| 80 | if ( Responsive_Lightbox()->options['settings']['image_links'] === true || Responsive_Lightbox()->options['settings']['images_as_gallery'] === true ) { |
| 81 | |
| 82 | preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links ); |
| 83 | |
| 84 | if ( isset( $links[0] ) ) { |
| 85 | |
| 86 | // generate hash for single images gallery |
| 87 | if ( Responsive_Lightbox()->options['settings']['images_as_gallery'] === true ) |
| 88 | $rel_hash = '-gallery-' . $this->generate_password( 4 ); |
| 89 | |
| 90 | foreach ( $links[0] as $id => $link ) { |
| 91 | |
| 92 | // single image title |
| 93 | $title = ''; |
| 94 | |
| 95 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['image_title'] ) !== 'default' ) { |
| 96 | |
| 97 | $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] ); |
| 98 | |
| 99 | if ( $image_id ) { |
| 100 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ); |
| 101 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, $title_arg ) ) ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // link contains rel / data-rel attribute |
| 106 | if ( preg_match( '/<a.*?(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 107 | |
| 108 | // single images gallery |
| 109 | if ( Responsive_Lightbox()->options['settings']['images_as_gallery'] === true ) { |
| 110 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . $rel_hash . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '"', $link ), $content ); |
| 111 | // single image |
| 112 | } else { |
| 113 | if ( isset( $result[1] ) ) { |
| 114 | $new_rels = array(); |
| 115 | $rels = explode( ' ', $result[1] ); |
| 116 | |
| 117 | if ( in_array( Responsive_Lightbox()->options['settings']['selector'], $rels, true ) ) { |
| 118 | foreach ( $rels as $no => $rel ) { |
| 119 | if ( $rel !== Responsive_Lightbox()->options['settings']['selector'] ) |
| 120 | $new_rels[] = $rel; |
| 121 | } |
| 122 | |
| 123 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . ( ! empty( $new_rels ) ? implode( ' ', $new_rels ) . ' ' : '' ) . Responsive_Lightbox()->options['settings']['selector'] . '-' . $id . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '"', $link ), $content ); |
| 124 | } else |
| 125 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . ( $result[1] !== '' ? $result[1] . ' ' : '' ) . Responsive_Lightbox()->options['settings']['selector'] . '-' . $id . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '"', $link ), $content ); |
| 126 | } |
| 127 | } |
| 128 | // link without rel/data-rel |
| 129 | } else { |
| 130 | $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'] === true ? $rel_hash : '-' . $id ) . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '">', $content ); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 137 | return $content; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add lightbox to gallery |
| 142 | */ |
| 143 | public function add_gallery_lightbox_selector( $link, $id, $size, $permalink, $icon, $text ) { |
| 144 | |
| 145 | if ( Responsive_Lightbox()->options['settings']['galleries'] === true && wp_attachment_is_image( $id ) ) { |
| 146 | |
| 147 | // gallery link target image |
| 148 | $src = array(); |
| 149 | |
| 150 | // gallery image title |
| 151 | $title = ''; |
| 152 | |
| 153 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) { |
| 154 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $link, $id ); |
| 155 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $id, $title_arg ) ) ); |
| 156 | } |
| 157 | |
| 158 | if ( $title ) { |
| 159 | $link = str_replace( '<a href', '<a title="'. $title .'" href', $link ); |
| 160 | } |
| 161 | |
| 162 | $link = ( preg_match( '/<a.*? (?:rel|data-rel)=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? data-rel=(?:"|\').*?)((?:"|\').*?>)/', '$1 ' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '">', $link ) ); |
| 163 | |
| 164 | // gallery image size |
| 165 | if ( Responsive_Lightbox()->options['settings']['gallery_image_size'] != 'full' ) { |
| 166 | $src = wp_get_attachment_image_src( $id, Responsive_Lightbox()->options['settings']['gallery_image_size'] ); |
| 167 | |
| 168 | $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) ); |
| 169 | } else { |
| 170 | $src = wp_get_attachment_image_src( $id, 'full' ); |
| 171 | |
| 172 | $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) ); |
| 173 | } |
| 174 | |
| 175 | return apply_filters( 'rl_lightbox_attachment_link', $link, $id, $size, $permalink, $icon, $text, $src ); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | return $link; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Add lightbox to Jetpack tiled gallery |
| 184 | * |
| 185 | * @param mixed $content |
| 186 | * @param array $attr |
| 187 | * @return mixed |
| 188 | */ |
| 189 | public function add_custom_gallery_lightbox_selector( $content, $attr ) { |
| 190 | |
| 191 | if ( Responsive_Lightbox()->options['settings']['force_custom_gallery'] === true ) { |
| 192 | |
| 193 | preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links ); |
| 194 | |
| 195 | if ( isset( $links[0] ) ) { |
| 196 | |
| 197 | foreach ( $links[0] as $id => $link ) { |
| 198 | |
| 199 | // gallery image title |
| 200 | $title = ''; |
| 201 | |
| 202 | if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) { |
| 203 | |
| 204 | $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] ); |
| 205 | |
| 206 | if ( $image_id ) { |
| 207 | $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] ); |
| 208 | $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, $title_arg ) ) ); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if ( preg_match( '/<a.*?(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) { |
| 213 | $content = str_replace( $link, preg_replace( '/(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"' . ( ! empty ( $title ) ? ' title="' . $title . '"' : '' ) . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ), $link ), $content ); |
| 214 | } else { |
| 215 | $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'] . '-gallery-' . $this->gallery_no . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ( ! empty ( $title ) ? ' title="' . $title . '"' : '' ) . '>', $content ); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | return $content; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get attachment title function |
| 227 | * |
| 228 | * @param int $id |
| 229 | * @param string $title_arg |
| 230 | * @return string |
| 231 | */ |
| 232 | public function get_attachment_title( $id, $title_arg ) { |
| 233 | |
| 234 | if ( empty( $title_arg ) || empty( $id ) ) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | switch( $title_arg ) { |
| 239 | case 'title': |
| 240 | $title = get_the_title( $id ); |
| 241 | break; |
| 242 | case 'caption': |
| 243 | $title = get_post_field( 'post_excerpt', $id ) ; |
| 244 | break; |
| 245 | case 'alt': |
| 246 | $title = get_post_meta( $id, '_wp_attachment_image_alt', true ); |
| 247 | break; |
| 248 | case 'description': |
| 249 | $title = get_post_field( 'post_content', $id ) ; |
| 250 | break; |
| 251 | default: |
| 252 | $title = ''; |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | return apply_filters( 'rl_get_attachment_title', $title, $id, $title_arg ); |
| 257 | |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get attachment id by url function, adjusted to work cropped images |
| 262 | * |
| 263 | * @param string $url |
| 264 | * @return int |
| 265 | */ |
| 266 | public function get_attachment_id_by_url( $url ) { |
| 267 | $post_id = attachment_url_to_postid( $url ); |
| 268 | |
| 269 | if ( ! $post_id ) { |
| 270 | $dir = wp_upload_dir(); |
| 271 | $path = $url; |
| 272 | |
| 273 | if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { |
| 274 | $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
| 275 | } |
| 276 | |
| 277 | if ( preg_match( '/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches ) ){ |
| 278 | $url = $dir['baseurl'] . '/' . $matches[1] . $matches[3]; |
| 279 | $post_id = attachment_url_to_postid( $url ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return (int) $post_id; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Helper: generate password without wp_rand() and DB call it uses |
| 288 | * |
| 289 | * @param int $length |
| 290 | * @return string |
| 291 | */ |
| 292 | private function generate_password( $length = 64 ) { |
| 293 | $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; |
| 294 | $password = ''; |
| 295 | |
| 296 | for( $i = 0; $i < $length; $i++ ) { |
| 297 | $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 ); |
| 298 | } |
| 299 | |
| 300 | return $password; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Helper: gallery number function |
| 305 | * |
| 306 | * @param mixed $content |
| 307 | * @param array $attr |
| 308 | * @return mixed |
| 309 | */ |
| 310 | public function gallery_attributes( $content, $attr ) { |
| 311 | |
| 312 | ++ $this->gallery_no; |
| 313 | |
| 314 | return $content; |
| 315 | } |
| 316 | |
| 317 | } |