PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 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 All 506 releases
← All changes | modules/carousel/jetpack-carousel.php +45 -2 16.2 → 16.3-a.7 View file →
@@ -454,8 +454,9 @@
454 454 $swiper_library_path = array(
455 455 'url' => plugins_url( '_inc/blocks/swiper.js', JETPACK__PLUGIN_FILE ),
456 456 );
457 457 wp_localize_script( 'jetpack-carousel', 'jetpackSwiperLibraryPath', $swiper_library_path );
458 + add_action( 'wp_footer', array( $this, 'prefetch_swiper_library' ) );
458 459
459 460 // 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).
460 461 // Also: not hardcoding path since there is no guarantee site is running on site root in self-hosted context.
461 462 $is_logged_in = is_user_logged_in();
@@ -571,8 +572,24 @@
571 572 }
572 573 }
573 574
574 575 /**
576 + * Hint the browser to fetch the Swiper library while it is idle.
577 + *
578 + * Swiper is only requested when the lightbox is first opened, which puts a network
579 + * round trip in front of that first click. This is deliberately `prefetch` rather than
580 + * `preload`: most visitors never open the lightbox, so the fetch must stay at low
581 + * priority and out of the way of the page's own images. `loadSwiper()` still loads the
582 + * library on demand, since a prefetch is a hint the browser is free to ignore.
583 + */
584 + public function prefetch_swiper_library() {
585 + printf(
586 + '<link rel="prefetch" href="%s" as="script" />' . "\n",
587 + esc_url( plugins_url( '_inc/blocks/swiper.js', JETPACK__PLUGIN_FILE ) )
588 + );
589 + }
590 +
591 + /**
575 592 * Generate the HTML skeleton that will be picked up by the Carousel JS and used for showing the carousel.
576 593 */
577 594 public function add_carousel_skeleton() {
578 595 $localize_strings = $this->localize_strings;
@@ -984,8 +1001,18 @@
984 1001 $attr['data-orig-file'] = esc_attr( $orig_file );
985 1002 $attr['data-orig-size'] = $size;
986 1003 $attr['data-comments-opened'] = $comments_opened;
987 1004
1005 + /*
1006 + Lets the Carousel show its "has comments" badge without fetching the comments
1007 + themselves. Omitted when there are none, which is the common case, so galleries
1008 + without comments pay nothing for it.
1009 + */
1010 + $comments_count = (int) $attachment->comment_count;
1011 + if ( $comments_count > 0 ) {
1012 + $attr['data-comments-count'] = $comments_count;
1013 + }
1014 +
988 1015 if ( $display_exif ) {
989 1016 // See https://github.com/Automattic/jetpack/issues/2765.
990 1017 if ( isset( $img_meta['keywords'] ) ) {
991 1018 unset( $img_meta['keywords'] );
@@ -990,10 +1017,26 @@
990 1017 if ( isset( $img_meta['keywords'] ) ) {
991 1018 unset( $img_meta['keywords'] );
992 1019 }
993 1020
994 - $img_meta = wp_json_encode( array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ), JSON_UNESCAPED_SLASHES | JSON_HEX_AMP );
995 - $attr['data-image-meta'] = esc_attr( $img_meta );
1021 + /*
1022 + Filtering on `is_scalar` alone kept every "" and "0" in the metadata array, which
1023 + on a typical photo is most of it. The carousel skips those values when it renders
1024 + the EXIF panel anyway, so serialising them only inflates the page. Mirror that
1025 + check here: drop empties and numeric zeroes, but keep text that merely casts to
1026 + zero, such as a camera name.
1027 + */
1028 + $img_meta = array_filter(
1029 + array_map( 'strval', array_filter( $img_meta, 'is_scalar' ) ),
1030 + function ( $value ) {
1031 + return '' !== $value && ! ( is_numeric( $value ) && 0.0 === (float) $value );
1032 + }
1033 + );
1034 +
1035 + // With nothing left to show, the attribute itself is dead weight.
1036 + if ( ! empty( $img_meta ) ) {
1037 + $attr['data-image-meta'] = esc_attr( wp_json_encode( $img_meta, JSON_UNESCAPED_SLASHES | JSON_HEX_AMP ) );
1038 + }
996 1039 }
997 1040
998 1041 // 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.
999 1042 $attr['data-image-title'] = esc_attr( htmlspecialchars( $attachment_title, ENT_COMPAT ) );