ID) && $post->ID) { self::clearJsonStorageForPost($post->ID, true); } } }); // Autoptimize Compatibility: Make sure Asset CleanUp's changes are applied add_filter('autoptimize_filter_html_before_minify', static function($htmlSource) { return self::alterHtmlSource($htmlSource, true); }); if (Misc::isPluginActive('cache-enabler/cache-enabler.php')) { if (defined('CE_VERSION') && version_compare(CE_VERSION, '1.6.0', '<')) { // Cache Enabler: BEFORE 1.6.0 (1.5.5 and below) // Make sure HTML changes are applied to cached pages from "Cache Enabler" plugin add_filter( 'cache_enabler_before_store', static function( $htmlSource ) { return self::alterHtmlSource( $htmlSource ); // deprecated, include it in case other users have an older version of "Cache Enabler" }, 1, 1 ); } else { // Cache Enabler: 1.6.0+ global $cache_enabler_constants; if (isset($cache_enabler_constants['CACHE_ENABLER_VERSION']) && version_compare($cache_enabler_constants['CACHE_ENABLER_VERSION'], '1.6.0', '>=')) { add_filter( 'cache_enabler_page_contents_before_store', static function( $htmlSource ) { return self::alterHtmlSource( $htmlSource ); }, 1, 1 ); } } } // In case HTML Minify is enabled in W3 Total Cache, make sure any settings (e.g. JS combine) in Asset CleanUp will be applied add_filter('w3tc_minify_before', static function ($htmlSource) { return self::alterHtmlSource($htmlSource); }, 1, 1); // LiteSpeed Cache (partial compatibility) add_filter('litespeed_optm_html_head', static function($htmlHead) { if (! Main::instance()->preventAssetsSettings()) { $htmlHead = OptimizeCss::ignoreDependencyRuleAndKeepChildrenLoaded($htmlHead); $htmlHead = OptimizeJs::ignoreDependencyRuleAndKeepChildrenLoaded($htmlHead); } return $htmlHead; }); add_filter('litespeed_optm_html_foot', static function($htmlFoot) { if (! Main::instance()->preventAssetsSettings()) { $htmlFoot = OptimizeCss::ignoreDependencyRuleAndKeepChildrenLoaded($htmlFoot); $htmlFoot = OptimizeJs::ignoreDependencyRuleAndKeepChildrenLoaded($htmlFoot); } return $htmlFoot; }); // Make sure HTML changes, especially rules such as the ones from "Ignore dependency rules and keep 'children' loaded" // are applied to cached pages from "WP Rocket" plugin if (Misc::isPluginActive('wp-rocket/wp-rocket.php')) { add_filter('rocket_buffer', static function($htmlSource) { return self::alterHtmlSource($htmlSource, true); }); } // "Hide My WP Ghost – Security Plugin" - Make sure the alter the HTML (some files might be cached) before the security plugin proceeds with the alteration of the paths // This way, "Hide My WP Ghost – Security Plugin" would process the paths to the CSS/JS files that are already cached from /wp-content/cache/ if ( ( ! (defined('DOING_AJAX') && DOING_AJAX) ) // not when /wp-admin/admin-ajax.php is called && class_exists('\HMWP_Classes_ObjController') && method_exists('\HMWP_Models_Rewrite', 'find_replace') && apply_filters('hmwp_process_buffer', true) // only when processing the buffer is turned ON ) { add_filter('hmwp_process_buffer', '__return_false'); add_filter('wpacu_print_info_comments_in_cached_assets', '__return_false'); // hide comments revealing the paths to serve the purpose of "Hide My WP Ghost – Security Plugin" add_filter('wpacu_html_source_after_optimization', function($htmlSource) { return \HMWP_Classes_ObjController::getClass('HMWP_Models_Rewrite')->find_replace($htmlSource); }); } add_action('wp_loaded', array($this, 'maybeAlterHtmlSource'), 1); HardcodedAssets::init(); } /** * */ public function maybeAlterHtmlSource() { if (is_admin()) { // Don't apply any changes if not in the front-end view (e.g. Dashboard view) return; } if (is_feed()) { // The plugin should be inactive for feed URLs return; } /* * CASE 1: The admin is logged-in and manages the assets in the front-end view * */ if (HardcodedAssets::useBufferingForEditFrontEndView()) { // Alter the HTML via "shutdown" action hook to catch hardcoded CSS/JS that is added via output buffering such as the ones in "Smart Slider 3" // via HardcodedAssets.php return; } /* * CASE (most common): The admin is logged-in, but "Manage in the front-end" is deactivated OR the visitor is just a guest * */ ob_start(static function($htmlSource) { // Do not do any optimization if "Test Mode" is Enabled if (! Menu::userCanManageAssets() && Main::instance()->settings['test_mode']) { return $htmlSource; } return self::alterHtmlSource($htmlSource); }); } /** * @param $htmlSource * @param $triggerOnlyOnce bool * * @return mixed|string|string[]|void|null */ public static function alterHtmlSource($htmlSource, $triggerOnlyOnce = false) { // e.g. if it was called from "autoptimize_filter_html_before_minify", then there's no point in triggering it again from a different hook if (defined('WPACU_ALTER_HTML_SOURCE_DONE')) { return $htmlSource; } if ($triggerOnlyOnce && ! defined('WPACU_ALTER_HTML_SOURCE_DONE')) { define('WPACU_ALTER_HTML_SOURCE_DONE', 1); } if (is_feed()) { // The plugin should not do any alterations for the feed content return $htmlSource; } // Dashboard View // Return the HTML as it is without performing any optimisations to save resources // Since the page has to be as clean as possible when fetching the assets if (Main::instance()->isGetAssetsCall) { return $htmlSource; } /* [wpacu_timing] */ Misc::scriptExecTimer( 'alter_html_source' ); /* [/wpacu_timing] */ // Front-end View // The printing of the hardcoded assets is made via "wpacu_final_frontend_output" filter hook // located within "shutdown" action hook only if the user is logged-in and has the right permissions // This is useful to avoid changing the DOM via wp_loaded action hook // In order to check how fast the page loads without the DOM changes (for debugging purposes) $wpacuNoHtmlChanges = isset($_REQUEST['wpacu_no_html_changes']) || ( defined('WPACU_NO_HTML_CHANGES') && WPACU_NO_HTML_CHANGES ); // Not a normal WordPress page load // e.g. it could be JS content loaded dynamically such as /?wpml-app=ate-widget if ( ! (did_action('wp_head') && did_action('wp_footer')) && Plugin::preventAnyFrontendOptimization('', $htmlSource) ) { /* [wpacu_timing] */ Misc::scriptExecTimer( 'alter_html_source', 'end' ); /* [/wpacu_timing] */ return $htmlSource; } if ( $wpacuNoHtmlChanges || Plugin::preventAnyFrontendOptimization() ) { /* [wpacu_timing] */ Misc::scriptExecTimer( 'alter_html_source', 'end' ); /* [/wpacu_timing] */ return $htmlSource; } $htmlSource = apply_filters( 'wpacu_html_source_before_optimization', $htmlSource ); // For the admin $anyHardCodedAssetsList = HardcodedAssets::getAll( $htmlSource, false ); // The admin is editing the CSS/JS list within the front-end view if (HardcodedAssets::useBufferingForEditFrontEndView()) { ObjectCache::wpacu_cache_set('wpacu_hardcoded_assets_encoded', base64_encode( wp_json_encode($anyHardCodedAssetsList) )); } $htmlSource = OptimizeCss::alterHtmlSource( $htmlSource ); $htmlSource = OptimizeJs::alterHtmlSource( $htmlSource ); /* [wpacu_timing] */ Misc::scriptExecTimer( 'alter_html_source_cleanup' ); /* [/wpacu_timing] */ /* [wpacu_timing] */ Misc::scriptExecTimer('alter_html_source_for_remove_html_comments'); /* [/wpacu_timing] */ $htmlSource = Main::instance()->settings['remove_html_comments'] ? CleanUp::removeHtmlComments( $htmlSource, false ) : $htmlSource; /* [wpacu_timing] */ Misc::scriptExecTimer('alter_html_source_for_remove_html_comments', 'end'); /* [/wpacu_timing] */ /* [wpacu_timing] */ Misc::scriptExecTimer('alter_html_source_for_remove_meta_generators'); /* [/wpacu_timing] */ $htmlSource = Main::instance()->settings['remove_generator_tag'] ? CleanUp::removeMetaGenerators( $htmlSource ) : $htmlSource; /* [wpacu_timing] */ Misc::scriptExecTimer('alter_html_source_for_remove_meta_generators', 'end'); /* [/wpacu_timing] */ $htmlSource = preg_replace('#settings['disable_xmlrpc'], array( 'disable_all', 'disable_pingback' ) ) ) { // Also clean it up from the in case it's hardcoded $htmlSource = CleanUp::cleanPingbackLinkRel( $htmlSource ); } // A script like this one shouldn't be in an AMP page if (defined('WPACU_DO_EXTRA_CHECKS_FOR_AMP') && strpos($htmlSource, '