PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 1.6.2
Responsive Lightbox & Gallery v1.6.2
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / class-frontend.php
responsive-lightbox / includes Last commit date
class-frontend.php 10 years ago class-settings.php 10 years ago
class-frontend.php
304 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( 'post_gallery', array( $this, 'add_custom_gallery_lightbox_selector' ), 2000, 10, 2 );
23 add_filter( 'wp_get_attachment_link', array( $this, 'add_gallery_lightbox_selector' ), 1000, 6 );
24 add_filter( 'the_content', array( $this, 'add_videos_lightbox_selector' ) );
25 add_filter( 'the_content', array( $this, 'add_links_lightbox_selector' ) );
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 // search for image-links
39 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;:@#?&%=+\/\$_.-]*)|(?:vimeo\.com\/[0-9]+)))(?:\'|")(.*?)>/i', $content, $links );
40
41 // found?
42 if ( ! empty ( $links[0] ) ) {
43 foreach ( $links[0] as $id => $link ) {
44 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) {
45
46 // do not modify this link
47 if ( $result[1] === 'norl' )
48 continue;
49
50 // swipebox video fix
51 if ( Responsive_Lightbox()->options['settings']['script'] === 'swipebox' && strpos( $links[2][$id], 'vimeo.com') !== false )
52 $content = str_replace( $link, str_replace( $links[2][$id], $links[2][$id] . '?width=' . Responsive_Lightbox()->options['configuration']['swipebox']['video_max_width'], $link ), $content );
53
54 // replace data-rel
55 $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-video-' . $id . '"', $link ), $content );
56 } else {
57 // swipebox video fix
58 if ( Responsive_Lightbox()->options['settings']['script'] === 'swipebox' && strpos( $links[2][$id], 'vimeo.com') !== false )
59 $links[2][$id] = $links[2][$id] . '?width=' . Responsive_Lightbox()->options['configuration']['swipebox']['video_max_width'];
60
61 // replace data-rel
62 $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 );
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 if ( Responsive_Lightbox()->options['settings']['image_links'] === true || Responsive_Lightbox()->options['settings']['images_as_gallery'] === true ) {
80
81 // search for image-links
82 preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links );
83
84 // found?
85 if ( ! empty ( $links[0] ) ) {
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 $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] );
97
98 if ( $image_id )
99 $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] ) ) ) );
100 }
101
102 // link contains data-rel attribute
103 if ( preg_match( '/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) {
104
105 // do not modify this link
106 if ( $result[1] === 'norl' )
107 continue;
108
109 // single images gallery
110 if ( Responsive_Lightbox()->options['settings']['images_as_gallery'] === true )
111 $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(?:.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . $rel_hash . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '"', $link ), $content );
112 // single image
113 else {
114 $content = str_replace( $link, preg_replace( '/(?:data-rel)=(?:\'|")(?:.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-' . $id . '"' . ( Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '' ) . ' title="' . $title . '"', $link ), $content );
115 }
116 // link without data-rel
117 } else {
118 $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 );
119 }
120 }
121 }
122
123 }
124
125 return $content;
126 }
127
128 /**
129 * Add lightbox to gallery
130 */
131 public function add_gallery_lightbox_selector( $link, $id, $size, $permalink, $icon, $text ) {
132
133 if ( Responsive_Lightbox()->options['settings']['galleries'] === true && wp_attachment_is_image( $id ) ) {
134
135 // gallery link target image
136 $src = array();
137
138 // gallery image title
139 $title = '';
140
141 if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) {
142 $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $link, $id );
143 $title = wp_strip_all_tags( trim( $this->get_attachment_title( $id, $title_arg ) ) );
144 }
145
146 if ( $title )
147 $link = str_replace( '<a href', '<a title="'. $title .'" href', $link );
148
149 $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 ) );
150
151 // gallery image size
152 if ( Responsive_Lightbox()->options['settings']['gallery_image_size'] != 'full' ) {
153 $src = wp_get_attachment_image_src( $id, Responsive_Lightbox()->options['settings']['gallery_image_size'] );
154
155 $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) );
156 } else {
157 $src = wp_get_attachment_image_src( $id, 'full' );
158
159 $link = ( preg_match( '/<a.*? href=("|\').*?("|\')>/', $link ) === 1 ? preg_replace( '/(<a.*? href=(?:"|\')).*?((?:"|\').*?>)/', '$1' . $src[0] . '$2', $link ) : preg_replace( '/(<a.*?)>/', '$1 href="' . $src[0] . '">', $link ) );
160 }
161
162 return apply_filters( 'rl_lightbox_attachment_link', $link, $id, $size, $permalink, $icon, $text, $src );
163
164 }
165
166 return $link;
167 }
168
169 /**
170 * Add lightbox to Jetpack tiled gallery
171 *
172 * @param mixed $content
173 * @param array $attr
174 * @return mixed
175 */
176 public function add_custom_gallery_lightbox_selector( $content, $attr ) {
177
178 if ( Responsive_Lightbox()->options['settings']['force_custom_gallery'] === true ) {
179
180 preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links );
181
182 if ( isset( $links[0] ) ) {
183
184 foreach ( $links[0] as $id => $link ) {
185
186 // gallery image title
187 $title = '';
188
189 if ( ( $title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title'] ) !== 'default' ) {
190
191 $image_id = (int) $this->get_attachment_id_by_url( $links[2][$id] . '.' . $links[3][$id] );
192
193 if ( $image_id ) {
194 $title_arg = apply_filters( 'rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id] );
195 $title = wp_strip_all_tags( trim( $this->get_attachment_title( $image_id, $title_arg ) ) );
196 }
197 }
198
199 if ( preg_match( '/<a.*?(?:rel|data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result ) === 1 ) {
200 $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 );
201 } else {
202 $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 );
203 }
204 }
205 }
206
207 }
208
209 return $content;
210 }
211
212 /**
213 * Get attachment title function
214 *
215 * @param int $id
216 * @param string $title_arg
217 * @return string
218 */
219 public function get_attachment_title( $id, $title_arg ) {
220
221 if ( empty( $title_arg ) || empty( $id ) ) {
222 return false;
223 }
224
225 switch( $title_arg ) {
226 case 'title':
227 $title = get_the_title( $id );
228 break;
229 case 'caption':
230 $title = get_post_field( 'post_excerpt', $id ) ;
231 break;
232 case 'alt':
233 $title = get_post_meta( $id, '_wp_attachment_image_alt', true );
234 break;
235 case 'description':
236 $title = get_post_field( 'post_content', $id ) ;
237 break;
238 default:
239 $title = '';
240 break;
241 }
242
243 return apply_filters( 'rl_get_attachment_title', $title, $id, $title_arg );
244
245 }
246
247 /**
248 * Get attachment id by url function, adjusted to work cropped images
249 *
250 * @param string $url
251 * @return int
252 */
253 public function get_attachment_id_by_url( $url ) {
254 $post_id = attachment_url_to_postid( $url );
255
256 if ( ! $post_id ) {
257 $dir = wp_upload_dir();
258 $path = $url;
259
260 if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) {
261 $path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
262 }
263
264 if ( preg_match( '/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches ) ){
265 $url = $dir['baseurl'] . '/' . $matches[1] . $matches[3];
266 $post_id = attachment_url_to_postid( $url );
267 }
268 }
269
270 return (int) $post_id;
271 }
272
273 /**
274 * Helper: generate password without wp_rand() and DB call it uses
275 *
276 * @param int $length
277 * @return string
278 */
279 private function generate_password( $length = 64 ) {
280 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
281 $password = '';
282
283 for( $i = 0; $i < $length; $i++ ) {
284 $password .= substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
285 }
286
287 return $password;
288 }
289
290 /**
291 * Helper: gallery number function
292 *
293 * @param mixed $content
294 * @param array $attr
295 * @return mixed
296 */
297 public function gallery_attributes( $content, $attr ) {
298
299 ++ $this->gallery_no;
300
301 return $content;
302 }
303
304 }