wp-asset-clean-up
Last commit date
assets
1 month ago
classes
1 month ago
templates
1 month ago
vendor
1 month ago
changelog.txt
1 month ago
composer.json
1 month ago
early-triggers.php
1 month ago
readme.txt
1 month ago
wpacu-load.php
1 month ago
wpacu.php
1 month ago
wpacu.php
195 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Plugin Name: Asset CleanUp: Page Speed Booster |
| 4 | * Plugin URI: https://wordpress.org/plugins/wp-asset-clean-up/ |
| 5 | * Version: 1.4.0.4 |
| 6 | * Requires at least: 4.6 |
| 7 | * Requires PHP: 5.6 |
| 8 | * Description: Unload Chosen Scripts & Styles from Posts/Pages to reduce HTTP Requests, Combine/Minify CSS/JS files |
| 9 | * Author: Gabe Livan |
| 10 | * Author URI: http://www.gabelivan.com/ |
| 11 | * Text Domain: wp-asset-clean-up |
| 12 | * Domain Path: /languages |
| 13 | */ |
| 14 | |
| 15 | // Premium plugin version already exists, is it active? |
| 16 | // This action is valid starting from LITE version 1.2.6.8 |
| 17 | // From 1.0.3, the PRO version works independently (does not need anymore LITE to be active and act as a parent plugin) |
| 18 | // However, it's good to have both versions active for compatibility with plugins such as "WP Cloudflare Super Page Cache" |
| 19 | |
| 20 | // If the pro version (version above 1.0.2) was triggered first, we'll just check one of its constants |
| 21 | // If the lite version was triggered first, then we'll check if the pro version is active |
| 22 | // Lastly, check if the Pro version is activated via is_plugin_active() |
| 23 | if ( (defined('WPACU_PRO_NO_LITE_NEEDED') && WPACU_PRO_NO_LITE_NEEDED !== false && defined('WPACU_PRO_PLUGIN_VERSION') && WPACU_PRO_PLUGIN_VERSION !== false) |
| 24 | || (function_exists('is_plugin_active') && is_plugin_active('wp-asset-clean-up-pro/wpacu.php')) |
| 25 | || in_array('wp-asset-clean-up-pro/wpacu.php', apply_filters('active_plugins', get_option('active_plugins', array()))) ) { |
| 26 | // Stop here as the Pro version handles everything the Lite does |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | // Is the Pro version triggered before the Lite one and are both plugins active? |
| 31 | if (! defined('WPACU_PLUGIN_VERSION')) { |
| 32 | define('WPACU_PLUGIN_VERSION', '1.4.0.4'); |
| 33 | } |
| 34 | |
| 35 | // Exit if accessed directly |
| 36 | if (! defined('ABSPATH')) { |
| 37 | exit; |
| 38 | } |
| 39 | |
| 40 | if (! defined('WPACU_PLUGIN_ID')) { |
| 41 | define( 'WPACU_PLUGIN_ID', 'wpassetcleanup' ); // unique prefix (same plugin ID name for 'lite' and 'pro') |
| 42 | } |
| 43 | |
| 44 | if (! defined('WPACU_PLUGIN_SLUG')) { |
| 45 | define( 'WPACU_PLUGIN_SLUG', 'wp-asset-clean-up' ); // useful to detect which functions to trigger (e.g. JS files) |
| 46 | } |
| 47 | |
| 48 | require_once __DIR__ . '/early-triggers.php'; |
| 49 | |
| 50 | if (assetCleanUpNoLoad()) { |
| 51 | return; // do not continue |
| 52 | } |
| 53 | |
| 54 | define('WPACU_PLUGIN_FILE', __FILE__); |
| 55 | define('WPACU_PLUGIN_BASE', plugin_basename(WPACU_PLUGIN_FILE)); |
| 56 | |
| 57 | define('WPACU_ADMIN_PAGE_ID_START', WPACU_PLUGIN_ID . '_getting_started'); |
| 58 | |
| 59 | // Do not load the plugin if the PHP version is below 5.6 |
| 60 | // If PHP_VERSION_ID is not defined, then the PHP version is below 5.2.7, thus the plugin is not usable |
| 61 | $wpacuWrongPhp = ((! defined('PHP_VERSION_ID')) || (defined('PHP_VERSION_ID') && PHP_VERSION_ID < 50600)); |
| 62 | |
| 63 | wpacuDefineConstant( 'WPACU_WRONG_PHP_VERSION', ( ( $wpacuWrongPhp ) ? 'true' : 'false' ) ); |
| 64 | |
| 65 | if ($wpacuWrongPhp && is_admin()) { // Dashboard |
| 66 | add_action('admin_notices', function() { |
| 67 | /** |
| 68 | * Print the message to the user after the plugin was deactivated |
| 69 | */ |
| 70 | echo '<div class="wpacu-error is-dismissible"><p>'. |
| 71 | |
| 72 | sprintf( |
| 73 | esc_html__('%1$s requires %2$s PHP version installed. You have %3$s.', 'wp-asset-clean-up'), |
| 74 | '<strong>'.WPACU_PLUGIN_TITLE.'</strong>', |
| 75 | '<span style="color: green;"><strong>5.6+</strong></span>', |
| 76 | '<strong>'.PHP_VERSION.'</strong>' |
| 77 | ) . ' '. |
| 78 | esc_html__('If your website is compatible with PHP 7+ (e.g. you can check with your developers or contact the hosting company), it\'s strongly recommended to upgrade to a newer PHP version for a better performance.', 'wp-asset-clean-up').' '. |
| 79 | esc_html__('Thus, the plugin will not trigger on the front-end view to avoid any possible errors.', 'wp-asset-clean-up'). |
| 80 | |
| 81 | '</p></div>'; |
| 82 | |
| 83 | if (isset($_GET['active'])) { |
| 84 | unset($_GET['activate']); |
| 85 | } |
| 86 | }); |
| 87 | } elseif ($wpacuWrongPhp) { // Front |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | define('WPACU_PLUGIN_DIR', __DIR__); |
| 92 | define('WPACU_PLUGIN_CLASSES_PATH', WPACU_PLUGIN_DIR.'/classes/'); |
| 93 | define('WPACU_PLUGIN_URL', plugins_url('', WPACU_PLUGIN_FILE)); |
| 94 | |
| 95 | // Upgrade to Pro Sales Page |
| 96 | define('WPACU_PLUGIN_GO_PRO_URL', 'https://www.gabelivan.com/items/wp-asset-cleanup-pro/'); // no query strings to be added |
| 97 | |
| 98 | // Global Values |
| 99 | define('WPACU_LOAD_ASSETS_REQ_KEY', WPACU_PLUGIN_ID . '_load'); |
| 100 | define('WPACU_FORM_ASSETS_POST_KEY', WPACU_PLUGIN_ID.'_form_assets'); // starting from Pro version 1.1.9.9 & Lite version 1.3.8.1 |
| 101 | |
| 102 | $wpacuGetLoadedAssetsAction = ((isset($_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) && $_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) |
| 103 | || (isset($_REQUEST['action']) && $_REQUEST['action'] === WPACU_PLUGIN_ID.'_get_loaded_assets')); |
| 104 | define('WPACU_GET_LOADED_ASSETS_ACTION', $wpacuGetLoadedAssetsAction); |
| 105 | |
| 106 | require_once WPACU_PLUGIN_DIR.'/wpacu-load.php'; |
| 107 | |
| 108 | $isDashboardManageAssets = isset( $_GET['page'] ) && ( $_GET['page'] === WPACU_PLUGIN_ID . '_assets_manager' ); |
| 109 | $isDashboardCriticalCssPage = isset( $_GET['wpacu_sub_page'] ) && ( $_GET['wpacu_sub_page'] === 'manage_critical_css' ); |
| 110 | $isDashboardPluginsPage = isset( $_GET['wpacu_sub_page'] ) && ( strpos( $_GET['wpacu_sub_page'], 'manage_plugins_' ) === 0 ); |
| 111 | |
| 112 | // In which situations should the composer libraries be loaded? |
| 113 | // Only load them when necessary |
| 114 | $wpacuIsWpacuAjaxRequest = ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) === 'xmlhttprequest' ) |
| 115 | && ( strpos( $_SERVER['REQUEST_URI'], 'admin-ajax.php' ) !== false ) // The request URI contains 'admin-ajax.php' |
| 116 | && isset ($_POST['action']) && $_POST['action'] && strpos($_POST['action'], WPACU_PLUGIN_ID.'_') === 0; |
| 117 | |
| 118 | if (WPACU_GET_LOADED_ASSETS_ACTION === true || |
| 119 | ! is_admin() || |
| 120 | (is_admin() && ($wpacuIsWpacuAjaxRequest || $isDashboardManageAssets || $isDashboardCriticalCssPage || $isDashboardPluginsPage))) { |
| 121 | add_action('init', static function() { |
| 122 | // "Smart Slider 3" & "WP Rocket" compatibility fix | triggered ONLY when the assets are fetched |
| 123 | if ( ! function_exists('get_rocket_option') && class_exists( 'NextendSmartSliderWPRocket' ) ) { |
| 124 | function get_rocket_option($option) { return ''; } |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | add_action('parse_query', static function() { // very early triggering to set WPACU_ALL_ACTIVE_PLUGINS_LOADED |
| 129 | if (defined('WPACU_ALL_ACTIVE_PLUGINS_LOADED')) { return; } // only trigger it once in this action |
| 130 | define('WPACU_ALL_ACTIVE_PLUGINS_LOADED', true); |
| 131 | \WpAssetCleanUp\OptimiseAssets\OptimizeCommon::preventAnyFrontendOptimization('parse_query'); |
| 132 | }, 1); |
| 133 | } |
| 134 | |
| 135 | // No plugin changes are needed when a feed is loaded |
| 136 | // Only in the front-end view and when a request URI is there (e.g. not triggering the WP environment via an SSH terminal) |
| 137 | if ( isset($_SERVER['REQUEST_URI']) && ! is_admin() ) { |
| 138 | add_action('setup_theme', static function () { |
| 139 | global $wp_rewrite; |
| 140 | |
| 141 | if (isset($wp_rewrite->feed_base) && |
| 142 | $wp_rewrite->feed_base && |
| 143 | strpos($_SERVER['REQUEST_URI'], '/' . $wp_rewrite->feed_base) !== false) { |
| 144 | $currentPageUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . parse_url(site_url(), |
| 145 | PHP_URL_HOST) . $_SERVER['REQUEST_URI']; |
| 146 | |
| 147 | $cleanCurrentPageUrl = $currentPageUrl; |
| 148 | if (strpos($currentPageUrl, '?') !== false) { |
| 149 | list($cleanCurrentPageUrl) = explode('?', $currentPageUrl); |
| 150 | } |
| 151 | |
| 152 | // /{feed_slug_here}/ or /{feed_slug_here}/atom/ |
| 153 | if ($cleanCurrentPageUrl === site_url() . '/' . $wp_rewrite->feed_base . '/' |
| 154 | || $cleanCurrentPageUrl === site_url() . '/' . $wp_rewrite->feed_base . '/atom/') { |
| 155 | \WpAssetCleanUp\OptimiseAssets\OptimizeCommon::preventAnyFrontendOptimization(); |
| 156 | } |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | // Make sure the plugin doesn't load when the editor of either "X" theme or "Pro" website creator (theme.co) is ON |
| 162 | add_action('init', static function() { |
| 163 | if (is_admin()) { |
| 164 | return; // Not relevant for the Dashboard view, stop here! |
| 165 | } |
| 166 | |
| 167 | if ( ! \WpAssetCleanUp\Menu::userCanAccessPlugin() ) { |
| 168 | return; // Not relevant if the logged-in user does not have full rights |
| 169 | } |
| 170 | |
| 171 | if (method_exists('Cornerstone_Common', 'get_app_slug') && in_array(get_stylesheet(), array('x', 'pro'))) { |
| 172 | $customAppSlug = get_stylesheet(); // default one ('x' or 'pro') |
| 173 | |
| 174 | // Is there any custom slug set in "/wp-admin/admin.php?page=cornerstone-settings"? |
| 175 | // "Settings" -> "Custom Path" (check it out below) |
| 176 | $cornerStoneSettings = get_option('cornerstone_settings'); |
| 177 | if (isset($cornerStoneSettings['custom_app_slug']) && $cornerStoneSettings['custom_app_slug'] !== '') { |
| 178 | $customAppSlug = $cornerStoneSettings['custom_app_slug']; |
| 179 | } |
| 180 | |
| 181 | $lengthToUse = strlen($customAppSlug) + 2; // add the slashes to the count |
| 182 | |
| 183 | if (substr($_SERVER['REQUEST_URI'], -$lengthToUse) === '/'.$customAppSlug.'/') { |
| 184 | add_filter( 'wpacu_prevent_any_frontend_optimization', '__return_true' ); |
| 185 | } |
| 186 | } |
| 187 | }, PHP_INT_MAX); |
| 188 | |
| 189 | // "Transliterator - WordPress Transliteration" breaks the HTML content in Asset CleanUp's admin pages |
| 190 | // by converting characters such as < (that should stay as they are) to < thus, a fix is attempted to be made here |
| 191 | if (isset($_GET['page']) && is_string($_GET['page']) && (strpos($_GET['page'], WPACU_PLUGIN_ID.'_') !== false) && is_admin() && method_exists('Serbian_Transliteration_Cache', 'set')) { |
| 192 | Serbian_Transliteration_Cache::set('is_editor', true); |
| 193 | } |
| 194 | |
| 195 |