PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
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 3 years ago jetpack-carousel.css 3 years ago jetpack-carousel.js 3 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
1481 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 } else {
100 if ( ! $this->in_jetpack ) {
101 if ( 0 === $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) {
102 return; // Carousel disabled, abort early.
103 }
104 }
105 // If on front-end, do the Carousel thang.
106 /**
107 * Filter the array of default prebuilt widths used in Carousel.
108 *
109 * @module carousel
110 *
111 * @since 1.6.0
112 *
113 * @param array $this->prebuilt_widths Array of default widths.
114 */
115 $this->prebuilt_widths = apply_filters( 'jp_carousel_widths', $this->prebuilt_widths );
116 // below: load later than other callbacks hooked it (e.g. 3rd party plugins handling gallery shortcode).
117 add_filter( 'post_gallery', array( $this, 'check_if_shortcode_processed_and_enqueue_assets' ), 1000, 2 );
118 add_filter( 'post_gallery', array( $this, 'set_in_gallery' ), -1000 );
119 add_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
120 add_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ), 10, 2 );
121 add_filter( 'jetpack_tiled_galleries_block_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
122 if ( $this->single_image_gallery_enabled ) {
123 add_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
124 }
125
126 if (
127 ! class_exists( 'Jetpack_AMP_Support' )
128 || ! Jetpack_AMP_Support::is_amp_request()
129 ) {
130 add_filter( 'render_block_core/gallery', array( $this, 'filter_gallery_block_render' ), 10, 2 );
131 add_filter( 'render_block_jetpack/tiled-gallery', array( $this, 'filter_gallery_block_render' ), 10, 2 );
132 }
133 }
134
135 if ( $this->in_jetpack ) {
136 Jetpack::enable_module_configurable( dirname( __DIR__ ) . '/carousel.php' );
137 }
138 }
139
140 /**
141 * Returns the value of the applied jp_carousel_maybe_disable filter
142 *
143 * @since 1.6.0
144 *
145 * @return bool - Should Carousel be disabled? Default to false.
146  */
147 public function maybe_disable_jp_carousel() {
148 /**
149 * Allow third-party plugins or themes to disable Carousel.
150 *
151 * @module carousel
152 *
153 * @since 1.6.0
154 *
155 * @param bool false Should Carousel be disabled? Default to false.
156 */
157 return apply_filters( 'jp_carousel_maybe_disable', false );
158 }
159
160 /**
161 * Returns the value of the applied jp_carousel_maybe_disable_single_images filter
162 *
163 * @since 4.5.0
164 *
165 * @return bool - Should Carousel be disabled for single images? Default to false.
166 */
167 public function maybe_disable_jp_carousel_single_images() {
168 /**
169 * Allow third-party plugins or themes to disable Carousel for single images.
170 *
171 * @module carousel
172 *
173 * @since 4.5.0
174 *
175 * @param bool false Should Carousel be disabled for single images? Default to false.
176 */
177 return apply_filters( 'jp_carousel_maybe_disable_single_images', false );
178 }
179
180 /**
181 * Returns the value of the applied jp_carousel_load_for_images_linked_to_file filter
182 *
183 * @since 4.5.0
184 *
185 * @return bool - Should Carousel be enabled for single images linking to 'Media File'? Default to false.
186 */
187 public function maybe_enable_jp_carousel_single_images_media_file() {
188 /**
189 * Allow third-party plugins or themes to enable Carousel
190 * for single images linking to 'Media File' (full size image).
191 *
192 * @module carousel
193 *
194 * @since 4.5.0
195 *
196 * @param bool false Should Carousel be enabled for single images linking to 'Media File'? Default to false.
197 */
198 return apply_filters( 'jp_carousel_load_for_images_linked_to_file', false );
199 }
200
201 /**
202 * Returns the value of the applied jp_carousel_asset_version filter
203 *
204 * @since 1.6.0
205 *
206 * @param string $version Asset version.
207 *
208 * @return string
209 */
210 public function asset_version( $version ) {
211 /**
212 * Filter the version string used when enqueuing Carousel assets.
213 *
214 * @module carousel
215 *
216 * @since 1.6.0
217 *
218 * @param string $version Asset version.
219 */
220 return apply_filters( 'jp_carousel_asset_version', $version );
221 }
222
223 /**
224 * Displays a message on top of gallery if carousel has bailed.
225 *
226 * @param string $output Gallery shortcode output.
227 *
228 * @return string Shortcode output with bail message prepended.
229 */
230 public function display_bail_message( $output = '' ) {
231 $message = '<div class="jp-carousel-msg"><p>';
232 $message .= __( 'Jetpack\'s Carousel has been disabled, because another plugin or your theme is overriding the [gallery] shortcode.', 'jetpack' );
233 $message .= '</p></div>';
234 // put before gallery output.
235 $output = $message . $output;
236 return $output;
237 }
238
239 /**
240 * Determine whether Carousel is enabled, and adjust filters and enqueue assets accordingly.
241 *
242 * If no other filter hook produced output for the gallery shortcode or something returns true for
243 * the `jp_carousel_force_enable` filter, Carousel is enabled and we queue our assets. Otherwise
244 * it's disabled and we remove some of our subsequent filter hooks.
245 *
246 * @since 1.9.0
247 *
248 * @param string $output Gallery shortcode output.
249 *
250 * @return string Gallery shortcode output.
251 */
252 public function check_if_shortcode_processed_and_enqueue_assets( $output ) {
253 if (
254 class_exists( 'Jetpack_AMP_Support' )
255 && Jetpack_AMP_Support::is_amp_request()
256 ) {
257 return $output;
258 }
259
260 if (
261 ! empty( $output ) &&
262 /**
263 * Allow third-party plugins or themes to force-enable Carousel.
264 *
265 * @module carousel
266 *
267 * @since 1.9.0
268 *
269 * @param bool false Should we force enable Carousel? Default to false.
270 */
271 ! apply_filters( 'jp_carousel_force_enable', false )
272 ) {
273 // Bail because someone is overriding the [gallery] shortcode.
274 remove_filter( 'gallery_style', array( $this, 'add_data_to_container' ) );
275 remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'add_data_to_images' ) );
276 remove_filter( 'the_content', array( $this, 'add_data_img_tags_and_enqueue_assets' ) );
277 // Display message that carousel has bailed, if user is super_admin, and if we're not on WordPress.com.
278 if (
279 is_super_admin() &&
280 ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
281 ) {
282 add_filter( 'post_gallery', array( $this, 'display_bail_message' ) );
283 }
284 return $output;
285 }
286
287 /**
288 * Fires when thumbnails are shown in Carousel.
289 *
290 * @module carousel
291 *
292 * @since 1.6.0
293 */
294 do_action( 'jp_carousel_thumbnails_shown' );
295
296 $this->enqueue_assets();
297
298 return $output;
299 }
300
301 /**
302 * Check if the content of a post uses gallery blocks. To be used by 'the_content' filter.
303 *
304 * @since 6.8.0
305 * @deprecated since 11.3 We now hook into the 'block_render_{block_name}' hook to add markup.
306 *
307 * @param string $content Post content.
308 *
309 * @return string $content Post content.
310 */
311 public function check_content_for_blocks( $content ) {
312 _deprecated_function( __METHOD__, 'jetpack-11.3' );
313
314 if (
315 class_exists( 'Jetpack_AMP_Support' )
316 && Jetpack_AMP_Support::is_amp_request()
317 ) {
318 return $content;
319 }
320
321 if ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) {
322 $this->enqueue_assets();
323 $content = $this->add_data_to_container( $content );
324 }
325
326 return $content;
327 }
328
329 /**
330 * Enrich the gallery block content using the render_block_{$this->name} filter.
331 * This function is triggered after block render to make sure we track galleries within
332 * reusable blocks.
333 *
334 * @see https://developer.wordpress.org/reference/hooks/render_block_this-name/
335 *
336 * @param string $block_content The rendered HTML for the carousel or gallery block.
337 * @param array $block The parsed block details for the block.
338 * @return string The fully-processed HTML for the carousel or gallery block.
339 *
340 * @since 11.3
341 */
342 public function filter_gallery_block_render( $block_content, $block ) {
343 global $post;
344
345 if ( empty( $block['blockName'] ) || ! in_array( $block['blockName'], array( 'core/gallery', 'jetpack/tiled-gallery' ), true ) ) {
346 return $block_content;
347 }
348
349 $this->enqueue_assets();
350
351 if ( ! isset( $post ) ) {
352 return $block_content;
353 }
354
355 $blog_id = (int) get_current_blog_id();
356
357 $extra_data = array(
358 'data-carousel-extra' => array(
359 'blog_id' => $blog_id,
360 'permalink' => get_permalink( $post->ID ),
361 ),
362 );
363
364 /**
365 * Filter the data added to the Gallery container.
366 *
367 * @module carousel
368 *
369 * @since 1.6.0
370 *
371 * @param array $extra_data Array of data about the site and the post.
372 */
373 $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
374 $extra_data = (array) $extra_data;
375
376 if ( empty( $extra_data ) ) {
377 return $block_content;
378 }
379
380 $extra_attributes = implode(
381 ' ',
382 array_map(
383 function ( $data_key, $data_values ) {
384 return esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "'";
385 },
386 array_keys( $extra_data ),
387 array_values( $extra_data )
388 )
389 );
390
391 // Add extra attributes to first HTML element (which may have leading whitespace)
392 return preg_replace(
393 '/^(\s*<(div|ul|figure))/',
394 '$1 ' . $extra_attributes . ' ',
395 $block_content,
396 1
397 );
398 }
399
400 /**
401 * Enqueueing Carousel assets.
402 */
403 public function enqueue_assets() {
404 if ( $this->first_run ) {
405 wp_enqueue_script(
406 'jetpack-carousel',
407 Assets::get_file_url_for_environment(
408 '_inc/build/carousel/jetpack-carousel.min.js',
409 'modules/carousel/jetpack-carousel.js'
410 ),
411 array(),
412 $this->asset_version( JETPACK__VERSION ),
413 true
414 );
415
416 $swiper_library_path = array(
417 'url' => Assets::get_file_url_for_environment(
418 '_inc/build/carousel/swiper-bundle.min.js',
419 'modules/carousel/swiper-bundle.js'
420 ),
421 );
422 wp_localize_script( 'jetpack-carousel', 'jetpackSwiperLibraryPath', $swiper_library_path );
423
424 // 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).
425 // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
426 $is_logged_in = is_user_logged_in();
427 $comment_registration = (int) get_option( 'comment_registration' );
428 $require_name_email = (int) get_option( 'require_name_email' );
429 $localize_strings = array(
430 'widths' => $this->prebuilt_widths,
431 'is_logged_in' => $is_logged_in,
432 'lang' => strtolower( substr( get_locale(), 0, 2 ) ),
433 'ajaxurl' => set_url_scheme( admin_url( 'admin-ajax.php' ) ),
434 'nonce' => wp_create_nonce( 'carousel_nonce' ),
435 'display_exif' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_exif', true ) ),
436 'display_comments' => $this->test_1or0_option( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_display_comments', true ) ),
437 'single_image_gallery' => $this->single_image_gallery_enabled,
438 'single_image_gallery_media_file' => $this->single_image_gallery_enabled_media_file,
439 'background_color' => $this->carousel_background_color_sanitize( Jetpack_Options::get_option_and_ensure_autoload( 'carousel_background_color', '' ) ),
440 'comment' => __( 'Comment', 'jetpack' ),
441 'post_comment' => __( 'Post Comment', 'jetpack' ),
442 'write_comment' => __( 'Write a Comment...', 'jetpack' ),
443 'loading_comments' => __( 'Loading Comments...', 'jetpack' ),
444 'download_original' => sprintf(
445 /* translators: %1s is the full-size image width, and %2s is the height. */
446 __( 'View full size <span class="photo-size">%1$s<span class="photo-size-times">&times;</span>%2$s</span>', 'jetpack' ),
447 '{0}',
448 '{1}'
449 ),
450 'no_comment_text' => __( 'Please be sure to submit some text with your comment.', 'jetpack' ),
451 'no_comment_email' => __( 'Please provide an email address to comment.', 'jetpack' ),
452 'no_comment_author' => __( 'Please provide your name to comment.', 'jetpack' ),
453 'comment_post_error' => __( 'Sorry, but there was an error posting your comment. Please try again later.', 'jetpack' ),
454 'comment_approved' => __( 'Your comment was approved.', 'jetpack' ),
455 'comment_unapproved' => __( 'Your comment is in moderation.', 'jetpack' ),
456 'camera' => __( 'Camera', 'jetpack' ),
457 'aperture' => __( 'Aperture', 'jetpack' ),
458 'shutter_speed' => __( 'Shutter Speed', 'jetpack' ),
459 'focal_length' => __( 'Focal Length', 'jetpack' ),
460 'copyright' => __( 'Copyright', 'jetpack' ),
461 'comment_registration' => $comment_registration,
462 'require_name_email' => $require_name_email,
463 /** This action is documented in core/src/wp-includes/link-template.php */
464 'login_url' => wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ),
465 'blog_id' => (int) get_current_blog_id(),
466 'meta_data' => array( 'camera', 'aperture', 'shutter_speed', 'focal_length', 'copyright' ),
467 );
468
469 /**
470 * Handle WP stats for images in full-screen.
471 * Build string with tracking info.
472 */
473
474 /**
475 * Filter if Jetpack should enable stats collection on carousel views
476 *
477 * @module carousel
478 *
479 * @since 4.3.2
480 *
481 * @param bool Enable Jetpack Carousel stat collection. Default false.
482 */
483 if ( apply_filters( 'jetpack_enable_carousel_stats', false ) && in_array( 'stats', Jetpack::get_active_modules(), true ) && ! ( new Status() )->is_offline_mode() ) {
484 $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;
485
486 // Set the stats as empty if user is logged in but logged-in users shouldn't be tracked.
487 if ( is_user_logged_in() ) {
488 $stats_options = Stats_Options::get_options();
489 $track_loggedin_users = isset( $stats_options['count_roles'] ) ? (bool) $stats_options['count_roles'] : false;
490
491 if ( ! $track_loggedin_users ) {
492 $localize_strings['stats'] = '';
493 }
494 }
495 }
496
497 /**
498 * Filter the strings passed to the Carousel's js file.
499 *
500 * @module carousel
501 *
502 * @since 1.6.0
503 *
504 * @param array $localize_strings Array of strings passed to the Jetpack js file.
505 */
506 $localize_strings = apply_filters( 'jp_carousel_localize_strings', $localize_strings );
507 wp_localize_script( 'jetpack-carousel', 'jetpackCarouselStrings', $localize_strings );
508 wp_enqueue_style(
509 'jetpack-carousel-swiper-css',
510 plugins_url( 'swiper-bundle.css', __FILE__ ),
511 array(),
512 $this->asset_version( JETPACK__VERSION )
513 );
514 wp_enqueue_style( 'jetpack-carousel', plugins_url( 'jetpack-carousel.css', __FILE__ ), array(), $this->asset_version( JETPACK__VERSION ) );
515 wp_style_add_data( 'jetpack-carousel', 'rtl', 'replace' );
516
517 /**
518 * Fires after carousel assets are enqueued for the first time.
519 * Allows for adding additional assets to the carousel page.
520 *
521 * @module carousel
522 *
523 * @since 1.6.0
524 *
525 * @param bool $first_run First load if Carousel on the page.
526 * @param array $localized_strings Array of strings passed to the Jetpack js file.
527 */
528 do_action( 'jp_carousel_enqueue_assets', $this->first_run, $localize_strings );
529
530 // Add the carousel skeleton to the page.
531 $this->localize_strings = $localize_strings;
532 add_action( 'wp_footer', array( $this, 'add_carousel_skeleton' ) );
533
534 $this->first_run = false;
535 }
536 }
537
538 /**
539 * Generate the HTML skeleton that will be picked up by the Carousel JS and used for showing the carousel.
540 */
541 public function add_carousel_skeleton() {
542 $localize_strings = $this->localize_strings;
543 $is_light = ( 'white' === $localize_strings['background_color'] );
544 // Determine whether to fall back to standard local comments.
545 $use_local_comments = ! isset( $localize_strings['jetpack_comments_iframe_src'] ) || empty( $localize_strings['jetpack_comments_iframe_src'] );
546 $current_user = wp_get_current_user();
547 $require_name_email = (int) get_option( 'require_name_email' );
548 /* translators: %s is replaced with a field name in the form, e.g. "Email" */
549 $required = ( $require_name_email ) ? __( '%s (Required)', 'jetpack' ) : '%s';
550 ?>
551 <div id="jp-carousel-loading-overlay">
552 <div id="jp-carousel-loading-wrapper">
553 <span id="jp-carousel-library-loading">&nbsp;</span>
554 </div>
555 </div>
556 <div class="jp-carousel-overlay<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>" style="display: none;">
557
558 <div class="jp-carousel-container<?php echo( $is_light ? ' jp-carousel-light' : '' ); ?>">
559 <!-- The Carousel Swiper -->
560 <div
561 class="jp-carousel-wrap swiper-container jp-carousel-swiper-container jp-carousel-transitions"
562 itemscope
563 itemtype="https://schema.org/ImageGallery">
564 <div class="jp-carousel swiper-wrapper"></div>
565 <div class="jp-swiper-button-prev swiper-button-prev">
566 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
567 <mask id="maskPrev" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="9" height="12">
568 <path d="M16.2072 16.59L11.6496 12L16.2072 7.41L14.8041 6L8.8335 12L14.8041 18L16.2072 16.59Z" fill="white"/>
569 </mask>
570 <g mask="url(#maskPrev)">
571 <rect x="0.579102" width="23.8823" height="24" fill="#FFFFFF"/>
572 </g>
573 </svg>
574 </div>
575 <div class="jp-swiper-button-next swiper-button-next">
576 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
577 <mask id="maskNext" mask-type="alpha" maskUnits="userSpaceOnUse" x="8" y="6" width="8" height="12">
578 <path d="M8.59814 16.59L13.1557 12L8.59814 7.41L10.0012 6L15.9718 12L10.0012 18L8.59814 16.59Z" fill="white"/>
579 </mask>
580 <g mask="url(#maskNext)">
581 <rect x="0.34375" width="23.8822" height="24" fill="#FFFFFF"/>
582 </g>
583 </svg>
584 </div>
585 </div>
586 <!-- The main close buton -->
587 <div class="jp-carousel-close-hint">
588 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
589 <mask id="maskClose" mask-type="alpha" maskUnits="userSpaceOnUse" x="5" y="5" width="15" height="14">
590 <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"/>
591 </mask>
592 <g mask="url(#maskClose)">
593 <rect x="0.409668" width="23.8823" height="24" fill="#FFFFFF"/>
594 </g>
595 </svg>
596 </div>
597 <!-- Image info, comments and meta -->
598 <div class="jp-carousel-info">
599 <div class="jp-carousel-info-footer">
600 <div class="jp-carousel-pagination-container">
601 <div class="jp-swiper-pagination swiper-pagination"></div>
602 <div class="jp-carousel-pagination"></div>
603 </div>
604 <div class="jp-carousel-photo-title-container">
605 <h2 class="jp-carousel-photo-caption"></h2>
606 </div>
607 <div class="jp-carousel-photo-icons-container">
608 <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-info" aria-label="<?php esc_attr_e( 'Toggle photo metadata visibility', 'jetpack' ); ?>">
609 <span class="jp-carousel-icon">
610 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
611 <mask id="maskInfo" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
612 <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"/>
613 </mask>
614 <g mask="url(#maskInfo)">
615 <rect x="0.8125" width="23.8823" height="24" fill="#FFFFFF"/>
616 </g>
617 </svg>
618 </span>
619 </a>
620 <?php if ( $localize_strings['display_comments'] ) : ?>
621 <a href="#" class="jp-carousel-icon-btn jp-carousel-icon-comments" aria-label="<?php esc_attr_e( 'Toggle photo comments visibility', 'jetpack' ); ?>">
622 <span class="jp-carousel-icon">
623 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
624 <mask id="maskComments" mask-type="alpha" maskUnits="userSpaceOnUse" x="2" y="2" width="21" height="20">
625 <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"/>
626 </mask>
627 <g mask="url(#maskComments)">
628 <rect x="0.34668" width="23.8823" height="24" fill="#FFFFFF"/>
629 </g>
630 </svg>
631
632 <span class="jp-carousel-has-comments-indicator" aria-label="<?php esc_attr_e( 'This image has comments.', 'jetpack' ); ?>"></span>
633 </span>
634 </a>
635 <?php endif; ?>
636 </div>
637 </div>
638 <div class="jp-carousel-info-extra">
639 <div class="jp-carousel-info-content-wrapper">
640 <div class="jp-carousel-photo-title-container">
641 <h2 class="jp-carousel-photo-title"></h2>
642 </div>
643 <div class="jp-carousel-comments-wrapper">
644 <?php if ( $localize_strings['display_comments'] ) : ?>
645 <div id="jp-carousel-comments-loading">
646 <span><?php echo esc_html( $localize_strings['loading_comments'] ); ?></span>
647 </div>
648 <div class="jp-carousel-comments"></div>
649 <div id="jp-carousel-comment-form-container">
650 <span id="jp-carousel-comment-form-spinner">&nbsp;</span>
651 <div id="jp-carousel-comment-post-results"></div>
652 <?php if ( $use_local_comments ) : ?>
653 <?php if ( ! $localize_strings['is_logged_in'] && $localize_strings['comment_registration'] ) : ?>
654 <div id="jp-carousel-comment-form-commenting-as">
655 <p id="jp-carousel-commenting-as">
656 <?php
657 echo wp_kses(
658 __( 'You must be <a href="#" class="jp-carousel-comment-login">logged in</a> to post a comment.', 'jetpack' ),
659 array(
660 'a' => array(
661 'href' => array(),
662 'class' => array(),
663 ),
664 )
665 );
666 ?>
667 </p>
668 </div>
669 <?php else : ?>
670 <form id="jp-carousel-comment-form">
671 <label for="jp-carousel-comment-form-comment-field" class="screen-reader-text"><?php echo esc_attr( $localize_strings['write_comment'] ); ?></label>
672 <textarea
673 name="comment"
674 class="jp-carousel-comment-form-field jp-carousel-comment-form-textarea"
675 id="jp-carousel-comment-form-comment-field"
676 placeholder="<?php echo esc_attr( $localize_strings['write_comment'] ); ?>"
677 ></textarea>
678 <div id="jp-carousel-comment-form-submit-and-info-wrapper">
679 <div id="jp-carousel-comment-form-commenting-as">
680 <?php if ( $localize_strings['is_logged_in'] ) : ?>
681 <p id="jp-carousel-commenting-as">
682 <?php
683 printf(
684 /* translators: %s is replaced with the user's display name */
685 esc_html__( 'Commenting as %s', 'jetpack' ),
686 esc_html( $current_user->data->display_name )
687 );
688 ?>
689 </p>
690 <?php else : ?>
691 <fieldset>
692 <label for="jp-carousel-comment-form-email-field"><?php echo esc_html( sprintf( $required, __( 'Email', 'jetpack' ) ) ); ?></label>
693 <input type="text" name="email" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-email-field" />
694 </fieldset>
695 <fieldset>
696 <label for="jp-carousel-comment-form-author-field"><?php echo esc_html( sprintf( $required, __( 'Name', 'jetpack' ) ) ); ?></label>
697 <input type="text" name="author" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-author-field" />
698 </fieldset>
699 <fieldset>
700 <label for="jp-carousel-comment-form-url-field"><?php esc_html_e( 'Website', 'jetpack' ); ?></label>
701 <input type="text" name="url" class="jp-carousel-comment-form-field jp-carousel-comment-form-text-field" id="jp-carousel-comment-form-url-field" />
702 </fieldset>
703 <?php endif ?>
704 </div>
705 <input
706 type="submit"
707 name="submit"
708 class="jp-carousel-comment-form-button"
709 id="jp-carousel-comment-form-button-submit"
710 value="<?php echo esc_attr( $localize_strings['post_comment'] ); ?>" />
711 </div>
712 </form>
713 <?php endif ?>
714 <?php endif ?>
715 </div>
716 <?php endif ?>
717 </div>
718 <div class="jp-carousel-image-meta">
719 <div class="jp-carousel-title-and-caption">
720 <div class="jp-carousel-photo-info">
721 <h3 class="jp-carousel-caption" itemprop="caption description"></h3>
722 </div>
723
724 <div class="jp-carousel-photo-description"></div>
725 </div>
726 <ul class="jp-carousel-image-exif" style="display: none;"></ul>
727 <a class="jp-carousel-image-download" target="_blank" style="display: none;">
728 <svg width="25" height="24" viewBox="0 0 25 24" fill="none" xmlns="http://www.w3.org/2000/svg">
729 <mask id="mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="3" y="3" width="19" height="18">
730 <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"/>
731 </mask>
732 <g mask="url(#mask0)">
733 <rect x="0.870605" width="23.8823" height="24" fill="#FFFFFF"/>
734 </g>
735 </svg>
736 <span class="jp-carousel-download-text"></span>
737 </a>
738 <div class="jp-carousel-image-map" style="display: none;"></div>
739 </div>
740 </div>
741 </div>
742 </div>
743 </div>
744
745 </div>
746 <?php
747 }
748
749 /**
750 * Sets the "in_gallery" flag when the first gallery is encountered (unless in AMP mode).
751 *
752 * @deprecated since 10.8
753 *
754 * @param string $output Gallery shortcode output. Passed through unchanged.
755 *
756 * @return string
757 */
758 public function set_in_gallery( $output ) {
759 if (
760 class_exists( 'Jetpack_AMP_Support' )
761 && Jetpack_AMP_Support::is_amp_request()
762 ) {
763 return $output;
764 }
765 $this->in_gallery = true;
766 return $output;
767 }
768
769 /**
770 * Adds data-* attributes required by carousel to img tags in post HTML
771 * content. To be used by 'the_content' filter.
772 *
773 * @see add_data_to_images()
774 * @see wp_make_content_images_responsive() in wp-includes/media.php
775 *
776 * @param string $content HTML content of the post.
777 * @return string
778 */
779 public function add_data_img_tags_and_enqueue_assets( $content ) {
780 if (
781 class_exists( 'Jetpack_AMP_Support' )
782 && Jetpack_AMP_Support::is_amp_request()
783 ) {
784 return $this->maybe_add_amp_lightbox( $content );
785 }
786
787 if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
788 return $content;
789 }
790 $selected_images = array();
791 foreach ( $matches[0] as $image_html ) {
792 if (
793 preg_match( '/(wp-image-|data-id=)\"?([0-9]+)\"?/i', $image_html, $class_id )
794 && ! preg_match( '/wp-block-jetpack-slideshow_image/', $image_html )
795 ) {
796 /**
797 * Allow filtering the attachment ID used to fetch and populate metadata about an image in a gallery.
798 *
799 * @module carousel
800 *
801 * @since 12.6
802 *
803 * @param int $attachment_id Attachment ID pulled from image HTML.
804 * @param string $image_html Full HTML image tag.
805 */
806 $attachment_id = absint(
807 apply_filters(
808 'jetpack_carousel_image_attachment_id',
809 $class_id[2],
810 $image_html
811 )
812 );
813
814 /**
815 * The same image tag may be used more than once but with different attribs,
816 * so save each of them against the attachment id.
817 */
818 if ( ! isset( $selected_images[ $attachment_id ] ) || ! in_array( $image_html, $selected_images[ $attachment_id ], true ) ) {
819 $selected_images[ $attachment_id ][] = $image_html;
820 }
821 }
822 }
823
824 $find = array();
825 $replace = array();
826 if ( empty( $selected_images ) ) {
827 return $content;
828 }
829
830 $attachments = get_posts(
831 array(
832 'include' => array_keys( $selected_images ),
833 'post_type' => 'any',
834 'post_status' => 'any',
835 'suppress_filters' => false,
836 )
837 );
838
839 foreach ( $attachments as $attachment ) {
840 /*
841 * If the item from get_posts isn't an attachment, skip. This can occur when copy-pasta from another WP site.
842 * 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" />"
843 * then, we're going to look up post 7 below, which making sure it is an attachment.
844 *
845 * This is meant as a relatively quick fix, as a better fix is likely to update the get_posts call above to only
846 * include attachments.
847 */
848 if ( ! isset( $attachment->ID ) || ! wp_attachment_is_image( $attachment->ID ) ) {
849 continue;
850 }
851 $image_elements = $selected_images[ $attachment->ID ];
852
853 if ( ! is_array( $image_elements ) ) {
854 continue;
855 }
856
857 $attributes = $this->add_data_to_images( array(), $attachment );
858 $attributes_html = '';
859 foreach ( $attributes as $k => $v ) {
860 $attributes_html .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" ';
861 }
862 foreach ( $image_elements as $image_html ) {
863 $find[] = $image_html;
864 $replace[] = str_replace( '<img ', "<img $attributes_html", $image_html );
865 }
866 }
867
868 $content = str_replace( $find, $replace, $content );
869 $this->enqueue_assets();
870 return $content;
871 }
872
873 /**
874 * Adds the data attributes themselves to img tags.
875 *
876 * @see add_data_img_tags_and_enqueue_assets()
877 * @see https://developer.wordpress.org/reference/functions/wp_get_attachment_image/ Documentation about wp_get_attachment_image
878 *
879 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
880 * @param WP_Post $attachment Image attachment post.
881 *
882 * @return string[] Modified image attributes.
883 */
884 public function add_data_to_images( $attr, $attachment = null ) {
885 if (
886 class_exists( 'Jetpack_AMP_Support' )
887 && Jetpack_AMP_Support::is_amp_request()
888 ) {
889 return $attr;
890 }
891
892 $attachment_id = (int) $attachment->ID;
893 if ( ! wp_attachment_is_image( $attachment_id ) ) {
894 return $attr;
895 }
896
897 $orig_file = wp_get_attachment_image_src( $attachment_id, 'full' );
898 $orig_file = isset( $orig_file[0] ) ? $orig_file[0] : wp_get_attachment_url( $attachment_id );
899 $meta = wp_get_attachment_metadata( $attachment_id );
900 $size = isset( $meta['width'] ) ? (int) $meta['width'] . ',' . (int) $meta['height'] : '';
901 $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
902 $comments_opened = (int) comments_open( $attachment_id );
903
904 /**
905 * Note: Cannot generate a filename from the width and height wp_get_attachment_image_src() returns because
906 * it takes the $content_width global variable themes can set in consideration, therefore returning sizes
907 * which when used to generate a filename will likely result in a 404 on the image.
908 * $content_width has no filter we could temporarily de-register, run wp_get_attachment_image_src(), then
909 * re-register. So using returned file URL instead, which we can define the sizes from through filename
910 * parsing in the JS, as this is a failsafe file reference.
911 *
912 * EG with Twenty Eleven activated:
913 * 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) }
914 *
915 * EG with Twenty Ten activated:
916 * 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) }
917 */
918
919 $medium_file_info = wp_get_attachment_image_src( $attachment_id, 'medium' );
920 $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
921
922 $large_file_info = wp_get_attachment_image_src( $attachment_id, 'large' );
923 $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
924
925 $attachment = get_post( $attachment_id );
926 $attachment_title = ! empty( $attachment ) ? wptexturize( $attachment->post_title ) : '';
927 $attachment_desc = ! empty( $attachment ) ? wpautop( wptexturize( $attachment->post_content ) ) : '';
928 $attachment_caption = ! empty( $attachment ) ? wpautop( wptexturize( $attachment->post_excerpt ) ) : '';
929
930 // See https://github.com/Automattic/jetpack/issues/2765.
931 if ( isset( $img_meta['keywords'] ) ) {
932 unset( $img_meta['keywords'] );
933 }
934
935 $img_meta = wp_json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ) );
936
937 $attr['data-attachment-id'] = $attachment_id;
938 $attr['data-permalink'] = esc_attr( get_permalink( $attachment_id ) );
939 $attr['data-orig-file'] = esc_attr( $orig_file );
940 $attr['data-orig-size'] = $size;
941 $attr['data-comments-opened'] = $comments_opened;
942 $attr['data-image-meta'] = esc_attr( $img_meta );
943 // 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.
944 $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title, ENT_COMPAT ) );
945 $attr['data-image-description'] = esc_attr( htmlspecialchars( $attachment_desc, ENT_COMPAT ) );
946 $attr['data-image-caption'] = esc_attr( htmlspecialchars( $attachment_caption, ENT_COMPAT ) );
947 $attr['data-medium-file'] = esc_attr( $medium_file );
948 $attr['data-large-file'] = esc_attr( $large_file );
949
950 return $attr;
951 }
952
953 /**
954 * Add additional attributes to the Gallery container HTML.
955 *
956 * @param string $html The HTML to which the additional attributes are added.
957 *
958 * @return string
959 */
960 public function add_data_to_container( $html ) {
961 global $post;
962 if (
963 class_exists( 'Jetpack_AMP_Support' )
964 && Jetpack_AMP_Support::is_amp_request()
965 ) {
966 return $html;
967 }
968
969 if ( isset( $post ) ) {
970 $blog_id = (int) get_current_blog_id();
971
972 $extra_data = array(
973 'data-carousel-extra' => array(
974 'blog_id' => $blog_id,
975 'permalink' => get_permalink( $post->ID ),
976 ),
977 );
978
979 /**
980 * Filter the data added to the Gallery container.
981 *
982 * @module carousel
983 *
984 * @since 1.6.0
985 *
986 * @param array $extra_data Array of data about the site and the post.
987 */
988 $extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
989 foreach ( (array) $extra_data as $data_key => $data_values ) {
990 $html = str_replace( '<div ', '<div ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' ", $html );
991 $html = str_replace( '<ul class="wp-block-gallery', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"wp-block-gallery", $html );
992 $html = str_replace( '<ul class="blocks-gallery-grid', '<ul ' . esc_attr( $data_key ) . "='" . wp_json_encode( $data_values ) . "' class=\"blocks-gallery-grid", $html );
993 $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 );
994 }
995 }
996
997 return $html;
998 }
999
1000 /**
1001 * Conditionally adds amp-lightbox to galleries and images.
1002 *
1003 * This applies to gallery blocks and shortcodes,
1004 * in addition to images that are wrapped in a link to the page.
1005 * Images wrapped in a link to the media file shouldn't get an amp-lightbox.
1006 *
1007 * @param string $content The content to possibly add amp-lightbox to.
1008 * @return string The content, with amp-lightbox possibly added.
1009 */
1010 public function maybe_add_amp_lightbox( $content ) {
1011 $content = preg_replace(
1012 array(
1013 '#(<figure)[^>]*(?=class=(["\']?)[^>]*wp-block-gallery[^>]*\2)#is', // Gallery block.
1014 '#(\[gallery)(?=\s+)#', // Gallery shortcode.
1015 ),
1016 array(
1017 '\1 data-amp-lightbox="true" ', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/sanitizers/class-amp-gallery-block-sanitizer.php#L84.
1018 '\1 amp-lightbox="true"', // https://github.com/ampproject/amp-wp/blob/1094ea03bd5dc92889405a47a8c41de1a88908de/includes/embeds/class-amp-gallery-embed.php#L64.
1019 ),
1020 $content
1021 );
1022
1023 return preg_replace_callback(
1024 '#(<a[^>]* href=(["\']?)(\S+)\2>)\s*(<img[^>]*)(class=(["\']?)[^>]*wp-image-[0-9]+[^>]*\6.*>)\s*</a>#is',
1025 static function ( $matches ) {
1026 if ( ! preg_match( '#\.\w+$#', $matches[3] ) ) {
1027 // 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.
1028 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.
1029 }
1030
1031 return $matches[0];
1032 },
1033 $content
1034 );
1035 }
1036
1037 /**
1038 * Retrieves comment information
1039 *
1040 * @return string
1041 */
1042 public function get_attachment_comments() {
1043 if ( ! headers_sent() ) {
1044 header( 'Content-type: text/javascript' );
1045 }
1046
1047 /**
1048 * Allows for the checking of privileges of the blog user before comments
1049 * are packaged as JSON and sent back from the get_attachment_comments
1050 * AJAX endpoint
1051 *
1052 * @module carousel
1053 *
1054 * @since 1.6.0
1055 */
1056 do_action( 'jp_carousel_check_blog_user_privileges' );
1057
1058 // 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).
1059 $attachment_id = ( isset( $_REQUEST['id'] ) ) ? (int) $_REQUEST['id'] : 0;
1060 $offset = ( isset( $_REQUEST['offset'] ) ) ? (int) $_REQUEST['offset'] : 0;
1061 // phpcs:enable
1062
1063 if ( ! $attachment_id ) {
1064 wp_send_json_error(
1065 __( 'Missing attachment ID.', 'jetpack' ),
1066 403
1067 );
1068 return;
1069 }
1070
1071 $attachment_post = get_post( $attachment_id );
1072 // If we have no info about that attachment, bail.
1073 if ( ! ( $attachment_post instanceof WP_Post ) ) {
1074 wp_send_json_error(
1075 __( 'Missing attachment info.', 'jetpack' ),
1076 403
1077 );
1078 return;
1079 }
1080
1081 // This AJAX call should only be used to fetch comments of attachments.
1082 if ( 'attachment' !== $attachment_post->post_type ) {
1083 wp_send_json_error(
1084 __( 'You aren’t authorized to do that.', 'jetpack' ),
1085 403
1086 );
1087 return;
1088 }
1089
1090 $parent_post = get_post_parent( $attachment_id );
1091
1092 /*
1093 * If we have no info about that parent post, no extra checks.
1094 * The attachment doesn't have a parent post, so is public.
1095 * If we have a parent post, let's ensure the user has access to it.
1096 */
1097 if ( $parent_post instanceof WP_Post ) {
1098 /*
1099 * Fetch info about user making the request.
1100 * If we have no info, bail.
1101 * Even logged out users should get a WP_User user with id 0.
1102 */
1103 $current_user = wp_get_current_user();
1104 if ( ! ( $current_user instanceof WP_User ) ) {
1105 wp_send_json_error(
1106 __( 'Missing user info.', 'jetpack' ),
1107 403
1108 );
1109 return;
1110 }
1111
1112 /*
1113 * If a post is private / draft
1114 * and the current user doesn't have access to it,
1115 * bail.
1116 */
1117 if (
1118 'publish' !== $parent_post->post_status
1119 && ! current_user_can( 'read_post', $parent_post->ID )
1120 ) {
1121 wp_send_json_error(
1122 __( 'You aren’t authorized to do that.', 'jetpack' ),
1123 403
1124 );
1125 return;
1126 }
1127 }
1128
1129 if ( $offset < 1 ) {
1130 $offset = 0;
1131 }
1132
1133 $comments = get_comments(
1134 array(
1135 'status' => 'approve',
1136 'order' => ( 'asc' === get_option( 'comment_order' ) ) ? 'ASC' : 'DESC',
1137 'number' => 10,
1138 'offset' => $offset,
1139 'post_id' => $attachment_id,
1140 )
1141 );
1142
1143 $out = array();
1144
1145 // Can't just send the results, they contain the commenter's email address.
1146 foreach ( $comments as $comment ) {
1147 $avatar = get_avatar( $comment->comment_author_email, 64 );
1148 if ( ! $avatar ) {
1149 $avatar = '';
1150 }
1151 $out[] = array(
1152 'id' => $comment->comment_ID,
1153 'parent_id' => $comment->comment_parent,
1154 'author_markup' => get_comment_author_link( $comment->comment_ID ),
1155 'gravatar_markup' => $avatar,
1156 'date_gmt' => $comment->comment_date_gmt,
1157 'content' => wpautop( $comment->comment_content ),
1158 );
1159 }
1160
1161 die( wp_json_encode( $out ) );
1162 }
1163
1164 /**
1165 * Adds a new comment to the database
1166 */
1167 public function post_attachment_comment() {
1168 if ( ! headers_sent() ) {
1169 header( 'Content-type: text/javascript' );
1170 }
1171
1172 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
1173 die( wp_json_encode( array( 'error' => __( 'Nonce verification failed.', 'jetpack' ) ) ) );
1174 }
1175
1176 $_blog_id = isset( $_POST['blog_id'] ) ? (int) $_POST['blog_id'] : 0;
1177 $_post_id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
1178 $comment = isset( $_POST['comment'] ) ? filter_var( wp_unslash( $_POST['comment'] ) ) : null;
1179
1180 if ( empty( $_blog_id ) ) {
1181 die( wp_json_encode( array( 'error' => __( 'Missing target blog ID.', 'jetpack' ) ) ) );
1182 }
1183
1184 if ( empty( $_post_id ) ) {
1185 die( wp_json_encode( array( 'error' => __( 'Missing target post ID.', 'jetpack' ) ) ) );
1186 }
1187
1188 if ( empty( $comment ) ) {
1189 die( wp_json_encode( array( 'error' => __( 'No comment text was submitted.', 'jetpack' ) ) ) );
1190 }
1191
1192 // Used in context like NewDash.
1193 $switched = false;
1194 if ( is_multisite() && get_current_blog_id() !== $_blog_id ) {
1195 switch_to_blog( $_blog_id );
1196 $switched = true;
1197 }
1198
1199 /** This action is documented in modules/carousel/jetpack-carousel.php */
1200 do_action( 'jp_carousel_check_blog_user_privileges' );
1201
1202 if ( ! comments_open( $_post_id ) ) {
1203 if ( $switched ) {
1204 restore_current_blog();
1205 }
1206 die( wp_json_encode( array( 'error' => __( 'Comments on this post are closed.', 'jetpack' ) ) ) );
1207 }
1208
1209 if ( is_user_logged_in() ) {
1210 $user = wp_get_current_user();
1211 $user_id = $user->ID;
1212 $display_name = $user->display_name;
1213 $email = $user->user_email;
1214 $url = $user->user_url;
1215
1216 if ( empty( $user_id ) ) {
1217 if ( $switched ) {
1218 restore_current_blog();
1219 }
1220 die( wp_json_encode( array( 'error' => __( 'Sorry, but we could not authenticate your request.', 'jetpack' ) ) ) );
1221 }
1222 } else {
1223 $user_id = 0;
1224 $display_name = isset( $_POST['author'] ) ? sanitize_text_field( wp_unslash( $_POST['author'] ) ) : null;
1225 $email = isset( $_POST['email'] ) ? wp_unslash( $_POST['email'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Checked or sanitized below.
1226 $url = isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : null;
1227
1228 if ( get_option( 'require_name_email' ) ) {
1229 if ( empty( $display_name ) ) {
1230 if ( $switched ) {
1231 restore_current_blog();
1232 }
1233 die( wp_json_encode( array( 'error' => __( 'Please provide your name.', 'jetpack' ) ) ) );
1234 }
1235
1236 if ( empty( $email ) ) {
1237 if ( $switched ) {
1238 restore_current_blog();
1239 }
1240 die( wp_json_encode( array( 'error' => __( 'Please provide an email address.', 'jetpack' ) ) ) );
1241 }
1242
1243 if ( ! is_email( $email ) ) {
1244 if ( $switched ) {
1245 restore_current_blog();
1246 }
1247 die( wp_json_encode( array( 'error' => __( 'Please provide a valid email address.', 'jetpack' ) ) ) );
1248 }
1249 } else {
1250 $email = $email !== null ? sanitize_email( $email ) : null;
1251 }
1252 }
1253
1254 $comment_data = array(
1255 'comment_content' => $comment,
1256 'comment_post_ID' => $_post_id,
1257 'comment_author' => $display_name,
1258 'comment_author_email' => $email,
1259 'comment_author_url' => $url,
1260 'comment_approved' => 0,
1261 'comment_type' => 'comment',
1262 );
1263
1264 if ( ! empty( $user_id ) ) {
1265 $comment_data['user_id'] = $user_id;
1266 }
1267
1268 // Note: wp_new_comment() sanitizes and validates the values (too).
1269 $comment_id = wp_new_comment( $comment_data );
1270
1271 /**
1272 * Fires before adding a new comment to the database via the get_attachment_comments ajax endpoint.
1273 *
1274 * @module carousel
1275 *
1276 * @since 1.6.0
1277 */
1278 do_action( 'jp_carousel_post_attachment_comment' );
1279 $comment_status = wp_get_comment_status( $comment_id );
1280
1281 if ( $switched ) {
1282 restore_current_blog();
1283 }
1284
1285 die(
1286 wp_json_encode(
1287 array(
1288 'comment_id' => $comment_id,
1289 'comment_status' => $comment_status,
1290 )
1291 )
1292 );
1293 }
1294
1295 /**
1296 * Register Carousel settings
1297 */
1298 public function register_settings() {
1299 add_settings_section( 'carousel_section', __( 'Image Gallery Carousel', 'jetpack' ), array( $this, 'carousel_section_callback' ), 'media' );
1300
1301 if ( ! $this->in_jetpack ) {
1302 add_settings_field( 'carousel_enable_it', __( 'Enable carousel', 'jetpack' ), array( $this, 'carousel_enable_it_callback' ), 'media', 'carousel_section' );
1303 register_setting( 'media', 'carousel_enable_it', array( $this, 'carousel_enable_it_sanitize' ) );
1304 }
1305
1306 add_settings_field( 'carousel_background_color', __( 'Background color', 'jetpack' ), array( $this, 'carousel_background_color_callback' ), 'media', 'carousel_section' );
1307 register_setting( 'media', 'carousel_background_color', array( $this, 'carousel_background_color_sanitize' ) );
1308
1309 add_settings_field( 'carousel_display_exif', __( 'Metadata', 'jetpack' ), array( $this, 'carousel_display_exif_callback' ), 'media', 'carousel_section' );
1310 register_setting( 'media', 'carousel_display_exif', array( $this, 'carousel_display_exif_sanitize' ) );
1311
1312 add_settings_field( 'carousel_display_comments', __( 'Comments', 'jetpack' ), array( $this, 'carousel_display_comments_callback' ), 'media', 'carousel_section' );
1313 register_setting( 'media', 'carousel_display_comments', array( $this, 'carousel_display_comments_sanitize' ) );
1314 }
1315
1316 /**
1317 * Fulfill the settings section callback requirement by returning nothing.
1318 */
1319 public function carousel_section_callback() {
1320 }
1321
1322 /**
1323 * Tests if a value is set
1324 *
1325 * @param mixed $value The value passed into this function with which to test.
1326 * @param bool $default_to_1 Default is true.
1327 *
1328 * @return bool
1329 */
1330 public function test_1or0_option( $value, $default_to_1 = true ) {
1331 if ( $default_to_1 ) {
1332 // Boolean false (===) of $value means it has not yet been set, in which case we do want to default to 1.
1333 if ( false === $value ) {
1334 $value = 1;
1335 }
1336 }
1337 return ( 1 == $value ) ? 1 : 0; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
1338 }
1339
1340 /**
1341 * Ensures the value returned is in the correct format.
1342 *
1343 * @see test_1or0_option()
1344 * @param mixed $value The value returned from the test_1or0_option function.
1345 *
1346 * @return int
1347 */
1348 public function sanitize_1or0_option( $value ) {
1349 return ( 1 == $value ) ? 1 : 0; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
1350 }
1351
1352 /**
1353 * Outputs a settings checkbox.
1354 *
1355 * @param string $name - For name attribute.
1356 * @param string $label_text - For label attribute.
1357 * @param string $extra_text - Additional checkbox description text. Defaults to empty.
1358 * @param bool $default_to_checked - If the checkbox is checked. Default is true.
1359 */
1360 public function settings_checkbox( $name, $label_text, $extra_text = '', $default_to_checked = true ) {
1361 if ( empty( $name ) ) {
1362 return;
1363 }
1364 $option = $this->test_1or0_option( get_option( $name ), $default_to_checked );
1365 echo '<fieldset>';
1366 echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '" value="1" ';
1367 checked( '1', $option );
1368 echo '/> <label for="' . esc_attr( $name ) . '">' . wp_kses_post( $label_text ) . '</label>';
1369 if ( ! empty( $extra_text ) ) {
1370 echo '<p class="description">' . wp_kses_post( $extra_text ) . '</p>';
1371 }
1372 echo '</fieldset>';
1373 }
1374
1375 /**
1376 * Output a selection list options
1377 *
1378 * @param string $name - For name attribute.
1379 * @param string $values - For the different option values.
1380 * @param string $extra_text - Additional option section description text. Defaults to empty.
1381 */
1382 public function settings_select( $name, $values, $extra_text = '' ) {
1383 if ( empty( $name ) || ! is_array( $values ) || empty( $values ) ) {
1384 return;
1385 }
1386 $option = get_option( $name );
1387 echo '<fieldset>';
1388 echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $name ) . '">';
1389 foreach ( $values as $key => $value ) {
1390 echo '<option value="' . esc_attr( $key ) . '" ';
1391 selected( $key, $option );
1392 echo '>' . esc_html( $value ) . '</option>';
1393 }
1394 echo '</select>';
1395 if ( ! empty( $extra_text ) ) {
1396 echo '<p class="description">' . wp_kses_post( $extra_text ) . '</p>';
1397 }
1398 echo '</fieldset>';
1399 }
1400
1401 /**
1402 * Callback for checkbox and label of field that allows to toggle exif display.
1403 */
1404 public function carousel_display_exif_callback() {
1405 $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' ) );
1406 }
1407
1408 /**
1409 * Callback for checkbox and label of field that allows to toggle comments.
1410 */
1411 public function carousel_display_comments_callback() {
1412 $this->settings_checkbox( 'carousel_display_comments', esc_html__( 'Show comments area in carousel', 'jetpack' ) );
1413 }
1414
1415 /**
1416 * Sanitize input for the `carousel_display_exif` setting.
1417 *
1418 * @param mixed $value User input setting value.
1419 *
1420 * @return number Sanitized value, only 1 or 0.
1421 */
1422 public function carousel_display_exif_sanitize( $value ) {
1423 return $this->sanitize_1or0_option( $value );
1424 }
1425
1426 /**
1427 * Return sanitized option for value that controls whether comments will be hidden or not.
1428 *
1429 * @param number $value Value to sanitize.
1430 *
1431 * @return number Sanitized value, only 1 or 0.
1432 */
1433 public function carousel_display_comments_sanitize( $value ) {
1434 return $this->sanitize_1or0_option( $value );
1435 }
1436
1437 /**
1438 * Callback for the Carousel background color.
1439 */
1440 public function carousel_background_color_callback() {
1441 $this->settings_select(
1442 'carousel_background_color',
1443 array(
1444 'black' => __( 'Black', 'jetpack' ),
1445 'white' => __( 'White', 'jetpack' ),
1446 )
1447 );
1448 }
1449
1450 /**
1451 * Sanitizing the Carousel backgound color selection.
1452 *
1453 * @param string $value The color string to sanitize.
1454 *
1455 * @return string Sanitized value, 'white' or 'black'.
1456 */
1457 public function carousel_background_color_sanitize( $value ) {
1458 return ( 'white' === $value ) ? 'white' : 'black';
1459 }
1460
1461 /**
1462 * Callback to display text for the carousel_enable_it settings field.
1463 */
1464 public function carousel_enable_it_callback() {
1465 $this->settings_checkbox( 'carousel_enable_it', __( 'Display images in full-size carousel slideshow.', 'jetpack' ) );
1466 }
1467
1468 /**
1469 * Sanitize input for the `carousel_enable_it` setting.
1470 *
1471 * @param mixed $value User input.
1472 *
1473 * @return number Sanitized value, only 1 or 0.
1474 */
1475 public function carousel_enable_it_sanitize( $value ) {
1476 return $this->sanitize_1or0_option( $value );
1477 }
1478 }
1479
1480 new Jetpack_Carousel();
1481