Skip to content

Commit

Permalink
[not verified] Various: Use wp_resource_hints (#16305)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraftbj authored and pereirinha committed Jul 27, 2020
1 parent f4b9767 commit 917b3d9
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 82 deletions.
2 changes: 2 additions & 0 deletions bin/phpcs-requirelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ module.exports = [
'json-endpoints/jetpack/class-jetpack-json-api-delete-backup-helper-script-endpoint.php',
'json-endpoints/jetpack/class-jetpack-json-api-install-backup-helper-script-endpoint.php',
'load-jetpack.php',
'modules/comments.php',
'modules/masterbar/',
'modules/memberships/',
'modules/module-extras.php',
'modules/module-info.php',
'modules/photon.php',
'modules/post-by-email.php',
'modules/post-by-email/',
'modules/publicize.php',
Expand Down
25 changes: 8 additions & 17 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -6900,23 +6900,14 @@ public static function build_raw_urls( $url ) {
/**
* Stores and prints out domains to prefetch for page speed optimization.
*
* @param mixed $new_urls
*/
public static function dns_prefetch( $new_urls = null ) {
static $prefetch_urls = array();
if ( empty( $new_urls ) && ! empty( $prefetch_urls ) ) {
echo "\r\n";
foreach ( $prefetch_urls as $this_prefetch_url ) {
printf( "<link rel='dns-prefetch' href='%s'/>\r\n", esc_attr( $this_prefetch_url ) );
}
} elseif ( ! empty( $new_urls ) ) {
if ( ! has_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ) ) {
add_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) );
}
foreach ( (array) $new_urls as $this_new_url ) {
$prefetch_urls[] = strtolower( untrailingslashit( preg_replace( '#^https?://#i', '//', $this_new_url ) ) );
}
$prefetch_urls = array_unique( $prefetch_urls );
* @deprecated 8.8.0 Use Jetpack::add_resource_hints.
*
* @param string|array $urls URLs to hint.
*/
public static function dns_prefetch( $urls = null ) {
_deprecated_function( __FUNCTION__, 'jetpack-8.8.0', 'Automattic\Jetpack\Assets::add_resource_hint' );
if ( $urls ) {
Assets::add_resource_hint( $urls );
}
}

Expand Down
6 changes: 1 addition & 5 deletions modules/comment-likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@

use Automattic\Jetpack\Assets;

Jetpack::dns_prefetch(
array(
'//widgets.wp.com',
)
);
Assets::add_resource_hint( '//widgets.wp.com', 'dns-prefetch' );

require_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
require_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
Expand Down
38 changes: 27 additions & 11 deletions modules/comments.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Module Name: Comments
* Module Description: Let visitors use a WordPress.com, Twitter, Facebook, or Google account to comment
Expand All @@ -10,27 +9,44 @@
* Module Tags: Social
* Feature: Engagement
* Additional Search Queries: comments, comment, facebook, twitter, google+, social
*
* @package Jetpack.
*/

use Automattic\Jetpack\Assets;

Assets::add_resource_hint(
array(
'//jetpack.wordpress.com',
'//s0.wp.com',
'//s1.wp.com',
'//s2.wp.com',
'//public-api.wordpress.com',
'//0.gravatar.com',
'//1.gravatar.com',
'//2.gravatar.com',
),
'dns-prefetch'
);

/*
* Add the main commenting system.
*/
require dirname( __FILE__ ) . '/comments/comments.php';

if ( is_admin() ) {
/**
* Add the admin functionality.
*/
require dirname( __FILE__ ) . '/comments/admin.php';
}

/**
* Module loader.
*/
function jetpack_comments_load() {
Jetpack::enable_module_configurable( __FILE__ );
}

add_action( 'jetpack_modules_loaded', 'jetpack_comments_load' );

Jetpack::dns_prefetch( array(
'//jetpack.wordpress.com',
'//s0.wp.com',
'//s1.wp.com',
'//s2.wp.com',
'//public-api.wordpress.com',
'//0.gravatar.com',
'//1.gravatar.com',
'//2.gravatar.com',
) );
17 changes: 10 additions & 7 deletions modules/likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

use Automattic\Jetpack\Assets;

Jetpack::dns_prefetch( array(
'//widgets.wp.com',
'//s0.wp.com',
'//0.gravatar.com',
'//1.gravatar.com',
'//2.gravatar.com',
) );
Assets::add_resource_hint(
array(
'//widgets.wp.com',
'//s0.wp.com',
'//0.gravatar.com',
'//1.gravatar.com',
'//2.gravatar.com',
),
'dns-prefetch'
);

include_once dirname( __FILE__ ) . '/likes/jetpack-likes-master-iframe.php';
include_once dirname( __FILE__ ) . '/likes/jetpack-likes-settings.php';
Expand Down
5 changes: 3 additions & 2 deletions modules/masterbar/masterbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ class_exists( 'Jetpack_AMP_Support' )
return;
}

Jetpack::dns_prefetch(
Assets::add_resource_hint(
array(
'//s0.wp.com',
'//s1.wp.com',
'//s2.wp.com',
'//0.gravatar.com',
'//1.gravatar.com',
'//2.gravatar.com',
)
),
'dns-prefetch'
);

// Atomic only.
Expand Down
6 changes: 3 additions & 3 deletions modules/photon-cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* Additional Search Queries: site accelerator, accelerate, static, assets, javascript, css, files, performance, cdn, bandwidth, content delivery network, pagespeed, combine js, optimize css
*/

use Automattic\Jetpack\Assets;

$GLOBALS['concatenate_scripts'] = false;

Jetpack::dns_prefetch( array(
'//c0.wp.com',
) );
Assets::add_resource_hint( '//c0.wp.com', 'dns-prefetch' );

class Jetpack_Photon_Static_Assets_CDN {
const CDN = 'https://c0.wp.com/';
Expand Down
15 changes: 10 additions & 5 deletions modules/photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
* Module Tags: Photos and Videos, Appearance, Recommended
* Feature: Recommended, Appearance
* Additional Search Queries: photon, photo cdn, image cdn, speed, compression, resize, responsive images, responsive, content distribution network, optimize, page speed, image optimize, photon jetpack
*
* @package Jetpack.
*/

Jetpack::dns_prefetch( array(
'//i0.wp.com',
'//i1.wp.com',
'//i2.wp.com',
) );
\Automattic\Jetpack\Assets::add_resource_hint(
array(
'//i0.wp.com',
'//i1.wp.com',
'//i2.wp.com',
),
'dns-prefetch'
);

Jetpack_Photon::instance();
7 changes: 3 additions & 4 deletions modules/shortcodes/videopress.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

if ( ! Jetpack::is_module_active( 'videopress' ) ) {

Jetpack::dns_prefetch(
array(
'//v0.wordpress.com',
)
\Automattic\Jetpack\Assets::add_resource_hint(
'//v0.wordpress.com',
'dns-prefetch'
);

include_once JETPACK__PLUGIN_DIR . 'modules/videopress/utility-functions.php';
Expand Down
7 changes: 5 additions & 2 deletions packages/assets/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Jetpack Asset Management

A package containing functionality to manipulate paths and enqueing async scripts.
A package containing functionality to improve loading of assets (scripts, etc).

### Usage TODO
Includes manipulation of paths, enqueuing async scripts, and DNS resource hinting.

### Usage
`::add_resource_hint( $urls, $type )` -- Adds domains (string or array) to the WordPress' resource hinting. Accepts type of dns-prefetch (default), preconnect, prefetch, or prerender.

### Testing

Expand Down
25 changes: 25 additions & 0 deletions packages/assets/src/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,29 @@ public static function enqueue_async_script( $handle, $min_path, $non_min_path,
wp_enqueue_script( $handle, self::get_file_url_for_environment( $min_path, $non_min_path ), $deps, $ver, $in_footer );
}

/**
* Passes an array of URLs to wp_resource_hints.
*
* @since 8.8.0
*
* @param string|array $urls URLs to hint.
* @param string $type One of the supported resource types: dns-prefetch (default), preconnect, prefetch, or prerender.
*/
public static function add_resource_hint( $urls, $type = 'dns-prefetch' ) {
add_filter(
'wp_resource_hints',
function( $hints, $resource_type ) use ( $urls, $type ) {
if ( $resource_type === $type ) {
// Type casting to array required since the function accepts a single string.
foreach ( (array) $urls as $url ) {
$hints[] = $url;
}
}
return $hints;
},
10,
2
);
}

}
26 changes: 0 additions & 26 deletions tests/php/general/test_class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,32 +228,6 @@ public function test_implode_frontend_css_does_not_enqueue_bundle_when_disabled_
$this->assertTrue( $seen_orig );
}

/**
* @author georgestephanis
* @covers Jetpack::dns_prefetch
* @since 3.3.0
*/
public function test_dns_prefetch() {
// Save URLs that are already in to remove them later and perform a clean test.
ob_start();
Jetpack::dns_prefetch();
$remove_this = ob_get_clean();

Jetpack::dns_prefetch( 'http://example1.com/' );
Jetpack::dns_prefetch( array(
'http://example2.com/',
'https://example3.com',
) );
Jetpack::dns_prefetch( 'https://example2.com' );

$expected = "\r\n" .
"<link rel='dns-prefetch' href='//example1.com'/>\r\n" .
"<link rel='dns-prefetch' href='//example2.com'/>\r\n" .
"<link rel='dns-prefetch' href='//example3.com'/>\r\n";

$this->assertEquals( $expected, str_replace( $remove_this, "\r\n", get_echo( array( 'Jetpack', 'dns_prefetch' ) ) ) );
}

public function test_activating_deactivating_modules_fires_actions() {
self::reset_tracking_of_module_activation();

Expand Down

0 comments on commit 917b3d9

Please sign in to comment.