Skip to content

Commit

Permalink
Merge pull request #232 from cloudflare/PI-1307-http2-autooptimize-pl…
Browse files Browse the repository at this point in the history
…ugin

Fix HTTP/2 server push breaking Autoptimize plugin
  • Loading branch information
zackproser authored Mar 6, 2019
2 parents a46a7cb + 9eb5d0e commit 7378730
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/WordPress/HTTP2ServerPush.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

// TODO:
// TODO:
// Get rid of $GLOBALS and use static variables
// Make class functions non-static
// Add debug logs. Need dependency injection of logger to this class
Expand All @@ -23,9 +23,25 @@ public static function initHooks()
{
self::$initiated = true;

$autoptimize_js_enabled = (get_option('autoptimize_js') && get_option('autoptimize_js') === 'on');
$autoptimize_css_enabled = (get_option('autoptimize_css') && get_option('autoptimize_css') === 'on');

add_action('wp_head', array('\CF\WordPress\HTTP2ServerPush', 'http2ResourceHints'), 99, 1);
add_filter('script_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
add_filter('style_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);

// If Autoptimize exists, prefer the optimized assets it emits over usual WordPress enqueued scripts
if (class_exists('autoptimizeMain')) {
add_filter('autoptimize_filter_cache_getname', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
if (!$autoptimize_js_enabled) {
add_filter('script_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
}
if (!$autoptimize_css_enabled) {
add_filter('style_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
}
} else {
// Autoptimize plugin is not activated, so fallback to the usual WordPress script and style queues
add_filter('script_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
add_filter('style_loader_src', array('\CF\WordPress\HTTP2ServerPush', 'http2LinkPreloadHeader'), 99, 1);
}
}

public static function http2LinkPreloadHeader($src)
Expand All @@ -37,7 +53,7 @@ public static function http2LinkPreloadHeader($src)
$newHeader = sprintf(
'Link: <%s>; rel=preload; as=%s',
esc_url(self::http2LinkUrlToRelativePath($preload_src)),
sanitize_html_class(self::http2LinkResourceHintAs(current_filter()))
sanitize_html_class(self::http2LinkResourceHintAs(current_filter(), $preload_src))
);

// If the current header size is larger than 3KB (3072 bytes)
Expand All @@ -55,7 +71,7 @@ public static function http2LinkPreloadHeader($src)

header($newHeader, false);

$GLOBALS['http2_'.self::http2LinkResourceHintAs(current_filter()).'_srcs'][] = self::http2LinkUrlToRelativePath($preload_src);
$GLOBALS['http2_'.self::http2LinkResourceHintAs(current_filter(), $preload_src).'_srcs'][] = self::http2LinkUrlToRelativePath($preload_src);
}
}

Expand All @@ -68,10 +84,12 @@ public static function http2LinkPreloadHeader($src)
*/
public static function http2ResourceHints()
{

$resource_types = array('script', 'style');
array_walk($resource_types, function ($resource_type) {
if (is_array($GLOBALS["http2_{$resource_type}_srcs"])) {
array_walk($GLOBALS["http2_{$resource_type}_srcs"], function ($src) use ($resource_type) {
$key = "http2_{$resource_type}_srcs";
if (isset($GLOBALS[$key]) && is_array($GLOBALS[$key])) {
array_walk($GLOBALS[$key], function ($src) use ($resource_type) {
printf('<link rel="preload" href="%s" as="%s">', esc_url($src), esc_html($resource_type));
});
}
Expand All @@ -97,8 +115,24 @@ public static function http2LinkUrlToRelativePath($src)
*
* @return string 'style' or 'script'
*/
public static function http2LinkResourceHintAs($current_hook)
public static function http2LinkResourceHintAs($current_hook, $src)
{
return 'style_loader_src' === $current_hook ? 'style' : 'script';

switch ($current_hook) {
case 'style_loader_src':
return 'style';
case 'script_loader_src':
return 'script';
case 'autoptimize_filter_cache_getname':
$ext = pathinfo($src, PATHINFO_EXTENSION);
if ($ext === 'js') {
return 'script';
} else if ($ext === 'css') {
return 'style';
}
return '';
default:
return '';
}
}
}

0 comments on commit 7378730

Please sign in to comment.