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