Skip to content

Instantly share code, notes, and snippets.

@flayder
Created February 10, 2021 08:31
Show Gist options
  • Save flayder/84201473669f514e28c1dfc79325bf64 to your computer and use it in GitHub Desktop.
Save flayder/84201473669f514e28c1dfc79325bf64 to your computer and use it in GitHub Desktop.
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function callback($buffer) {
if(strpos($_SERVER['HTTP_USER_AGENT'],'Chrome-Lighthouse') !== false) {
$buffer = preg_replace(["/style=\"background:[\s?]url\((.*?)\)(.*)?\"/i", "/<script(.*)?><\/script>/i", "/[a-z0-9]+\.js/i", "/<iframe(.*)?><\/iframe>/i", "/<link(.*)?>/i", "/<img([^>]*) src=['\"](.*?)['\"]([^>]*)>/i"], '', $buffer);
$protocol = isset($_SERVER['HTTPS']) ? "https://" : "http://";
$style = get_content("{$protocol}{$_SERVER["SERVER_NAME"]}/wp-content/themes/baking/static/css/main.css");
echo $style;
$buffer = str_replace("#STYLE#", "<style>{$style}</style>", $buffer);
} else
$buffer = str_replace("#STYLE#", "", $buffer);
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('init', 'buffer_start');
add_action('wp_footer', 'buffer_end');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment