| @@ -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 ) ); |