summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury German <blueknight@gentoo.org>2016-02-12 22:22:00 -0500
committerYury German <blueknight@gentoo.org>2016-02-12 22:22:00 -0500
commit657cafe0e955cf88033597f131aa50835140c617 (patch)
treecf21a30d319cb2a238a6cfb8b4eb3b20b1b5dcff /plugins/jetpack/class.photon.php
parentAdding New Mantra version 2.4.1.1 - Bug 574468 (diff)
downloadblogs-gentoo-657cafe0e955cf88033597f131aa50835140c617.tar.gz
blogs-gentoo-657cafe0e955cf88033597f131aa50835140c617.tar.bz2
blogs-gentoo-657cafe0e955cf88033597f131aa50835140c617.zip
Updating plugins easy-table, jetpack, openid, public-post preview, talbe-of-contents-plus, wordress-mobile-pack - Bug 574468
Diffstat (limited to 'plugins/jetpack/class.photon.php')
-rw-r--r--plugins/jetpack/class.photon.php264
1 files changed, 243 insertions, 21 deletions
diff --git a/plugins/jetpack/class.photon.php b/plugins/jetpack/class.photon.php
index b9c56f35..c5627fef 100644
--- a/plugins/jetpack/class.photon.php
+++ b/plugins/jetpack/class.photon.php
@@ -58,6 +58,9 @@ class Jetpack_Photon {
// Core image retrieval
add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 10, 3 );
+ // Responsive image srcset substitution
+ add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ), 10, 4 );
+
// Helpers for maniuplated images
add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 );
}
@@ -151,7 +154,17 @@ class Jetpack_Photon {
// Identify image source
$src = $src_orig = $images['img_url'][ $index ];
- // Allow specific images to be skipped
+ /**
+ * Allow specific images to be skipped by Photon.
+ *
+ * @module photon
+ *
+ * @since 2.0.3
+ *
+ * @param bool false Should Photon ignore this image. Default to false.
+ * @param string $src Image URL.
+ * @param string $tag Image Tag (Image HTML output).
+ */
if ( apply_filters( 'jetpack_photon_skip_image', false, $src, $tag ) )
continue;
@@ -195,7 +208,30 @@ class Jetpack_Photon {
}
// WP Attachment ID, if uploaded to this site
- if ( preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id ) && ( 0 === strpos( $src, $upload_dir['baseurl'] ) || apply_filters( 'jetpack_photon_image_is_local', false, compact( 'src', 'tag', 'images', 'index' ) ) ) ) {
+ if (
+ preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id ) &&
+ (
+ 0 === strpos( $src, $upload_dir['baseurl'] ) ||
+ /**
+ * Filter whether an image using an attachment ID in its class has to be uploaded to the local site to go through Photon.
+ *
+ * @module photon
+ *
+ * @since 2.0.3
+ *
+ * @param bool false Was the image uploaded to the local site. Default to false.
+ * @param array $args {
+ * Array of image details.
+ *
+ * @type $src Image URL.
+ * @type tag Image tag (Image HTML output).
+ * @type $images Array of information about the image.
+ * @type $index Image index.
+ * }
+ */
+ apply_filters( 'jetpack_photon_image_is_local', false, compact( 'src', 'tag', 'images', 'index' ) )
+ )
+ ) {
$attachment_id = intval( array_pop( $attachment_id ) );
if ( $attachment_id ) {
@@ -275,6 +311,26 @@ class Jetpack_Photon {
elseif ( false !== $height )
$args['h'] = $height;
+ /**
+ * Filter the array of Photon arguments added to an image when it goes through Photon.
+ * By default, only includes width and height values.
+ * @see https://developer.wordpress.com/docs/photon/api/
+ *
+ * @module photon
+ *
+ * @since 2.0.0
+ *
+ * @param array $args Array of Photon Arguments.
+ * @param array $args {
+ * Array of image details.
+ *
+ * @type $tag Image tag (Image HTML output).
+ * @type $src Image URL.
+ * @type $src_orig Original Image URL.
+ * @type $width Image width.
+ * @type $height Image height.
+ * }
+ */
$args = apply_filters( 'jetpack_photon_post_image_args', $args, compact( 'tag', 'src', 'src_orig', 'width', 'height' ) );
$photon_url = jetpack_photon_url( $src, $args );
@@ -361,17 +417,41 @@ class Jetpack_Photon {
*/
public function filter_image_downsize( $image, $attachment_id, $size ) {
// Don't foul up the admin side of things, and provide plugins a way of preventing Photon from being applied to images.
- if ( is_admin() || apply_filters( 'jetpack_photon_override_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) ) )
+ if (
+ is_admin() ||
+ /**
+ * Provide plugins a way of preventing Photon from being applied to images retrieved from WordPress Core.
+ *
+ * @module photon
+ *
+ * @since 2.0.0
+ *
+ * @param bool false Stop Photon from being applied to the image. Default to false.
+ * @param array $args {
+ * Array of image details.
+ *
+ * @type $image Image URL.
+ * @type $attachment_id Attachment ID of the image.
+ * @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer.
+ * }
+ */
+ apply_filters( 'jetpack_photon_override_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) )
+ )
return $image;
// Get the image URL and proceed with Photon-ification if successful
$image_url = wp_get_attachment_url( $attachment_id );
+ // Set this to true later when we know we have size meta.
+ $has_size_meta = false;
+
if ( $image_url ) {
// Check if image URL should be used with Photon
if ( ! self::validate_image_url( $image_url ) )
return $image;
+ $intermediate = true; // For the fourth array item returned by the image_downsize filter.
+
// If an image is requested with a size known to WordPress, use that size's settings with Photon
if ( ( is_string( $size ) || is_int( $size ) ) && array_key_exists( $size, self::image_sizes() ) ) {
$image_args = self::image_sizes();
@@ -379,20 +459,34 @@ class Jetpack_Photon {
$photon_args = array();
- // `full` is a special case in WP
- // To ensure filter receives consistent data regardless of requested size, `$image_args` is overridden with dimensions of original image.
+ $image_meta = image_get_intermediate_size( $attachment_id, $size );
+
+ // 'full' is a special case: We need consistent data regardless of the requested size.
if ( 'full' == $size ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
+ $intermediate = false;
+ } elseif ( ! $image_meta ) {
+ // If we still don't have any image meta at this point, it's probably from a custom thumbnail size
+ // for an image that was uploaded before the custom image was added to the theme. Try to determine the size manually.
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
+
if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
- // 'crop' is true so Photon's `resize` method is used
- $image_args = array(
- 'width' => $image_meta['width'],
- 'height' => $image_meta['height'],
- 'crop' => true
- );
+ $image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $image_args['width'], $image_args['height'], $image_args['crop'] );
+ if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
+ $image_meta['width'] = $image_resized[6];
+ $image_meta['height'] = $image_resized[7];
+ }
}
}
+ if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
+ $image_args['width'] = $image_meta['width'];
+ $image_args['height'] = $image_meta['height'];
+
+ list( $image_args['width'], $image_args['height'] ) = image_constrain_size_for_editor( $image_args['width'], $image_args['height'], $size, 'display' );
+ $has_size_meta = true;
+ }
+
// Expose determined arguments to a filter before passing to Photon
$transform = $image_args['crop'] ? 'resize' : 'fit';
@@ -405,24 +499,48 @@ class Jetpack_Photon {
}
} else {
if ( ( 'resize' === $transform ) && $image_meta = wp_get_attachment_metadata( $attachment_id ) ) {
- // Lets make sure that we don't upscale images since wp never upscales them as well
- $smaller_width = ( ( $image_meta['width'] < $image_args['width'] ) ? $image_meta['width'] : $image_args['width'] );
- $smaller_height = ( ( $image_meta['height'] < $image_args['height'] ) ? $image_meta['height'] : $image_args['height'] );
+ if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
+ // Lets make sure that we don't upscale images since wp never upscales them as well
+ $smaller_width = ( ( $image_meta['width'] < $image_args['width'] ) ? $image_meta['width'] : $image_args['width'] );
+ $smaller_height = ( ( $image_meta['height'] < $image_args['height'] ) ? $image_meta['height'] : $image_args['height'] );
- $photon_args[ $transform ] = $smaller_width . ',' . $smaller_height;
+ $photon_args[ $transform ] = $smaller_width . ',' . $smaller_height;
+ }
} else {
$photon_args[ $transform ] = $image_args['width'] . ',' . $image_args['height'];
}
}
+
+ /**
+ * Filter the Photon Arguments added to an image when going through Photon, when that image size is a string.
+ * Image size will be a string (e.g. "full", "medium") when it is known to WordPress.
+ *
+ * @module photon
+ *
+ * @since 2.0.0
+ *
+ * @param array $photon_args Array of Photon arguments.
+ * @param array $args {
+ * Array of image details.
+ *
+ * @type $image_args Array of Image arguments (width, height, crop).
+ * @type $image_url Image URL.
+ * @type $attachment_id Attachment ID of the image.
+ * @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer.
+ * @type $transform Value can be resize or fit.
+ * @see https://developer.wordpress.com/docs/photon/api
+ * }
+ */
$photon_args = apply_filters( 'jetpack_photon_image_downsize_string', $photon_args, compact( 'image_args', 'image_url', 'attachment_id', 'size', 'transform' ) );
// Generate Photon URL
$image = array(
jetpack_photon_url( $image_url, $photon_args ),
- false,
- false
+ $has_size_meta ? $image_args['width'] : false,
+ $has_size_meta ? $image_args['height'] : false,
+ $intermediate
);
} elseif ( is_array( $size ) ) {
// Pull width and height values from the provided array, if possible
@@ -430,21 +548,58 @@ class Jetpack_Photon {
$height = isset( $size[1] ) ? (int) $size[1] : false;
// Don't bother if necessary parameters aren't passed.
- if ( ! $width || ! $height )
+ if ( ! $width || ! $height ) {
return $image;
+ }
+
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
+ if ( isset( $image_meta['width'], $image_meta['height'] ) ) {
+ $image_resized = image_resize_dimensions( $image_meta['width'], $image_meta['height'], $width, $height );
+
+ if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
+ $width = $image_resized[6];
+ $height = $image_resized[7];
+ } else {
+ $width = $image_meta['width'];
+ $height = $image_meta['height'];
+ }
+
+ $has_size_meta = true;
+ }
+
+ list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
// Expose arguments to a filter before passing to Photon
$photon_args = array(
'fit' => $width . ',' . $height
);
+ /**
+ * Filter the Photon Arguments added to an image when going through Photon,
+ * when the image size is an array of height and width values.
+ *
+ * @module photon
+ *
+ * @since 2.0.0
+ *
+ * @param array $photon_args Array of Photon arguments.
+ * @param array $args {
+ * Array of image details.
+ *
+ * @type $width Image width.
+ * @type height Image height.
+ * @type $image_url Image URL.
+ * @type $attachment_id Attachment ID of the image.
+ * }
+ */
$photon_args = apply_filters( 'jetpack_photon_image_downsize_array', $photon_args, compact( 'width', 'height', 'image_url', 'attachment_id' ) );
// Generate Photon URL
$image = array(
jetpack_photon_url( $image_url, $photon_args ),
- false,
- false
+ $has_size_meta ? $width : false,
+ $has_size_meta ? $height : false,
+ $intermediate
);
}
}
@@ -453,6 +608,48 @@ class Jetpack_Photon {
}
/**
+ * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent.
+ *
+ * @since 3.8.0
+ * @param array $sources An array of image urls and widths.
+ * @uses self::validate_image_url, jetpack_photon_url
+ * @return array An array of Photon image urls and widths.
+ */
+ public function filter_srcset_array( $sources, $size_array, $image_src, $image_meta ) {
+ $upload_dir = wp_upload_dir();
+
+ foreach ( $sources as $i => $source ) {
+ if ( ! self::validate_image_url( $source['url'] ) ) {
+ continue;
+ }
+
+ $url = $source['url'];
+ list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $url );
+
+ // It's quicker to get the full size with the data we have already, if available
+ if ( isset( $image_meta['file'] ) ) {
+ $url = trailingslashit( $upload_dir['baseurl'] ) . $image_meta['file'];
+ } else {
+ $url = Jetpack_Photon::strip_image_dimensions_maybe( $url );
+ }
+
+ $args = array();
+ if ( 'w' === $source['descriptor'] ) {
+ if ( $height && ( $source['value'] == $width ) ) {
+ $args['resize'] = $width . ',' . $height;
+ } else {
+ $args['w'] = $source['value'];
+ }
+
+ }
+
+ $sources[ $i ]['url'] = jetpack_photon_url( $url, $args );
+ }
+
+ return $sources;
+ }
+
+ /**
** GENERAL FUNCTIONS
**/
@@ -479,8 +676,22 @@ class Jetpack_Photon {
) );
// Bail if scheme isn't http or port is set that isn't port 80
- if ( ( 'http' != $url_info['scheme'] || ! in_array( $url_info['port'], array( 80, null ) ) ) && apply_filters( 'jetpack_photon_reject_https', true ) )
+ if (
+ ( 'http' != $url_info['scheme'] || ! in_array( $url_info['port'], array( 80, null ) ) ) &&
+ /**
+ * Allow Photon to fetch images that are served via HTTPS.
+ *
+ * @module photon
+ *
+ * @since 2.4.0
+ * @since 3.9.0 Default to false.
+ *
+ * @param bool $reject_https Should Photon ignore images using the HTTPS scheme. Default to false.
+ */
+ apply_filters( 'jetpack_photon_reject_https', false )
+ ) {
return false;
+ }
// Bail if no host is found
if ( is_null( $url_info['host'] ) )
@@ -500,6 +711,17 @@ class Jetpack_Photon {
// If we got this far, we should have an acceptable image URL
// But let folks filter to decline if they prefer.
+ /**
+ * Overwrite the results of the validation steps an image goes through before to be considered valid to be used by Photon.
+ *
+ * @module photon
+ *
+ * @since 3.0.0
+ *
+ * @param bool true Is the image URL valid and can it be used by Photon. Default to true.
+ * @param string $url Image URL.
+ * @param array $parsed_url Array of information about the image.
+ */
return apply_filters( 'photon_validate_image_url', true, $url, $parsed_url );
}