Completed
Push — update/phpcs-carousel ( e90e29...bed528 )
by
unknown
439:26 queued 429:23
created

Jetpack_Carousel::test_1or0_option()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin Name: Jetpack Carousel
4
 * Plugin URL: https://wordpress.com/
5
 * Description: Transform your standard image galleries into an immersive full-screen experience.
6
 * Version: 0.1
7
 * Author: Automattic
8
 * Text Domain: jetpack
9
 *
10
 * @package jetpack
11
 *
12
 * Released under the GPL v.2 license.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 */
19
20
use Automattic\Jetpack\Assets;
21
use Automattic\Jetpack\Status;
22
23
/**
24
 * Initiate Jetpack Carousel Class
25
 */
26
class Jetpack_Carousel {
27
28
	/**
29
	 * Base width of carousel.
30
	 *
31
	 * @var $prebuilt_widths - prebuilt widths of carousel.
32
	 */
33
	public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
34
35
	/**
36
	 * First time the carousel loaded on page? Default is true.
37
	 *
38
	 * @var $first_run - bool, true.
39
	 */
40
	public $first_run = true;
41
42
	/**
43
	 * Don't know what this does yet?
44
	 *
45
	 * @var $in_gallery - bool, false.
46
	 */
47
	public $in_gallery = false;
48
49
	/**
50
	 * Checks if Jetpack is enabled.
51
	 *
52
	 * @var $in_jetpack - bool, true.
53
	 */
54
	public $in_jetpack = true;
55
56
	/**
57
	 * Disables carousel for single images by default.
58
	 *
59
	 * @var $single_image_gallery_enabled - bool, false.
60
	 */
61
	public $single_image_gallery_enabled = false;
62
63
	/**
64
	 * Disables carousel for single images linking to 'Media File' by default.
65
	 *
66
	 * @var $single_image_gallery_enabled_media_file - bool, false.
67
	 */
68
	public $single_image_gallery_enabled_media_file = false;
69
70
	/**
71
	 * Initializes carousel initialization.
72
	 */
73
	public function __construct() {
74
		add_action( 'init', array( $this, 'init' ) );
75
	}
76
77
	/**
78
	 * Initializes carousel class.
79
	 */
80
	public function init() {
81
		if ( $this->maybe_disable_jp_carousel() ) {
82
			return;
83
		}
84
85
		$this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
86
87
		$this->single_image_gallery_enabled            = ! $this->maybe_disable_jp_carousel_single_images();
88
		$this->single_image_gallery_enabled_media_file = $this->maybe_enable_jp_carousel_single_images_media_file();
89
90
		if ( is_admin() ) {
91
			// Register the Carousel-related related settings.
92
			add_action( 'admin_init', array( $this, 'register_settings' ), 5 );
93
			if ( ! $this->in_jetpack ) {
94
				if ( 0 === $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
95
					return; // Carousel disabled, abort early, but still register setting so user can switch it back on.
96
				}
97
			}
98
			// If in admin, register the ajax endpoints.
99
			add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
100
			add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
101
			add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
102
			add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
103
		} else {
104
			if ( ! $this->in_jetpack ) {
105
				if ( 0 === $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
106
					return; // Carousel disabled, abort early.
107
				}
108
			}
109
			// If on front-end, do the Carousel thang.
110
			/**
111
			 * Filter the array of default prebuilt widths used in Carousel.
112
			 *
113
			 * @module carousel
114
			 *
115
			 * @since 1.6.0
116
			 *
117
			 * @param array $this->prebuilt_widths Array of default widths.
118
			 */
119
			$this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
120
			// below: load later than other callbacks hooked it (e.g. 3rd party plugins handling gallery shortcode).
121
			add_filter( 'post_gallery', array( $this, 'check_if_shortcode_processed_and_enqueue_assets' ), 1000, 2 );
122
			add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
123
			add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
124
			add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
125
			add_filter( 'the_content', array( $this, 'check_content_for_blocks' ), 1 );
126
			add_filter( 'jetpack_tiled_galleries_block_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
127
			if ( $this->single_image_gallery_enabled ) {
128
				add_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
129
			}
130
		}
131
132
		if ( $this->in_jetpack ) {
133
			Jetpack::enable_module_configurable( dirname( dirname( __DIR__ ) ) . '/carousel.php' );
134
		}
135
	}
136
137
	/**
138
	 * Allow third-party plugins or themes to disable Carousel.
139
	 *
140
	 * @module carousel
141
	 *
142
	 * @since 1.6.0
143
	 */
144
	public function maybe_disable_jp_carousel() {
145
		// Should Carousel be disabled? Default to false.
146
		return apply_filters( 'jp_carousel_maybe_disable', false );
147
	}
148
149
	/**
150
	 * Allow third-party plugins or themes to disable Carousel for single images.
151
	 *
152
	 * @module carousel
153
	 *
154
	 * @since 4.5.0
155
	 */
156
	public function maybe_disable_jp_carousel_single_images() {
157
		// Should Carousel be disabled for single images? Default to false.
158
		return apply_filters( 'jp_carousel_maybe_disable_single_images', false );
159
	}
160
161
	/**
162
	 * Allow third-party plugins or themes to enable Carousel
163
	 * for single images linking to 'Media File' (full size image).
164
	 *
165
	 * @module carousel
166
	 *
167
	 * @since 4.5.0
168
	 */
169
	public function maybe_enable_jp_carousel_single_images_media_file() {
170
		// Should Carousel be enabled for single images linking to 'Media File'? Default to false.
171
		return apply_filters( 'jp_carousel_load_for_images_linked_to_file', false );
172
	}
173
174
	/**
175
	 * Filter the version string used when enqueuing Carousel assets.
176
	 *
177
	 * @module carousel
178
	 *
179
	 * @since 1.6.0
180
	 *
181
	 * @param string $version Asset version.
182
	 */
183
	public function asset_version( $version ) {
184
		return apply_filters( 'jp_carousel_asset_version', $version );
185
	}
186
187
	/**
188
	 * Displays a message on top of gallery if carousel has bailed.
189
	 *
190
	 * @param string $output Message we're outputting.
191
	 */
192
	public function display_bail_message( $output = '' ) {
193
		$message  = '<div class="jp-carousel-msg"><p>';
194
		$message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
195
		$message .= '</p></div>';
196
		// Put before gallery output.
197
		$output = $message . $output;
198
		return $output;
199
	}
200
201
	/**
202
	 * Checks if gallery shortcode is processed and enqueues assets.
203
	 *
204
	 * @param string $output The gallery output.
205
	 */
206
	public function check_if_shortcode_processed_and_enqueue_assets( $output ) {
207
		if (
208
			class_exists( 'Jetpack_AMP_Support' )
209
			&& Jetpack_AMP_Support::is_amp_request()
210
		) {
211
			return $output;
212
		}
213
214
		if (
215
			! empty( $output ) &&
216
			/**
217
			 * Allow third-party plugins or themes to force-enable Carousel.
218
			 *
219
			 * @module carousel
220
			 *
221
			 * @since 1.9.0
222
			 *
223
			 * @param bool false Should we force enable Carousel? Default to false.
224
			 */
225
			! apply_filters( 'jp_carousel_force_enable', false )
226
		) {
227
			// Bail because someone is overriding the [gallery] shortcode.
228
			remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
229
			remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
230
			remove_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
231
			// Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
232
			if (
233
				is_super_admin() &&
234
				! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
235
			) {
236
				add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
237
			}
238
			return $output;
239
		}
240
241
		/**
242
		 * Fires when thumbnails are shown in Carousel.
243
		 *
244
		 * @module carousel
245
		 *
246
		 * @since 1.6.0
247
		 */
248
		do_action( 'jp_carousel_thumbnails_shown' );
249
250
		$this->enqueue_assets();
251
252
		return $output;
253
	}
254
255
	/**
256
	 * Check if the content of a post uses gallery blocks. To be used by 'the_content' filter.
257
	 *
258
	 * @since 6.8.0
259
	 *
260
	 * @param string $content Post content.
261
	 *
262
	 * @return string $content Post content.
263
	 */
264
	public function check_content_for_blocks( $content ) {
265
		if (
266
			class_exists( 'Jetpack_AMP_Support' )
267
			&& Jetpack_AMP_Support::is_amp_request()
268
		) {
269
			return $content;
270
		}
271
272
		if ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) {
273
			$this->enqueue_assets();
274
			$content = $this->add_data_to_container( $content );
275
		}
276
		return $content;
277
	}
278
279
	/** Enqueue assets */
280
	public function enqueue_assets() {
281
		if ( $this->first_run ) {
282
			wp_enqueue_script(
283
				'jetpack-carousel',
284
				Assets::get_file_url_for_environment(
285
					'_inc/build/carousel/jetpack-carousel.min.js',
286
					'modules/carousel/jetpack-carousel.js'
287
				),
288
				array( 'jquery' ),
289
				$this->asset_version( JETPACK__VERSION ),
290
				true
291
			);
292
293
			// 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)
294
			// Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
295
			$is_logged_in         = is_user_logged_in();
296
			$current_user         = wp_get_current_user();
297
			$comment_registration = (int) get_option( 'comment_registration' );
298
			$require_name_email   = (int) get_option( 'require_name_email' );
299
			$localize_strings     = array(
300
				'widths'                          => $this->prebuilt_widths,
301
				'is_logged_in'                    => $is_logged_in,
302
				'lang'                            => strtolower( substr( get_locale(), 0, 2 ) ),
303
				'ajaxurl'                         => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
304
				'nonce'                           => wp_create_nonce( 'carousel_nonce' ),
305
				'display_exif'                    => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
306
				'display_comments'                => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_comments', true ) ),
307
				'display_geo'                     => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_geo', true ) ),
308
				'single_image_gallery'            => $this->single_image_gallery_enabled,
309
				'single_image_gallery_media_file' => $this->single_image_gallery_enabled_media_file,
310
				'background_color'                => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
311
				'comment'                         => __( 'Comment', 'jetpack' ),
312
				'post_comment'                    => __( 'Post Comment', 'jetpack' ),
313
				'write_comment'                   => __( 'Write a Comment...', 'jetpack' ),
314
				'loading_comments'                => __( 'Loading Comments...', 'jetpack' ),
315
				// translators: placeholder is size of the image.
316
				'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}' ),
317
				'no_comment_text'                 => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
318
				'no_comment_email'                => __( 'Please provide an email address to comment.', 'jetpack' ),
319
				'no_comment_author'               => __( 'Please provide your name to comment.', 'jetpack' ),
320
				'comment_post_error'              => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
321
				'comment_approved'                => __( 'Your comment was approved.', 'jetpack' ),
322
				'comment_unapproved'              => __( 'Your comment is in moderation.', 'jetpack' ),
323
				'camera'                          => __( 'Camera', 'jetpack' ),
324
				'aperture'                        => __( 'Aperture', 'jetpack' ),
325
				'shutter_speed'                   => __( 'Shutter Speed', 'jetpack' ),
326
				'focal_length'                    => __( 'Focal Length', 'jetpack' ),
327
				'copyright'                       => __( 'Copyright', 'jetpack' ),
328
				'comment_registration'            => $comment_registration,
329
				'require_name_email'              => $require_name_email,
330
				/** This action is documented in core/src/wp-includes/link-template.php */
331
				'login_url'                       => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
332
				'blog_id'                         => (int) get_current_blog_id(),
333
				'meta_data'                       => array( 'camera', 'aperture', 'shutter_speed', 'focal_length', 'copyright' ),
334
			);
335
336
			if ( ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] ) ) {
337
				// We're not using Comments after all, so fallback to standard local comments.
338
339
				if ( $is_logged_in ) {
340
					// translators: display name of commenter.
341
					$localize_strings['local_comments_commenting_as'] = '<p id="jp-carousel-commenting-as">' . sprintf( __( 'Commenting as %s', 'jetpack' ), $current_user->data->display_name ) . '</p>';
342
				} else {
343
					if ( $comment_registration ) {
344
						$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>';
345
					} else {
346
						// translators: name or email that's required.
347
						$required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
348
						$localize_strings['local_comments_commenting_as'] = ''
349
							. '<fieldset><label for="email">' . sprintf( $required, __( 'Email', 'jetpack' ) ) . '</label> '
350
							. '<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>'
351
							. '<fieldset><label for="author">' . sprintf( $required, __( 'Name', 'jetpack' ) ) . '</label> '
352
							. '<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>'
353
							. '<fieldset><label for="url">' . __( 'Website', 'jetpack' ) . '</label> '
354
							. '<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>';
355
					}
356
				}
357
			}
358
359
			/**
360
			 * Handle WP stats for images in full-screen.
361
			 * Build string with tracking info.
362
			 */
363
364
			/**
365
			 * Filter if Jetpack should enable stats collection on carousel views
366
			 *
367
			 * @module carousel
368
			 *
369
			 * @since 4.3.2
370
			 *
371
			 * @param bool Enable Jetpack Carousel stat collection. Default false.
372
			 */
373
			if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
374
				$localize_strings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . wp_parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
375
376
				// Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
377 View Code Duplication
				if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
378
					$stats_options        = stats_get_options();
379
					$track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
380
381
					if ( ! $track_loggedin_users ) {
382
						$localize_strings['stats'] = '';
383
					}
384
				}
385
			}
386
387
			/**
388
			 * Filter the strings passed to the Carousel's js file.
389
			 *
390
			 * @module carousel
391
			 *
392
			 * @since 1.6.0
393
			 *
394
			 * @param array $localize_strings Array of strings passed to the Jetpack js file.
395
			 */
396
			$localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
397
			wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
398
			wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( JETPACK__VERSION ) );
399
			wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
400
401
			/**
402
			 * Fires after carousel assets are enqueued for the first time.
403
			 * Allows for adding additional assets to the carousel page.
404
			 *
405
			 * @module carousel
406
			 *
407
			 * @since 1.6.0
408
			 *
409
			 * @param bool $first_run First load if Carousel on the page.
410
			 * @param array $localized_strings Array of strings passed to the Jetpack js file.
411
			 */
412
			do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
413
414
			$this->first_run = false;
415
		}
416
	}
417
418
	/** Still do'nt quite know what this does
419
	 *
420
	 * @param string $output - the gallery output.
421
	 */
422
	public function set_in_gallery( $output ) {
423
		if (
424
			class_exists( 'Jetpack_AMP_Support' )
425
			&& Jetpack_AMP_Support::is_amp_request()
426
		) {
427
			return $output;
428
		}
429
		$this->in_gallery = true;
430
		return $output;
431
	}
432
433
	/**
434
	 * Adds data-* attributes required by carousel to img tags in post HTML
435
	 * content. To be used by 'the_content' filter.
436
	 *
437
	 * @see add_data_to_images()
438
	 * @see wp_make_content_images_responsive() in wp-includes/media.php
439
	 *
440
	 * @param string $content HTML content of the post.
441
	 * @return string Modified HTML content of the post.
442
	 */
443
	public function add_data_img_tags_and_enqueue_assets( $content ) {
444
		if (
445
			class_exists( 'Jetpack_AMP_Support' )
446
			&& Jetpack_AMP_Support::is_amp_request()
447
		) {
448
			return $this->maybe_add_amp_lightbox( $content );
449
		}
450
451
		if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
452
			return $content;
453
		}
454
		$selected_images = array();
455
		foreach ( $matches[0] as $image_html ) {
456
			if ( preg_match( '/(wp-image-|data-id=)\"?([0-9]+)\"?/i', $image_html, $class_id ) &&
457
				! preg_match( '/wp-block-jetpack-slideshow_image/', $image_html ) ) {
458
				$attachment_id = absint( $class_id[2] );
459
				/**
460
				 * If exactly the same image tag is used more than once, overwrite it.
461
				 * All identical tags will be replaced later with 'str_replace()'.
462
				 */
463
				$selected_images[ $attachment_id ] = $image_html;
464
			}
465
		}
466
467
		$find    = array();
468
		$replace = array();
469
		if ( empty( $selected_images ) ) {
470
			return $content;
471
		}
472
473
		$attachments = get_posts(
474
			array(
475
				'include'          => array_keys( $selected_images ),
476
				'post_type'        => 'any',
477
				'post_status'      => 'any',
478
				'suppress_filters' => false,
479
			)
480
		);
481
482
		foreach ( $attachments as $attachment ) {
483
			$image_html = $selected_images[ $attachment->ID ];
484
485
			$attributes      = $this->add_data_to_images( array(), $attachment );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
486
			$attributes_html = '';
487
			foreach ( $attributes as $k => $v ) {
0 ignored issues
show
Bug introduced by
The expression $attributes of type string is not traversable.
Loading history...
488
				$attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
489
			}
490
491
			$find[]    = $image_html;
492
			$replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
493
		}
494
495
		$content = str_replace( $find, $replace, $content );
496
		$this->enqueue_assets();
497
		return $content;
498
	}
499
500
	/**
501
	 * Add data to images
502
	 *
503
	 * @param string $attr - data attributes.
504
	 * @param array  $attachment - the image we're adding data to.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $attachment not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
505
	 */
506
	public function add_data_to_images( $attr, $attachment = null ) {
507
		if (
508
			class_exists( 'Jetpack_AMP_Support' )
509
			&& Jetpack_AMP_Support::is_amp_request()
510
		) {
511
			return $attr;
512
		}
513
514
		$attachment_id = (int) $attachment->ID;
515
		if ( ! wp_attachment_is_image( $attachment_id ) ) {
516
			return $attr;
517
		}
518
519
		$orig_file       = wp_get_attachment_image_src( $attachment_id, 'full' );
520
		$orig_file       = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
521
		$meta            = wp_get_attachment_metadata( $attachment_id );
522
		$size            = isset( $meta['width'] ) ? (int) $meta['width'] . ',' . (int) $meta['height'] : '';
523
		$img_meta        = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
524
		$comments_opened = (int) comments_open( $attachment_id );
525
526
		/**
527
		 * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
528
		 * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
529
		 * which when used to generate a filename will likely result in a 404 on the image.
530
		 * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
531
		 * re-register. So using returned file URL instead, which we can define the sizes from through filename
532
		 * parsing in the JS, as this is a failsafe file reference.
533
		 *
534
		 * EG with Twenty Eleven activated:
535
		 * 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) }
536
		 *
537
		 * EG with Twenty Ten activated:
538
		 * 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) }
539
		 */
540
541
		$medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
542
		$medium_file      = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
543
544
		$large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
545
		$large_file      = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
546
547
		$attachment       = get_post( $attachment_id );
548
		$attachment_title = wptexturize( $attachment->post_title );
549
		$attachment_desc  = wpautop( wptexturize( $attachment->post_content ) );
550
		// Not yet providing geo-data, need to "fuzzify" for privacy.
551 View Code Duplication
		if ( ! empty( $img_meta ) ) {
552
			foreach ( $img_meta as $k => $v ) {
553
				if ( 'latitude' === $k || 'longitude' === $k ) {
554
					unset( $img_meta[ $k ] );
555
				}
556
			}
557
		}
558
559
		// See https://github.com/Automattic/jetpack/issues/2765.
560
		if ( isset( $img_meta['keywords'] ) ) {
561
			unset( $img_meta['keywords'] );
562
		}
563
564
		$img_meta = wp_json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
565
566
		$attr['data-attachment-id']     = $attachment_id;
567
		$attr['data-permalink']         = esc_attr( get_permalink( $attachment->ID ) );
568
		$attr['data-orig-file']         = esc_attr( $orig_file );
569
		$attr['data-orig-size']         = $size;
570
		$attr['data-comments-opened']   = $comments_opened;
571
		$attr['data-image-meta']        = esc_attr( $img_meta );
572
		$attr['data-image-title']       = esc_attr( htmlspecialchars( $attachment_title ) );
573
		$attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc ) );
574
		$attr['data-medium-file']       = esc_attr( $medium_file );
575
		$attr['data-large-file']        = esc_attr( $large_file );
576
577
		return $attr;
578
	}
579
580
	/**
581
	 * Adds data to container.
582
	 *
583
	 * @param string $html - gallery html.
584
	 */
585
	public function add_data_to_container( $html ) {
586
		global $post;
587
		if (
588
			class_exists( 'Jetpack_AMP_Support' )
589
			&& Jetpack_AMP_Support::is_amp_request()
590
		) {
591
			return $html;
592
		}
593
594
		if ( isset( $post ) ) {
595
			$blog_id = (int) get_current_blog_id();
596
597
			$extra_data = array(
598
				'data-carousel-extra' => array(
599
					'blog_id'   => $blog_id,
600
					'permalink' => get_permalink( $post->ID ),
601
				),
602
			);
603
604
			/**
605
			 * Filter the data added to the Gallery container.
606
			 *
607
			 * @module carousel
608
			 *
609
			 * @since 1.6.0
610
			 *
611
			 * @param array $extra_data Array of data about the site and the post.
612
			 */
613
			$extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
614
			foreach ( (array) $extra_data as $data_key => $data_values ) {
615
				$html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' ", $html );
616
				$html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery", $html );
617
				$html = str_replace( '<ul class="blocks-gallery-grid', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"blocks-gallery-grid", $html );
618
				$html = str_replace( '<figure class="wp-block-gallery blocks-gallery-grid', '<figure ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery  blocks-gallery-grid", $html );
619
			}
620
		}
621
622
		return $html;
623
	}
624
625
	/**
626
	 * Conditionally adds amp-lightbox to galleries and images.
627
	 *
628
	 * This applies to gallery blocks and shortcodes,
629
	 * in addition to images that are wrapped in a link to the page.
630
	 * Images wrapped in a link to the media file shouldn't get an amp-lightbox.
631
	 *
632
	 * @param string $content The content to possibly add amp-lightbox to.
633
	 * @return string The content, with amp-lightbox possibly added.
634
	 */
635
	public function maybe_add_amp_lightbox( $content ) {
636
		$content = preg_replace(
637
			array(
638
				'#(<figure)[^>]*(?=class=(["\']?)[^>]*wp-block-gallery[^>]*\2)#is', // Gallery block.
639
				'#(\[gallery)(?=\s+)#', // Gallery shortcode.
640
			),
641
			array(
642
				'\1 data-amp-lightbox="true" ', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-gallery-block-sanitizer.php#L84.
643
				'\1 amp-lightbox="true"', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/embeds/class-amp-gallery-embed.php#L64.
644
			),
645
			$content
646
		);
647
648
		return preg_replace_callback(
649
			'#(<a[^>]* href=(["\']?)(\S+)\2>)\s*(<img[^>]*)(class=(["\']?)[^>]*wp-image-[0-9]+[^>]*\6.*>)\s*</a>#is',
650
			static function ( $matches ) {
651 View Code Duplication
				if ( ! preg_match( '#\.\w+$#', $matches[3] ) ) {
652
					// The a[href] doesn't end in a file extension like .jpeg, so this is not a link to the media file, and should get a lightbox.
653
					return $matches[4] . ' data-amp-lightbox="true" lightbox="true" ' . $matches[5]; // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-img-sanitizer.php#L419.
654
				}
655
656
				return $matches[0];
657
			},
658
			$content
659
		);
660
	}
661
662
	/** Gets comments for images. */
663
	public function get_attachment_comments() {
664
		if ( ! headers_sent() ) {
665
			header( 'Content-type: text/javascript' );
666
		}
667
668
		/**
669
		 * Allows for the checking of privileges of the blog user before comments
670
		 * are packaged as JSON and sent back from the get_attachment_comments
671
		 * AJAX endpoint
672
		 *
673
		 * @module carousel
674
		 *
675
		 * @since 1.6.0
676
		 */
677
		do_action( 'jp_carousel_check_blog_user_privileges' );
678
679 View Code Duplication
		if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'carousel_nonce' ) ) {
680
			die( wp_json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
681
		}
682
683
		$attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
684
		$offset        = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
685
686
		if ( ! $attachment_id ) {
687
			echo wp_json_encode( __( 'Missing attachment ID.', 'jetpack' ) );
688
			die();
689
		}
690
691
		if ( $offset < 1 ) {
692
			$offset = 0;
693
		}
694
695
		$comments = get_comments(
696
			array(
697
				'status'  => 'approve',
698
				'order'   => ( 'asc' === get_option( 'comment_order' ) ) ? 'ASC' : 'DESC',
699
				'number'  => 10,
700
				'offset'  => $offset,
701
				'post_id' => $attachment_id,
702
			)
703
		);
704
705
		$out = array();
706
707
		// Can't just send the results, they contain the commenter's email address.
708
		foreach ( $comments as $comment ) {
709
			$avatar = get_avatar( $comment->comment_author_email, 64 );
710
			if ( ! $avatar ) {
711
				$avatar = '';
712
			}
713
			$out[] = array(
714
				'id'              => $comment->comment_ID,
715
				'parent_id'       => $comment->comment_parent,
716
				'author_markup'   => get_comment_author_link( $comment->comment_ID ),
717
				'gravatar_markup' => $avatar,
718
				'date_gmt'        => $comment->comment_date_gmt,
719
				'content'         => wpautop( $comment->comment_content ),
720
			);
721
		}
722
723
		die( wp_json_encode( $out ) );
724
	}
725
726
	/** Post a comment to an image attachment. */
727
	public function post_attachment_comment() {
728
		if ( ! headers_sent() ) {
729
			header( 'Content-type: text/javascript' );
730
		}
731
732 View Code Duplication
		if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'carousel_nonce' ) ) {
733
			die( wp_json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
734
		}
735
736
		$_blog_id = (int) $_POST['blog_id'];
737
		$_post_id = (int) $_POST['id'];
738
		$comment  = $_POST['comment'];
739
740
		if ( empty( $_blog_id ) ) {
741
			die( wp_json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
742
		}
743
744
		if ( empty( $_post_id ) ) {
745
			die( wp_json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
746
		}
747
748
		if ( empty( $comment ) ) {
749
			die( wp_json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
750
		}
751
752
		// Used in context like NewDash.
753
		$switched = false;
754
		if ( is_multisite() && get_current_blog_id() !== $_blog_id ) {
755
			switch_to_blog( $_blog_id );
756
			$switched = true;
757
		}
758
759
		/** This action is documented in modules/carousel/jetpack-carousel.php */
760
		do_action( 'jp_carousel_check_blog_user_privileges' );
761
762 View Code Duplication
		if ( ! comments_open( $_post_id ) ) {
763
			if ( $switched ) {
764
				restore_current_blog();
765
			}
766
			die( wp_json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
767
		}
768
769
		if ( is_user_logged_in() ) {
770
			$user         = wp_get_current_user();
771
			$user_id      = $user->ID;
772
			$display_name = $user->display_name;
773
			$email        = $user->user_email;
774
			$url          = $user->user_url;
775
776 View Code Duplication
			if ( empty( $user_id ) ) {
777
				if ( $switched ) {
778
					restore_current_blog();
779
				}
780
				die( wp_json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
781
			}
782
		} else {
783
			$user_id      = 0;
784
			$display_name = $_POST['author'];
785
			$email        = $_POST['email'];
786
			$url          = $_POST['url'];
787
788
			if ( get_option( 'require_name_email' ) ) {
789 View Code Duplication
				if ( empty( $display_name ) ) {
790
					if ( $switched ) {
791
						restore_current_blog();
792
					}
793
					die( wp_json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
794
				}
795
796 View Code Duplication
				if ( empty( $email ) ) {
797
					if ( $switched ) {
798
						restore_current_blog();
799
					}
800
					die( wp_json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
801
				}
802
803 View Code Duplication
				if ( ! is_email( $email ) ) {
804
					if ( $switched ) {
805
						restore_current_blog();
806
					}
807
					die( wp_json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
808
				}
809
			}
810
		}
811
812
		$comment_data = array(
813
			'comment_content'      => $comment,
814
			'comment_post_ID'      => $_post_id,
815
			'comment_author'       => $display_name,
816
			'comment_author_email' => $email,
817
			'comment_author_url'   => $url,
818
			'comment_approved'     => 0,
819
			'comment_type'         => 'comment',
820
		);
821
822
		if ( ! empty( $user_id ) ) {
823
			$comment_data['user_id'] = $user_id;
824
		}
825
826
		// Note: wp_new_comment() sanitizes and validates the values (too).
827
		$comment_id = wp_new_comment( $comment_data );
828
829
		/**
830
		 * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
831
		 *
832
		 * @module carousel
833
		 *
834
		 * @since 1.6.0
835
		 */
836
		do_action( 'jp_carousel_post_attachment_comment' );
837
		$comment_status = wp_get_comment_status( $comment_id );
838
839
		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...
840
			restore_current_blog();
841
		}
842
843
		die(
844
			wp_json_encode(
845
				array(
846
					'comment_id'     => $comment_id,
847
					'comment_status' => $comment_status,
848
				)
849
			)
850
		);
851
	}
852
853
	function register_settings() {
854
		add_settings_section( 'carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media' );
855
856
		if ( ! $this->in_jetpack ) {
857
			add_settings_field( 'carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
858
			register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
859
		}
860
861
		add_settings_field( 'carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
862
		register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
863
864
		add_settings_field( 'carousel_display_exif', __( 'Metadata', 'jetpack' ), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
865
		register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
866
867
		add_settings_field( 'carousel_display_comments', __( 'Comments', 'jetpack' ), array( $this, 'carousel_display_comments_callback' ), 'media', 'carousel_section' );
868
		register_setting( 'media', 'carousel_display_comments', array( $this, 'carousel_display_comments_sanitize' ) );
869
870
		// No geo setting yet, need to "fuzzify" data first, for privacy
871
		// add_settings_field('carousel_display_geo', __( 'Geolocation', 'jetpack' ), array( $this, 'carousel_display_geo_callback' ), 'media', 'carousel_section' );
872
		// register_setting( 'media', 'carousel_display_geo', array( $this, 'carousel_display_geo_sanitize' ) );
873
	}
874
875
	// Fulfill the settings section callback requirement by returning nothing
876
	function carousel_section_callback() {
877
		return;
878
	}
879
880
	function test_1or0_option( $value, $default_to_1 = true ) {
881
		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...
882
			// Binary false (===) of $value means it has not yet been set, in which case we do want to default sites to 1
883
			if ( false === $value ) {
884
				$value = 1;
885
			}
886
		}
887
		return ( 1 == $value ) ? 1 : 0;
888
	}
889
890
	function sanitize_1or0_option( $value ) {
891
		return ( 1 == $value ) ? 1 : 0;
892
	}
893
894
	function settings_checkbox( $name, $label_text, $extra_text = '', $default_to_checked = true ) {
895
		if ( empty( $name ) ) {
896
			return;
897
		}
898
		$option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
899
		echo '<fieldset>';
900
		echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '" value="1" ';
901
		checked( '1', $option );
902
		echo '/> <label for="' . esc_attr( $name ) . '">' . $label_text . '</label>';
903
		if ( ! empty( $extra_text ) ) {
904
			echo '<p class="description">' . $extra_text . '</p>';
905
		}
906
		echo '</fieldset>';
907
	}
908
909
	function settings_select( $name, $values, $extra_text = '' ) {
910
		if ( empty( $name ) || ! is_array( $values ) || empty( $values ) ) {
911
			return;
912
		}
913
		$option = get_option( $name );
914
		echo '<fieldset>';
915
		echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">';
916
		foreach ( $values as $key => $value ) {
917
			echo '<option value="' . esc_attr( $key ) . '" ';
918
			selected( $key, $option );
919
			echo '>' . esc_html( $value ) . '</option>';
920
		}
921
		echo '</select>';
922
		if ( ! empty( $extra_text ) ) {
923
			echo '<p class="description">' . $extra_text . '</p>';
924
		}
925
		echo '</fieldset>';
926
	}
927
928
	function carousel_display_exif_callback() {
929
		$this->settings_checkbox( 'carousel_display_exif', __( 'Show photo metadata (<a href="https://en.wikipedia.org/wiki/Exchangeable_image_file_format" rel="noopener noreferrer" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) );
930
	}
931
932
	/**
933
	 * Callback for checkbox and label of field that allows to toggle comments.
934
	 */
935
	public function carousel_display_comments_callback() {
936
		$this->settings_checkbox( 'carousel_display_comments', esc_html__( 'Show comments area in carousel', 'jetpack' ) );
937
	}
938
939
	function carousel_display_exif_sanitize( $value ) {
940
		return $this->sanitize_1or0_option( $value );
941
	}
942
943
	/**
944
	 * Return sanitized option for value that controls whether comments will be hidden or not.
945
	 *
946
	 * @param number $value Value to sanitize.
947
	 *
948
	 * @return number Sanitized value, only 1 or 0.
949
	 */
950
	public function carousel_display_comments_sanitize( $value ) {
951
		return $this->sanitize_1or0_option( $value );
952
	}
953
954
	function carousel_display_geo_callback() {
955
		$this->settings_checkbox( 'carousel_display_geo', __( 'Show map of photo location in carousel, when available.', 'jetpack' ) );
956
	}
957
958
	function carousel_display_geo_sanitize( $value ) {
959
		return $this->sanitize_1or0_option( $value );
960
	}
961
962
	function carousel_background_color_callback() {
963
		$this->settings_select(
964
			'carousel_background_color', array(
965
				'black' => __( 'Black', 'jetpack' ),
966
				'white' => __( 'White', 'jetpack' ),
967
			)
968
		);
969
	}
970
971
	function carousel_background_color_sanitize( $value ) {
972
		return ( 'white' == $value ) ? 'white' : 'black';
973
	}
974
975
	function carousel_enable_it_callback() {
976
		$this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
977
	}
978
979
	function carousel_enable_it_sanitize( $value ) {
980
		return $this->sanitize_1or0_option( $value );
981
	}
982
}
983
984
new Jetpack_Carousel;
985