summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/functions.photon.php')
-rw-r--r--plugins/jetpack/functions.photon.php159
1 files changed, 116 insertions, 43 deletions
diff --git a/plugins/jetpack/functions.photon.php b/plugins/jetpack/functions.photon.php
index 015d713e..b3465207 100644
--- a/plugins/jetpack/functions.photon.php
+++ b/plugins/jetpack/functions.photon.php
@@ -1,12 +1,23 @@
<?php
+use Automattic\Jetpack\Status;
+
+/**
+ * Generic functions using the Photon service.
+ *
+ * Some are used outside of the Photon module being active, so intentionally not within the module.
+ *
+ * @package jetpack
+ */
+
/**
* Generates a Photon URL.
*
- * @see http://developer.wordpress.com/docs/photon/
+ * @see https://developer.wordpress.com/docs/photon/
*
- * @param string $image_url URL to the publicly accessible image you want to manipulate
- * @param array|string $args An array of arguments, i.e. array( 'w' => '300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456)
+ * @param string $image_url URL to the publicly accessible image you want to manipulate.
+ * @param array|string $args An array of arguments, i.e. array( 'w' => '300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456).
+ * @param string|null $scheme URL protocol.
* @return string The raw final URL. You should run this through esc_url() before displaying it.
*/
function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
@@ -20,9 +31,9 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
*
* @since 4.1.0
*
- * @param bool false Result of Jetpack::is_development_mode.
+ * @param bool false Result of Automattic\Jetpack\Status->is_development_mode().
*/
- if ( true === apply_filters( 'jetpack_photon_development_mode', Jetpack::is_development_mode() ) ) {
+ if ( true === apply_filters( 'jetpack_photon_development_mode', ( new Status() )->is_development_mode() ) ) {
return $image_url;
}
}
@@ -72,23 +83,23 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
return $image_url;
}
- $image_url_parts = @jetpack_photon_parse_url( $image_url );
+ $image_url_parts = wp_parse_url( $image_url );
- // Unable to parse
+ // Unable to parse.
if ( ! is_array( $image_url_parts ) || empty( $image_url_parts['host'] ) || empty( $image_url_parts['path'] ) ) {
return $image_url;
}
- if ( is_array( $args ) ){
- // Convert values that are arrays into strings
+ if ( is_array( $args ) ) {
+ // Convert values that are arrays into strings.
foreach ( $args as $arg => $value ) {
if ( is_array( $value ) ) {
- $args[$arg] = implode( ',', $value );
+ $args[ $arg ] = implode( ',', $value );
}
}
- // Encode values
- // See http://core.trac.wordpress.org/ticket/17923
+ // Encode values.
+ // See https://core.trac.wordpress.org/ticket/17923 .
$args = rawurlencode_deep( $args );
}
@@ -97,7 +108,7 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
if ( wp_endswith( strtolower( $image_url_parts['host'] ), '.files.wordpress.com' ) ) {
$is_wpcom_image = true;
if ( isset( $args['ssl'] ) ) {
- // Do not send the ssl argument to prevent caching issues
+ // Do not send the ssl argument to prevent caching issues.
unset( $args['ssl'] );
}
}
@@ -110,10 +121,20 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
// So if the image is already a Photon URL, append the new arguments to the existing URL.
// Alternately, if it's a *.files.wordpress.com url, then keep the domain as is.
if (
- in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ) )
- || $image_url_parts['host'] === jetpack_photon_parse_url( $custom_photon_url, PHP_URL_HOST )
+ in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ), true )
+ || wp_parse_url( $custom_photon_url, PHP_URL_HOST ) === $image_url_parts['host']
|| $is_wpcom_image
) {
+ /*
+ * VideoPress Poster images should only keep one param, ssl.
+ */
+ if (
+ is_array( $args )
+ && 'videos.files.wordpress.com' === strtolower( $image_url_parts['host'] )
+ ) {
+ $args = array_intersect_key( array( 'ssl' => 1 ), $args );
+ }
+
$photon_url = add_query_arg( $args, $image_url );
return jetpack_photon_url_scheme( $photon_url, $scheme );
}
@@ -135,17 +156,38 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
// However some source images are served via PHP so check the no-query-string extension.
// For future proofing, this is a blacklist of common issues rather than a whitelist.
$extension = pathinfo( $image_url_parts['path'], PATHINFO_EXTENSION );
- if ( empty( $extension ) || in_array( $extension, array( 'php', 'ashx' ) ) ) {
+ if ( empty( $extension ) || in_array( $extension, array( 'php', 'ashx' ), true ) ) {
return $image_url;
}
}
$image_host_path = $image_url_parts['host'] . $image_url_parts['path'];
- // Figure out which CDN subdomain to use
- srand( crc32( $image_host_path ) );
- $subdomain = rand( 0, 2 );
- srand();
+ /*
+ * Figure out which CDN subdomain to use.
+ *
+ * The goal is to have the same subdomain for any particular image to prevent multiple runs resulting in multiple
+ * images needing to be downloaded by the browser.
+ *
+ * We are providing our own generated value by taking the modulus of the crc32 value of the URL.
+ *
+ * Valid values are 0, 1, and 2.
+ */
+ $subdomain = abs( crc32( $image_host_path ) % 3 );
+
+ /*
+ * Need to perform a slowroll out per pMz3w-arH-p2
+ *
+ * 7.9 - Use the old method if the value is not 0 (thus 1 or 2).
+ * 8.0 - Use the old method if the value is 2 (thus not 0 or 1). [current step]
+ * 8.1 - Remove this completely.
+ */
+ if ( 2 === $subdomain ) {
+ // Figure out which CDN subdomain to use.
+ srand( crc32( $image_host_path ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_seeding_srand
+ $subdomain = rand( 0, 2 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand
+ srand(); // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_seeding_srand
+ }
/**
* Filters the domain used by the Photon module.
@@ -159,7 +201,7 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
*/
$photon_domain = apply_filters( 'jetpack_photon_domain', "https://i{$subdomain}.wp.com", $image_url );
$photon_domain = trailingslashit( esc_url( $photon_domain ) );
- $photon_url = $photon_domain . $image_host_path;
+ $photon_url = $photon_domain . $image_host_path;
/**
* Add query strings to Photon URL.
@@ -186,7 +228,7 @@ function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
}
}
- if ( isset( $image_url_parts['scheme'] ) && 'https' == $image_url_parts['scheme'] ) {
+ if ( isset( $image_url_parts['scheme'] ) && 'https' === $image_url_parts['scheme'] ) {
$photon_url = add_query_arg( array( 'ssl' => 1 ), $photon_url );
}
@@ -201,29 +243,43 @@ add_filter( 'jetpack_photon_url', 'jetpack_photon_url', 10, 3 );
*/
add_filter( 'jetpack_photon_pre_args', 'jetpack_photon_parse_wpcom_query_args', 10, 2 );
+/**
+ * Parses WP.com-hosted image args to replicate the crop.
+ *
+ * @param mixed $args Args set during Photon's processing.
+ * @param string $image_url URL of the image.
+ * @return array|string Args for Photon to use for the URL.
+ */
function jetpack_photon_parse_wpcom_query_args( $args, $image_url ) {
- $parsed_url = @parse_url( $image_url );
+ $parsed_url = wp_parse_url( $image_url );
- if ( ! $parsed_url )
+ if ( ! $parsed_url ) {
return $args;
+ }
- $image_url_parts = wp_parse_args( $parsed_url, array(
- 'host' => '',
- 'query' => ''
- ) );
+ $image_url_parts = wp_parse_args(
+ $parsed_url,
+ array(
+ 'host' => '',
+ 'query' => '',
+ )
+ );
- if ( '.files.wordpress.com' != substr( $image_url_parts['host'], -20 ) )
+ if ( '.files.wordpress.com' !== substr( $image_url_parts['host'], -20 ) ) {
return $args;
+ }
- if ( empty( $image_url_parts['query'] ) )
+ if ( empty( $image_url_parts['query'] ) ) {
return $args;
+ }
$wpcom_args = wp_parse_args( $image_url_parts['query'] );
- if ( empty( $wpcom_args['w'] ) || empty( $wpcom_args['h'] ) )
+ if ( empty( $wpcom_args['w'] ) || empty( $wpcom_args['h'] ) ) {
return $args;
+ }
- // Keep the crop by using "resize"
+ // Keep the crop by using "resize".
if ( ! empty( $wpcom_args['crop'] ) ) {
if ( is_array( $args ) ) {
$args = array_merge( array( 'resize' => array( $wpcom_args['w'], $wpcom_args['h'] ) ), $args );
@@ -241,8 +297,16 @@ function jetpack_photon_parse_wpcom_query_args( $args, $image_url ) {
return $args;
}
+/**
+ * Sets the scheme for a URL
+ *
+ * @param string $url URL to set scheme.
+ * @param string $scheme Scheme to use. Accepts http, https, network_path.
+ *
+ * @return string URL.
+ */
function jetpack_photon_url_scheme( $url, $scheme ) {
- if ( ! in_array( $scheme, array( 'http', 'https', 'network_path' ) ) ) {
+ if ( ! in_array( $scheme, array( 'http', 'https', 'network_path' ), true ) ) {
if ( preg_match( '#^(https?:)?//#', $url ) ) {
return $url;
}
@@ -250,7 +314,7 @@ function jetpack_photon_url_scheme( $url, $scheme ) {
$scheme = 'http';
}
- if ( 'network_path' == $scheme ) {
+ if ( 'network_path' === $scheme ) {
$scheme_slashes = '//';
} else {
$scheme_slashes = "$scheme://";
@@ -263,21 +327,30 @@ function jetpack_photon_url_scheme( $url, $scheme ) {
* A wrapper for PHP's parse_url, prepending assumed scheme for network path
* URLs. PHP versions 5.4.6 and earlier do not correctly parse without scheme.
*
- * @see http://php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-changelog
+ * WP ships with a wrapper for parse_url, wp_parse_url, that should be used instead.
+ *
+ * @see https://php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-changelog
+ * @deprecated 7.8.0 Use wp_parse_url instead.
*
- * @param string $url The URL to parse
- * @param integer $component Retrieve specific URL component
+ * @param string $url The URL to parse.
+ * @param integer $component Retrieve specific URL component.
* @return mixed Result of parse_url
*/
function jetpack_photon_parse_url( $url, $component = -1 ) {
- if ( 0 === strpos( $url, '//' ) ) {
- $url = ( is_ssl() ? 'https:' : 'http:' ) . $url;
- }
-
- return parse_url( $url, $component );
+ _deprecated_function( 'jetpack_photon_parse_url', 'jetpack-7.8.0', 'wp_parse_url' );
+ return wp_parse_url( $url, $component );
}
add_filter( 'jetpack_photon_skip_for_url', 'jetpack_photon_banned_domains', 9, 2 );
+
+/**
+ * Check to skip Photon for a known domain that shouldn't be Photonized.
+ *
+ * @param bool $skip If the image should be skipped by Photon.
+ * @param string $image_url URL of the image.
+ *
+ * @return bool Should the image be skipped by Photon.
+ */
function jetpack_photon_banned_domains( $skip, $image_url ) {
$banned_host_patterns = array(
'/^chart\.googleapis\.com$/',
@@ -289,7 +362,7 @@ function jetpack_photon_banned_domains( $skip, $image_url ) {
'/\.cdninstagram\.com$/',
);
- $host = jetpack_photon_parse_url( $image_url, PHP_URL_HOST );
+ $host = wp_parse_url( $image_url, PHP_URL_HOST );
foreach ( $banned_host_patterns as $banned_host_pattern ) {
if ( 1 === preg_match( $banned_host_pattern, $host ) ) {