PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 13.9.2
Jetpack – WP Security, Backup, Speed, & Growth v13.9.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / carousel / jetpack-carousel.php
jetpack / modules / carousel Last commit date
jetpack-carousel-rtl.css 2 years ago jetpack-carousel.css 3 years ago jetpack-carousel.js 2 years ago jetpack-carousel.php 2 years ago readme.md 5 years ago swiper-bundle.css 5 years ago swiper-bundle.js 4 years ago
jetpack-carousel.php
1531 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Module: Jetpack Carousel
4 *
5 * @package automattic/jetpack
6 */
7
8 use Automattic\Jetpack\Assets;
9 use Automattic\Jetpack\Stats\Options as Stats_Options;
10 use Automattic\Jetpack\Status;
11 /**
12 * Jetpack_Carousel class.
13 */
14 class Jetpack_Carousel {
15 /**
16 * Defines Carousel pre-built widths
17 *
18 * @var array
19 */
20 public $prebuilt_widths = array( 370, 700, 1000, 1200, 1400, 2000 );
21
22 /**
23 * Localization strings and other data for the JavaScript
24 *
25 * @var array
26 */
27 public $localize_strings;
28
29 /**
30 * Represents whether or not this is the first load of Carousel on a page. Default is true.
31 *
32 * @var bool
33 */
34 public $first_run = true;
35
36 /**
37 * Determines whether or not to set in the gallery. Default is false.
38 *
39 * @deprecated since 10.8
40 *
41 * @var bool
42 */
43 public $in_gallery = false;
44
45 /**
46 * Determines whether the Jetpack class and method exists. Default is true.
47 *
48 * @var bool
49 */
50 public $in_jetpack = true;
51
52 /**
53 * Determines whether or not a single image gallery is enabled. Default is false.
54 *
55 * @var bool
56 */
57 public $single_image_gallery_enabled = false;
58
59 /**
60 * Determines whether images that link to themselves should be replaced with a one image gallery. Default is false.
61 *
62 * @var bool
63 */
64 public $single_image_gallery_enabled_media_file = false;
65
66 /**
67 * Constructor.
68 */
69 public function __construct() {
70 add_action( 'init', array( $this, 'init' ) );
71 }
72
73 /**
74 * Initialize class
75 */
76 public function init() {
77 if ( $this->maybe_disable_jp_carousel() ) {
78 return;
79 }
80
81 $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
82
83 $this->single_image_gallery_enabled = ! $this->maybe_disable_jp_carousel_single_images();
84 $this->single_image_gallery_enabled_media_file = $this->maybe_enable_jp_carousel_single_images_media_file();
85
86 if ( is_admin() ) {
87 // Register the Carousel-related related settings.
88 add_action( 'admin_init', array( $this, 'register_settings' ), 5 );
89 if ( ! $this->in_jetpack ) {
90 if ( 0 === $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
91 return; // Carousel disabled, abort early, but still register setting so user can switch it back on.
92 }
93 }
94 // If in admin, register the ajax endpoints.
95 add_action( 'wp_ajax_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
96 add_action( 'wp_ajax_nopriv_get_attachment_comments', array( $this, 'get_attachment_comments' ) );
97 add_action( 'wp_ajax_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
98 add_action( 'wp_ajax_nopriv_post_attachment_comment', array( $this, 'post_attachment_comment' ) );
99
100 // Disable core lightbox when Carousel is enabled.
101 add_action( 'wp_theme_json_data_theme', array( $this, 'disable_core_lightbox' ) );
102 } else {
103 if ( ! $this->in_jetpack ) {
104 if ( 0 === $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
105 return; // Carousel disabled, abort early.
106 }
107 }
108 // If on front-end, do the Carousel thang.
109 /**
110 * Filter the array of default prebuilt widths used in Carousel.
111 *
112 * @module carousel
113 *
114 * @since 1.6.0
115 *
116 * @param array $this->prebuilt_widths Array of default widths.
117 */
118 $this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
119 // below: load later than other callbacks hooked it (e.g. 3rd party plugins handling gallery shortcode).
120 add_filter( 'post_gallery', array( $this, 'check_if_shortcode_processed_and_enqueue_assets' ), 1000, 2 );
121 add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
122 add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
123 add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
124 add_filter( 'jetpack_tiled_galleries_block_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
125 if ( $this->single_image_gallery_enabled ) {
126 add_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
127 }
128
129 // `is_amp_request()` can't be called until the 'wp' filter.
130 add_action( 'wp', array( $this, 'check_amp_support' ) );
131
132 // Disable core lightbox when Carousel is enabled.
133 add_action( 'wp_theme_json_data_theme', array( $this, 'disable_core_lightbox' ) );
134 }
135
136 if ( $this->in_jetpack ) {
137 Jetpack::enable_module_configurable( dirname( __DIR__ ) . '/carousel.php' );
138 }
139 }
140
141 /**
142 * Check AMP and add filters.
143 */
144 public function check_amp_support() {
145 if (
146 ! class_exists( 'Jetpack_AMP_Support' )
147 || ! Jetpack_AMP_Support::is_amp_request()
148 ) {
149 add_filter( 'render_block_core/gallery', array( $this, 'filter_gallery_block_render' ), 10, 2 );
150 add_filter( 'render_block_jetpack/tiled-gallery', array( $this, 'filter_gallery_block_render' ), 10, 2 );
151 }
152 }
153
154 /**
155 * Returns the value of the applied jp_carousel_maybe_disable filter
156 *
157 * @since 1.6.0
158 *
159 * @return bool - Should Carousel be disabled? Default to false.
160  */
161 public function maybe_disable_jp_carousel() {
162 /**
163 * Allow third-party plugins or themes to disable Carousel.
164 *
165 * @module carousel
166 *
167 * @since 1.6.0
168 *
169 * @param bool false Should Carousel be disabled? Default to false.
170 */
171 return apply_filters( 'jp_carousel_maybe_disable', false );
172 }
173
174 /**
175 * Returns the value of the applied jp_carousel_maybe_disable_single_images filter
176 *
177 * @since 4.5.0
178 *
179 * @return bool - Should Carousel be disabled for single images? Default to false.
180 */
181 public function maybe_disable_jp_carousel_single_images() {
182 /**
183 * Allow third-party plugins or themes to disable Carousel for single images.
184 *
185 * @module carousel
186 *
187 * @since 4.5.0
188 *
189 * @param bool false Should Carousel be disabled for single images? Default to false.
190 */
191 return apply_filters( 'jp_carousel_maybe_disable_single_images', false );
192 }
193
194 /**
195 * Returns the value of the applied jp_carousel_load_for_images_linked_to_file filter
196 *
197 * @since 4.5.0
198 *
199 * @return bool - Should Carousel be enabled for single images linking to 'Media File'? Default to false.
200 */
201 public function maybe_enable_jp_carousel_single_images_media_file() {
202 /**
203 * Allow third-party plugins or themes to enable Carousel
204 * for single images linking to 'Media File' (full size image).
205 *
206 * @module carousel
207 *
208 * @since 4.5.0
209 *
210 * @param bool false Should Carousel be enabled for single images linking to 'Media File'? Default to false.
211 */
212 return apply_filters( 'jp_carousel_load_for_images_linked_to_file', false );
213 }
214
215 /**
216 * Disable the "Lightbox" option offered in WordPress core
217 * whenever Jetpack's Carousel feature is enabled.
218 *
219 * @since 13.3
220 *
221 * @param WP_Theme_JSON_Data $theme_json Class to access and update theme.json data.
222 */
223 public function disable_core_lightbox( $theme_json ) {
224 return $theme_json->update_with(
225 array(
226 'version' => 2,
227 'settings' => array(
228 'blocks' => array(
229 'core/image' => array(
230 'lightbox' => array(
231 'allowEditing' => false,
232 'enabled' => false,
233 ),
234 ),
235 ),
236 ),
237 )
238 );
239 }
240
241 /**
242 * Returns the value of the applied jp_carousel_asset_version filter
243 *
244 * @since 1.6.0
245 *
246 * @param string $version Asset version.
247 *
248 * @return string
249 */
250 public function asset_version( $version ) {
251 /**
252 * Filter the version string used when enqueuing Carousel assets.
253 *
254 * @module carousel
255 *
256 * @since 1.6.0
257 *
258 * @param string $version Asset version.
259 */
260 return apply_filters( 'jp_carousel_asset_version', $version );
261 }
262
263 /**
264 * Displays a message on top of gallery if carousel has bailed.
265 *
266 * @param string $output Gallery shortcode output.
267 *
268 * @return string Shortcode output with bail message prepended.
269 */
270 public function display_bail_message( $output = '' ) {
271 $message = '<div class="jp-carousel-msg"><p>';
272 $message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
273 $message .= '</p></div>';
274 // put before gallery output.
275 $output = $message . $output;
276 return $output;
277 }
278
279 /**
280 * Determine whether Carousel is enabled, and adjust filters and enqueue assets accordingly.
281 *
282 * If no other filter hook produced output for the gallery shortcode or something returns true for
283 * the `jp_carousel_force_enable` filter, Carousel is enabled and we queue our assets. Otherwise
284 * it's disabled and we remove some of our subsequent filter hooks.
285 *
286 * @since 1.9.0
287 *
288 * @param string $output Gallery shortcode output.
289 *
290 * @return string Gallery shortcode output.
291 */
292 public function check_if_shortcode_processed_and_enqueue_assets( $output ) {
293 if (
294 class_exists( 'Jetpack_AMP_Support' )
295 && Jetpack_AMP_Support::is_amp_request()
296 ) {
297 return $output;
298 }
299
300 if (
301 ! empty( $output ) &&
302 /**
303 * Allow third-party plugins or themes to force-enable Carousel.
304 *
305 * @module carousel
306 *
307 * @since 1.9.0
308 *
309 * @param bool false Should we force enable Carousel? Default to false.
310 */
311 ! apply_filters( 'jp_carousel_force_enable', false )
312 ) {
313 // Bail because someone is overriding the [gallery] shortcode.
314 remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
315 remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
316 remove_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
317 // Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
318 if (
319 is_super_admin() &&
320 ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
321 ) {
322 add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
323 }
324 return $output;
325 }
326
327 /**
328 * Fires when thumbnails are shown in Carousel.
329 *
330 * @module carousel
331 *
332 * @since 1.6.0
333 */
334 do_action( 'jp_carousel_thumbnails_shown' );
335
336 $this->enqueue_assets();
337
338 return $output;
339 }
340
341 /**
342 * Check if the content of a post uses gallery blocks. To be used by 'the_content' filter.
343 *
344 * @since 6.8.0
345 * @deprecated since 11.3 We now hook into the 'block_render_{block_name}' hook to add markup.
346 *
347 * @param string $content Post content.
348 *
349 * @return string $content Post content.
350 */
351 public function check_content_for_blocks( $content ) {
352 _deprecated_function( __METHOD__, 'jetpack-11.3' );
353
354 if (
355 class_exists( 'Jetpack_AMP_Support' )
356 && Jetpack_AMP_Support::is_amp_request()
357 ) {
358 return $content;
359 }
360
361 if ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) {
362 $this->enqueue_assets();
363 $content = $this->add_data_to_container( $content );
364 }
365
366 return $content;
367 }
368
369 /**
370 * Enrich the gallery block content using the render_block_{$this->name} filter.
371 * This function is triggered after block render to make sure we track galleries within
372 * reusable blocks.
373 *
374 * @see https://developer.wordpress.org/reference/hooks/render_block_this-name/
375 *
376 * @param string $block_content The rendered HTML for the carousel or gallery block.
377 * @param array $block The parsed block details for the block.
378 * @return string The fully-processed HTML for the carousel or gallery block.
379 *
380 * @since 11.3
381 */
382 public function filter_gallery_block_render( $block_content, $block ) {
383 global $post;
384
385 if ( empty( $block['blockName'] ) || ! in_array( $block['blockName'], array( 'core/gallery', 'jetpack/tiled-gallery' ), true ) ) {
386 return $block_content;
387 }
388
389 $this->enqueue_assets();
390
391 if ( ! isset( $post ) ) {
392 return $block_content;
393 }
394
395 $blog_id = (int) get_current_blog_id();
396
397 $extra_data = array(
398 'data-carousel-extra' => array(
399 'blog_id' => $blog_id,
400 'permalink' => get_permalink( $post->ID ),
401 ),
402 );
403
404 /**
405 * Filter the data added to the Gallery container.
406 *
407 * @module carousel
408 *
409 * @since 1.6.0
410 *
411 * @param array $extra_data Array of data about the site and the post.
412 */
413 $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
414 $extra_data = (array) $extra_data;
415
416 if ( empty( $extra_data ) ) {
417 return $block_content;
418 }
419
420 $extra_attributes = implode(
421 ' ',
422 array_map(
423 function ( $data_key, $data_values ) {
424 return esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "'";
425 },
426 array_keys( $extra_data ),
427 array_values( $extra_data )
428 )
429 );
430
431 // Add extra attributes to first HTML element (which may have leading whitespace)
432 return preg_replace(
433 '/^(\s*<(div|ul|figure))/',
434 '$1 ' . $extra_attributes . ' ',
435 $block_content,
436 1
437 );
438 }
439
440 /**
441 * Enqueueing Carousel assets.
442 */
443 public function enqueue_assets() {
444 if ( $this->first_run ) {
445 wp_enqueue_script(
446 'jetpack-carousel',
447 Assets::get_file_url_for_environment(
448 '_inc/build/carousel/jetpack-carousel.min.js',
449 'modules/carousel/jetpack-carousel.js'
450 ),
451 array(),
452 $this->asset_version( JETPACK__VERSION ),
453 true
454 );
455
456 $swiper_library_path = array(
457 'url' => Assets::get_file_url_for_environment(
458 '_inc/build/carousel/swiper-bundle.min.js',
459 'modules/carousel/swiper-bundle.js'
460 ),
461 );
462 wp_localize_script( 'jetpack-carousel', 'jetpackSwiperLibraryPath', $swiper_library_path );
463
464 // 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).
465 // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
466 $is_logged_in = is_user_logged_in();
467 $comment_registration = (int) get_option( 'comment_registration' );
468 $require_name_email = (int) get_option( 'require_name_email' );
469 $localize_strings = array(
470 'widths' => $this->prebuilt_widths,
471 'is_logged_in' => $is_logged_in,
472 'lang' => strtolower( substr( get_locale(), 0, 2 ) ),
473 'ajaxurl' => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
474 'nonce' => wp_create_nonce( 'carousel_nonce' ),
475 'display_exif' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
476 'display_comments' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_comments', true ) ),
477 'single_image_gallery' => $this->single_image_gallery_enabled,
478 'single_image_gallery_media_file' => $this->single_image_gallery_enabled_media_file,
479 'background_color' => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
480 'comment' => __( 'Comment', 'jetpack' ),
481 'post_comment' => __( 'Post Comment', 'jetpack' ),
482 'write_comment' => __( 'Write a Comment...', 'jetpack' ),
483 'loading_comments' => __( 'Loading Comments...', 'jetpack' ),
484 'download_original' => sprintf(
485 /* translators: %1s is the full-size image width, and %2s is the height. */
486 __( 'View full size <span class="photo-size">%1$s<span class="photo-size-times">&times;</span>%2$s</span>', 'jetpack' ),
487 '{0}',
488 '{1}'
489 ),
490 'no_comment_text' => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
491 'no_comment_email' => __( 'Please provide an email address to comment.', 'jetpack' ),
492 'no_comment_author' => __( 'Please provide your name to comment.', 'jetpack' ),
493 'comment_post_error' => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
494 'comment_approved' => __( 'Your comment was approved.', 'jetpack' ),
495 'comment_unapproved' => __( 'Your comment is in moderation.', 'jetpack' ),
496 'camera' => __( 'Camera', 'jetpack' ),
497 'aperture' => __( 'Aperture', 'jetpack' ),
498 'shutter_speed' => __( 'Shutter Speed', 'jetpack' ),
499 'focal_length' => __( 'Focal Length', 'jetpack' ),
500 'copyright' => __( 'Copyright', 'jetpack' ),
501 'comment_registration' => $comment_registration,
502 'require_name_email' => $require_name_email,
503 /** This action is documented in core/src/wp-includes/link-template.php */
504 'login_url' => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
505 'blog_id' => (int) get_current_blog_id(),
506 'meta_data' => array( 'camera', 'aperture', 'shutter_speed', 'focal_length', 'copyright' ),
507 );
508
509 /**
510 * Handle WP stats for images in full-screen.
511 * Build string with tracking info.
512 */
513
514 /**
515 * Filter if Jetpack should enable stats collection on carousel views
516 *
517 * @module carousel
518 *
519 * @since 4.3.2
520 *
521 * @param bool Enable Jetpack Carousel stat collection. Default false.
522 */
523 if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
524 $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;
525
526 // Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
527 if ( is_user_logged_in() ) {
528 $stats_options = Stats_Options::get_options();
529 $track_loggedin_users = isset( $stats_options['count_roles'] ) ? (bool) $stats_options['count_roles'] : false;
530
531 if ( ! $track_loggedin_users ) {
532 $localize_strings['stats'] = '';
533 }
534 }
535 }
536
537 /**
538 * Filter the strings passed to the Carousel's js file.
539 *
540 * @module carousel
541 *
542 * @since 1.6.0
543 *
544 * @param array $localize_strings Array of strings passed to the Jetpack js file.
545 */
546 $localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
547 wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
548 wp_enqueue_style(
549 'jetpack-carousel-swiper-css',
550 plugins_url( 'swiper-bundle.css', __FILE__ ),
551 array(),
552 $this->asset_version( JETPACK__VERSION )
553 );
554 wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( JETPACK__VERSION ) );
555 wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
556
557 /**
558 * Fires after carousel assets are enqueued for the first time.
559 * Allows for adding additional assets to the carousel page.
560 *
561 * @module carousel
562 *
563 * @since 1.6.0
564 *
565 * @param bool $first_run First load if Carousel on the page.
566 * @param array $localized_strings Array of strings passed to the Jetpack js file.
567 */
568 do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
569
570 // Add the carousel skeleton to the page.
571 $this->localize_strings = $localize_strings;
572 add_action( 'wp_footer', array( $this, 'add_carousel_skeleton' ) );
573
574 $this->first_run = false;
575 }
576 }
577
578 /**
579 * Generate the HTML skeleton that will be picked up by the Carousel JS and used for showing the carousel.
580 */
581 public function add_carousel_skeleton() {
582 $localize_strings = $this->localize_strings;
583 $is_light = ( 'white' === $localize_strings['background_color'] );
584 // Determine whether to fall back to standard local comments.
585 $use_local_comments = ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] );
586 $current_user = wp_get_current_user();
587 $require_name_email = (int) get_option( 'require_name_email' );
588 /* translators: %s is replaced with a field name in the form, e.g. "Email" */
589 $required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
590 ?>
591 <div id="jp-carousel-loading-overlay">
592 <div id="jp-carousel-loading-wrapper">
593 <span id="jp-carousel-library-loading">&nbsp;</span>
594 </div>
595 </div>
596 <div class="jp-carousel-overlay<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>" style="display: none;">
597
598 <div class="jp-carousel-container<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>">
599 <!-- The Carousel Swiper -->
600 <div
601 class="jp-carousel-wrap swiper-container jp-carousel-swiper-container jp-carousel-transitions"
602 itemscope
603 itemtype="https://schema.org/ImageGallery">
604 <div class="jp-carousel swiper-wrapper"></div>
605 <div class="jp-swiper-button-prev swiper-button-prev">
606 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
607 <mask id="maskPrev" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="9" height="12">
608 <path d="M16.2072 16.59L11.6496 12L16.2072 7.41L14.8041 6L8.8335 12L14.8041 18L16.2072 16.59Z" fill="white"/>
609 </mask>
610 <g mask="url(#maskPrev)">
611 <rect x="0.579102" width="23.8823" height="24" fill="#FFFFFF"/>
612 </g>
613 </svg>
614 </div>
615 <div class="jp-swiper-button-next swiper-button-next">
616 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
617 <mask id="maskNext" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="8" height="12">
618 <path d="M8.59814 16.59L13.1557 12L8.59814 7.41L10.0012 6L15.9718 12L10.0012 18L8.59814 16.59Z" fill="white"/>
619 </mask>
620 <g mask="url(#maskNext)">
621 <rect x="0.34375" width="23.8822" height="24" fill="#FFFFFF"/>
622 </g>
623 </svg>
624 </div>
625 </div>
626 <!-- The main close buton -->
627 <div class="jp-carousel-close-hint">
628 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
629 <mask id="maskClose" mask-type="alpha" maskUnits="userSpaceOnUse" x="5" y="5" width="15" height="14">
630 <path d="M19.3166 6.41L17.9135 5L12.3509 10.59L6.78834 5L5.38525 6.41L10.9478 12L5.38525 17.59L6.78834 19L12.3509 13.41L17.9135 19L19.3166 17.59L13.754 12L19.3166 6.41Z" fill="white"/>
631 </mask>
632 <g mask="url(#maskClose)">
633 <rect x="0.409668" width="23.8823" height="24" fill="#FFFFFF"/>
634 </g>
635 </svg>
636 </div>
637 <!-- Image info, comments and meta -->
638 <div class="jp-carousel-info">
639 <div class="jp-carousel-info-footer">
640 <div class="jp-carousel-pagination-container">
641 <div class="jp-swiper-pagination swiper-pagination"></div>
642 <div class="jp-carousel-pagination"></div>
643 </div>
644 <div class="jp-carousel-photo-title-container">
645 <h2 class="jp-carousel-photo-caption"></h2>
646 </div>
647 <div class="jp-carousel-photo-icons-container">
648 <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-info" aria-label="<?php esc_attr_e( 'Toggle photo metadata visibility', 'jetpack' ); ?>">
649 <span class="jp-carousel-icon">
650 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
651 <mask id="maskInfo" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
652 <path fill-rule="evenodd" clip-rule="evenodd" d="M12.7537 2C7.26076 2 2.80273 6.48 2.80273 12C2.80273 17.52 7.26076 22 12.7537 22C18.2466 22 22.7046 17.52 22.7046 12C22.7046 6.48 18.2466 2 12.7537 2ZM11.7586 7V9H13.7488V7H11.7586ZM11.7586 11V17H13.7488V11H11.7586ZM4.79292 12C4.79292 16.41 8.36531 20 12.7537 20C17.142 20 20.7144 16.41 20.7144 12C20.7144 7.59 17.142 4 12.7537 4C8.36531 4 4.79292 7.59 4.79292 12Z" fill="white"/>
653 </mask>
654 <g mask="url(#maskInfo)">
655 <rect x="0.8125" width="23.8823" height="24" fill="#FFFFFF"/>
656 </g>
657 </svg>
658 </span>
659 </a>
660 <?php if ( $localize_strings['display_comments'] ) : ?>
661 <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-comments" aria-label="<?php esc_attr_e( 'Toggle photo comments visibility', 'jetpack' ); ?>">
662 <span class="jp-carousel-icon">
663 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
664 <mask id="maskComments" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
665 <path fill-rule="evenodd" clip-rule="evenodd" d="M4.3271 2H20.2486C21.3432 2 22.2388 2.9 22.2388 4V16C22.2388 17.1 21.3432 18 20.2486 18H6.31729L2.33691 22V4C2.33691 2.9 3.2325 2 4.3271 2ZM6.31729 16H20.2486V4H4.3271V18L6.31729 16Z" fill="white"/>
666 </mask>
667 <g mask="url(#maskComments)">
668 <rect x="0.34668" width="23.8823" height="24" fill="#FFFFFF"/>
669 </g>
670 </svg>
671
672 <span class="jp-carousel-has-comments-indicator" aria-label="<?php esc_attr_e( 'This image has comments.', 'jetpack' ); ?>"></span>
673 </span>
674 </a>
675 <?php endif; ?>
676 </div>
677 </div>
678 <div class="jp-carousel-info-extra">
679 <div class="jp-carousel-info-content-wrapper">
680 <div class="jp-carousel-photo-title-container">
681 <h2 class="jp-carousel-photo-title"></h2>
682 </div>
683 <div class="jp-carousel-comments-wrapper">
684 <?php if ( $localize_strings['display_comments'] ) : ?>
685 <div id="jp-carousel-comments-loading">
686 <span><?php echo esc_html( $localize_strings['loading_comments'] ); ?></span>
687 </div>
688 <div class="jp-carousel-comments"></div>
689 <div id="jp-carousel-comment-form-container">
690 <span id="jp-carousel-comment-form-spinner">&nbsp;</span>
691 <div id="jp-carousel-comment-post-results"></div>
692 <?php if ( $use_local_comments ) : ?>
693 <?php if ( ! $localize_strings['is_logged_in'] && $localize_strings['comment_registration'] ) : ?>
694 <div id="jp-carousel-comment-form-commenting-as">
695 <p id="jp-carousel-commenting-as">
696 <?php
697 echo wp_kses(
698 __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'jetpack' ),
699 array(
700 'a' => array(
701 'href' => array(),
702 'class' => array(),
703 ),
704 )
705 );
706 ?>
707 </p>
708 </div>
709 <?php else : ?>
710 <form id="jp-carousel-comment-form">
711 <label for="jp-carousel-comment-form-comment-field" class="screen-reader-text"><?php echo esc_attr( $localize_strings['write_comment'] ); ?></label>
712 <textarea
713 name="comment"
714 class="jp-carousel-comment-form-field jp-carousel-comment-form-textarea"
715 id="jp-carousel-comment-form-comment-field"
716 placeholder="<?php echo esc_attr( $localize_strings['write_comment'] ); ?>"
717 ></textarea>
718 <div id="jp-carousel-comment-form-submit-and-info-wrapper">
719 <div id="jp-carousel-comment-form-commenting-as">
720 <?php if ( $localize_strings['is_logged_in'] ) : ?>
721 <p id="jp-carousel-commenting-as">
722 <?php
723 printf(
724 /* translators: %s is replaced with the user's display name */
725 esc_html__( 'Commenting as %s', 'jetpack' ),
726 esc_html( $current_user->data->display_name )
727 );
728 ?>
729 </p>
730 <?php else : ?>
731 <fieldset>
732 <label for="jp-carousel-comment-form-email-field"><?php echo esc_html( sprintf( $required, __( 'Email', 'jetpack' ) ) ); ?></label>
733 <input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" />
734 </fieldset>
735 <fieldset>
736 <label for="jp-carousel-comment-form-author-field"><?php echo esc_html( sprintf( $required, __( 'Name', 'jetpack' ) ) ); ?></label>
737 <input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" />
738 </fieldset>
739 <fieldset>
740 <label for="jp-carousel-comment-form-url-field"><?php esc_html_e( 'Website', 'jetpack' ); ?></label>
741 <input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" />
742 </fieldset>
743 <?php endif ?>
744 </div>
745 <input
746 type="submit"
747 name="submit"
748 class="jp-carousel-comment-form-button"
749 id="jp-carousel-comment-form-button-submit"
750 value="<?php echo esc_attr( $localize_strings['post_comment'] ); ?>" />
751 </div>
752 </form>
753 <?php endif ?>
754 <?php endif ?>
755 </div>
756 <?php endif ?>
757 </div>
758 <div class="jp-carousel-image-meta">
759 <div class="jp-carousel-title-and-caption">
760 <div class="jp-carousel-photo-info">
761 <h3 class="jp-carousel-caption" itemprop="caption description"></h3>
762 </div>
763
764 <div class="jp-carousel-photo-description"></div>
765 </div>
766 <ul class="jp-carousel-image-exif" style="display: none;"></ul>
767 <a class="jp-carousel-image-download" href="#" target="_blank" style="display: none;">
768 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
769 <mask id="mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="3" y="3" width="19" height="18">
770 <path fill-rule="evenodd" clip-rule="evenodd" d="M5.84615 5V19H19.7775V12H21.7677V19C21.7677 20.1 20.8721 21 19.7775 21H5.84615C4.74159 21 3.85596 20.1 3.85596 19V5C3.85596 3.9 4.74159 3 5.84615 3H12.8118V5H5.84615ZM14.802 5V3H21.7677V10H19.7775V6.41L9.99569 16.24L8.59261 14.83L18.3744 5H14.802Z" fill="white"/>
771 </mask>
772 <g mask="url(#mask0)">
773 <rect x="0.870605" width="23.8823" height="24" fill="#FFFFFF"/>
774 </g>
775 </svg>
776 <span class="jp-carousel-download-text"></span>
777 </a>
778 <div class="jp-carousel-image-map" style="display: none;"></div>
779 </div>
780 </div>
781 </div>
782 </div>
783 </div>
784
785 </div>
786 <?php
787 }
788
789 /**
790 * Sets the "in_gallery" flag when the first gallery is encountered (unless in AMP mode).
791 *
792 * @deprecated since 10.8
793 *
794 * @param string $output Gallery shortcode output. Passed through unchanged.
795 *
796 * @return string
797 */
798 public function set_in_gallery( $output ) {
799 if (
800 class_exists( 'Jetpack_AMP_Support' )
801 && Jetpack_AMP_Support::is_amp_request()
802 ) {
803 return $output;
804 }
805 $this->in_gallery = true;
806 return $output;
807 }
808
809 /**
810 * Adds data-* attributes required by carousel to img tags in post HTML
811 * content. To be used by 'the_content' filter.
812 *
813 * @see add_data_to_images()
814 * @see wp_make_content_images_responsive() in wp-includes/media.php
815 *
816 * @param string $content HTML content of the post.
817 * @return string
818 */
819 public function add_data_img_tags_and_enqueue_assets( $content ) {
820 if (
821 class_exists( 'Jetpack_AMP_Support' )
822 && Jetpack_AMP_Support::is_amp_request()
823 ) {
824 return $this->maybe_add_amp_lightbox( $content );
825 }
826
827 if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
828 return $content;
829 }
830 $selected_images = array();
831 foreach ( $matches[0] as $image_html ) {
832 if (
833 preg_match( '/(wp-image-|data-id=)\"?([0-9]+)\"?/i', $image_html, $class_id )
834 && ! preg_match( '/wp-block-jetpack-slideshow_image/', $image_html )
835 ) {
836 /**
837 * Allow filtering the attachment ID used to fetch and populate metadata about an image in a gallery.
838 *
839 * @module carousel
840 *
841 * @since 12.6
842 *
843 * @param int $attachment_id Attachment ID pulled from image HTML.
844 * @param string $image_html Full HTML image tag.
845 */
846 $attachment_id = absint(
847 apply_filters(
848 'jetpack_carousel_image_attachment_id',
849 $class_id[2],
850 $image_html
851 )
852 );
853
854 /**
855 * The same image tag may be used more than once but with different attribs,
856 * so save each of them against the attachment id.
857 */
858 if ( ! isset( $selected_images[ $attachment_id ] ) || ! in_array( $image_html, $selected_images[ $attachment_id ], true ) ) {
859 $selected_images[ $attachment_id ][] = $image_html;
860 }
861 }
862 }
863
864 $find = array();
865 $replace = array();
866 if ( empty( $selected_images ) ) {
867 return $content;
868 }
869
870 $attachments = get_posts(
871 array(
872 'include' => array_keys( $selected_images ),
873 'post_type' => 'any',
874 'post_status' => 'any',
875 'suppress_filters' => false,
876 )
877 );
878
879 foreach ( $attachments as $attachment ) {
880 /*
881 * If the item from get_posts isn't an attachment, skip. This can occur when copy-pasta from another WP site.
882 * For example, if one copies "<img class="wp-image-7 size-full" src="https://twentysixteendemo.files.wordpress.com/2015/11/post.png" alt="post" width="1000" height="563" />"
883 * then, we're going to look up post 7 below, which making sure it is an attachment.
884 *
885 * This is meant as a relatively quick fix, as a better fix is likely to update the get_posts call above to only
886 * include attachments.
887 */
888 if (
889 ! isset( $attachment->ID )
890 || ! wp_attachment_is_image( $attachment->ID )
891 || ! isset( $selected_images[ $attachment->ID ] )
892 ) {
893 continue;
894 }
895 $image_elements = $selected_images[ $attachment->ID ];
896
897 if ( ! is_array( $image_elements ) ) {
898 continue;
899 }
900
901 $attributes = $this->add_data_to_images( array(), $attachment );
902 $attributes_html = '';
903 foreach ( $attributes as $k => $v ) {
904 $attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
905 }
906 foreach ( $image_elements as $image_html ) {
907 $find[] = $image_html;
908 $replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
909 }
910 }
911
912 $content = str_replace( $find, $replace, $content );
913 $this->enqueue_assets();
914 return $content;
915 }
916
917 /**
918 * Adds the data attributes themselves to img tags.
919 *
920 * @see add_data_img_tags_and_enqueue_assets()
921 * @see https://developer.wordpress.org/reference/functions/wp_get_attachment_image/ Documentation about wp_get_attachment_image
922 *
923 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
924 * @param null|WP_Post $attachment Image attachment post.
925 *
926 * @return string[] Modified image attributes.
927 */
928 public function add_data_to_images( $attr, $attachment = null ) {
929 if (
930 class_exists( 'Jetpack_AMP_Support' )
931 && Jetpack_AMP_Support::is_amp_request()
932 ) {
933 return $attr;
934 }
935
936 if (
937 ! $attachment instanceof WP_Post
938 || ! isset( $attachment->ID )
939 || ! wp_attachment_is_image( $attachment )
940 ) {
941 return $attr;
942 }
943
944 $attachment_id = (int) $attachment->ID;
945 $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' );
946 $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
947 $meta = wp_get_attachment_metadata( $attachment_id );
948 $size = isset( $meta['width'] ) ? (int) $meta['width'] . ',' . (int) $meta['height'] : '';
949 $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
950 $comments_opened = (int) comments_open( $attachment_id );
951
952 /**
953 * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
954 * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
955 * which when used to generate a filename will likely result in a 404 on the image.
956 * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
957 * re-register. So using returned file URL instead, which we can define the sizes from through filename
958 * parsing in the JS, as this is a failsafe file reference.
959 *
960 * EG with Twenty Eleven activated:
961 * 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) }
962 *
963 * EG with Twenty Ten activated:
964 * 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) }
965 */
966
967 $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
968 $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
969
970 $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
971 $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
972
973 $attachment_title = wptexturize( $attachment->post_title );
974 $attachment_desc = wpautop( wptexturize( $attachment->post_content ) );
975 $attachment_caption = wpautop( wptexturize( $attachment->post_excerpt ) );
976
977 // See https://github.com/Automattic/jetpack/issues/2765.
978 if ( isset( $img_meta['keywords'] ) ) {
979 unset( $img_meta['keywords'] );
980 }
981
982 $img_meta = wp_json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
983
984 $attr['data-attachment-id'] = $attachment_id;
985 $attr['data-permalink'] = esc_attr( get_permalink( $attachment_id ) );
986 $attr['data-orig-file'] = esc_attr( $orig_file );
987 $attr['data-orig-size'] = $size;
988 $attr['data-comments-opened'] = $comments_opened;
989 $attr['data-image-meta'] = esc_attr( $img_meta );
990 // The lines below use `esc_attr( htmlspecialchars( ) )` because esc_attr tries to be too smart and won't double-encode, and we need that here.
991 $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title, ENT_COMPAT ) );
992 $attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc, ENT_COMPAT ) );
993 $attr['data-image-caption'] = esc_attr( htmlspecialchars( $attachment_caption, ENT_COMPAT ) );
994 $attr['data-medium-file'] = esc_attr( $medium_file );
995 $attr['data-large-file'] = esc_attr( $large_file );
996 $attr['tabindex'] = '0';
997 $attr['role'] = 'button';
998 return $attr;
999 }
1000
1001 /**
1002 * Add additional attributes to the Gallery container HTML.
1003 *
1004 * @param string $html The HTML to which the additional attributes are added.
1005 *
1006 * @return string
1007 */
1008 public function add_data_to_container( $html ) {
1009 global $post;
1010 if (
1011 class_exists( 'Jetpack_AMP_Support' )
1012 && Jetpack_AMP_Support::is_amp_request()
1013 ) {
1014 return $html;
1015 }
1016
1017 if ( isset( $post ) ) {
1018 $blog_id = (int) get_current_blog_id();
1019
1020 $extra_data = array(
1021 'data-carousel-extra' => array(
1022 'blog_id' => $blog_id,
1023 'permalink' => get_permalink( $post->ID ),
1024 ),
1025 );
1026
1027 /**
1028 * Filter the data added to the Gallery container.
1029 *
1030 * @module carousel
1031 *
1032 * @since 1.6.0
1033 *
1034 * @param array $extra_data Array of data about the site and the post.
1035 */
1036 $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
1037 foreach ( (array) $extra_data as $data_key => $data_values ) {
1038 $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' ", $html );
1039 $html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery", $html );
1040 $html = str_replace( '<ul class="blocks-gallery-grid', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"blocks-gallery-grid", $html );
1041 $html = preg_replace( '/\<figure([^>]*)class="(wp-block-gallery[^"]*?has-nested-images.*?)"/', '<figure ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' $1 class=\"$2\"", $html );
1042 }
1043 }
1044
1045 return $html;
1046 }
1047
1048 /**
1049 * Conditionally adds amp-lightbox to galleries and images.
1050 *
1051 * This applies to gallery blocks and shortcodes,
1052 * in addition to images that are wrapped in a link to the page.
1053 * Images wrapped in a link to the media file shouldn't get an amp-lightbox.
1054 *
1055 * @param string $content The content to possibly add amp-lightbox to.
1056 * @return string The content, with amp-lightbox possibly added.
1057 */
1058 public function maybe_add_amp_lightbox( $content ) {
1059 $content = preg_replace(
1060 array(
1061 '#(<figure)[^>]*(?=class=(["\']?)[^>]*wp-block-gallery[^>]*\2)#is', // Gallery block.
1062 '#(\[gallery)(?=\s+)#', // Gallery shortcode.
1063 ),
1064 array(
1065 '\1 data-amp-lightbox="true" ', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-gallery-block-sanitizer.php#L84.
1066 '\1 amp-lightbox="true"', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/embeds/class-amp-gallery-embed.php#L64.
1067 ),
1068 $content
1069 );
1070
1071 return preg_replace_callback(
1072 '#(<a[^>]* href=(["\']?)(\S+)\2>)\s*(<img[^>]*)(class=(["\']?)[^>]*wp-image-[0-9]+[^>]*\6.*>)\s*</a>#is',
1073 static function ( $matches ) {
1074 if ( ! preg_match( '#\.\w+$#', $matches[3] ) ) {
1075 // 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.
1076 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.
1077 }
1078
1079 return $matches[0];
1080 },
1081 $content
1082 );
1083 }
1084
1085 /**
1086 * Retrieves comment information
1087 *
1088 * @return string
1089 */
1090 public function get_attachment_comments() {
1091 if ( ! headers_sent() ) {
1092 header( 'Content-type: text/javascript' );
1093 }
1094
1095 /**
1096 * Allows for the checking of privileges of the blog user before comments
1097 * are packaged as JSON and sent back from the get_attachment_comments
1098 * AJAX endpoint
1099 *
1100 * @module carousel
1101 *
1102 * @since 1.6.0
1103 */
1104 do_action( 'jp_carousel_check_blog_user_privileges' );
1105
1106 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- we do not need to verify the nonce for this public request for publicly accessible data (as checked below).
1107 $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
1108 $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
1109 // phpcs:enable
1110
1111 if ( ! $attachment_id ) {
1112 wp_send_json_error(
1113 __( 'Missing attachment ID.', 'jetpack' ),
1114 403
1115 );
1116 return;
1117 }
1118
1119 $attachment_post = get_post( $attachment_id );
1120 // If we have no info about that attachment, bail.
1121 if ( ! ( $attachment_post instanceof WP_Post ) ) {
1122 wp_send_json_error(
1123 __( 'Missing attachment info.', 'jetpack' ),
1124 403
1125 );
1126 return;
1127 }
1128
1129 // This AJAX call should only be used to fetch comments of attachments.
1130 if ( 'attachment' !== $attachment_post->post_type ) {
1131 wp_send_json_error(
1132 __( 'You aren’t authorized to do that.', 'jetpack' ),
1133 403
1134 );
1135 return;
1136 }
1137
1138 $parent_post = get_post_parent( $attachment_id );
1139
1140 /*
1141 * If we have no info about that parent post, no extra checks.
1142 * The attachment doesn't have a parent post, so is public.
1143 * If we have a parent post, let's ensure the user has access to it.
1144 */
1145 if ( $parent_post instanceof WP_Post ) {
1146 /*
1147 * Fetch info about user making the request.
1148 * If we have no info, bail.
1149 * Even logged out users should get a WP_User user with id 0.
1150 */
1151 $current_user = wp_get_current_user();
1152 if ( ! ( $current_user instanceof WP_User ) ) {
1153 wp_send_json_error(
1154 __( 'Missing user info.', 'jetpack' ),
1155 403
1156 );
1157 return;
1158 }
1159
1160 /*
1161 * If a post is private / draft
1162 * and the current user doesn't have access to it,
1163 * bail.
1164 */
1165 if (
1166 'publish' !== $parent_post->post_status
1167 && ! current_user_can( 'read_post', $parent_post->ID )
1168 ) {
1169 wp_send_json_error(
1170 __( 'You aren’t authorized to do that.', 'jetpack' ),
1171 403
1172 );
1173 return;
1174 }
1175 }
1176
1177 if ( $offset < 1 ) {
1178 $offset = 0;
1179 }
1180
1181 $comments = get_comments(
1182 array(
1183 'status' => 'approve',
1184 'order' => ( 'asc' === get_option( 'comment_order' ) ) ? 'ASC' : 'DESC',
1185 'number' => 10,
1186 'offset' => $offset,
1187 'post_id' => $attachment_id,
1188 )
1189 );
1190
1191 $out = array();
1192
1193 // Can't just send the results, they contain the commenter's email address.
1194 foreach ( $comments as $comment ) {
1195 $avatar = get_avatar( $comment->comment_author_email, 64 );
1196 if ( ! $avatar ) {
1197 $avatar = '';
1198 }
1199 $out[] = array(
1200 'id' => $comment->comment_ID,
1201 'parent_id' => $comment->comment_parent,
1202 'author_markup' => get_comment_author_link( $comment->comment_ID ),
1203 'gravatar_markup' => $avatar,
1204 'date_gmt' => $comment->comment_date_gmt,
1205 'content' => wpautop( $comment->comment_content ),
1206 );
1207 }
1208
1209 die( wp_json_encode( $out ) );
1210 }
1211
1212 /**
1213 * Adds a new comment to the database
1214 *
1215 * @return never
1216 */
1217 public function post_attachment_comment() {
1218 if ( ! headers_sent() ) {
1219 header( 'Content-type: text/javascript' );
1220 }
1221
1222 if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'carousel_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WP Core doesn't unslash or sanitize nonces either
1223 die( wp_json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
1224 }
1225
1226 $_blog_id = isset( $_POST['blog_id'] ) ? (int) $_POST['blog_id'] : 0;
1227 $_post_id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
1228 $comment = isset( $_POST['comment'] ) ? filter_var( wp_unslash( $_POST['comment'] ) ) : null;
1229
1230 if ( empty( $_blog_id ) ) {
1231 die( wp_json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
1232 }
1233
1234 if ( empty( $_post_id ) ) {
1235 die( wp_json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
1236 }
1237
1238 if ( empty( $comment ) ) {
1239 die( wp_json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
1240 }
1241
1242 // Used in context like NewDash.
1243 $switched = false;
1244 if ( is_multisite() && get_current_blog_id() !== $_blog_id ) {
1245 switch_to_blog( $_blog_id );
1246 $switched = true;
1247 }
1248
1249 /** This action is documented in modules/carousel/jetpack-carousel.php */
1250 do_action( 'jp_carousel_check_blog_user_privileges' );
1251
1252 if ( ! comments_open( $_post_id ) ) {
1253 if ( $switched ) {
1254 restore_current_blog();
1255 }
1256 die( wp_json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
1257 }
1258
1259 if ( is_user_logged_in() ) {
1260 $user = wp_get_current_user();
1261 $user_id = $user->ID;
1262 $display_name = $user->display_name;
1263 $email = $user->user_email;
1264 $url = $user->user_url;
1265
1266 if ( empty( $user_id ) ) {
1267 if ( $switched ) {
1268 restore_current_blog();
1269 }
1270 die( wp_json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
1271 }
1272 } else {
1273 $user_id = 0;
1274 $display_name = isset( $_POST['author'] ) ? sanitize_text_field( wp_unslash( $_POST['author'] ) ) : null;
1275 $email = isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Checked or sanitized below.
1276 $url = isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : null;
1277
1278 if ( get_option( 'require_name_email' ) ) {
1279 if ( empty( $display_name ) ) {
1280 if ( $switched ) {
1281 restore_current_blog();
1282 }
1283 die( wp_json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
1284 }
1285
1286 if ( empty( $email ) ) {
1287 if ( $switched ) {
1288 restore_current_blog();
1289 }
1290 die( wp_json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
1291 }
1292
1293 if ( ! is_email( $email ) ) {
1294 if ( $switched ) {
1295 restore_current_blog();
1296 }
1297 die( wp_json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
1298 }
1299 } else {
1300 $email = $email !== null ? sanitize_email( $email ) : null;
1301 }
1302 }
1303
1304 $comment_data = array(
1305 'comment_content' => $comment,
1306 'comment_post_ID' => $_post_id,
1307 'comment_author' => $display_name,
1308 'comment_author_email' => $email,
1309 'comment_author_url' => $url,
1310 'comment_approved' => 0,
1311 'comment_type' => 'comment',
1312 );
1313
1314 if ( ! empty( $user_id ) ) {
1315 $comment_data['user_id'] = $user_id;
1316 }
1317
1318 // Note: wp_new_comment() sanitizes and validates the values (too).
1319 $comment_id = wp_new_comment( $comment_data );
1320
1321 /**
1322 * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
1323 *
1324 * @module carousel
1325 *
1326 * @since 1.6.0
1327 */
1328 do_action( 'jp_carousel_post_attachment_comment' );
1329 $comment_status = wp_get_comment_status( $comment_id );
1330
1331 if ( $switched ) {
1332 restore_current_blog();
1333 }
1334
1335 die(
1336 wp_json_encode(
1337 array(
1338 'comment_id' => $comment_id,
1339 'comment_status' => $comment_status,
1340 )
1341 )
1342 );
1343 }
1344
1345 /**
1346 * Register Carousel settings
1347 */
1348 public function register_settings() {
1349 add_settings_section( 'carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media' );
1350
1351 if ( ! $this->in_jetpack ) {
1352 add_settings_field( 'carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
1353 register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
1354 }
1355
1356 add_settings_field( 'carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
1357 register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
1358
1359 add_settings_field( 'carousel_display_exif', __( 'Metadata', 'jetpack' ), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
1360 register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
1361
1362 add_settings_field( 'carousel_display_comments', __( 'Comments', 'jetpack' ), array( $this, 'carousel_display_comments_callback' ), 'media', 'carousel_section' );
1363 register_setting( 'media', 'carousel_display_comments', array( $this, 'carousel_display_comments_sanitize' ) );
1364 }
1365
1366 /**
1367 * Fulfill the settings section callback requirement by returning nothing.
1368 */
1369 public function carousel_section_callback() {
1370 }
1371
1372 /**
1373 * Tests if a value is set
1374 *
1375 * @param mixed $value The value passed into this function with which to test.
1376 * @param bool $default_to_1 Default is true.
1377 *
1378 * @return bool
1379 */
1380 public function test_1or0_option( $value, $default_to_1 = true ) {
1381 if ( $default_to_1 ) {
1382 // Boolean false (===) of $value means it has not yet been set, in which case we do want to default to 1.
1383 if ( false === $value ) {
1384 $value = 1;
1385 }
1386 }
1387 return ( 1 == $value ) ? 1 : 0; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
1388 }
1389
1390 /**
1391 * Ensures the value returned is in the correct format.
1392 *
1393 * @see test_1or0_option()
1394 * @param mixed $value The value returned from the test_1or0_option function.
1395 *
1396 * @return int
1397 */
1398 public function sanitize_1or0_option( $value ) {
1399 return ( 1 == $value ) ? 1 : 0; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
1400 }
1401
1402 /**
1403 * Outputs a settings checkbox.
1404 *
1405 * @param string $name - For name attribute.
1406 * @param string $label_text - For label attribute.
1407 * @param string $extra_text - Additional checkbox description text. Defaults to empty.
1408 * @param bool $default_to_checked - If the checkbox is checked. Default is true.
1409 */
1410 public function settings_checkbox( $name, $label_text, $extra_text = '', $default_to_checked = true ) {
1411 if ( empty( $name ) ) {
1412 return;
1413 }
1414 $option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
1415 echo '<fieldset>';
1416 echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '" value="1" ';
1417 checked( '1', $option );
1418 echo '/> <label for="' . esc_attr( $name ) . '">' . wp_kses_post( $label_text ) . '</label>';
1419 if ( ! empty( $extra_text ) ) {
1420 echo '<p class="description">' . wp_kses_post( $extra_text ) . '</p>';
1421 }
1422 echo '</fieldset>';
1423 }
1424
1425 /**
1426 * Output a selection list options
1427 *
1428 * @param string $name - For name attribute.
1429 * @param string $values - For the different option values.
1430 * @param string $extra_text - Additional option section description text. Defaults to empty.
1431 */
1432 public function settings_select( $name, $values, $extra_text = '' ) {
1433 if ( empty( $name ) || ! is_array( $values ) || empty( $values ) ) {
1434 return;
1435 }
1436 $option = get_option( $name );
1437 echo '<fieldset>';
1438 echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">';
1439 foreach ( $values as $key => $value ) {
1440 echo '<option value="' . esc_attr( $key ) . '" ';
1441 selected( $key, $option );
1442 echo '>' . esc_html( $value ) . '</option>';
1443 }
1444 echo '</select>';
1445 if ( ! empty( $extra_text ) ) {
1446 echo '<p class="description">' . wp_kses_post( $extra_text ) . '</p>';
1447 }
1448 echo '</fieldset>';
1449 }
1450
1451 /**
1452 * Callback for checkbox and label of field that allows to toggle exif display.
1453 */
1454 public function carousel_display_exif_callback() {
1455 $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' ) );
1456 }
1457
1458 /**
1459 * Callback for checkbox and label of field that allows to toggle comments.
1460 */
1461 public function carousel_display_comments_callback() {
1462 $this->settings_checkbox( 'carousel_display_comments', esc_html__( 'Show comments area in carousel', 'jetpack' ) );
1463 }
1464
1465 /**
1466 * Sanitize input for the `carousel_display_exif` setting.
1467 *
1468 * @param mixed $value User input setting value.
1469 *
1470 * @return int Sanitized value, only 1 or 0.
1471 */
1472 public function carousel_display_exif_sanitize( $value ) {
1473 return $this->sanitize_1or0_option( $value );
1474 }
1475
1476 /**
1477 * Return sanitized option for value that controls whether comments will be hidden or not.
1478 *
1479 * @param mixed $value Value to sanitize.
1480 *
1481 * @return int Sanitized value, only 1 or 0.
1482 */
1483 public function carousel_display_comments_sanitize( $value ) {
1484 return $this->sanitize_1or0_option( $value );
1485 }
1486
1487 /**
1488 * Callback for the Carousel background color.
1489 */
1490 public function carousel_background_color_callback() {
1491 $this->settings_select(
1492 'carousel_background_color',
1493 array(
1494 'black' => __( 'Black', 'jetpack' ),
1495 'white' => __( 'White', 'jetpack' ),
1496 )
1497 );
1498 }
1499
1500 /**
1501 * Sanitizing the Carousel backgound color selection.
1502 *
1503 * @param string $value The color string to sanitize.
1504 *
1505 * @return string Sanitized value, 'white' or 'black'.
1506 */
1507 public function carousel_background_color_sanitize( $value ) {
1508 return ( 'white' === $value ) ? 'white' : 'black';
1509 }
1510
1511 /**
1512 * Callback to display text for the carousel_enable_it settings field.
1513 */
1514 public function carousel_enable_it_callback() {
1515 $this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
1516 }
1517
1518 /**
1519 * Sanitize input for the `carousel_enable_it` setting.
1520 *
1521 * @param mixed $value User input.
1522 *
1523 * @return int Sanitized value, only 1 or 0.
1524 */
1525 public function carousel_enable_it_sanitize( $value ) {
1526 return $this->sanitize_1or0_option( $value );
1527 }
1528 }
1529
1530 new Jetpack_Carousel();
1531