PluginProbe
Catch Gallery / 1.2
Catch Gallery v1.2
trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 All 27 releases
catch-gallery / inc / jetpack-carousel.php

jetpack-carousel.php in Catch Gallery 1.2, at inc/jetpack-carousel.php

403 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! class_exists( 'Catch_Gallery_Carousel' ) ) :
3 class Catch_Gallery_Carousel {
4
5 var $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
6
7 var $first_run = true;
8
9 var $in_jetpack = true;
10
11 function __construct() {
12 add_action( 'init', array( $this, 'init' ) );
13
14 }
15
16 function init() {
17 if ( $this->maybe_disable_jp_carousel() )
18 return;
19
20 $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
21
22 $options = catch_gallery_get_options();
23
24 if ( is_admin() ) {
25 // Register the Carousel-related related settings
26
27 if ( ! $this->in_jetpack ) {
28 $carousel_disabled = isset( $options['carousel_enable'] ) ? $options['carousel_enable'] : false;
29 if ( ! $carousel_disabled ) {
30 return; // Carousel disabled, abort early, but still register setting so user can switch it back on
31 }
32 }
33 // If in admin, register the ajax endpoints.
34 add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
35 add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
36 add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
37 add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
38 } else {
39 $carousel_disabled = isset( $options['carousel_enable'] ) ? $options['carousel_enable'] : false;
40 if ( ! $carousel_disabled ) {
41 return; // Carousel disabled, abort early, but still register setting so user can switch it back on
42 }
43 // If on front-end, do the Carousel thang.
44 $this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
45 add_filter( 'post_gallery', array( $this, 'enqueue_assets' ), 1000, 2 ); // load later than other callbacks hooked it
46 add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
47 add_filter( 'wp_get_attachment_link', array( $this, 'add_data_to_images' ), 10, 2 );
48 }
49
50 if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
51 Jetpack::enable_module_configurable( dirname( dirname( __FILE__ ) ) . '/carousel.php' );
52 Jetpack::module_configuration_load( dirname( dirname( __FILE__ ) ) . '/carousel.php', array( $this, 'jetpack_configuration_load' ) );
53 }
54 }
55
56 function maybe_disable_jp_carousel() {
57 return apply_filters( 'jp_carousel_maybe_disable', false );
58 }
59
60 function jetpack_configuration_load() {
61 wp_safe_redirect( admin_url( 'options-media.php#catch_gallery_options[carousel_background_color]' ) );
62 exit;
63 }
64
65 function asset_version( $version ) {
66 return apply_filters( 'jp_carousel_asset_version', $version );
67 }
68
69 function enqueue_assets( $output ) {
70 if ( ! empty( $output ) && ! apply_filters( 'jp_carousel_force_enable', false ) ) {
71 // Bail because someone is overriding the [gallery] shortcode.
72 remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
73 remove_filter( 'wp_get_attachment_link', array( $this, 'add_data_to_images' ) );
74 return $output;
75 }
76
77 do_action( 'jp_carousel_thumbnails_shown' );
78
79 if ( $this->first_run ) {
80 wp_register_script( 'spin', plugin_dir_url( __FILE__ ) . '../js/spin.js', false, '1.3' );
81 wp_register_script( 'jquery-spin', plugin_dir_url( __FILE__ ) . '../js/jquery.spin.js', array( 'jquery', 'spin' ) );
82
83 wp_enqueue_script( 'jetpack-carousel', plugin_dir_url( __FILE__ ) . '../js/jetpack-carousel.js', array( 'jquery-spin' ), $this->asset_version( CATCH_GALLERY_VERSION ), true );
84
85 // Note: using home_url() instead of admin_url() for ajaxurl to be sure to get same domain on wpcom when using mapped domains (also works on self-hosted)
86 // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
87 $is_logged_in = is_user_logged_in();
88 $current_user = wp_get_current_user();
89 $comment_registration = intval( get_option( 'comment_registration' ) );
90 $require_name_email = intval( get_option( 'require_name_email' ) );
91
92 $options = catch_gallery_get_options();
93 $localize_strings = array(
94 'widths' => $this->prebuilt_widths,
95 'is_logged_in' => $is_logged_in,
96 'lang' => strtolower( substr( get_locale(), 0, 2 ) ),
97 'ajaxurl' => admin_url( 'admin-ajax.php', is_ssl() ? 'https' : 'http' ),
98 'nonce' => wp_create_nonce( 'carousel_nonce' ),
99 'display_exif' => isset( $options['carousel_display_exif'] ) ? $options['carousel_display_exif'] : false,
100 'display_geo' => isset( $options['carousel_display_geo'] ) ? $options['carousel_display_geo'] : false,
101 'display_comments' => isset( $options['comments_display'] ) ? $options['comments_display']: false,
102 'fullsize_display' => isset( $options['fullsize_display'] ) ? $options['fullsize_display'] : false,
103
104 'background_color' => isset( $options['carousel_background_color'] ) ? $options['carousel_background_color'] : 'black',
105 'comment' => esc_html__( 'Comment', 'catch-gallery' ),
106 'post_comment' => esc_html__( 'Post Comment', 'catch-gallery' ),
107 'loading_comments' => esc_html__( 'Loading Comments...', 'catch-gallery' ),
108
109 'download_original' => sprintf( __( 'View full size <span class="photo-size">%1$s<span class="photo-size-times">&times;</span>%2$s</span>', 'catch-gallery' ), '{0}', '{1}' ),
110
111 'no_comment_text' => esc_html__( 'Please be sure to submit some text with your comment.', 'catch-gallery' ),
112 'no_comment_email' => esc_html__( 'Please provide an email address to comment.', 'catch-gallery' ),
113 'no_comment_author' => esc_html__( 'Please provide your name to comment.', 'catch-gallery' ),
114 'comment_post_error' => esc_html__( 'Sorry, but there was an error posting your comment. Please try again later.', 'catch-gallery' ),
115 'comment_approved' => esc_html__( 'Your comment was approved.', 'catch-gallery' ),
116 'comment_unapproved' => esc_html__( 'Your comment is in moderation.', 'catch-gallery' ),
117 'camera' => esc_html__( 'Camera', 'catch-gallery' ),
118 'aperture' => esc_html__( 'Aperture', 'catch-gallery' ),
119 'shutter_speed' => esc_html__( 'Shutter Speed', 'catch-gallery' ),
120 'focal_length' => esc_html__( 'Focal Length', 'catch-gallery' ),
121 'comment_registration' => $comment_registration,
122 'require_name_email' => $require_name_email,
123 'login_url' => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
124 );
125
126 if ( ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] ) ) {
127 // We're not using Jetpack comments after all, so fallback to standard local comments.
128 if ( isset( $localize_strings['display_comments'] )){
129 if ( $localize_strings['display_comments']) {
130
131
132 if ( $is_logged_in ) {
133 $localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . sprintf( __( 'Commenting as %s', 'catch-gallery' ), $current_user->data->display_name ) . '</p>';
134 } else {
135 if ( $comment_registration ) {
136 $localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'catch-gallery' ) . '</p>';
137 } else {
138 $required = ( $require_name_email ) ? __( '%s (Required)', 'catch-gallery' ) : '%s';
139 $localize_strings['local_comments_commenting_as'] = ''
140 . '<fieldset><label for="email">' . sprintf( $required, esc_html__( 'Email', 'catch-gallery' ) ) . '</label> '
141 . '<input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" /></fieldset>'
142 . '<fieldset><label for="author">' . sprintf( $required, esc_html__( 'Name', 'catch-gallery' ) ) . '</label> '
143 . '<input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" /></fieldset>'
144 . '<fieldset><label for="url">' . esc_html__( 'Website', 'catch-gallery' ) . '</label> '
145 . '<input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" /></fieldset>';
146 }
147 }
148 }else{
149 $localize_strings['loading_comments'] = '';
150 $localize_strings['comment'] = '';
151 }
152 }
153 }
154
155 $localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
156
157 wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
158
159 wp_enqueue_style( 'jetpack-carousel', plugin_dir_url( __FILE__ ) . '../css/jetpack-carousel.css', array(), $this->asset_version( CATCH_GALLERY_VERSION ) );
160 wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
161
162 wp_enqueue_style( 'jetpack-carousel-ie8fix', plugin_dir_url( __FILE__ ) . '../css/jetpack-carousel-ie8fix.css', '', $this->asset_version( CATCH_GALLERY_VERSION ) );
163 wp_style_add_data( 'jetpack-carousel-ie8fix', 'conditional', 'lt IE 9' );
164
165 do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
166
167 $this->first_run = false;
168 }
169
170 return $output;
171 }
172
173 function add_data_to_images( $html, $attachment_id ) {
174 if ( $this->first_run ) // not in a gallery
175 return $html;
176
177 $attachment_id = intval( $attachment_id );
178 $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' );
179 $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
180 $meta = wp_get_attachment_metadata( $attachment_id );
181 $size = isset( $meta['width'] ) ? intval( $meta['width'] ) . ',' . intval( $meta['height'] ) : '';
182 $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
183 $comments_opened = intval( comments_open( $attachment_id ) );
184
185 /*
186 * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
187 * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
188 * which when used to generate a filename will likely result in a 404 on the image.
189 * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
190 * re-register. So using returned file URL instead, which we can define the sizes from through filename
191 * parsing in the JS, as this is a failsafe file reference.
192 *
193 * EG with Twenty Eleven activated:
194 * array(4) { [0]=> string(82) "http://vanillawpinstall.blah/wp-content/uploads/2012/06/IMG_3534-1024x764.jpg" [1]=> int(584) [2]=> int(435) [3]=> bool(true) }
195 *
196 * EG with Twenty Ten activated:
197 * array(4) { [0]=> string(82) "http://vanillawpinstall.blah/wp-content/uploads/2012/06/IMG_3534-1024x764.jpg" [1]=> int(640) [2]=> int(477) [3]=> bool(true) }
198 */
199
200 $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
201 $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
202
203 $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
204 $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
205
206 $attachment = get_post( $attachment_id );
207 $attachment_title = wptexturize( $attachment->post_title );
208 $attachment_desc = wpautop( wptexturize( $attachment->post_content ) );
209
210 // Not yet providing geo-data, need to "fuzzify" for privacy
211 if ( ! empty( $img_meta ) ) {
212 foreach ( $img_meta as $k => $v ) {
213 if ( 'latitude' == $k || 'longitude' == $k )
214 unset( $img_meta[$k] );
215 }
216 }
217
218 // See https://github.com/Automattic/jetpack/issues/2765
219 if ( isset( $img_meta['keywords'] ) ) {
220 unset( $img_meta['keywords'] );
221 }
222
223 $img_meta = json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
224
225 $html = str_replace(
226 '<img ',
227 sprintf(
228 '<img data-attachment-id="%1$d" data-orig-file="%2$s" data-orig-size="%3$s" data-comments-opened="%4$s" data-image-meta="%5$s" data-image-title="%6$s" data-image-description="%7$s" data-medium-file="%8$s" data-large-file="%9$s" ',
229 $attachment_id,
230 esc_attr( $orig_file ),
231 $size,
232 $comments_opened,
233 esc_attr( $img_meta ),
234 esc_attr( $attachment_title ),
235 esc_attr( $attachment_desc ),
236 esc_attr( $medium_file ),
237 esc_attr( $large_file )
238 ),
239 $html
240 );
241
242 $html = apply_filters( 'jp_carousel_add_data_to_images', $html, $attachment_id );
243
244 return $html;
245 }
246
247 function add_data_to_container( $html ) {
248 global $post;
249
250 if ( isset( $post ) ) {
251 $blog_id = (int) get_current_blog_id();
252
253 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
254 $likes_blog_id = $blog_id;
255 } else {
256 //$likes_blog_id = Jetpack_Options::get_option( 'id' );
257 }
258
259 $extra_data = array(
260 'data-carousel-extra' => array(
261 'blog_id' => $blog_id,
262 'permalink' => get_permalink( $post->ID ),
263 //'likes_blog_id' => $likes_blog_id
264 )
265 );
266
267 $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
268 foreach ( (array) $extra_data as $data_key => $data_values ) {
269 $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . json_encode( $data_values ) . "' ", $html );
270 }
271 }
272
273 return $html;
274 }
275
276 function get_attachment_comments() {
277 if ( ! headers_sent() )
278 header( 'Content-type: text/javascript' );
279
280 do_action( 'jp_carousel_check_blog_user_privileges' );
281
282 $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
283 $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
284
285 if ( ! $attachment_id ) {
286 echo json_encode( esc_html__( 'Missing attachment ID.', 'catch-gallery' ) );
287 die();
288 }
289
290 if ( $offset < 1 )
291 $offset = 0;
292
293 $comments = get_comments( array(
294 'status' => 'approve',
295 'order' => ( 'asc' == get_option( 'comment_order' ) ) ? 'ASC' : 'DESC',
296 'number' => 10,
297 'offset' => $offset,
298 'post_id' => $attachment_id,
299 ) );
300
301 $out = array();
302
303 // Can't just send the results, they contain the commenter's email address.
304 foreach ( $comments as $comment ) {
305 $out[] = array(
306 'id' => $comment->comment_ID,
307 'parent_id' => $comment->comment_parent,
308 'author_markup' => get_comment_author_link( $comment->comment_ID ),
309 'gravatar_markup' => get_avatar( $comment->comment_author_email, 64 ),
310 'date_gmt' => $comment->comment_date_gmt,
311 'content' => wpautop($comment->comment_content),
312 );
313 }
314
315 die( json_encode( $out ) );
316 }
317
318 function post_attachment_comment() {
319 if ( ! headers_sent() )
320 header( 'Content-type: text/javascript' );
321
322 if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce($_POST['nonce'], 'carousel_nonce' ) )
323 die( json_encode( array( 'error' => esc_html__( 'Nonce verification failed.', 'catch-gallery' ) ) ) );
324
325 $_blog_id = (int) $_POST['blog_id'];
326 $_post_id = (int) $_POST['id'];
327 $comment = $_POST['comment'];
328
329 if ( empty( $_blog_id ) )
330 die( json_encode( array( 'error' => esc_html__( 'Missing target blog ID.', 'catch-gallery' ) ) ) );
331
332 if ( empty( $_post_id ) )
333 die( json_encode( array( 'error' => esc_html__( 'Missing target post ID.', 'catch-gallery' ) ) ) );
334
335 if ( empty( $comment ) )
336 die( json_encode( array( 'error' => esc_html__( 'No comment text was submitted.', 'catch-gallery' ) ) ) );
337
338 // Used in context like NewDash
339 $switched = false;
340 if ( is_multisite() && $_blog_id != get_current_blog_id() ) {
341 switch_to_blog( $_blog_id );
342 $switched = true;
343 }
344
345 do_action( 'jp_carousel_check_blog_user_privileges' );
346
347 if ( ! comments_open( $_post_id ) )
348 die( json_encode( array( 'error' => esc_html__( 'Comments on this post are closed.', 'catch-gallery' ) ) ) );
349
350 if ( is_user_logged_in() ) {
351 $user = wp_get_current_user();
352 $user_id = $user->ID;
353 $display_name = $user->display_name;
354 $email = $user->user_email;
355 $url = $user->user_url;
356
357 if ( empty( $user_id ) )
358 die( json_encode( array( 'error' => esc_html__( 'Sorry, but we could not authenticate your request.', 'catch-gallery' ) ) ) );
359 } else {
360 $user_id = 0;
361 $display_name = $_POST['author'];
362 $email = $_POST['email'];
363 $url = $_POST['url'];
364
365 if ( get_option( 'require_name_email' ) ) {
366 if ( empty( $display_name ) )
367 die( json_encode( array( 'error' => esc_html__( 'Please provide your name.', 'catch-gallery' ) ) ) );
368
369 if ( empty( $email ) )
370 die( json_encode( array( 'error' => esc_html__( 'Please provide an email address.', 'catch-gallery' ) ) ) );
371
372 if ( ! is_email( $email ) )
373 die( json_encode( array( 'error' => esc_html__( 'Please provide a valid email address.', 'catch-gallery' ) ) ) );
374 }
375 }
376
377 $comment_data = array(
378 'comment_content' => $comment,
379 'comment_post_ID' => $_post_id,
380 'comment_author' => $display_name,
381 'comment_author_email' => $email,
382 'comment_author_url' => $url,
383 'comment_approved' => 0,
384 'comment_type' => '',
385 );
386
387 if ( ! empty( $user_id ) )
388 $comment_data['user_id'] = $user_id;
389
390 // Note: wp_new_comment() sanitizes and validates the values (too).
391 $comment_id = wp_new_comment( $comment_data );
392 do_action( 'jp_carousel_post_attachment_comment' );
393 $comment_status = wp_get_comment_status( $comment_id );
394
395 if ( true == $switched )
396 restore_current_blog();
397
398 die( json_encode( array( 'comment_id' => $comment_id, 'comment_status' => $comment_status ) ) );
399 }
400 }
401 endif;
402 new Catch_Gallery_Carousel;
403