diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2016-06-22 20:52:57 -0400 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2016-06-22 20:52:57 -0400 |
commit | f7f019456122473c53bd3661b910e09fe4fbc92a (patch) | |
tree | 7349a3b80b9ea5e7bfbc9039926dfba34eba8bfb /plugins/jetpack/class.photon.php | |
parent | Update plugin wordpress-mobile-pack to 2.2.4 (diff) | |
download | blogs-gentoo-f7f019456122473c53bd3661b910e09fe4fbc92a.tar.gz blogs-gentoo-f7f019456122473c53bd3661b910e09fe4fbc92a.tar.bz2 blogs-gentoo-f7f019456122473c53bd3661b910e09fe4fbc92a.zip |
Update plugin jecpack to 4.0.4
Diffstat (limited to 'plugins/jetpack/class.photon.php')
-rw-r--r-- | plugins/jetpack/class.photon.php | 104 |
1 files changed, 103 insertions, 1 deletions
diff --git a/plugins/jetpack/class.photon.php b/plugins/jetpack/class.photon.php index 1f6cc4c4..6e0f2ef7 100644 --- a/plugins/jetpack/class.photon.php +++ b/plugins/jetpack/class.photon.php @@ -60,6 +60,7 @@ class Jetpack_Photon { // Responsive image srcset substitution add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ), 10, 4 ); + add_filter( 'wp_calculate_image_sizes', array( $this, 'filter_sizes' ), 1, 2 ); // Early so themes can still easily filter. // Helpers for maniuplated images add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 ); @@ -611,8 +612,10 @@ class Jetpack_Photon { * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent. * * @since 3.8.0 + * @since 4.0.4 Added automatically additional sizes beyond declared image sizes. * @param array $sources An array of image urls and widths. - * @uses self::validate_image_url, jetpack_photon_url + * @uses self::validate_image_url, jetpack_photon_url, Jetpack_Photon::parse_from_filename + * @uses Jetpack_Photon::strip_image_dimensions_maybe, Jetpack::get_content_width * @return array An array of Photon image urls and widths. */ public function filter_srcset_array( $sources, $size_array, $image_src, $image_meta ) { @@ -646,10 +649,109 @@ class Jetpack_Photon { $sources[ $i ]['url'] = jetpack_photon_url( $url, $args ); } + /** + * At this point, $sources is the original srcset with Photonized URLs. + * Now, we're going to construct additional sizes based on multiples of the content_width. + * This will reduce the gap between the largest defined size and the original image. + */ + + /** + * Filter the multiplier Photon uses to create new srcset items. + * Return false to short-circuit and bypass auto-generation. + * + * @module photon + * + * @since 4.0.4 + * + * @param array|bool $multipliers Array of multipliers to use or false to bypass. + */ + $multipliers = apply_filters( 'jetpack_photon_srcset_multipliers', array( 2, 3 ) ); + + if ( is_array( $multipliers ) // Short-circuit via jetpack_photon_srcset_multipliers filter. + && isset( $image_meta['width'] ) && isset( $image_meta['height'] ) && isset( $image_meta['file'] ) // Verify basic meta is intact. + && isset( $size_array[0] ) && isset( $size_array[1] ) // Verify we have the requested width/height. + ) { + + $url = trailingslashit( $upload_dir['baseurl'] ) . $image_meta['file']; + $fullwidth = $image_meta['width']; + $fullheight = $image_meta['height']; + $reqwidth = $size_array[0]; + $reqheight = $size_array[1]; + + $constrained_size = wp_constrain_dimensions( $fullwidth, $fullheight, $reqwidth ); + $expected_size = array( $reqwidth, $reqheight ); + + if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { + $crop = 'soft'; + $base = Jetpack::get_content_width() ? Jetpack::get_content_width() : 1000; // Provide a default width if none set by the theme. + } + else { + $crop = 'hard'; + $base = $reqwidth; + } + + + $currentwidths = array_keys( $sources ); + $newsources = null; + + foreach ( $multipliers as $multiplier ) { + $newwidth = $base * $multiplier; + foreach ( $currentwidths as $currentwidth ){ + // If a new width would be within 100 pixes of an existing one or larger than the full size image, skip. + if ( abs( $currentwidth - $newwidth ) < 50 || ( $newwidth > $fullwidth ) ) { + continue 2; // Back to the foreach ( $multipliers as $multiplier ) + } + } // foreach ( $currentwidths as $currentwidth ){ + + if ( 'soft' == $crop ) { + $args = array( + 'w' => $newwidth, + ); + } + else { // hard crop, e.g. add_image_size( 'example', 200, 200, true ); + $args = array( + 'zoom' => $multiplier, + 'resize' => $reqwidth . ',' . $reqheight, + ); + } + + $newsources[ $newwidth ] = array( + 'url' => jetpack_photon_url( $url, $args ), + 'descriptor' => 'w', + 'value' => $newwidth, + ); + } // foreach ( $multipliers as $multiplier ) + if ( is_array( $newsources ) ) { + $sources = array_merge( $sources, $newsources ); + } + } // if ( isset( $image_meta['width'] ) && isset( $image_meta['file'] ) ) + return $sources; } /** + * Filters an array of image `sizes` values, using $content_width instead of image's full size. + * + * @since 4.0.4 + * @param array $sizes An array of media query breakpoints. + * @param array $size Width and height of the image + * @uses Jetpack::get_content_width + * @return array An array of media query breakpoints. + */ + public function filter_sizes( $sizes, $size ) { + $content_width = Jetpack::get_content_width(); + if ( ! $content_width ) { + $content_width = 1000; + } + + if ( ( is_array( $size ) && $size[0] < $content_width ) ) { + return $sizes; + } + + return sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width ); + } + + /** ** GENERAL FUNCTIONS **/ |