Completed
Push — add/carousel-for-single-image ( 56d26e )
by
unknown
22:10 queued 09:46
created

Jetpack_Carousel::jetpack_configuration_load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
Plugin Name: Jetpack Carousel
5
Plugin URL: https://wordpress.com/
6
Description: Transform your standard image galleries into an immersive full-screen experience.
7
Version: 0.1
8
Author: Automattic
9
10
Released under the GPL v.2 license.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
*/
17
class Jetpack_Carousel {
18
19
	public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
20
21
	public $first_run = true;
22
23
	public $in_gallery = false;
24
25
	public $in_jetpack = true;
26
27
	function __construct() {
28
		add_action( 'init', array( $this, 'init' ) );
29
	}
30
31
	function init() {
32
		if ( $this->maybe_disable_jp_carousel() )
33
			return;
34
35
		$this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
36
37
		if ( is_admin() ) {
38
			// Register the Carousel-related related settings
39
			add_action( 'admin_init', array( $this, 'register_settings' ), 5 );
40
			if ( ! $this->in_jetpack ) {
41
				if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) )
42
					return; // Carousel disabled, abort early, but still register setting so user can switch it back on
43
			}
44
			// If in admin, register the ajax endpoints.
45
			add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
46
			add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
47
			add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
48
			add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
49
			// TODO: add setting checkbox for "single_image_linked_to_self", check for it here
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
50
			add_filter( 'image_send_to_editor', array( $this, 'handle_single_image' ), 10, 8 );
51
		} else {
52
			if ( ! $this->in_jetpack ) {
53
				if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) )
54
					return; // Carousel disabled, abort early
55
			}
56
			// If on front-end, do the Carousel thang.
57
			/**
58
			 * Filter the array of default prebuilt widths used in Carousel.
59
			 *
60
			 * @module carousel
61
			 *
62
			 * @since 1.6.0
63
			 *
64
			 * @param array $this->prebuilt_widths Array of default widths.
65
			 */
66
			$this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
67
			add_filter( 'post_gallery', array( $this, 'enqueue_assets' ), 1000, 2 ); // load later than other callbacks hooked it
68
			add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
69
			add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
70
			add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
71
		}
72
73
		if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
74
			Jetpack::enable_module_configurable( dirname( dirname( __FILE__ ) ) . '/carousel.php' );
75
			Jetpack::module_configuration_load( dirname( dirname( __FILE__ ) ) . '/carousel.php', array( $this, 'jetpack_configuration_load' ) );
76
		}
77
	}
78
79
	function handle_single_image( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
0 ignored issues
show
Unused Code introduced by
The parameter $alt is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
		$url_without_extension = substr( $url, 0, strrpos( $url, '.' ) );
81
		if( $url && strpos( $html, "src=\"$url_without_extension" ) !== false ) {
82
			// image links to itself when img src contains same location as $url (a href value)
83
			return "[gallery columns=\"1\" size=\"{$size}\" ids=\"{$id}\"]";
84
		} else {
85
			return $html;
86
		}
87
	}
88
89
	function maybe_disable_jp_carousel() {
90
		/**
91
		 * Allow third-party plugins or themes to disable Carousel.
92
		 *
93
		 * @module carousel
94
		 *
95
		 * @since 1.6.0
96
		 *
97
		 * @param bool false Should Carousel be disabled? Default to false.
98
		 */
99
		return apply_filters( 'jp_carousel_maybe_disable', false );
100
	}
101
102
	function jetpack_configuration_load() {
103
		wp_safe_redirect( admin_url( 'options-media.php#carousel_background_color' ) );
104
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method jetpack_configuration_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
105
	}
106
107
	function asset_version( $version ) {
108
		/**
109
		 * Filter the version string used when enqueuing Carousel assets.
110
		 *
111
		 * @module carousel
112
		 *
113
		 * @since 1.6.0
114
		 *
115
		 * @param string $version Asset version.
116
		 */
117
		return apply_filters( 'jp_carousel_asset_version', $version );
118
	}
119
120
	function display_bail_message( $output= '' ) {
121
		// Displays a message on top of gallery if carousel has bailed
122
		$message = '<div class="jp-carousel-msg"><p>';
123
		$message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
124
		$message .= '</p></div>';
125
		// put before gallery output
126
		$output = $message . $output;
127
		return $output;
128
	}
129
130
	function enqueue_assets( $output ) {
131
		if (
132
			! empty( $output ) &&
133
			/**
134
			 * Allow third-party plugins or themes to force-enable Carousel.
135
			 *
136
			 * @module carousel
137
			 *
138
			 * @since 1.9.0
139
			 *
140
			 * @param bool false Should we force enable Carousel? Default to false.
141
			 */
142
			! apply_filters( 'jp_carousel_force_enable', false )
143
		) {
144
			// Bail because someone is overriding the [gallery] shortcode.
145
			remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
146
			remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
147
			// Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
148
			if (
149
				is_super_admin() &&
150
				! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
151
			) {
152
				add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
153
			}
154
			return $output;
155
		}
156
157
		/**
158
		 * Fires when thumbnails are shown in Carousel.
159
		 *
160
		 * @module carousel
161
		 *
162
		 * @since 1.6.0
163
		 **/
164
		do_action( 'jp_carousel_thumbnails_shown' );
165
166
		if ( $this->first_run ) {
167
			wp_enqueue_script( 'jetpack-carousel', plugins_url( 'jetpack-carousel.js', __FILE__ ), array( 'jquery.spin' ), $this->asset_version( '20160325' ), true );
168
169
			// 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)
170
			// Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
171
			$is_logged_in = is_user_logged_in();
172
			$current_user = wp_get_current_user();
173
			$comment_registration = intval( get_option( 'comment_registration' ) );
174
			$require_name_email   = intval( get_option( 'require_name_email' ) );
175
			$localize_strings = array(
176
				'widths'               => $this->prebuilt_widths,
177
				'is_logged_in'         => $is_logged_in,
178
				'lang'                 => strtolower( substr( get_locale(), 0, 2 ) ),
179
				'ajaxurl'              => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
180
				'nonce'                => wp_create_nonce( 'carousel_nonce' ),
181
				'display_exif'         => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
182
				'display_geo'          => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_geo', true ) ),
183
				'background_color'     => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
184
				'comment'              => __( 'Comment', 'jetpack' ),
185
				'post_comment'         => __( 'Post Comment', 'jetpack' ),
186
				'write_comment'        => __( 'Write a Comment...', 'jetpack' ),
187
				'loading_comments'     => __( 'Loading Comments...', 'jetpack' ),
188
				'download_original'    => sprintf( __( 'View full size <span class="photo-size">%1$s<span class="photo-size-times">&times;</span>%2$s</span>', 'jetpack' ), '{0}', '{1}' ),
189
				'no_comment_text'      => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
190
				'no_comment_email'     => __( 'Please provide an email address to comment.', 'jetpack' ),
191
				'no_comment_author'    => __( 'Please provide your name to comment.', 'jetpack' ),
192
				'comment_post_error'   => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
193
				'comment_approved'     => __( 'Your comment was approved.', 'jetpack' ),
194
				'comment_unapproved'   => __( 'Your comment is in moderation.', 'jetpack' ),
195
				'camera'               => __( 'Camera', 'jetpack' ),
196
				'aperture'             => __( 'Aperture', 'jetpack' ),
197
				'shutter_speed'        => __( 'Shutter Speed', 'jetpack' ),
198
				'focal_length'         => __( 'Focal Length', 'jetpack' ),
199
				'comment_registration' => $comment_registration,
200
				'require_name_email'   => $require_name_email,
201
				/** This action is documented in core/src/wp-includes/link-template.php */
202
				'login_url'            => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
203
			);
204
205
			if ( ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] ) ) {
206
				// We're not using Comments after all, so fallback to standard local comments.
207
208
				if ( $is_logged_in ) {
209
					$localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . sprintf( __( 'Commenting as %s', 'jetpack' ), $current_user->data->display_name ) . '</p>';
210
				} else {
211
					if ( $comment_registration ) {
212
						$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.', 'jetpack' ) . '</p>';
213
					} else {
214
						$required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
215
						$localize_strings['local_comments_commenting_as'] = ''
216
							. '<fieldset><label for="email">' . sprintf( $required, __( 'Email', 'jetpack' ) ) . '</label> '
217
							. '<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>'
218
							. '<fieldset><label for="author">' . sprintf( $required, __( 'Name', 'jetpack' ) ) . '</label> '
219
							. '<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>'
220
							. '<fieldset><label for="url">' . __( 'Website', 'jetpack' ) . '</label> '
221
							. '<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>';
222
						}
223
				}
224
			}
225
226
			/**
227
			 * Handle WP stats for images in full-screen.
228
			 * Build string with tracking info.
229
			 */
230
231
			/**
232
			 * Filter if Jetpack should enable stats collection on carousel views
233
			 *
234
			 * @module carousel
235
			 *
236
			 * @since 4.3.2
237
			 *
238
			 * @param bool Enable Jetpack Carousel stat collection. Default false.
239
			 */
240
			if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules() ) && ! Jetpack::is_development_mode() ) {
241
				$localize_strings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
242
243
				// Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
244 View Code Duplication
				if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
245
					$stats_options = stats_get_options();
246
					$track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
247
248
					if ( ! $track_loggedin_users ) {
249
						$localize_strings['stats'] = '';
250
					}
251
				}
252
			}
253
254
			/**
255
			 * Filter the strings passed to the Carousel's js file.
256
			 *
257
			 * @module carousel
258
			 *
259
			 * @since 1.6.0
260
			 *
261
			 * @param array $localize_strings Array of strings passed to the Jetpack js file.
262
			 */
263
			$localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
264
			wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
265
			if( is_rtl() ) {
266
				wp_enqueue_style( 'jetpack-carousel', plugins_url( '/rtl/jetpack-carousel-rtl.css', __FILE__ ), array(), $this->asset_version( '20120629' ) );
267
			} else {
268
				wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( '20120629' ) );
269
			}
270
271
			wp_register_style( 'jetpack-carousel-ie8fix', plugins_url( 'jetpack-carousel-ie8fix.css', __FILE__ ), array(), $this->asset_version( '20121024' ) );
272
			$GLOBALS['wp_styles']->add_data( 'jetpack-carousel-ie8fix', 'conditional', 'lte IE 8' );
273
			wp_enqueue_style( 'jetpack-carousel-ie8fix' );
274
275
			/**
276
			 * Fires after carousel assets are enqueued for the first time.
277
			 * Allows for adding additional assets to the carousel page.
278
			 *
279
			 * @module carousel
280
			 *
281
			 * @since 1.6.0
282
			 *
283
			 * @param bool $first_run First load if Carousel on the page.
284
			 * @param array $localized_strings Array of strings passed to the Jetpack js file.
285
			 */
286
			do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
287
288
			$this->first_run = false;
289
		}
290
291
		return $output;
292
	}
293
294
	function set_in_gallery( $output ) {
295
		$this->in_gallery = true;
296
		return $output;
297
	}
298
299
	function add_data_to_images( $attr, $attachment = null ) {
300
301
		// not in a gallery?
302
		if ( ! $this->in_gallery ) {
303
			return $attr;
304
		}
305
306
		$attachment_id   = intval( $attachment->ID );
307
		$orig_file       = wp_get_attachment_image_src( $attachment_id, 'full' );
308
		$orig_file       = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
309
		$meta            = wp_get_attachment_metadata( $attachment_id );
310
		$size            = isset( $meta['width'] ) ? intval( $meta['width'] ) . ',' . intval( $meta['height'] ) : '';
311
		$img_meta        = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
312
		$comments_opened = intval( comments_open( $attachment_id ) );
313
314
		 /*
315
		 * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
316
		 * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
317
		 * which when used to generate a filename will likely result in a 404 on the image.
318
		 * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
319
		 * re-register. So using returned file URL instead, which we can define the sizes from through filename
320
		 * parsing in the JS, as this is a failsafe file reference.
321
		 *
322
		 * EG with Twenty Eleven activated:
323
		 * 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) }
324
		 *
325
		 * EG with Twenty Ten activated:
326
		 * 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) }
327
		 */
328
329
		$medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
330
		$medium_file      = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
331
332
		$large_file_info  = wp_get_attachment_image_src( $attachment_id, 'large' );
333
		$large_file       = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
334
335
		$attachment       = get_post( $attachment_id );
336
		$attachment_title = wptexturize( $attachment->post_title );
337
		$attachment_desc  = wpautop( wptexturize( $attachment->post_content ) );
338
339
		// Not yet providing geo-data, need to "fuzzify" for privacy
340 View Code Duplication
		if ( ! empty( $img_meta ) ) {
341
			foreach ( $img_meta as $k => $v ) {
342
				if ( 'latitude' == $k || 'longitude' == $k )
343
					unset( $img_meta[$k] );
344
			}
345
		}
346
347
		// See https://github.com/Automattic/jetpack/issues/2765
348
		if ( isset( $img_meta['keywords'] ) ) {
349
			unset( $img_meta['keywords'] );
350
		}
351
352
		$img_meta = json_encode( array_map( 'strval', $img_meta ) );
353
354
		$attr['data-attachment-id']     = $attachment_id;
355
		$attr['data-orig-file']         = esc_attr( $orig_file );
356
		$attr['data-orig-size']         = $size;
357
		$attr['data-comments-opened']   = $comments_opened;
358
		$attr['data-image-meta']        = esc_attr( $img_meta );
359
		$attr['data-image-title']       = esc_attr( $attachment_title );
360
		$attr['data-image-description'] = esc_attr( $attachment_desc );
361
		$attr['data-medium-file']       = esc_attr( $medium_file );
362
		$attr['data-large-file']        = esc_attr( $large_file );
363
364
		return $attr;
365
	}
366
367
	function add_data_to_container( $html ) {
368
		global $post;
369
370
		if ( isset( $post ) ) {
371
			$blog_id = (int) get_current_blog_id();
372
373
			$extra_data = array(
374
				'data-carousel-extra' => array(
375
					'blog_id' => $blog_id,
376
					'permalink' => get_permalink( $post->ID ),
377
					)
378
				);
379
380
			/**
381
			 * Filter the data added to the Gallery container.
382
			 *
383
			 * @module carousel
384
			 *
385
			 * @since 1.6.0
386
			 *
387
			 * @param array $extra_data Array of data about the site and the post.
388
			 */
389
			$extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
390
			foreach ( (array) $extra_data as $data_key => $data_values ) {
391
				$html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . json_encode( $data_values ) . "' ", $html );
392
			}
393
		}
394
395
		return $html;
396
	}
397
398
	function get_attachment_comments() {
399
		if ( ! headers_sent() )
400
			header('Content-type: text/javascript');
401
402
		/**
403
		 * Allows for the checking of privileges of the blog user before comments
404
		 * are packaged as JSON and sent back from the get_attachment_comments
405
		 * AJAX endpoint
406
		 *
407
		 * @module carousel
408
		 *
409
		 * @since 1.6.0
410
		 */
411
		do_action('jp_carousel_check_blog_user_privileges');
412
413
		$attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
414
		$offset        = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
415
416
		if ( ! $attachment_id ) {
417
			echo json_encode( __( 'Missing attachment ID.', 'jetpack' ) );
418
			die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_attachment_comments() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
419
		}
420
421
		if ( $offset < 1 )
422
			$offset = 0;
423
424
		$comments = get_comments( array(
425
			'status'  => 'approve',
426
			'order'   => ( 'asc' == get_option('comment_order') ) ? 'ASC' : 'DESC',
427
			'number'  => 10,
428
			'offset'  => $offset,
429
			'post_id' => $attachment_id,
430
		) );
431
432
		$out      = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 6 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
433
434
		// Can't just send the results, they contain the commenter's email address.
435
		foreach ( $comments as $comment ) {
436
			$avatar = get_avatar( $comment->comment_author_email, 64 );
437
			if( ! $avatar )
438
				$avatar = '';
439
			$out[] = array(
440
				'id'              => $comment->comment_ID,
441
				'parent_id'       => $comment->comment_parent,
442
				'author_markup'   => get_comment_author_link( $comment->comment_ID ),
443
				'gravatar_markup' => $avatar,
444
				'date_gmt'        => $comment->comment_date_gmt,
445
				'content'         => wpautop($comment->comment_content),
446
			);
447
		}
448
449
		die( json_encode( $out ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_attachment_comments() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
450
	}
451
452
	function post_attachment_comment() {
453
		if ( ! headers_sent() )
454
			header('Content-type: text/javascript');
455
456
		if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce($_POST['nonce'], 'carousel_nonce') )
457
			die( json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
458
459
		$_blog_id = (int) $_POST['blog_id'];
460
		$_post_id = (int) $_POST['id'];
461
		$comment = $_POST['comment'];
462
463
		if ( empty( $_blog_id ) )
464
			die( json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
465
466
		if ( empty( $_post_id ) )
467
			die( json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
468
469
		if ( empty( $comment ) )
470
			die( json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
471
472
		// Used in context like NewDash
473
		$switched = false;
474
		if ( is_multisite() && $_blog_id != get_current_blog_id() ) {
475
			switch_to_blog( $_blog_id );
476
			$switched = true;
477
		}
478
479
		/** This action is documented in modules/carousel/jetpack-carousel.php */
480
		do_action('jp_carousel_check_blog_user_privileges');
481
482
		if ( ! comments_open( $_post_id ) )
483
			die( json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
484
485
		if ( is_user_logged_in() ) {
486
			$user         = wp_get_current_user();
487
			$user_id      = $user->ID;
488
			$display_name = $user->display_name;
489
			$email        = $user->user_email;
490
			$url          = $user->user_url;
491
492
			if ( empty( $user_id ) )
493
				die( json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
494
		} else {
495
			$user_id      = 0;
496
			$display_name = $_POST['author'];
497
			$email        = $_POST['email'];
498
			$url          = $_POST['url'];
499
500
			if ( get_option( 'require_name_email' ) ) {
501
				if ( empty( $display_name ) )
502
					die( json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
503
504
				if ( empty( $email ) )
505
					die( json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
506
507
				if ( ! is_email( $email ) )
508
					die( json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
509
			}
510
		}
511
512
		$comment_data =  array(
513
			'comment_content'      => $comment,
514
			'comment_post_ID'      => $_post_id,
515
			'comment_author'       => $display_name,
516
			'comment_author_email' => $email,
517
			'comment_author_url'   => $url,
518
			'comment_approved'     => 0,
519
			'comment_type'         => '',
520
		);
521
522
		if ( ! empty( $user_id ) )
523
			$comment_data['user_id'] = $user_id;
524
525
		// Note: wp_new_comment() sanitizes and validates the values (too).
526
		$comment_id = wp_new_comment( $comment_data );
527
528
		/**
529
		 * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
530
		 *
531
		 * @module carousel
532
		 *
533
		 * @since 1.6.0
534
		 */
535
		do_action( 'jp_carousel_post_attachment_comment' );
536
		$comment_status = wp_get_comment_status( $comment_id );
537
538
		if ( true == $switched )
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
539
			restore_current_blog();
540
541
		die( json_encode( array( 'comment_id' => $comment_id, 'comment_status' => $comment_status ) ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method post_attachment_comment() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
542
	}
543
544
	function register_settings() {
545
		add_settings_section('carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media');
546
547
		if ( ! $this->in_jetpack ) {
548
			add_settings_field('carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
549
			register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
550
		}
551
552
		add_settings_field('carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
553
		register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
554
555
		add_settings_field('carousel_display_exif', __( 'Metadata', 'jetpack'), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
556
		register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
557
558
		// No geo setting yet, need to "fuzzify" data first, for privacy
559
		// add_settings_field('carousel_display_geo', __( 'Geolocation', 'jetpack' ), array( $this, 'carousel_display_geo_callback' ), 'media', 'carousel_section' );
560
		// register_setting( 'media', 'carousel_display_geo', array( $this, 'carousel_display_geo_sanitize' ) );
561
	}
562
563
	// Fulfill the settings section callback requirement by returning nothing
564
	function carousel_section_callback() {
565
		return;
566
	}
567
568
	function test_1or0_option( $value, $default_to_1 = true ) {
569
		if ( true == $default_to_1 ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
570
			// Binary false (===) of $value means it has not yet been set, in which case we do want to default sites to 1
571
			if ( false === $value )
572
				$value = 1;
573
		}
574
		return ( 1 == $value ) ? 1 : 0;
575
	}
576
577
	function sanitize_1or0_option( $value ) {
578
		return ( 1 == $value ) ? 1 : 0;
579
	}
580
581
	function settings_checkbox($name, $label_text, $extra_text = '', $default_to_checked = true) {
582
		if ( empty( $name ) )
583
			return;
584
		$option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
585
		echo '<fieldset>';
586
		echo '<input type="checkbox" name="'.esc_attr($name).'" id="'.esc_attr($name).'" value="1" ';
587
		checked( '1', $option );
588
		echo '/> <label for="'.esc_attr($name).'">'.$label_text.'</label>';
589
		if ( ! empty( $extra_text ) )
590
			echo '<p class="description">'.$extra_text.'</p>';
591
		echo '</fieldset>';
592
	}
593
594
	function settings_select($name, $values, $extra_text = '') {
595
		if ( empty( $name ) || ! is_array( $values ) || empty( $values ) )
596
			return;
597
		$option = get_option( $name );
598
		echo '<fieldset>';
599
		echo '<select name="'.esc_attr($name).'" id="'.esc_attr($name).'">';
600
		foreach( $values as $key => $value ) {
601
			echo '<option value="'.esc_attr($key).'" ';
602
			selected( $key, $option );
603
			echo '>'.esc_html($value).'</option>';
604
		}
605
		echo '</select>';
606
		if ( ! empty( $extra_text ) )
607
			echo '<p class="description">'.$extra_text.'</p>';
608
		echo '</fieldset>';
609
	}
610
611
	function carousel_display_exif_callback() {
612
		$this->settings_checkbox( 'carousel_display_exif', __( 'Show photo metadata (<a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) );
613
	}
614
615
	function carousel_display_exif_sanitize( $value ) {
616
		return $this->sanitize_1or0_option( $value );
617
	}
618
619
	function carousel_display_geo_callback() {
620
		$this->settings_checkbox( 'carousel_display_geo', __( 'Show map of photo location in carousel, when available.', 'jetpack' ) );
621
	}
622
623
	function carousel_display_geo_sanitize( $value ) {
624
		return $this->sanitize_1or0_option( $value );
625
	}
626
627
	function carousel_background_color_callback() {
628
		$this->settings_select( 'carousel_background_color', array( 'black' => __( 'Black', 'jetpack' ), 'white' => __( 'White', 'jetpack', 'jetpack' ) ) );
629
	}
630
631
	function carousel_background_color_sanitize( $value ) {
632
		return ( 'white' == $value ) ? 'white' : 'black';
633
	}
634
635
	function carousel_enable_it_callback() {
636
		$this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
637
	}
638
639
	function carousel_enable_it_sanitize( $value ) {
640
		return $this->sanitize_1or0_option( $value );
641
	}
642
}
643
644
new Jetpack_Carousel;
645