Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace Perfmatters;
class Buffer
{
//initialize buffer class
public static function init()
{
add_action('wp', array('Perfmatters\Buffer', 'queue'));
}
//queue functions
public static function queue()
{
//inital checks
if(is_admin() || perfmatters_is_dynamic_request() || perfmatters_is_page_builder() || isset($_GET['perfmatters']) || isset($_GET['perfmattersoff'])) {
return;
}
//user agent check
if(!empty($_SERVER['HTTP_USER_AGENT'])) {
$excluded_agents = array(
'usercentrics'
);
foreach($excluded_agents as $agent) {
if(stripos($_SERVER['HTTP_USER_AGENT'], $agent) !== false) {
return;
}
}
}
//buffer is allowed
if(!apply_filters('perfmatters_allow_buffer', true)) {
return;
}
//add buffer action
add_action('template_redirect', array('Perfmatters\Buffer', 'start'), -9999);
}
//start buffer
public static function start()
{
if(has_filter('perfmatters_output_buffer_template_redirect')) {
//exclude certain requests
if(is_embed() || is_feed() || is_preview() || is_customize_preview()) {
return;
}
//don't buffer amp
if(function_exists('is_amp_endpoint') && is_amp_endpoint()) {
return;
}
ob_start(function($html) {
//check for valid buffer
if(!self::is_valid_buffer($html)) {
return $html;
}
//run buffer filters
$html = (string) apply_filters('perfmatters_output_buffer_template_redirect', $html);
//return processed html
return $html;
});
}
}
//make sure buffer content is valid
private static function is_valid_buffer($html)
{
//check for valid/invalid tags
if(stripos($html, '<html') === false || stripos($html, '</body>') === false || stripos($html, '<xsl:stylesheet') !== false) {
return false;
}
//check for invalid urls
$current_url = home_url($_SERVER['REQUEST_URI']);
$matches = apply_filters('perfmatters_buffer_excluded_extensions', array('.xml', '.txt', '.php'));
foreach($matches as $match) {
if(stripos($current_url, $match) !== false) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace Perfmatters;
class CDN
{
//initialize cdn
public static function init()
{
add_action('wp', array('Perfmatters\CDN', 'queue'));
}
//queue functions
public static function queue()
{
//add cdn rewrite to the buffer
if(!empty(Config::$options['cdn']['enable_cdn']) && !empty(Config::$options['cdn']['cdn_url'])) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\CDN', 'rewrite'));
}
}
//rewrite urls in html
public static function rewrite($html)
{
//filter check
if(!apply_filters('perfmatters_cdn', true)) {
return $html;
}
//prep site url
$siteURL = '//' . ((!empty($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : parse_url(home_url(), PHP_URL_HOST));
$escapedSiteURL = quotemeta($siteURL);
$regExURL = 'https?:' . substr($escapedSiteURL, strpos($escapedSiteURL, '//'));
//prep included directories
$directories = 'wp\-content|wp\-includes';
if(!empty(Config::$options['cdn']['cdn_directories'])) {
$directoriesArray = array_map('trim', explode(',', Config::$options['cdn']['cdn_directories']));
if(count($directoriesArray) > 0) {
$directories = implode('|', array_map('quotemeta', array_filter($directoriesArray)));
}
}
//prep included extensions
$extensions_array = apply_filters('perfmatters_cdn_extensions', array(
'avif',
'css',
'gif',
'jpeg',
'jpg',
'js',
'json',
'mp3',
'mp4',
'otf',
'pdf',
'png',
'svg',
'ttf',
'webp',
'woff',
'woff2'
));
$extensions = implode('|', $extensions_array);
//rewrite urls in html
$regEx = '#(?<=[(\"\']|&quot;)(?:' . $regExURL . ')?\/(?:(?:' . $directories . ')[^\"\')]+)\.(' . $extensions . ')[^\"\')]*(?=[\"\')]|&quot;)#';
//base exclusions
$exclusions = array('script-manager.js');
//add user exclusions
if(!empty(Config::$options['cdn']['cdn_exclusions'])) {
$exclusions_user = array_map('trim', explode(',', Config::$options['cdn']['cdn_exclusions']));
$exclusions = array_merge($exclusions, $exclusions_user);
}
//set cdn url
$cdnURL = untrailingslashit(Config::$options['cdn']['cdn_url']);
//replace urls
$html = preg_replace_callback($regEx, function($url) use ($siteURL, $cdnURL, $exclusions) {
//check for exclusions
foreach($exclusions as $exclusion) {
if(!empty($exclusion) && stristr($url[0], $exclusion) != false) {
return $url[0];
}
}
//replace url with no scheme
if(strpos($url[0], '//') === 0) {
return str_replace($siteURL, $cdnURL, $url[0]);
}
//replace non relative site url
if(strstr($url[0], $siteURL)) {
return str_replace(array('http:' . $siteURL, 'https:' . $siteURL), $cdnURL, $url[0]);
}
//replace relative url
return $cdnURL . $url[0];
}, $html);
return $html;
}
}

View File

@@ -0,0 +1,666 @@
<?php
namespace Perfmatters;
use Sabberworm\CSS\CSSList\AtRuleBlockList;
use Sabberworm\CSS\CSSList\CSSBlockList;
use Sabberworm\CSS\CSSList\Document;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parser as CSSParser;
use Sabberworm\CSS\Property\Charset;
use Sabberworm\CSS\RuleSet\DeclarationBlock;
use Sabberworm\CSS\Settings;
use Sabberworm\CSS\Value\URL;
use WP_Admin_Bar;
class CSS
{
private static $used_selectors;
private static $excluded_selectors;
//initialize css functions
public static function init()
{
if(isset($_GET['perfmatterscssoff'])) {
return;
}
if(!empty(Config::$options['assets']['remove_unused_css'])) {
add_action('wp', array('Perfmatters\CSS', 'queue'));
add_action('wp_ajax_perfmatters_clear_post_used_css', array('Perfmatters\CSS', 'clear_post_used_css'));
add_action('admin_bar_menu', array('Perfmatters\CSS', 'admin_bar_menu'));
add_action('admin_notices', array('Perfmatters\CSS', 'admin_notices'));
add_action('admin_post_perfmatters_clear_used_css', array('Perfmatters\CSS', 'admin_bar_clear_used_css'));
}
}
//queue functions
public static function queue()
{
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\CSS', 'remove_unused_css'));
}
//remove unused css
public static function remove_unused_css($html)
{
if(empty(apply_filters('perfmatters_remove_unused_css', true))) {
return $html;
}
if(Utilities::get_post_meta('perfmatters_exclude_unused_css')) {
return $html;
}
//only logged out
if(is_user_logged_in()) {
return $html;
}
//skip woocommerce
if(Utilities::is_woocommerce()) {
return $html;
}
//only known url types
$type = self::get_url_type();
if(empty($type)) {
return $html;
}
//setup file variables
$used_css_path = PERFMATTERS_CACHE_DIR . 'css/' . $type . '.used.css';
$used_css_url = PERFMATTERS_CACHE_URL . 'css/' . $type . '.used.css';
$used_css_exists = file_exists($used_css_path);
//match all stylesheets
preg_match_all('#<link\s[^>]*?href=[\'"]([^\'"]+?\.css.*?)[\'"][^>]*?\/?>#i', $html, $stylesheets, PREG_SET_ORDER);
if(!empty($stylesheets)) {
//create our css cache directory
if(!is_dir(PERFMATTERS_CACHE_DIR . 'css/')) {
@mkdir(PERFMATTERS_CACHE_DIR . 'css/', 0755, true);
}
//populate used selectors
self::get_used_selectors($html);
self::get_excluded_selectors();
$used_css_string = '';
//loop through stylesheets
foreach($stylesheets as $key => $stylesheet) {
//stylesheet check
if(!preg_match('#\srel=[\'"]stylesheet[\'"]#is', $stylesheet[0])) {
continue;
}
//ignore google fonts
if(stripos($stylesheet[1], '//fonts.googleapis.com/css') !== false || stripos($stylesheet[1], '.google-fonts.css') !== false) {
continue;
}
//exclude entire stylesheets
$stylesheet_exclusions = array(
'dashicons.min.css', //core
'/uploads/elementor/css/post-', //elementor
'animations.min.css',
'woocommerce-mobile.min.css', //woocommerce
'woocommerce-smallscreen.css',
'/uploads/oxygen/css/', //oxygen
'/uploads/bb-plugin/cache/', //beaver builder
'/uploads/generateblocks/', //generateblocks
'/et-cache/', //divi
'/widget-google-reviews/assets/css/public-main.css' //plugin for google reviews
);
if(!empty(Config::$options['assets']['rucss_excluded_stylesheets'])) {
$stylesheet_exclusions = array_merge($stylesheet_exclusions, Config::$options['assets']['rucss_excluded_stylesheets']);
}
$stylesheet_exclusions = apply_filters('perfmatters_rucss_excluded_stylesheets', $stylesheet_exclusions);
foreach($stylesheet_exclusions as $exclude) {
if(strpos($stylesheet[1], $exclude) !== false) {
unset($stylesheets[$key]);
continue 2;
}
}
//need to generate used css
if(!$used_css_exists) {
//get local stylesheet path
$url = str_replace(trailingslashit(apply_filters('perfmatters_local_stylesheet_url', (!empty(Config::$options['assets']['rucss_cdn_url']) ? Config::$options['assets']['rucss_cdn_url'] : site_url()))), '', explode('?', $stylesheet[1])[0]);
$file = str_replace('/wp-content', '/', WP_CONTENT_DIR) . $url;
//make sure local file exists
if(!file_exists($file)) {
continue;
}
//get used css from stylesheet
$used_css = self::clean_stylesheet($stylesheet[1], @file_get_contents($file));
//add used stylesheet css to total used
$used_css_string.= $used_css;
}
//delay stylesheets
if(empty(Config::$options['assets']['rucss_stylesheet_behavior'])) {
$new_link = preg_replace('#href=([\'"]).+?\1#', 'data-pmdelayedstyle="' . $stylesheet[1] . '"',$stylesheet[0]);
$html = str_replace($stylesheet[0], $new_link, $html);
}
//async stylesheets
elseif(Config::$options['assets']['rucss_stylesheet_behavior'] == 'async') {
$new_link = preg_replace(array('#media=([\'"]).+?\1#', '#onload=([\'"]).+?\1#'), '', $stylesheet[0]);
$new_link = str_replace('<link', '<link media="print" onload="this.media=\'all\';this.onload=null;"', $new_link);
$html = str_replace($stylesheet[0], $new_link, $html);
}
//remove stylesheets
elseif(Config::$options['assets']['rucss_stylesheet_behavior'] == 'remove') {
$html = str_replace($stylesheet[0], '', $html);
}
}
//store used css file
if(!empty($used_css_string)) {
if(file_put_contents($used_css_path, apply_filters('perfmatters_used_css', $used_css_string)) !== false) {
$time = get_option('perfmatters_used_css_time', array());
$time[$type] = time();
//update stored timestamp
update_option('perfmatters_used_css_time', $time);
}
}
//print used css inline after first title tag
$pos = strpos($html, '</title>');
if($pos !== false) {
//print file
if(!empty(Config::$options['assets']['rucss_method']) && Config::$options['assets']['rucss_method'] == 'file') {
//grab stored timestamp for query string
$time = get_option('perfmatters_used_css_time', array());
if(!empty($time[$type])) {
$used_css_url = add_query_arg('ver', $time[$type], $used_css_url);
}
$used_css_output = "<link rel='preload' href='" . $used_css_url . "' as='style' onload=\"this.rel='stylesheet';this.removeAttribute('onload');\">";
$used_css_output.= '<link rel="stylesheet" id="perfmatters-used-css" href="' . $used_css_url . '" media="all" />';
}
//print inline
else {
$used_css_output = '<style id="perfmatters-used-css">' . file_get_contents($used_css_path) . '</style>';
}
$html = substr_replace($html, '</title>' . $used_css_output, $pos, 8);
}
//delay stylesheet script
if(empty(Config::$options['assets']['rucss_stylesheet_behavior'])) {
$delay_check = !empty(apply_filters('perfmatters_delay_js', !empty(Config::$options['assets']['delay_js']))) && !Utilities::get_post_meta('perfmatters_exclude_delay_js');
if(!$delay_check || empty(Config::$options['assets']['delay_js_behavior']) || isset($_GET['perfmattersjsoff'])) {
$script = '<script type="text/javascript" id="perfmatters-delayed-styles-js">!function(){const e=["keydown","mousemove","wheel","touchmove","touchstart","touchend"];function t(){document.querySelectorAll("link[data-pmdelayedstyle]").forEach(function(e){e.setAttribute("href",e.getAttribute("data-pmdelayedstyle"))}),e.forEach(function(e){window.removeEventListener(e,t,{passive:!0})})}e.forEach(function(e){window.addEventListener(e,t,{passive:!0})})}();</script>';
$html = str_replace('</body>', $script . '</body>', $html);
}
}
}
return $html;
}
//get url type
private static function get_url_type()
{
global $wp_query;
$type = '';
if($wp_query->is_page) {
$type = is_front_page() ? 'front' : 'page-' . $wp_query->post->ID;
}
elseif($wp_query->is_home) {
$type = 'home';
}
elseif($wp_query->is_single) {
$type = get_post_type() !== false ? get_post_type() : 'single';
}
elseif($wp_query->is_category) {
$type = 'category';
}
elseif($wp_query->is_tag) {
$type = 'tag';
}
elseif($wp_query->is_tax) {
$type = 'tax';
}
elseif($wp_query->is_archive) {
$type = $wp_query->is_day ? 'day' : ($wp_query->is_month ? 'month' : ($wp_query->is_year ? 'year' : ($wp_query->is_author ? 'author' : 'archive')));
}
elseif($wp_query->is_search) {
$type = 'search';
}
elseif($wp_query->is_404) {
$type = '404';
}
return $type;
}
//get used selectors in html
private static function get_used_selectors($html) {
if(!$html) {
return;
}
//get dom document
$libxml_previous = libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$result = $dom->loadHTML($html);
libxml_clear_errors();
libxml_use_internal_errors($libxml_previous);
if($result) {
$dom->xpath = new \DOMXPath($dom);
//setup used selectors array
self::$used_selectors = array('tags' => array(), 'ids' => array(), 'classes' => array());
//search for used selectors in dom
$classes = array();
foreach($dom->getElementsByTagName('*') as $tag) {
//add tag
self::$used_selectors['tags'][$tag->tagName] = 1;
//add add tag id
if($tag->hasAttribute('id')) {
self::$used_selectors['ids'][$tag->getAttribute('id')] = 1;
}
//store tag classes
if($tag->hasAttribute('class')) {
$class = $tag->getAttribute('class');
$tag_classes = preg_split('/\s+/', $class);
array_push($classes, ...$tag_classes);
}
}
//add classes
$classes = array_filter(array_unique($classes));
if($classes) {
self::$used_selectors['classes'] = array_fill_keys($classes, 1);
}
}
}
//get excluded selectors
private static function get_excluded_selectors() {
self::$excluded_selectors = array(
'.wp-embed-responsive', //core
'.wp-block-embed',
'.wp-block-embed__wrapper',
'.wp-caption',
'#elementor-device-mode', //elementor
'.elementor-nav-menu',
'.elementor-has-item-ratio',
'.elementor-popup-modal',
'.elementor-sticky--active',
'.dialog-type-lightbox',
'.dialog-widget-content',
'.lazyloaded',
'.elementor-motion-effects-container',
'.elementor-motion-effects-layer',
'.ast-header-break-point', //astra
'.dropdown-nav-special-toggle', //kadence
'rs-fw-forcer' //rev slider
);
if(!empty(Config::$options['assets']['rucss_excluded_selectors'])) {
self::$excluded_selectors = array_merge(self::$excluded_selectors, Config::$options['assets']['rucss_excluded_selectors']);
}
self::$excluded_selectors = apply_filters('perfmatters_rucss_excluded_selectors', self::$excluded_selectors);
}
//remove unusde css from stylesheet
private static function clean_stylesheet($url, $css)
{
//https://github.com/sabberworm/PHP-CSS-Parser/issues/150
$css = preg_replace('/^\xEF\xBB\xBF/', '', $css);
//setup css parser
$settings = Settings::create()->withMultibyteSupport(false);
$parser = new CSSParser($css, $settings);
$parsed_css = $parser->parse();
//convert relative urls to full urls
self::fix_relative_urls($url, $parsed_css);
$css_data = self::prep_css_data($parsed_css);
return self::remove_unused_selectors($css_data);
}
//convert relative urls to full urls
private static function fix_relative_urls($stylesheet_url, Document $data)
{
//get base url from stylesheet
$base_url = preg_replace('#[^/]+(\?.*)?$#', '', $stylesheet_url);
//search css for urls
$values = $data->getAllValues();
foreach($values as $value) {
if(!($value instanceof URL)) {
continue;
}
$url = $value->getURL()->getString();
//not relative
if(preg_match('/^(https?|data):/', $url)) {
continue;
}
$parsed_url = parse_url($url);
//final checks
if(!empty($parsed_url['host']) || empty($parsed_url['path']) || $parsed_url['path'][0] === '/') {
continue;
}
//create full url and replace
$new_url = $base_url . $url;
$value->getUrl()->setString($new_url);
}
}
//prep parsed css for cleaning
private static function prep_css_data(CSSBlockList $data)
{
$items = array();
foreach($data->getContents() as $content) {
//remove charset objects since were printing inline
if($content instanceof Charset) {
continue;
}
if($content instanceof AtRuleBlockList) {
$items[] = array(
'rulesets' => self::prep_css_data($content),
'at_rule' => "@{$content->atRuleName()} {$content->atRuleArgs()}",
);
}
else {
$item = array('css' => $content->render(OutputFormat::createCompact()));
if($content instanceof DeclarationBlock) {
$item['selectors'] = self::sort_selectors($content->getSelectors());
}
$items[] = $item;
}
}
return $items;
}
//sort selectors into different categories we need
private static function sort_selectors($selectors)
{
$selectors = array_map(
function($sel) {
return $sel->__toString();
},
$selectors
);
$selectors_data = array();
foreach($selectors as $selector) {
//setup selector data array
$data = array(
'selector' => trim($selector),
'classes' => array(),
'ids' => array(),
'tags' => array(),
'atts' => array()
);
//eliminate false negatives (:not(), pseudo, etc...)
$selector = preg_replace('/(?<!\\\\)::?[a-zA-Z0-9_-]+(\(.+?\))?/', '', $selector);
//atts
$selector = preg_replace_callback(
'/\[([A-Za-z0-9_:-]+)(\W?=[^\]]+)?\]/',
function($matches) use (&$data) {
$data['atts'][] = $matches[1];
return '';
},
$selector
);
//classes
$selector = preg_replace_callback(
'/\.((?:[a-zA-Z0-9_-]+|\\\\.)+)/',
function($matches) use (&$data) {
$data['classes'][] = stripslashes($matches[1]);
return '';
},
$selector
);
//ids
$selector = preg_replace_callback(
'/#([a-zA-Z0-9_-]+)/',
function($matches) use (&$data) {
$data['ids'][] = $matches[1];
return '';
},
$selector
);
//tags
$selector = preg_replace_callback(
'/[a-zA-Z0-9_-]+/',
function($matches) use (&$data) {
$data['tags'][] = $matches[0];
return '';
},
$selector
);
//add selector data to main array
$selectors_data[] = array_filter($data);
}
return array_filter($selectors_data);
}
//remove unused selectors from css data
private static function remove_unused_selectors($data)
{
$rendered = [];
foreach($data as $item) {
//has css
if(isset($item['css'])) {
//need at least one selector match
$should_render = !isset($item['selectors']) || 0 !== count(array_filter($item['selectors'],
function($selector) {
return self::is_selector_used($selector);
}
));
if($should_render) {
$rendered[] = $item['css'];
}
continue;
}
//nested rulesets
if(!empty($item['rulesets'])) {
$child_rulesets = self::remove_unused_selectors($item['rulesets']);
if($child_rulesets) {
$rendered[] = sprintf('%s{%s}', $item['at_rule'], $child_rulesets);
}
}
}
return implode("", $rendered);
}
//check if selector is used
private static function is_selector_used($selector)
{
//:root selector
if($selector['selector'] === ':root') {
return true;
}
//lone attribute selector
if(!empty($selector['atts']) && (empty($selector['classes']) && empty($selector['ids']) && empty($selector['tags']))) {
return true;
}
//search for excluded selector match
if(!empty(self::$excluded_selectors)) {
foreach(self::$excluded_selectors as $key => $value) {
if(preg_match('#(' . preg_quote($value) . ')(?=\s|\.|\:|,|\[|$)#', $selector['selector'])) {
return true;
}
}
}
//is selector used in the dom
foreach(array('classes', 'ids', 'tags') as $type) {
if(!empty($selector[$type])) {
//cast array if needed
$targets = (array)$selector[$type];
foreach($targets as $target) {
//bail if a target doesn't exist
if(!isset(self::$used_selectors[$type][$target])) {
return false;
}
}
}
}
return true;
}
//delete all files in the css cache directory
public static function clear_used_css()
{
$files = glob(PERFMATTERS_CACHE_DIR . 'css/*');
foreach($files as $file) {
if(is_file($file)) {
unlink($file);
}
}
delete_option('perfmatters_used_css_time');
}
//clear used css file for specific post or post type
public static function clear_post_used_css() {
if(empty($_POST['action']) || empty($_POST['nonce']) || empty($_POST['post_id'])) {
return;
}
if($_POST['action'] != 'perfmatters_clear_post_used_css') {
return;
}
if(!wp_verify_nonce($_POST['nonce'], 'perfmatters_clear_post_used_css')) {
return;
}
$post_id = (int)$_POST['post_id'];
$post_type = get_post_type($post_id);
$path = $post_type == 'page' ? 'page-' . $post_id : $post_type;
$file = PERFMATTERS_CACHE_DIR . 'css/' . $path . '.used.css';
if(is_file($file)) {
unlink($file);
}
wp_send_json_success();
exit;
}
//add admin bar menu item
public static function admin_bar_menu(WP_Admin_Bar $wp_admin_bar) {
if(!current_user_can('manage_options') || !perfmatters_network_access()) {
return;
}
$type = !is_admin() ? self::get_url_type() : '';
$menu_item = array(
'parent' => 'perfmatters',
'id' => 'perfmatters-clear-used-css',
'title' => __('Clear Used CSS', 'perfmatters') . ' (' . (!empty($type) ? __('Current', 'perfmatters') : __('All', 'perfmatters')) . ')',
'href' => add_query_arg(array(
'action' => 'perfmatters_clear_used_css',
'_wp_http_referer' => rawurlencode($_SERVER['REQUEST_URI']),
'_wpnonce' => wp_create_nonce('perfmatters_clear_used_css'),
'type' => $type
),
admin_url('admin-post.php'))
);
$wp_admin_bar->add_menu($menu_item);
}
//display admin notices
public static function admin_notices() {
if(get_transient('perfmatters_used_css_cleared') === false) {
return;
}
delete_transient('perfmatters_used_css_cleared');
echo '<div class="notice notice-success is-dismissible"><p><strong>' . __('Used CSS cleared.', 'perfmatters' ) . '</strong></p></div>';
}
//clear used css from admin bar
public static function admin_bar_clear_used_css() {
if(!isset($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'perfmatters_clear_used_css')) {
wp_nonce_ays('');
}
if(!empty($_GET['type'])) {
//clear specific type
$file = PERFMATTERS_CACHE_DIR . 'css/' . $_GET['type'] . '.used.css';
if(is_file($file)) {
unlink($file);
}
}
else {
//clear all
self::clear_used_css();
if(is_admin()) {
set_transient('perfmatters_used_css_cleared', 1);
}
}
//go back to url where button was pressed
wp_safe_redirect(esc_url_raw(wp_get_referer()));
exit;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Perfmatters;
use WP_Admin_Bar;
class Config
{
public static $options;
public static $tools;
//initialize config
public static function init()
{
//load plugin options
self::$options = get_option('perfmatters_options');
self::$tools = get_option('perfmatters_tools');
//actions
add_action('admin_bar_menu', array('Perfmatters\Config', 'admin_bar_menu'), 500);
}
//setup admin bar menu
public static function admin_bar_menu(WP_Admin_Bar $wp_admin_bar)
{
if(!current_user_can('manage_options') || !perfmatters_network_access() || !empty(self::$tools['hide_admin_bar_menu'])) {
return;
}
//add top level menu item
$wp_admin_bar->add_menu(array(
'id' => 'perfmatters',
'title' => 'Perfmatters',
'href' => admin_url('options-general.php?page=perfmatters')
));
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace Perfmatters;
class DatabaseOptimizationProcess extends \WP_Background_Process {
//prefix for process
protected $prefix = 'perfmatters';
//action name for process
protected $action = 'database_optimization';
//totals of removed items
protected $counts = array();
//run on each queue item
protected function task($item) {
global $wpdb;
switch($item) {
case 'post_revisions' :
$query = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'revision'");
if($query) {
$count = 0;
foreach($query as $id) {
$count += wp_delete_post_revision(intval($id)) instanceof \WP_Post ? 1 : 0;
}
$this->counts[$item] = $count;
}
break;
case 'post_auto_drafts' :
$query = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft'");
if($query) {
$count = 0;
foreach($query as $id) {
$count += wp_delete_post(intval($id), true) instanceof \WP_Post ? 1 : 0;
}
$this->counts[$item] = $count;
}
break;
case 'trashed_posts' :
$query = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'trash'");
if($query) {
$count = 0;
foreach($query as $id) {
$count += wp_delete_post($id, true) instanceof \WP_Post ? 1 : 0;
}
$this->counts[$item] = $count;
}
break;
case 'spam_comments' :
$query = $wpdb->get_col("SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = 'spam'");
if($query) {
$count = 0;
foreach($query as $id) {
$count += (int) wp_delete_comment(intval($id), true);
}
$this->counts[$item] = $count;
}
break;
case 'trashed_comments' :
$query = $wpdb->get_col("SELECT comment_ID FROM $wpdb->comments WHERE (comment_approved = 'trash' OR comment_approved = 'post-trashed')");
if($query) {
$count = 0;
foreach($query as $id) {
$count += (int) wp_delete_comment(intval($id), true);
}
$this->counts[$item] = $count;
}
break;
case 'expired_transients' :
$time = isset($_SERVER['REQUEST_TIME']) ? (int) $_SERVER['REQUEST_TIME'] : time();
$query = $wpdb->get_col($wpdb->prepare("SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s AND option_value < %d", $wpdb->esc_like('_transient_timeout') . '%', $time));
if($query) {
$count = 0;
foreach($query as $transient) {
$key = str_replace('_transient_timeout_', '', $transient);
$count += (int) delete_transient($key);
}
$this->counts[$item] = $count;
}
break;
case 'all_transients' :
$query = $wpdb->get_col($wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like('_transient_') . '%', $wpdb->esc_like('_site_transient_') . '%'));
if($query) {
$count = 0;
foreach($query as $transient) {
if(strpos($transient, '_site_transient_') !== false) {
$count += (int) delete_site_transient(str_replace('_site_transient_', '', $transient));
} else {
$count += (int) delete_transient(str_replace('_transient_', '', $transient));
}
}
$this->counts[$item] = $count;
}
break;
case 'tables' :
$query = $wpdb->get_results("SELECT table_name, data_free FROM information_schema.tables WHERE table_schema = '" . DB_NAME . "' and Engine <> 'InnoDB' and data_free > 0");
if($query) {
$count = 0;
foreach($query as $table) {
$count += (int) $wpdb->query("OPTIMIZE TABLE $table->table_name");
}
$this->counts[$item] = $count;
}
break;
}
return false;
}
//run background process on queue
public function dispatch() {
//set our working transient
set_transient('perfmatters_database_optimization_process', 'working', HOUR_IN_SECONDS);
//run parent dispatch
return parent::dispatch();
}
//run when background process is complete
protected function complete() {
//delete our working transient
delete_transient('perfmatters_database_optimization_process');
//set complete transient
set_transient('perfmatters_database_optimization_process_complete', $this->counts);
//run parent complete
parent::complete();
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace Perfmatters;
class DatabaseOptimizer {
//declare our optimizer
protected $optimizer;
//actions + filters
public function __construct() {
//initialize optimizer
$this->optimizer = new DatabaseOptimizationProcess();
add_filter('pre_update_option_perfmatters_tools', array($this, 'perfmatters_database_optimization_action'), 10, 2);
add_action('admin_notices', array($this, 'perfmatters_database_optimization_notices'));
add_filter('cron_schedules', array($this, 'perfmatters_add_database_optimization_cron_schedule'));
add_action('init', array($this, 'perfmatters_schedule_database_optimization'));
add_action('perfmatters_database_optimization', array($this, 'perfmatters_run_scheduled_database_optimization'));
}
//run the background process
public function process_handler($items) {
//push the requested items to the queue
array_map(array($this->optimizer, 'push_to_queue'), $items);
//run the process
$this->optimizer->save()->dispatch();
}
//watch and respond to the optimize button
public function perfmatters_database_optimization_action($new_value, $old_value) {
//optimize button was pressed
if(!empty($new_value['database']['optimize_database'])) {
//stop and show error if process is already running
$working = get_transient('perfmatters_database_optimization_process');
if($working !== false) {
add_settings_error('perfmatters', 'perfmatters-database-optimization-running', __('There is already an existing database optimization process running.', 'perfmatters'), 'error');
return $old_value;
}
//get available options array
$optimize_options = array_keys($this->perfmatters_get_database_options());
//build array of requested items
$items = array();
foreach($optimize_options as $item) {
if(!empty($new_value['database'][$item])) {
$items[] = $item;
}
}
//run process handler
if(!empty($items)) {
$this->process_handler($items);
}
//add hidden notice to prevent save message
add_settings_error('perfmatters', 'perfmatters-hidden-notice', '', 'success');
return $old_value;
}
$new_optimize_schedule = isset($new_value['database']['optimize_schedule']) ? $new_value['database']['optimize_schedule'] : '';
$old_optimize_schedule = isset($old_value['database']['optimize_schedule']) ? $old_value['database']['optimize_schedule'] : '';
//optimize schedule was changed
if($new_optimize_schedule !== $old_optimize_schedule) {
if(wp_next_scheduled('perfmatters_database_optimization')) {
wp_clear_scheduled_hook('perfmatters_database_optimization');
}
}
return $new_value;
}
//display notices for database optimization process
public function perfmatters_database_optimization_notices() {
//permissions check
if(!current_user_can('manage_options')) {
return;
}
//make sure were on our settings page
if(empty($_GET['page']) || $_GET['page'] !== 'perfmatters') {
return;
}
//get working transient
$working = get_transient('perfmatters_database_optimization_process');
if($working !== false) {
$notice_type = "info";
$message = __('Database optimization is running in the background.', 'perfmatters');
}
else {
//get completed optimized transient
$completed = get_transient('perfmatters_database_optimization_process_complete');
if($completed === false) {
return;
}
$notice_type = "success";
//get db options array
$database_options = $this->perfmatters_get_database_options();
//build admin notice message
if(!empty($completed)) {
$message = __('Database optimization completed. The following items were removed:', 'perfmatters');
$message.= "<ul style='margin: 0px; padding: 0px 2px;'>";
foreach($completed as $key => $count) {
$message.= "<li>";
$message.= "<strong>" . $database_options[$key] . ':</strong> ' . $count;
$message.= "</li>";
}
$message.= "</ul>";
}
else {
$message = __('Database optimization completed. No optimizations found.', 'perfmatters');
}
//delete our completed transient
delete_transient('perfmatters_database_optimization_process_complete');
}
//display admin notice
if(!empty($message)) {
echo "<div class='notice notice-" . $notice_type . " is-dismissible'>";
echo "<p>" . $message . "</p>";
echo"</div>";
}
}
//add cron schedule
public function perfmatters_add_database_optimization_cron_schedule($schedules) {
$perfmatters_tools = get_option('perfmatters_tools');
if(empty($perfmatters_tools['database']['optimize_schedule']) || $perfmatters_tools['database']['optimize_schedule'] == 'daily') {
return $schedules;
}
switch($perfmatters_tools['database']['optimize_schedule']) {
case 'weekly' :
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __('Once Weekly', 'perfmatters'),
);
break;
case 'monthly' :
$schedules['monthly'] = array(
'interval' => 2592000,
'display' => __('Once Monthly', 'perfmatters'),
);
break;
default :
break;
}
return $schedules;
}
//create database optimization scheduled event
public function perfmatters_schedule_database_optimization() {
$perfmatters_tools = get_option('perfmatters_tools');
if(!empty($perfmatters_tools['database']['optimize_schedule']) && !wp_next_scheduled('perfmatters_database_optimization')) {
wp_schedule_event(time(), $perfmatters_tools['database']['optimize_schedule'], 'perfmatters_database_optimization');
}
}
//scheduled event action
public function perfmatters_run_scheduled_database_optimization() {
$perfmatters_tools = get_option('perfmatters_tools');
$optimize_options = array_keys($this->perfmatters_get_database_options());
//build array of set items
$items = array();
foreach($optimize_options as $item) {
if(!empty($perfmatters_tools['database'][$item])) {
$items[] = $item;
}
}
//run process handler
if(!empty($items)) {
$this->process_handler($items);
}
}
//return array of database options
protected function perfmatters_get_database_options() {
return array(
'post_revisions' => __('Revisions', 'perfmatters'),
'post_auto_drafts' => __('Auto Drafts', 'perfmatters'),
'trashed_posts' => __('Trashed Posts', 'perfmatters'),
'spam_comments' => __('Spam Comments', 'perfmatters'),
'trashed_comments' => __('Trashed Comments', 'perfmatters'),
'expired_transients' => __('Expired transients', 'perfmatters'),
'all_transients' => __('Transients', 'perfmatters'),
'tables' => __('Tables', 'perfmatters')
);
}
}

View File

@@ -0,0 +1,188 @@
<?php
namespace Perfmatters;
use WpOrg\Requests\Requests; //wp 6.2+
if(!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
}
use Requests as RequestsOld; //deprecated
class Fonts
{
private static $font_file_cache_url = PERFMATTERS_CACHE_URL;
//initialize fonts
public static function init() {
if(empty(Config::$options['fonts']['disable_google_fonts'])) {
add_action('wp', array('Perfmatters\Fonts', 'queue'));
}
}
//queue functions
public static function queue()
{
//add display swap to the buffer
if(!empty(Config::$options['fonts']['display_swap'])) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\Fonts', 'display_swap'));
}
//add local google fonts to the buffer
if(!empty(Config::$options['fonts']['local_google_fonts'])) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\Fonts', 'local_google_fonts'));
}
}
//add display swap to google font files
public static function display_swap($html) {
//find google fonts
preg_match_all('#<link[^>]+?href=(["\'])([^>]*?fonts\.googleapis\.com\/css.*?)\1.*?>#i', $html, $google_fonts, PREG_SET_ORDER);
if(!empty($google_fonts)) {
foreach($google_fonts as $google_font) {
//replace display parameter
$new_href = preg_replace('/&display=(auto|block|fallback|optional|swap)/', '', html_entity_decode($google_font[2]));
$new_href.= '&display=swap';
//create font tag with new href
$new_google_font = str_replace($google_font[2], $new_href, $google_font[0]);
//replace original font tag
$html = str_replace($google_font[0], $new_google_font, $html);
}
}
return $html;
}
//download and host google font files locally
public static function local_google_fonts($html) {
//create our fonts cache directory
if(!is_dir(PERFMATTERS_CACHE_DIR . 'fonts/')) {
@mkdir(PERFMATTERS_CACHE_DIR . 'fonts/', 0755, true);
}
//rewrite cdn url in font file cache url
$cdn_url = !empty(Config::$options['fonts']['cdn_url']) ? Config::$options['fonts']['cdn_url'] : (!empty(Config::$options['cdn']['enable_cdn']) && !empty(Config::$options['cdn']['cdn_url']) ? Config::$options['cdn']['cdn_url'] : '');
if(!empty($cdn_url)) {
self::$font_file_cache_url = str_replace(site_url(), untrailingslashit($cdn_url), PERFMATTERS_CACHE_URL);
}
//remove existing google font preconnect + prefetch links
preg_match_all('#<link(?:[^>]+)?href=(["\'])([^>]*?fonts\.(gstatic|googleapis)\.com.*?)\1.*?>#i', $html, $google_links, PREG_SET_ORDER);
if(!empty($google_links)) {
foreach($google_links as $google_link) {
if(preg_match('#rel=(["\'])(.*?(preconnect|prefetch).*?)\1#i', $google_link[0])) {
$html = str_replace($google_link[0], '', $html);
}
}
}
//find google fonts
preg_match_all('#<link[^>]+?href=(["\'])([^>]*?fonts\.googleapis\.com\/css.*?)\1.*?>#i', $html, $google_fonts, PREG_SET_ORDER);
if(!empty($google_fonts)) {
foreach($google_fonts as $google_font) {
//create unique file details
$file_name = substr(md5($google_font[2]), 0, 12) . ".google-fonts.css";
$file_path = PERFMATTERS_CACHE_DIR . 'fonts/' . $file_name;
$file_url = PERFMATTERS_CACHE_URL . 'fonts/' . $file_name;
//download file if it doesn't exist
if(!file_exists($file_path)) {
if(!self::download_google_font($google_font[2], $file_path)) {
continue;
}
}
//create font tag with new url
$new_google_font = str_replace($google_font[2], $file_url, $google_font[0]);
//async
if(!empty(Config::$options['fonts']['async'])) {
$new_google_font = preg_replace(array('#media=([\'"]).+?\1#', '#onload=([\'"]).+?\1#'), '', $new_google_font);
$new_google_font = str_replace('<link', '<link media="print" onload="this.media=\'all\';this.onload=null;"', $new_google_font);
}
//replace original font tag
$html = str_replace($google_font[0], $new_google_font, $html);
}
}
return $html;
}
//download and save google font css file
private static function download_google_font($url, $file_path)
{
//add https if using relative scheme
if(substr($url, 0, 2) === '//') {
$url = 'https:' . $url;
}
//download css file
$css_response = wp_remote_get(html_entity_decode($url), array('user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'));
//check valid response
if(wp_remote_retrieve_response_code($css_response) !== 200) {
return false;
}
//css content
$css = $css_response['body'];
//find font files inside the css
$regex = '/url\((https:\/\/fonts\.gstatic\.com\/.*?)\)/';
preg_match_all($regex, $css, $matches);
$font_urls = array_unique($matches[1]);
$font_requests = array();
foreach($font_urls as $font_url) {
if(!file_exists(PERFMATTERS_CACHE_DIR . 'fonts/' . basename($font_url))) {
$font_requests[] = array('url' => $font_url, 'type' => 'GET');
}
$cached_font_url = self::$font_file_cache_url . 'fonts/' . basename($font_url);
$css = str_replace($font_url, $cached_font_url, $css);
}
//download new font files to cache directory
if(method_exists(WpOrg\Requests\Requests::class, 'request_multiple')) { //wp 6.2+
$font_responses = WpOrg\Requests\Requests::request_multiple($font_requests);
}
elseif(method_exists(RequestsOld::class, 'request_multiple')) { //deprecated
$font_responses = RequestsOld::request_multiple($font_requests);
}
if(!empty($font_responses)) {
foreach($font_responses as $font_response) {
if(is_a($font_response, 'Requests_Response') || is_a($font_response, 'WpOrg\Requests\Response')) {
$font_path = PERFMATTERS_CACHE_DIR . 'fonts/' . basename($font_response->url);
//save font file
file_put_contents($font_path, $font_response->body);
}
}
}
//save final css file
file_put_contents($file_path, $css);
return true;
}
//delete all files in the fonts cache directory
public static function clear_local_fonts()
{
$files = glob(PERFMATTERS_CACHE_DIR . 'fonts/*');
foreach($files as $file) {
if(is_file($file)) {
unlink($file);
}
}
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Perfmatters;
class Images
{
//initialize image functions
public static function init()
{
add_action('wp', array('Perfmatters\Images', 'queue'));
}
//queue functions
public static function queue()
{
//image dimensions
if(!empty(Config::$options['lazyload']['image_dimensions']) && !empty(Config::$options['lazyload']['image_dimensions'])) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\Images', 'image_dimensions'));
}
}
//fix images missing dimensions
public static function image_dimensions($html)
{
//match all img tags without width or height attributes
preg_match_all('#<img((?:[^>](?!(height|width)=[\'\"](?:\S+)[\'\"]))*+)>#is', $html, $images, PREG_SET_ORDER);
if(!empty($images)) {
//remove any duplicate images
$images = array_unique($images, SORT_REGULAR);
//exclude specific images
$image_exclusions = array(
';base64'
);
$image_exclusions = apply_filters('perfmatters_image_dimensions_exclusions', $image_exclusions);
//loop through images
foreach($images as $image) {
//get image attributes array
$image_atts = Utilities::get_atts_array($image[1]);
if(!empty($image_atts['src'])) {
foreach($image_exclusions as $exclude) {
if(strpos($image[1], $exclude) !== false) {
continue 2;
}
}
//get image dimensions
$dimensions = self::get_dimensions_from_url($image_atts['src']);
if(!empty($dimensions)) {
//remove any existing dimension attributes
$new_image = preg_replace('/(height|width)=[\'"](?:\S+)*[\'"]/i', '', $image[0]);
//add dimension attributes to img tag
$new_image = preg_replace('/<\s*img/i', '<img width="' . $dimensions['width'] . '" height="' . $dimensions['height'] . '"', $new_image);
//replace original img tag in html
if(!empty($new_image)) {
$html = str_replace($image[0], $new_image, $html);
}
}
}
}
}
return $html;
}
//return array of dimensions based on image url
private static function get_dimensions_from_url($url)
{
//grab dimensions from file name if available
if(preg_match('/(?:.+)-([0-9]+)x([0-9]+)\.(jpg|jpeg|png|gif|svg)$/', $url, $matches)) {
return array('width' => $matches[1], 'height' => $matches[2]);
}
//get image path
$parsed_url = parse_url($url);
if(empty($parsed_url['path'])) {
return false;
}
$image_path = str_replace('/wp-content', '', WP_CONTENT_DIR) . '/' . $parsed_url['path'];
if(file_exists($image_path)) {
//get dimensions from file
$sizes = getimagesize($image_path);
if(!empty($sizes)) {
return array('width' => $sizes[0], 'height' => $sizes[1]);
}
}
return false;
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,725 @@
<?php
namespace Perfmatters;
class LazyLoad
{
//initialize lazyload iframe functions
public static function init_iframes()
{
add_action('wp', array('Perfmatters\LazyLoad', 'queue_iframes'));
}
//queue iframe functions
public static function queue_iframes()
{
//check filters
if(empty(apply_filters('perfmatters_lazyload', !empty(Config::$options['lazyload']['lazy_loading_iframes'])))) {
return;
}
//skip woocommerce
if(Utilities::is_woocommerce()) {
return;
}
//check post meta
if(Utilities::get_post_meta('perfmatters_exclude_lazy_loading')) {
return;
}
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\LazyLoad', 'iframes_buffer'));
}
//process iframe buffer
public static function iframes_buffer($html) {
$clean_html = Utilities::clean_html($html);
$html = self::lazyload_iframes($html, $clean_html);
$html = self::lazyload_videos($html, $clean_html);
return $html;
}
//lazy load iframes
public static function lazyload_iframes($html, $buffer) {
//match all iframes
preg_match_all('#<iframe(\s.+)>.*</iframe>#iUs', $buffer, $iframes, PREG_SET_ORDER);
if(!empty($iframes)) {
//remove any duplicates
$iframes = array_unique($iframes, SORT_REGULAR);
foreach($iframes as $iframe) {
//get iframe attributes array
$iframe_atts = Utilities::get_atts_array($iframe[1]);
//dont check excluded if forced attribute was found
if(!self::lazyload_excluded($iframe[1], self::lazyload_forced_atts())) {
//skip if exluded attribute was found
if(self::lazyload_excluded($iframe[1], self::lazyload_excluded_atts())) {
continue;
}
//skip if no-lazy class is found
if(!empty($iframe_atts['class']) && strpos($iframe_atts['class'], 'no-lazy') !== false) {
continue;
}
}
//skip if no src is found
if(empty($iframe_atts['src'])) {
continue;
}
//try rendering youtube preview placeholder if we need to
if(!empty(Config::$options['lazyload']['youtube_preview_thumbnails'])) {
$iframe['src'] = trim($iframe_atts['src']);
$iframe_lazyload = self::lazyload_youtube_iframe($iframe);
}
//default iframe placeholder
if(empty($iframe_lazyload)) {
$iframe_atts['class'] = !empty($iframe_atts['class']) ? $iframe_atts['class'] . ' ' . 'perfmatters-lazy' : 'perfmatters-lazy';
//migrate src
$iframe_atts['data-src'] = $iframe_atts['src'];
unset($iframe_atts['src']);
//replace iframe attributes string
$iframe_lazyload = str_replace($iframe[1], ' ' . Utilities::get_atts_string($iframe_atts), $iframe[0]);
//add noscript original iframe
if(apply_filters('perfmatters_lazyload_noscript', true)) {
$iframe_lazyload.= '<noscript>' . $iframe[0] . '</noscript>';
}
}
//replace iframe with placeholder
$html = str_replace($iframe[0], $iframe_lazyload, $html);
unset($iframe_lazyload);
}
}
return $html;
}
//prep youtube iframe for lazy loading
public static function lazyload_youtube_iframe($iframe) {
if(!$iframe) {
return false;
}
//attempt to get the id based on url
$result = preg_match('#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU', $iframe['src'], $matches);
//return false if there is no usable id
if(!$result || $matches[1] === 'videoseries') {
return false;
}
$youtube_id = $matches[1];
//parse iframe src url
$query = wp_parse_url(htmlspecialchars_decode($iframe['src']), PHP_URL_QUERY);
//clean up the url
$parsed_url = wp_parse_url($iframe['src'], -1);
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '//';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$youtube_url = $scheme . $host . $path;
//thumbnail resolutions
$resolutions = array(
'default' => array(
'width' => 120,
'height' => 90,
),
'mqdefault' => array(
'width' => 320,
'height' => 180,
),
'hqdefault' => array(
'width' => 480,
'height' => 360,
),
'sddefault' => array(
'width' => 640,
'height' => 480,
),
'maxresdefault' => array(
'width' => 1280,
'height' => 720,
)
);
//filter set resolution
$resolution = apply_filters('perfmatters_lazyload_youtube_thumbnail_resolution', 'hqdefault');
//finished youtube lazy output
$youtube_lazyload = '<div class="perfmatters-lazy-youtube" data-src="' . esc_attr($youtube_url) . '" data-id="' . esc_attr($youtube_id) . '" data-query="' . esc_attr($query) . '" onclick="perfmattersLazyLoadYouTube(this);">';
$youtube_lazyload.= '<div>';
$youtube_lazyload.= '<img src="https://i.ytimg.com/vi/' . esc_attr($youtube_id) .'/' . $resolution . '.jpg" alt="YouTube ' . __('video', 'perfmatters') . '" width="' . $resolutions[$resolution]['width'] . '" height="' . $resolutions[$resolution]['height'] . '" data-pin-nopin="true" nopin="nopin">';
$youtube_lazyload.= '<div class="play"></div>';
$youtube_lazyload.= '</div>';
$youtube_lazyload.= '</div>';
//noscript tag
if(apply_filters('perfmatters_lazyload_noscript', true)) {
$youtube_lazyload.= '<noscript>' . $iframe[0] . '</noscript>';
}
return $youtube_lazyload;
}
//lazy load videos
public static function lazyload_videos($html, $buffer) {
//match all videos
preg_match_all('#<video(\s.+)>.*</video>#iUs', $buffer, $videos, PREG_SET_ORDER);
if(!empty($videos)) {
//remove any duplicates
$videos = array_unique($videos, SORT_REGULAR);
foreach($videos as $video) {
//get video attributes array
$video_atts = Utilities::get_atts_array($video[1]);
//dont check excluded if forced attribute was found
if(!self::lazyload_excluded($video[1], self::lazyload_forced_atts())) {
//skip if exluded attribute was found
if(self::lazyload_excluded($video[1], self::lazyload_excluded_atts())) {
continue;
}
//skip if no-lazy class is found
if(!empty($video_atts['class']) && strpos($video_atts['class'], 'no-lazy') !== false) {
continue;
}
}
//skip if no src is found
if(empty($video_atts['src'])) {
continue;
}
//add lazyload class
$video_atts['class'] = !empty($video_atts['class']) ? $video_atts['class'] . ' ' . 'perfmatters-lazy' : 'perfmatters-lazy';
//migrate src
$video_atts['data-src'] = $video_atts['src'];
unset($video_atts['src']);
//replace video attributes string
$video_lazyload = str_replace($video[1], ' ' . Utilities::get_atts_string($video_atts), $video[0]);
//add noscript original video
if(apply_filters('perfmatters_lazyload_noscript', true)) {
$video_lazyload .= '<noscript>' . $video[0] . '</noscript>';
}
//replace video with placeholder
$html = str_replace($video[0], $video_lazyload, $html);
unset($video_lazyload);
}
}
return $html;
}
//initialize lazy loading
public static function init_images()
{
add_action('wp', array('Perfmatters\LazyLoad', 'queue_images'));
}
//queue image functions
public static function queue_images()
{
//check filters
if(empty(apply_filters('perfmatters_lazyload', !empty(Config::$options['lazyload']['lazy_loading']) || !empty(Config::$options['lazyload']['lazy_loading_iframes']) || !empty(Config::$options['lazyload']['css_background_images'])))) {
return;
}
//skip woocommerce
if(Utilities::is_woocommerce()) {
return;
}
//check post meta
if(Utilities::get_post_meta('perfmatters_exclude_lazy_loading')) {
return;
}
//disable wp native lazy loading
add_filter('wp_lazy_loading_enabled', '__return_false');
//actions + filters
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\LazyLoad', 'lazyload_buffer'));
add_action('wp_enqueue_scripts', array('Perfmatters\LazyLoad', 'enqueue_scripts'));
add_filter('script_loader_tag', array('Perfmatters\LazyLoad', 'async_script'), 10, 2);
add_action('wp_head', array('Perfmatters\LazyLoad', 'lazyload_css'), PHP_INT_MAX);
//images only
if(!empty(Config::$options['lazyload']['lazy_loading'])) {
add_filter('wp_get_attachment_image_attributes', function($attr) {
unset($attr['loading']);
return $attr;
});
}
}
//lazy load buffer
public static function lazyload_buffer($html) {
$buffer = Utilities::clean_html($html);
//replace image tags
if(!empty(Config::$options['lazyload']['lazy_loading'])) {
$html = self::lazyload_parent_exclusions($html, $buffer);
$html = self::lazyload_pictures($html, $buffer);
$html = self::lazyload_background_images($html, $buffer);
$html = self::lazyload_images($html, $buffer);
}
//replace css background elements
if(!empty(Config::$options['lazyload']['css_background_images'])) {
$html = self::lazyload_css_background_images($html, $buffer);
}
return $html;
}
//enqueue lazy load script
public static function enqueue_scripts() {
wp_register_script('perfmatters-lazy-load', plugins_url('perfmatters/js/lazyload.min.js'), array(), PERFMATTERS_VERSION, true);
wp_enqueue_script('perfmatters-lazy-load');
wp_add_inline_script('perfmatters-lazy-load', self::inline_js(), 'before');
}
//add async tag to enqueued script
public static function async_script($tag, $handle) {
if($handle !== 'perfmatters-lazy-load') {
return $tag;
}
return str_replace(' src', ' async src', $tag);
}
//print lazy load styles
public static function lazyload_css() {
//print noscript styles
if(apply_filters('perfmatters_lazyload_noscript', true)) {
echo '<noscript><style>.perfmatters-lazy[data-src]{display:none !important;}</style></noscript>';
}
$styles = '';
//youtube thumbnails
if(!empty(Config::$options['lazyload']['lazy_loading_iframes']) && !empty(Config::$options['lazyload']['youtube_preview_thumbnails'])) {
$styles.= '.perfmatters-lazy-youtube{position:relative;width:100%;max-width:100%;height:0;padding-bottom:56.23%;overflow:hidden}.perfmatters-lazy-youtube img{position:absolute;top:0;right:0;bottom:0;left:0;display:block;width:100%;max-width:100%;height:auto;margin:auto;border:none;cursor:pointer;transition:.5s all;-webkit-transition:.5s all;-moz-transition:.5s all}.perfmatters-lazy-youtube img:hover{-webkit-filter:brightness(75%)}.perfmatters-lazy-youtube .play{position:absolute;top:50%;left:50%;right:auto;width:68px;height:48px;margin-left:-34px;margin-top:-24px;background:url('.plugins_url('perfmatters/img/youtube.svg').') no-repeat;background-position:center;background-size:cover;pointer-events:none}.perfmatters-lazy-youtube iframe{position:absolute;top:0;left:0;width:100%;height:100%;z-index:99}';
if(current_theme_supports('responsive-embeds')) {
$styles.= '.wp-has-aspect-ratio .wp-block-embed__wrapper{position:relative;}.wp-has-aspect-ratio .perfmatters-lazy-youtube{position:absolute;top:0;right:0;bottom:0;left:0;width:100%;height:100%;padding-bottom:0}';
}
}
//fade in effect
if(!empty(Config::$options['lazyload']['fade_in'])) {
$styles.= '.perfmatters-lazy.pmloaded,.perfmatters-lazy.pmloaded>img,.perfmatters-lazy>img.pmloaded,.perfmatters-lazy[data-ll-status=entered]{animation:' . apply_filters('perfmatters_fade_in_speed', 500) . 'ms pmFadeIn}@keyframes pmFadeIn{0%{opacity:0}100%{opacity:1}}';
}
//css background images
if(!empty(Config::$options['lazyload']['css_background_images'])) {
$styles.='body .perfmatters-lazy-css-bg:not([data-ll-status=entered]),body .perfmatters-lazy-css-bg:not([data-ll-status=entered]) *,body .perfmatters-lazy-css-bg:not([data-ll-status=entered])::before,body .perfmatters-lazy-css-bg:not([data-ll-status=entered])::after{background-image:none!important;will-change:transform;transition:opacity 0.025s ease-in,transform 0.025s ease-in!important;}';
}
//print styles
if(!empty($styles)) {
echo '<style>' . $styles . '</style>';
}
}
//inline lazy load js
private static function inline_js() {
$threshold = apply_filters('perfmatters_lazyload_threshold', !empty(Config::$options['lazyload']['threshold']) ? Config::$options['lazyload']['threshold'] : '0px');
if(ctype_digit($threshold)) {
$threshold.= 'px';
}
//declare lazy load options
$output = 'window.lazyLoadOptions={elements_selector:"img[data-src],.perfmatters-lazy,.perfmatters-lazy-css-bg",thresholds:"' . $threshold . ' 0px",class_loading:"pmloading",class_loaded:"pmloaded",callback_loaded:function(element){if(element.tagName==="IFRAME"){if(element.classList.contains("pmloaded")){if(typeof window.jQuery!="undefined"){if(jQuery.fn.fitVids){jQuery(element).parent().fitVids()}}}}}};';
//lazy loader initialized
$output.= 'window.addEventListener("LazyLoad::Initialized",function(e){var lazyLoadInstance=e.detail.instance;';
//dom monitoring
if(!empty(Config::$options['lazyload']['lazy_loading_dom_monitoring'])) {
$output.= 'var target=document.querySelector("body");var observer=new MutationObserver(function(mutations){lazyLoadInstance.update()});var config={childList:!0,subtree:!0};observer.observe(target,config);';
}
$output.= '});';
//youtube thumbnails
if(!empty(Config::$options['lazyload']['lazy_loading_iframes']) && !empty(Config::$options['lazyload']['youtube_preview_thumbnails'])) {
$autoplay = apply_filters('perfmatters_lazyload_youtube_autoplay', true);
$output.= 'function perfmattersLazyLoadYouTube(e){var t=document.createElement("iframe"),r="ID?";r+=0===e.dataset.query.length?"":e.dataset.query+"&"' . ($autoplay ? ',r+="autoplay=1"' : '') . ',t.setAttribute("src",r.replace("ID",e.dataset.src)),t.setAttribute("frameborder","0"),t.setAttribute("allowfullscreen","1"),t.setAttribute("allow","accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"),e.replaceChild(t,e.firstChild)}';
}
return $output;
}
//lazy load img tags
private static function lazyload_images($html, $buffer) {
//match all img tags
preg_match_all('#<img([^>]+?)\/?>#is', $buffer, $images, PREG_SET_ORDER);
if(!empty($images)) {
$lazy_image_count = 0;
$exclude_leading_images = apply_filters('perfmatters_exclude_leading_images', Config::$options['lazyload']['exclude_leading_images'] ?? 0);
//remove any duplicate images
$images = array_unique($images, SORT_REGULAR);
//loop through images
foreach($images as $image) {
$lazy_image_count++;
if($lazy_image_count <= $exclude_leading_images) {
continue;
}
//prepare lazy load image
$lazy_image = self::lazyload_image($image);
//replace image in html
$html = str_replace($image[0], $lazy_image, $html);
}
}
return $html;
}
//lazy load picture tags for webp
private static function lazyload_pictures($html, $buffer) {
//match all picture tags
preg_match_all('#<picture(.*)?>(.*)<\/picture>#isU', $buffer, $pictures, PREG_SET_ORDER);
if(!empty($pictures)) {
foreach($pictures as $picture) {
//get picture tag attributes
$picture_atts = Utilities::get_atts_array($picture[1]);
//dont check excluded if forced attribute was found
if(!self::lazyload_excluded($picture[1], self::lazyload_forced_atts())) {
//skip if no-lazy class is found
if((!empty($picture_atts['class']) && strpos($picture_atts['class'], 'no-lazy') !== false) || self::lazyload_excluded($picture[0], self::lazyload_excluded_atts())) {
//mark image for exclusion later
preg_match('#<img([^>]+?)\/?>#is', $picture[0], $image);
if(!empty($image)) {
$image_atts = Utilities::get_atts_array($image[1]);
$image_atts['class'] = (!empty($image_atts['class']) ? $image_atts['class'] . ' ' : '') . 'no-lazy';
$new_image = sprintf('<img %1$s />', Utilities::get_atts_string($image_atts));
$html = str_replace($image[0], $new_image, $html);
}
continue;
}
}
//match all source tags inside the picture
preg_match_all('#<source(\s.+)>#isU', $picture[2], $sources, PREG_SET_ORDER);
if(!empty($sources)) {
//remove any duplicate sources
$sources = array_unique($sources, SORT_REGULAR);
foreach($sources as $source) {
//skip if exluded attribute was found
if(self::lazyload_excluded($source[1], self::lazyload_excluded_atts())) {
continue;
}
//migrate srcet
$new_source = preg_replace('/([\s"\'])srcset/i', '${1}data-srcset', $source[0]);
//migrate sizes
$new_source = preg_replace('/([\s"\'])sizes/i', '${1}data-sizes', $new_source);
//replace source in html
$html = str_replace($source[0], $new_source, $html);
}
}
else {
continue;
}
}
}
return $html;
}
//lazy load background images
private static function lazyload_background_images($html, $buffer) {
//match all elements with inline styles
//preg_match_all('#<(?<tag>div|figure|section|span|li)(\s+[^>]+[\'"\s]?style\s*=\s*[\'"].*?background-image.*?[\'"][^>]*)>#is', $buffer, $elements, PREG_SET_ORDER); //alternate to possibly filter some out that don't have background images???
preg_match_all('#<(?<tag>div|figure|section|span|li|a)(\s+[^>]*[\'"\s]?style\s*=\s*[\'"].*?[\'"][^>]*)>#is', $buffer, $elements, PREG_SET_ORDER);
if(!empty($elements)) {
foreach($elements as $element) {
//get element tag attributes
$element_atts = Utilities::get_atts_array($element[2]);
//dont check excluded if forced attribute was found
if(!self::lazyload_excluded($element[2], self::lazyload_forced_atts())) {
//skip if no-lazy class is found
if(!empty($element_atts['class']) && strpos($element_atts['class'], 'no-lazy') !== false) {
continue;
}
//skip if exluded attribute was found
if(self::lazyload_excluded($element[2], self::lazyload_excluded_atts())) {
continue;
}
}
//skip if no style attribute
if(!isset($element_atts['style'])) {
continue;
}
//match background-image in style string
preg_match('#(([^;\s])*background(-(image|url))?)\s*:\s*(\s*url\s*\((?<url>[^)]+)\))\s*;?#is', $element_atts['style'], $url);
if(!empty($url)) {
$url['url'] = trim($url['url'], '\'" ');
//add lazyload class
$element_atts['class'] = !empty($element_atts['class']) ? $element_atts['class'] . ' ' . 'perfmatters-lazy' : 'perfmatters-lazy';
//remove background image url from inline style attribute
$element_atts['style'] = str_replace($url[0], '', $element_atts['style']);
//migrate src
$element_atts['data-bg'] = esc_url(trim(strip_tags(html_entity_decode($url['url'], ENT_QUOTES|ENT_HTML5)), '\'" '));
if(!empty($url[2])) {
$element_atts['data-bg-var'] = $url[1];
}
//build lazy element
$lazy_element = sprintf('<' . $element['tag'] . ' %1$s >', Utilities::get_atts_string($element_atts));
//replace element with placeholder
$html = str_replace($element[0], $lazy_element, $html);
unset($lazy_element);
}
else {
continue;
}
}
}
return $html;
}
//prep img tag for lazy loading
private static function lazyload_image($image) {
//if there are no attributes, return original match
if(empty($image[1])) {
return $image[0];
}
//get image attributes array
$image_atts = Utilities::get_atts_array($image[1]);
//get new attributes
if(empty($image_atts['src']) || (!self::lazyload_excluded($image[1], self::lazyload_forced_atts()) && ((!empty($image_atts['class']) && strpos($image_atts['class'], 'no-lazy') !== false) || self::lazyload_excluded($image[1], self::lazyload_excluded_atts()) || !empty($image_atts['fetchpriority'])))) {
return $image[0];
}
else {
//add lazyload class
$image_atts['class'] = !empty($image_atts['class']) ? $image_atts['class'] . ' ' . 'perfmatters-lazy' : 'perfmatters-lazy';
//migrate src
$image_atts['data-src'] = $image_atts['src'];
//add placeholder src
$width = !empty($image_atts['width']) ? $image_atts['width'] : 0;
$height = !empty($image_atts['height']) ? $image_atts['height'] : 0;
$image_atts['src'] = "data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='" . $width . "'%20height='" . $height . "'%20viewBox='0%200%20" . $width . "%20" . $height . "'%3E%3C/svg%3E";
//migrate srcset
if(!empty($image_atts['srcset'])) {
$image_atts['data-srcset'] = $image_atts['srcset'];
unset($image_atts['srcset']);
}
//migrate sizes
if(!empty($image_atts['sizes'])) {
$image_atts['data-sizes'] = $image_atts['sizes'];
unset($image_atts['sizes']);
}
//unset existing loading attribute
if(isset($image_atts['loading'])) {
unset($image_atts['loading']);
}
}
//replace attributes
$output = sprintf('<img %1$s />', Utilities::get_atts_string($image_atts));
//original noscript image
if(apply_filters('perfmatters_lazyload_noscript', true)) {
$output.= "<noscript>" . $image[0] . "</noscript>";
}
return $output;
}
//lazy load css background images
private static function lazyload_css_background_images($html, $buffer) {
if(!empty(Config::$options['lazyload']['css_background_selectors'])) {
//match all selectors
preg_match_all('#<(?>div|section)(\s[^>]*?(' . implode('|', Config::$options['lazyload']['css_background_selectors']) . ').*?)>#i', $buffer, $selectors, PREG_SET_ORDER);
if(!empty($selectors)) {
foreach($selectors as $selector) {
$selector_atts = Utilities::get_atts_array($selector[1]);
$selector_atts['class'] = !empty($selector_atts['class']) ? $selector_atts['class'] . ' ' . 'perfmatters-lazy-css-bg' : 'perfmatters-lazy-css-bg';
//replace video attributes string
$selector_lazyload = str_replace($selector[1], ' ' . Utilities::get_atts_string($selector_atts), $selector[0]);
//replace video with placeholder
$html = str_replace($selector[0], $selector_lazyload, $html);
unset($selector_lazyload);
}
}
}
return $html;
}
//mark images inside parent exclusions as no-lazy
private static function lazyload_parent_exclusions($html, $buffer) {
if(!empty(Config::$options['lazyload']['lazy_loading_parent_exclusions'])) {
//match all selectors
preg_match_all('#<(div|section|figure)(\s[^>]*?(' . implode('|', Config::$options['lazyload']['lazy_loading_parent_exclusions']) . ').*?)>.*?<img.*?<\/\g1>#is', $buffer, $selectors, PREG_SET_ORDER);
if(!empty($selectors)) {
foreach($selectors as $selector) {
//match all img tags
preg_match_all('#<img([^>]+?)\/?>#is', $selector[0], $images, PREG_SET_ORDER);
if(!empty($images)) {
//remove any duplicate images
$images = array_unique($images, SORT_REGULAR);
//loop through images
foreach($images as $image) {
$image_atts = Utilities::get_atts_array($image[1]);
$image_atts['class'] = !empty($image_atts['class']) ? $image_atts['class'] . ' ' . 'no-lazy' : 'no-lazy';
//replace video attributes string
$new_image = str_replace($image[1], ' ' . Utilities::get_atts_string($image_atts), $image[0]);
//replace video with placeholder
$html = str_replace($image[0], $new_image, $html);
unset($new_image);
}
}
}
}
}
return $html;
}
//get forced attributes
private static function lazyload_forced_atts() {
return apply_filters('perfmatters_lazyload_forced_attributes', array());
}
//get excluded attributes
private static function lazyload_excluded_atts() {
//base exclusions
$attributes = array(
'data-perfmatters-preload',
'gform_ajax_frame',
';base64'
);
//get exclusions added from settings
if(!empty(Config::$options['lazyload']['lazy_loading_exclusions']) && is_array(Config::$options['lazyload']['lazy_loading_exclusions'])) {
$attributes = array_unique(array_merge($attributes, Config::$options['lazyload']['lazy_loading_exclusions']));
}
return apply_filters('perfmatters_lazyload_excluded_attributes', $attributes);
}
//check for excluded attributes in attributes string
private static function lazyload_excluded($string, $excluded) {
if(!is_array($excluded)) {
(array) $excluded;
}
if(empty($excluded)) {
return false;
}
foreach($excluded as $exclude) {
if(strpos($string, $exclude) !== false) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,144 @@
<?php
namespace Perfmatters;
class Meta
{
private static $meta_options;
//initialize meta functions
public static function init()
{
if(!is_admin() || !current_user_can('manage_options')) {
return;
}
global $pagenow;
if(in_array($pagenow, ['post.php', 'new-post.php'])) {
//setup meta options
self::get_meta_options();
//exclude specific woocommerce pages
if(class_exists('WooCommerce') && !empty($_GET['post'])) {
$wc_pages = array_filter(array(
get_option('woocommerce_cart_page_id'),
get_option('woocommerce_checkout_page_id'),
get_option('woocommerce_myaccount_page_id')
));
if(in_array($_GET['post'], $wc_pages)) {
return;
}
}
//meta actions
add_action('add_meta_boxes', array('Perfmatters\Meta', 'add_meta_boxes'), 1);
add_action('save_post', array('Perfmatters\Meta', 'save_meta'), 1, 2);
}
}
//add meta boxes
public static function add_meta_boxes() {
foreach(self::$meta_options as $id => $details) {
if($details['value']) {
//display meta box if at least one value is set
add_meta_box('perfmatters', 'Perfmatters', array('Perfmatters\Meta', 'load_meta_box'), get_post_types(array('public' => true)), 'side', 'high');
break;
}
}
}
//display meta box
public static function load_meta_box() {
global $post;
//noncename needed to verify where the data originated
echo '<input type="hidden" name="perfmatters_meta_noncename" id="perfmatters_meta_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
//print inputs
foreach(self::$meta_options as $id => $details) {
//existing meta value
$meta = get_post_meta($post->ID, 'perfmatters_exclude_' . $id, true);
//individual input
echo '<div' . (!$details['value'] ? ' class="hidden"' : '') . '>';
echo '<label for="perfmatters_exclude_' . $id . '">';
echo '<input type="hidden" name="perfmatters_exclude_' . $id . '" value="1" />';
echo '<input type="checkbox" name="perfmatters_exclude_' . $id . '" id="perfmatters_exclude_' . $id . '"' . (!$meta ? " checked" : "") . ' value="" class="widefat" />';
echo $details['name'];
echo '</label>';
echo '</div>';
}
//clear used css
if(!empty(Config::$options['assets']['remove_unused_css'])) {
echo '<style>.perfmatters-clear-post-used-css{display:inline-block;margin-top:10px}.perfmatters-clear-post-used-css .spinner{display:none;float:none}.perfmatters-clear-post-used-css .spinner.is-active{display:inline-block}.perfmatters-clear-post-used-css .dashicons-yes{display:none;margin:2px 10px;color:green;font-size:26px}</style>';
echo '<div class="perfmatters-clear-post-used-css">';
echo '<a class="button button-secondary" id="perfmatters-clear-post-used-css" value="1">' . __('Clear Used CSS', 'perfmatters') . '</a>';
echo '<span class="spinner"></span>';
echo '<span class="dashicons dashicons-yes"></span>';
echo wp_nonce_field('perfmatters_clear_post_used_css', 'perfmatters_clear_post_used_css', false, false);
echo '</div>';
echo '<script>jQuery(document).ready(function(s){s("#perfmatters-clear-post-used-css").click(function(t){if(t.preventDefault(),$button=s(this),$button.hasClass("disabled"))return!1;$button.addClass("disabled"),$button.siblings(".spinner").addClass("is-active");var e={action:"perfmatters_clear_post_used_css",nonce:$button.siblings("#perfmatters_clear_post_used_css").val(),post_id:parseInt(s("#post_ID").val())};s.post(ajaxurl,e,function(s){$button.siblings(".spinner").removeClass("is-active"),$button.siblings(".dashicons-yes").fadeIn().css("display","inline-block"),setTimeout(function(){$button.siblings(".dashicons-yes").fadeOut()},1e3),$button.removeClass("disabled")})})});</script>';
}
}
//save meta box data
public static function save_meta($post_id, $post) {
//verify this came from the our screen and with proper authorization, because save_post can be triggered at other times
if(empty($_POST['perfmatters_meta_noncename']) || !wp_verify_nonce($_POST['perfmatters_meta_noncename'], plugin_basename(__FILE__))) {
return;
}
//dont save for revisions
if($post->post_type == 'revision') {
return;
}
//saved data
$perfmatters_meta = array();
foreach(self::$meta_options as $id => $details) {
$key = 'perfmatters_exclude_' . $id;
if(!empty($_POST[$key]) || get_post_meta($post->ID, $key, true) != false) {
//update option in post meta
update_post_meta($post->ID, $key, $_POST[$key] ?? "");
}
}
}
//populate meta options array for other functions
private static function get_meta_options() {
self::$meta_options = array(
'defer_js' => array(
'name' => __('Defer JavaScript', 'perfmatters'),
'value' => !empty(Config::$options['assets']['defer_js'])
),
'delay_js' => array(
'name' => __('Delay JavaScript', 'perfmatters'),
'value' => !empty(Config::$options['assets']['delay_js'])
),
'unused_css' => array(
'name' => __('Unused CSS', 'perfmatters'),
'value' => !empty(Config::$options['assets']['remove_unused_css'])
),
'lazy_loading' => array(
'name' => __('Lazy Loading', 'perfmatters'),
'value' => !empty(Config::$options['lazyload']['lazy_loading']) || !empty(Config::$options['lazyload']['lazy_loading_iframes'])
),
'instant_page' => array(
'name' => __('Instant Page', 'perfmatters'),
'value' => !empty(Config::$options['preload']['instant_page'])
)
);
}
}

View File

@@ -0,0 +1,359 @@
<?php
namespace Perfmatters;
class Preload
{
private static $fetch_priority;
private static $preloads;
private static $critical_images;
private static $preloads_ready = array();
private static $used_srcs = array();
//initialize preload functions
public static function init()
{
add_action('wp', array('Perfmatters\Preload', 'queue'));
}
//queue functions
public static function queue()
{
//fetch priority
self::$fetch_priority = apply_filters('perfmatters_fetch_priority', Config::$options['preload']['fetch_priority'] ?? array());
if(!empty(self::$fetch_priority)) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\Preload', 'add_fetch_priority'));
}
//preloads
self::$preloads = apply_filters('perfmatters_preloads', Config::$options['preload']['preload'] ?? array());
self::$critical_images = apply_filters('perfmatters_preload_critical_images', Config::$options['preload']['critical_images'] ?? 0);
if(!empty(self::$preloads) || !empty(self::$critical_images)) {
add_action('perfmatters_output_buffer_template_redirect', array('Perfmatters\Preload', 'add_preloads'));
}
}
//add fetch priority
public static function add_fetch_priority($html) {
$selectors = array();
$parent_selectors = array();
foreach(self::$fetch_priority as $line) {
//device type check
if(!empty($line['device'])) {
$device_type = wp_is_mobile() ? 'mobile' : 'desktop';
if($line['device'] != $device_type) {
continue;
}
}
//location check
if(!empty($line['locations'])) {
if(!self::location_check($line['locations'])) {
continue;
}
}
//add selector
if(!empty($line['parent'])) {
$parent_selectors[] = $line['selector'];
}
else {
$selectors[] = $line['selector'];
}
}
//parent selectors
if(!empty($parent_selectors)) {
//match all selectors
preg_match_all('#<(div|section|figure)(\s[^>]*?(' . implode('|', $parent_selectors) . ').*?)>.*?<(link|img|script).*?<\/\g1>#is', $html, $parents, PREG_SET_ORDER);
if(!empty($parents)) {
foreach($parents as $parent) {
//match all tags
preg_match_all('#<(?>link|img|script)(\s[^>]*?)>#is', $parent[0], $tags, PREG_SET_ORDER);
if(!empty($tags)) {
//loop through images
foreach($tags as $tag) {
$tag_atts = Utilities::get_atts_array($tag[1]);
$key = array_search($parent[2], array_column(self::$fetch_priority, 'selector'));
$tag_atts['fetchpriority'] = self::$fetch_priority[$key]['priority'];;
//replace video attributes string
$new_tag = str_replace($tag[1], ' ' . Utilities::get_atts_string($tag_atts), $tag[0]);
//replace video with placeholder
$html = str_replace($tag[0], $new_tag, $html);
unset($new_tag);
}
}
}
}
}
//selectors
if(!empty($selectors)) {
preg_match_all('#<(?>link|img|script)(\s[^>]*?(' . implode('|', $selectors) . ').*?)>#is', $html, $matches, PREG_SET_ORDER);
if(!empty($matches)) {
foreach($matches as $tag) {
$atts = Utilities::get_atts_array($tag[1]);
$key = array_search($tag[2], array_column(self::$fetch_priority, 'selector'));
$atts['fetchpriority'] = self::$fetch_priority[$key]['priority'];;
//replace video attributes string
$new_tag = str_replace($tag[1], ' ' . Utilities::get_atts_string($atts), $tag[0]);
//replace video with placeholder
$html = str_replace($tag[0], $new_tag, $html);
}
}
}
return $html;
}
//add preloads to html
public static function add_preloads($html) {
if(!empty(self::$critical_images)) {
self::add_critical_image_preloads($html, Utilities::clean_html($html));
}
if(!empty(self::$preloads)) {
$mime_types = array(
'svg' => 'image/svg+xml',
'ttf' => 'font/ttf',
'otf' => 'font/otf',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
'eot' => 'application/vnd.ms-fontobject',
'sfnt' => 'font/sfnt'
);
foreach(self::$preloads as $line) {
//device type check
if(!empty($line['device'])) {
$device_type = wp_is_mobile() ? 'mobile' : 'desktop';
if($line['device'] != $device_type) {
continue;
}
}
//location check
if(!empty($line['locations'])) {
if(!self::location_check($line['locations'])) {
continue;
}
}
$mime_type = "";
if(!empty($line['as']) && $line['as'] == 'font') {
$path_info = pathinfo($line['url']);
$mime_type = !empty($path_info['extension']) && isset($mime_types[$path_info['extension']]) ? $mime_types[$path_info['extension']] : "";
}
//print script/style handle as preload
if(!empty($line['as']) && in_array($line['as'], array('script', 'style'))) {
if(strpos($line['url'], '.') === false) {
global $wp_scripts;
global $wp_styles;
$scripts_arr = $line['as'] == 'script' ? $wp_scripts : $wp_styles;
if(!empty($scripts_arr)) {
$scripts_arr = $scripts_arr->registered;
if(array_key_exists($line['url'], $scripts_arr)) {
$url = $scripts_arr[$line['url']]->src;
$parsed_url = parse_url($scripts_arr[$line['url']]->src);
if(empty($parsed_url['host'])) {
$url = site_url($url);
}
if(strpos($url, '?') === false) {
$ver = $scripts_arr[$line['url']]->ver;
if(empty($ver)) {
$ver = get_bloginfo('version');
}
}
$line['url'] = $url . (!empty($ver) ? '?ver=' . $ver : '');
}
else {
continue;
}
}
else {
continue;
}
}
}
$preload = "<link rel='preload' href='" . $line['url'] . "'" . (!empty($line['as']) ? " as='" . $line['as'] . "'" : "") . (!empty($mime_type) ? " type='" . $mime_type . "'" : "") . (!empty($line['crossorigin']) ? " crossorigin" : "") . (!empty($line['as']) && $line['as'] == 'style' ? " onload=\"this.rel='stylesheet';this.removeAttribute('onload');\"" : "") . ">";
if($line['as'] == 'image') {
array_unshift(self::$preloads_ready, $preload);
}
else {
self::$preloads_ready[] = $preload;
}
}
}
if(!empty(self::$preloads_ready)) {
$preloads_string = "";
foreach(apply_filters('perfmatters_preloads_ready', self::$preloads_ready) as $preload) {
$preloads_string.= $preload;
}
$pos = strpos($html, '</title>');
if($pos !== false) {
$html = substr_replace($html, '</title>' . $preloads_string, $pos, 8);
}
}
return $html;
}
//add critical image preloads
public static function add_critical_image_preloads(&$html, $clean_html) {
//match all image formats
preg_match_all('#(<picture.*?)?<img([^>]+?)\/?>(?><\/picture>)?#is', $clean_html, $matches, PREG_SET_ORDER);
if(!empty($matches)) {
$exclusions = apply_filters('perfmatters_critical_image_exclusions', array(
';base64',
'w3.org'
));
$count = 0;
foreach($matches as $match) {
if($count >= self::$critical_images) {
break;
}
if(strpos($match[0], 'secure.gravatar.com') !== false) {
continue;
}
if(!empty($exclusions) && is_array($exclusions)) {
foreach($exclusions as $exclusion) {
if(strpos($match[0], $exclusion) !== false) {
continue 2;
}
}
}
//picture tag
if(!empty($match[1])) {
preg_match('#<source([^>]+?image\/(webp|avif)[^>]+?)\/?>#is', $match[0], $source);
if(!empty($source)) {
if(self::generate_critical_image_preload($source[1])) {
$new_picture = str_replace('<picture', '<picture data-perfmatters-preload', $match[0]);
$new_picture = str_replace('<img', '<img data-perfmatters-preload', $new_picture);
$html = str_replace($match[0], $new_picture, $html);
$count++;
}
continue;
}
}
//img tag
if(!empty($match[2])) {
if(self::generate_critical_image_preload($match[2])) {
$new_image = str_replace('<img', '<img data-perfmatters-preload', $match[0]);
$html = str_replace($match[0], $new_image, $html);
$count++;
}
}
}
}
if(!empty(self::$preloads_ready)) {
ksort(self::$preloads_ready);
}
}
//generate preload link from att string
private static function generate_critical_image_preload($att_string) {
if(!empty($att_string)) {
$atts = Utilities::get_atts_array($att_string);
$src = $atts['data-src'] ?? $atts['src'] ?? '';
//dont add if src was already used
if(in_array($src, self::$used_srcs)) {
return false;
}
//generate preload
self::$preloads_ready[] = '<link rel="preload" href="' . $src . '" as="image"' . (!empty($atts['srcset']) ? ' imagesrcset="' . $atts['srcset'] . '"' : '') . (!empty($atts['sizes']) ? ' imagesizes="' . $atts['sizes'] . '"' : '') . ' />';
//mark src used and return
if(!empty($src)) {
self::$used_srcs[] = $src;
}
return true;
}
}
private static function location_check($locations) {
$location_match = false;
$exploded_locations = explode(',', $locations);
$trimmed_locations = array_map('trim', $exploded_locations);
//single post exclusion
if(is_singular()) {
global $post;
if(in_array($post->ID, $trimmed_locations)) {
$location_match = true;
}
}
//posts page exclusion
elseif(is_home() && in_array('blog', $trimmed_locations)) {
$location_match = true;
}
elseif(is_archive()) {
//woocommerce shop check
if(function_exists('is_shop') && is_shop()) {
if(in_array(wc_get_page_id('shop'), $trimmed_locations)) {
$location_match = true;
}
}
}
return $location_match;
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Perfmatters;
class Utilities
{
//get given post meta option for current post
public static function get_post_meta($option) {
global $post;
if(!is_object($post)) {
return false;
}
if(is_home()) {
$post_id = get_queried_object_id();
}
if(is_singular() && isset($post)) {
$post_id = $post->ID;
}
return (isset($post_id)) ? get_post_meta($post_id, $option, true) : false;
}
//remove unecessary bits from html for search
public static function clean_html($html) {
//remove existing script tags
$html = preg_replace('/<script\b(?:[^>]*)>(?:.+)?<\/script>/Umsi', '', $html);
//remove existing noscript tags
$html = preg_replace('#<noscript>(?:.+)</noscript>#Umsi', '', $html);
return $html;
}
//get array of element attributes from attribute string
public static function get_atts_array($atts_string) {
if(!empty($atts_string)) {
$atts_array = array_map(
function(array $attribute) {
return $attribute['value'];
},
wp_kses_hair($atts_string, wp_allowed_protocols())
);
return $atts_array;
}
return false;
}
//get attribute string from array of element attributes
public static function get_atts_string($atts_array) {
if(!empty($atts_array)) {
$assigned_atts_array = array_map(
function($name, $value) {
if($value === '') {
return $name;
}
return sprintf('%s="%s"', $name, esc_attr($value));
},
array_keys($atts_array),
$atts_array
);
$atts_string = implode(' ', $assigned_atts_array);
return $atts_string;
}
return false;
}
//check for specific woocommerce pages
public static function is_woocommerce() {
if(class_exists('WooCommerce') && (is_cart() || is_checkout() || is_account_page())) {
return true;
}
return false;
}
}