diff --git a/.travis.yml b/.travis.yml index 30bd83eaa27..eb94773397a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,31 +4,10 @@ language: php # Opt to use Travis container-based environment. sudo: false -# PHP version used in first build configuration. -php: - - "7.1" - -# WordPress version used in first build configuration. -env: - - WP_VERSION=latest - # Newer versions like trusty don't have PHP 5.2 or 5.3 # https://blog.travis-ci.com/2017-07-11-trusty-as-default-linux-is-coming dist: precise -# Next we define our matrix of additional build configurations to test against. -# The versions listed above will automatically create our first configuration, -# so it doesn't need to be re-defined below. - -# WP_VERSION specifies the tag to use. The way these tests are configured to run -# requires at least WordPress 3.8. Specify "latest" to test against SVN trunk. - -# Note that Travis CI supports listing these above to automatically build a -# matrix of configurations, but we're being nice here by manually building a -# total of four configurations even though we're testing 4 versions of PHP -# along with 2 versions of WordPress (which would build 8 configs otherwise). -# This takes half as long to run while still providing adequate coverage. - notifications: email: on_success: never @@ -44,19 +23,20 @@ matrix: include: - php: "5.3" env: WP_VERSION=latest DEV_LIB_SKIP=composer,phpcs - - php: "5.3" - env: WP_VERSION=4.7 DEV_LIB_SKIP=composer,phpcs - php: "5.4" - env: WP_VERSION=latest - - php: "5.4" - env: WP_VERSION=4.7 - - php: "7.0" - env: WP_VERSION=latest + env: WP_VERSION=4.7 DEV_LIB_SKIP=composer,phpcs + - php: "5.5" + env: WP_VERSION=latest DEV_LIB_SKIP=phpcs + - php: "5.6" + env: WP_VERSION=4.8 DEV_LIB_SKIP=phpcs + - php: "5.6" + env: WP_VERSION=latest DEV_LIB_SKIP=phpcs - php: "7.0" - env: WP_VERSION=4.7 - # 7.1 / latest already included above as first build. + env: WP_VERSION=latest DEV_LIB_SKIP=phpcs - php: "7.1" - env: WP_VERSION=4.7 + env: WP_VERSION=latest DEV_LIB_SKIP=phpcs + - php: "7.2" + env: WP_VERSION=trunk install: - nvm install 6 && nvm use 6 diff --git a/wpcom-helper.php b/wpcom-helper.php index 37af6a9ee91..36f931107d3 100644 --- a/wpcom-helper.php +++ b/wpcom-helper.php @@ -1,17 +1,22 @@ 'ImageObject', - 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), - 'width' => $size, + '@type' => 'ImageObject', + 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), + 'width' => $size, 'height' => $size, ); return $metadata; } +/** + * Add image to metadata. + * + * @since 0.3.2 + * + * @param array $metadata Metadata. + * @param WP_Post $post Post. + * @return array Metadata. + */ function wpcom_amp_add_image_to_metadata( $metadata, $post ) { if ( ! class_exists( 'Jetpack_PostImages' ) ) { return wpcom_amp_add_fallback_image_to_metadata( $metadata ); @@ -67,10 +104,10 @@ function wpcom_amp_add_image_to_metadata( $metadata, $post ) { $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true, - 'avatar_size' => 200, - // AMP already attempts these - 'from_thumbnail' => false, - 'from_attachment' => false, + 'avatar_size' => 200, + // AMP already attempts these. + 'from_thumbnail' => false, + 'from_attachment' => false, ) ); if ( empty( $image ) ) { @@ -80,30 +117,37 @@ function wpcom_amp_add_image_to_metadata( $metadata, $post ) { if ( ! isset( $image['src_width'] ) ) { $dimensions = wpcom_amp_extract_image_dimensions_from_getimagesize( array( $image['src'] => false, - ) - ); + ) ); if ( false !== $dimensions[ $image['src'] ] ) { - $image['src_width'] = $dimensions['width']; + $image['src_width'] = $dimensions['width']; $image['src_height'] = $dimensions['height']; } } $metadata['image'] = array( - '@type' => 'ImageObject', - 'url' => $image['src'], - 'width' => $image['src_width'], + '@type' => 'ImageObject', + 'url' => $image['src'], + 'width' => $image['src_width'], 'height' => $image['src_height'], ); return $metadata; } +/** + * Add fallback image to metadata. + * + * @since 0.3.2 + * + * @param array $metadata Metadata. + * @return array Metadata. + */ function wpcom_amp_add_fallback_image_to_metadata( $metadata ) { $metadata['image'] = array( - '@type' => 'ImageObject', - 'url' => staticize_subdomain( 'https://wordpress.com/i/blank.jpg' ), - 'width' => 200, + '@type' => 'ImageObject', + 'url' => staticize_subdomain( 'https://wordpress.com/i/blank.jpg' ), + 'width' => 200, 'height' => 200, ); @@ -111,36 +155,50 @@ function wpcom_amp_add_fallback_image_to_metadata( $metadata ) { } add_action( 'amp_extract_image_dimensions_batch_callbacks_registered', 'wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks' ); + +/** + * Add hooks to extract image dimensions. + * + * @since 0.5 + */ function wpcom_amp_extract_image_dimensions_batch_add_custom_callbacks() { // If images are being served from Photon or WP.com files, try extracting the size using querystring. add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_querystring', 9, 1 ); // Hook in before the default extractors. // Uses a special wpcom lib (wpcom_getimagesize) to extract dimensions as a last resort if we weren't able to figure them out. - add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_getimagesize', 99, 1 ); // Our last resort, so run late + add_action( 'amp_extract_image_dimensions_batch', 'wpcom_amp_extract_image_dimensions_from_getimagesize', 99, 1 ); // Our last resort, so run late. // The wpcom override obviates this one, so take it out. - remove_filter( 'amp_extract_image_dimensions_batch', array( 'AMP_Image_Dimension_Extractor', 'extract_by_downloading_images' ), 999, 1 ); + remove_filter( 'amp_extract_image_dimensions_batch', array( 'AMP_Image_Dimension_Extractor', 'extract_by_downloading_images' ), 999 ); } +/** + * Extract image dimensions from query string. + * + * @since 0.5 + * + * @param array $dimensions Dimensions. + * @return array Dimensions. + */ function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions ) { - foreach ( $dimensions as $url => $value ) { + foreach ( $dimensions as $url => $value ) { if ( is_array( $value ) ) { continue; } - $host = parse_url( $url, PHP_URL_HOST ); + $host = wp_parse_url( $url, PHP_URL_HOST ); if ( ! wp_endswith( $host, '.wp.com' ) || ! wp_endswith( $host, '.files.wordpress.com' ) ) { continue; } - parse_str( parse_url( $url, PHP_URL_QUERY ), $query ); + parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query ); $w = isset( $query['w'] ) ? absint( $query['w'] ) : false; $h = isset( $query['h'] ) ? absint( $query['h'] ) : false; if ( false !== $w && false !== $h ) { $dimensions[ $url ] = array( - 'width' => $w, + 'width' => $w, 'height' => $h, ); } @@ -148,6 +206,14 @@ function wpcom_amp_extract_image_dimensions_from_querystring( $dimensions ) { return $dimensions; } +/** + * Extract image dimensions via wpcom/imagesize. + * + * @since 0.5 + * + * @param array $dimensions Dimensions. + * @return array Dimensions. + */ function wpcom_amp_extract_image_dimensions_from_getimagesize( $dimensions ) { if ( ! function_exists( 'require_lib' ) ) { return $dimensions; @@ -161,7 +227,7 @@ function wpcom_amp_extract_image_dimensions_from_getimagesize( $dimensions ) { $result = wpcom_getimagesize( $url ); if ( is_array( $result ) ) { $dimensions[ $url ] = array( - 'width' => $result[0], + 'width' => $result[0], 'height' => $result[1], ); }