array(), 'scripts' => array());
/**
*
*/
public function __construct()
{
self::prepareVars();
}
/**
*
*/
public static function prepareVars()
{
self::$ownAssets['styles'] = array(
'style_core' => array(
'handle' => WPACU_PLUGIN_ID . '-style',
'rel_path' => '/assets/style.min.css'
),
'chosen' => array(
'handle' => WPACU_PLUGIN_ID . '-chosen-style',
'rel_path' => '/assets/chosen/chosen.min.css'
),
'tooltipster' => array(
'handle' => WPACU_PLUGIN_ID . '-tooltipster-style',
'rel_path' => '/assets/tooltipster/tooltipster.bundle.min.css'
),
'sweetalert2' => array(
'handle' => WPACU_PLUGIN_ID . '-sweetalert2-style',
'rel_path' => '/assets/sweetalert2/dist/sweetalert2.min.css'
),
'autocomplete_search_jquery_ui_custom' => array(
'handle' => WPACU_PLUGIN_ID.'-autocomplete-jquery-ui-custom',
'rel_path' => '/assets/auto-complete/smoothness/jquery-ui-custom.min.css'
)
);
self::$ownAssets['scripts'] = array(
'script_core' => array(
'handle' => WPACU_PLUGIN_ID . '-script',
'rel_path' => '/assets/script.min.js'
),
'chosen' => array(
'handle' => WPACU_PLUGIN_ID . '-chosen-script',
'rel_path' => '/assets/chosen/chosen.jquery.min.js'
),
'tooltipster' => array(
'handle' => WPACU_PLUGIN_ID . '-tooltipster-script',
'rel_path' => '/assets/tooltipster/tooltipster.bundle.min.js'
),
'sweetalert2' => array(
'handle' => WPACU_PLUGIN_ID . '-sweetalert2-js',
'rel_path' => '/assets/sweetalert2/dist/sweetalert2.min.js'
),
'autocomplete_search' => array(
'handle' => WPACU_PLUGIN_ID . '-autocomplete-search',
'rel_path' => '/assets/auto-complete/main.min.js'
)
);
// If script debugging is enabled, load the non-minified versions of the plugin's assets
// Read more: https://wordpress.org/support/article/debugging-in-wordpress/#script_debug
if ( (defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG) || isset($_GET['wpacu_debug']) ) {
self::$ownAssets['styles']['style_core']['rel_path'] = '/assets/style.css';
self::$ownAssets['scripts']['script_core']['rel_path'] = '/assets/script.js';
self::$ownAssets['styles']['chosen']['rel_path'] = '/assets/chosen/chosen.css';
self::$ownAssets['scripts']['chosen']['rel_path'] = '/assets/chosen/chosen.jquery.js';
self::$ownAssets['styles']['sweetalert2']['rel_path'] = '/assets/sweetalert2/dist/sweetalert2.css';
self::$ownAssets['scripts']['sweetalert2']['rel_path'] = '/assets/sweetalert2/dist/sweetalert2.js';
self::$ownAssets['styles']
['autocomplete_search_jquery_ui_custom']['rel_path'] = '/assets/auto-complete/smoothness/jquery-ui-custom.css';
self::$ownAssets['scripts']
['autocomplete_search']['rel_path'] = '/assets/auto-complete/main.js';
}
}
/**
* @return array[]
*/
public static function getOwnAssetsHandles($assetType = '')
{
self::prepareVars();
$allPluginStyleHandles = $allPluginScriptHandles = array();
foreach (self::$ownAssets['styles'] as $assetValues) {
if (isset($assetValues['handle']) && $assetValues['handle']) {
$allPluginStyleHandles[] = $assetValues['handle'];
}
}
foreach (self::$ownAssets['scripts'] as $assetValues) {
if (isset($assetValues['handle']) && $assetValues['handle']) {
$allPluginScriptHandles[] = $assetValues['handle'];
}
}
if ($assetType !== '') {
if ($assetType === 'styles') {
return $allPluginStyleHandles;
}
return $allPluginScriptHandles;
}
return array_merge($allPluginStyleHandles, $allPluginScriptHandles);
}
/**
*
*/
public function init()
{
add_action('admin_enqueue_scripts', array($this, 'stylesAndScriptsForAdmin'));
add_action('wp_enqueue_scripts', array($this, 'stylesAndScriptsForPublic'));
// Code only for the Dashboard
add_action('admin_head', array($this, 'inlineAdminHeadCode'));
add_action('admin_footer', array($this, 'inlineAdminFooterCode'));
// Code for both the Dashboard and the Front-end view
add_action('admin_head', array($this, 'inlineCode'));
add_action('wp_head', array($this, 'inlineCode'));
// Rename ?ver= to ?wpacuversion to prevent other plugins from stripping "ver"
// This is valid in the front-end and the Dashboard
add_filter('script_loader_src', array($this, 'ownAssetLoaderSrc'), 10, 2);
add_filter('style_loader_src', array($this, 'ownAssetLoaderSrc'), 10, 2);
add_filter('script_loader_tag', array($this, 'ownAssetLoaderTag'), 10, 2);
add_filter('wpacu_object_data', static function($wpacu_object_data) {
$wpacu_object_data['source_load_error_msg'] = __('The source might not be reachable', 'wp-asset-clean-up');
$wpacu_object_data['plugin_prefix'] = WPACU_PLUGIN_ID; // the same for both Lite & Pro
$wpacu_object_data['plugin_slug'] = WPACU_PLUGIN_SLUG;
$wpacu_object_data['plugin_title'] = WPACU_PLUGIN_TITLE;
$wpacu_object_data['ajax_url'] = esc_url( admin_url( 'admin-ajax.php' ) );
$wpacu_object_data['is_frontend_view'] = false;
if ( isset($_GET['wpacu_manage_dash']) ) {
$wpacu_object_data['force_manage_dash'] = true;
}
// Current Page URL (for preloading) in the front-end view
if (! is_admin()) {
$wpacu_object_data['page_url'] = Misc::getCurrentPageUrl();
$wpacu_object_data['is_frontend_view'] = true;
}
if (isset($wpacu_object_data['page_url']) && is_admin() && Misc::isHttpsSecure()) {
$wpacu_object_data['page_url'] = str_replace('http://', 'https://', $wpacu_object_data['page_url']);
}
// Security nonces for AJAX calls
$wpacu_object_data['wpacu_update_specific_settings_nonce'] = wp_create_nonce('wpacu_update_specific_settings_nonce');
$wpacu_object_data['wpacu_update_asset_row_state_nonce'] = wp_create_nonce('wpacu_update_asset_row_state_nonce');
$wpacu_object_data['wpacu_area_update_assets_row_state_nonce'] = wp_create_nonce('wpacu_area_update_assets_row_state_nonce');
$wpacu_object_data['wpacu_print_loaded_hardcoded_assets_nonce'] = wp_create_nonce('wpacu_print_loaded_hardcoded_assets_nonce');
$wpacu_object_data['wpacu_ajax_check_remote_file_size_nonce'] = wp_create_nonce('wpacu_ajax_check_remote_file_size_nonce');
$wpacu_object_data['wpacu_ajax_check_external_urls_nonce'] = wp_create_nonce('wpacu_ajax_check_external_urls_nonce');
$wpacu_object_data['wpacu_ajax_get_loaded_assets_nonce'] = wp_create_nonce('wpacu_ajax_get_loaded_assets_nonce');
$wpacu_object_data['wpacu_ajax_load_page_restricted_area_nonce'] = wp_create_nonce('wpacu_ajax_load_page_restricted_area_nonce');
$wpacu_object_data['wpacu_ajax_clear_cache_nonce'] = wp_create_nonce('wpacu_ajax_clear_cache_nonce');
$wpacu_object_data['wpacu_ajax_preload_url_nonce'] = wp_create_nonce('wpacu_ajax_preload_url_nonce'); // After the CSS/JS manager's form is submitted (e.g. on an edit post/page)
$wpacu_object_data['jquery_unload_alert'] = 'jQuery library is a WordPress library that it is used in WordPress plugins/themes most of the time.'."\n\n".
'There are currently other JavaScript "children" files connected to it, that will stop working, if this library is unloaded'."\n\n".
'If you are positive this page does not require jQuery (very rare cases), then you can continue by pressing "OK"'."\n\n".
'Otherwise, it is strongly recommended to keep this library loaded by pressing "Cancel" to avoid breaking the functionality of the website.';
// js-cookie
$wpacu_object_data['woo_js_cookie_unload_alert'] = 'Please be careful when unloading "js-cookie" as there are other JS files that depend on it which will also be unloaded, including "wc-cart-fragments" which is required for the functionality of the WooCommerce mini cart.'."\n\n".
'Click "OK" to continue or "Cancel" if you have any doubts about unloading this file';
// wc-cart-fragments
$wpacu_object_data['woo_wc_cart_fragments_unload_alert'] = 'Please be careful when unloading "wc-cart-fragments" as it\'s required for the functionality of the WooCommerce mini cart. Unless you are sure you do not need it on this page, it is advisable to leave it loaded.'."\n\n".
'Click "OK" to continue or "Cancel" if you have any doubts about unloading this file.';
// backbone, underscore, etc.
$wpacu_object_data['sensitive_library_unload_alert'] = 'Please make sure to properly test this page after this particular JavaScript file is unloaded as it is usually loaded for a reason.'."\n\n".
'If you are not sure whether it is used or not, then consider using the "Cancel" button to avoid taking ay chances in breaking the website\'s functionality.'."\n\n".
'It is advised to check the browser\'s console via right-click and "Inspect" to check for any reported errors.';
$wpacu_object_data['dashicons_unload_alert_ninja_forms'] = 'It looks like you are using "Ninja Forms" plugin which is sometimes loading Dashicons for the forms\' styling.'."\n\n".
'If you are sure your forms do not use Dashicons, please use the following option \'Ignore dependency rule and keep the "children" loaded\' to avoid the unloading of the "nf-display" handle.'. "\n\n".
'Click "OK" to continue or "Cancel" if you have any doubts about unloading the Dashicons. It is better to have Dashicons loaded, then take a chance and break the forms\' layout.';
// After homepage/post/page is saved and the page is reloaded, clear the cache
// Cache clearing default values
$wpacu_object_data['clear_cache_on_page_load'] = $wpacu_object_data['clear_other_caches'] = false; // default
/*
* [Start] Trigger plugin cache and other plugins'/system caches
*/
// After editing post/page within the Dashboard
$unloadAssetsSubmit = (isset($_POST['wpacu_unload_assets_area_loaded']) && $_POST['wpacu_unload_assets_area_loaded']);
// After updating the CSS/JS manager within the front-end view (when "Manage in the front-end" is enabled)
$frontendViewPageAssetsJustUpdated = (! is_admin() && (isset($_GET['wpacu_time']) && $_GET['wpacu_time']) && get_transient('wpacu_page_just_updated'));
// After updating the "Settings" within the Dashboard
$pluginSettingsWithinDashboardJustUpdated = (is_admin() &&
(Misc::getVar('request', 'page') === WPACU_PLUGIN_ID . '_settings') &&
Misc::getVar('get', 'wpacu_selected_tab_area') &&
get_transient('wpacu_settings_updated'));
if ($unloadAssetsSubmit || $frontendViewPageAssetsJustUpdated || $pluginSettingsWithinDashboardJustUpdated) {
// Instruct the script to trigger clearing the cache via AJAX
$wpacu_object_data['clear_cache_on_page_load'] = true;
}
/*
* [End] Trigger plugin cache and other plugins'/system caches
*/
/*
* [Start] Trigger ONLY other plugins'/system caches
*/
// When click the "Clear CSS/JS Files Cache" link within the Dashboard (e.g. toolbar or quick action areas)
// Cache was already cleared; Do not clear it again (save resources); Clear other caches
// Make sure the referrer (it needs to have one) is the same URI as the currently loaded one (without any extra parameters)
$wpacuClearOtherCaches = false;
$wpacuReferrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if ($wpacuReferrer) {
list(,$wpacuUriFromReferrer ) = explode('//' . parse_url($wpacuReferrer, PHP_URL_HOST), $wpacuReferrer);
$wpacuRequestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$wpacuClearOtherCaches = ($wpacuUriFromReferrer === $wpacuRequestUri);
}
if ($wpacuClearOtherCaches && get_transient('wpacu_clear_assets_cache_via_link')) {
delete_transient('wpacu_clear_assets_cache_via_link');
$wpacu_object_data['clear_other_caches'] = true;
}
/*
* [End] Trigger ONLY other plugins'/system caches
*/
$wpacu_object_data['server_returned_404_not_found'] = sprintf(
__('When accessing this page the server responded with a status of %s404 (Not Found)%s. If this page is meant to return this status, you can ignore this message, otherwise you might have a problem with this page if it is meant to return a standard 200 OK status.', 'wp-asset-clean-up'),
'',
''
);
/*
* Whether to clear Autoptimize Cache or not (if the plugin is enabled)
*/
if ( ! Misc::isPluginActive('autoptimize/autoptimize.php') ) {
$wpacu_object_data['autoptimize_not_active'] = 1;
} else {
$wpacu_object_data['clear_autoptimize_cache'] = assetCleanUpClearAutoptimizeCache() ? 'true' : 'false';
}
/*
* Whether to clear "Cache Enabler" Cache or not (if the plugin is enabled)
*/
if ( ! Misc::isPluginActive('cache-enabler/cache-enabler.php') ) {
$wpacu_object_data['cache_enabler_not_active'] = 1;
} else {
$wpacu_object_data['clear_cache_enabler_cache'] = assetCleanUpClearCacheEnablerCache() ? 'true' : 'false';
if (assetCleanUpClearCacheEnablerCache()) {
$wpacu_object_data['wpacu_ajax_clear_cache_enabler_cache_nonce'] = wp_create_nonce( 'wpacu_ajax_clear_cache_enabler_cache_nonce' );
}
}
return $wpacu_object_data;
});
}
/**
*
*/
public function inlineCode()
{
if ( ! is_admin_bar_showing() ) {
return; // the code below is relevant only if the admin bar is shown
}
?>
settings['show_assets_meta_box']) {
// No point in loading the plugin JS if the management meta box is not shown
return;
}
// Were the meta boxes hidden for particular post types?
// There's no point in showing any plugin's CSS/JS there
if (isset($post->post_type) && in_array($post->post_type, MetaBoxes::hideMetaBoxesForPostTypes())) {
return;
}
// This refers only to the Dashboard pages generated by the plugin
$page = Misc::getVar('get', 'page');
// Only load the plugin's assets when they are needed
// This an example of assets that are correctly loaded in WordPress
if (isset($post->ID)) {
$this->loadPluginAssets = true;
}
if ($getPostId > 0) {
$this->loadPluginAssets = true;
}
if (strpos($page, WPACU_PLUGIN_ID) === 0) {
$this->loadPluginAssets = true;
}
if (! $this->loadPluginAssets) {
return;
}
$this->enqueueAdminStyles();
$this->enqueueAdminScripts();
}
/**
*
*/
public function stylesAndScriptsForPublic()
{
// Do not print it when an AJAX call is made from the Dashboard
if (WPACU_GET_LOADED_ASSETS_ACTION === true) {
return;
}
// Only for the administrator with the right permission
if (! Menu::userCanManageAssets()) {
return;
}
// If "Manage in the Front-end" option is not enabled in the plugin's "Settings", there's no point in loading the assets below
if (! Main::instance()->frontendShow()) {
return;
}
// Do not load any CSS & JS belonging to Asset CleanUp if in "Elementor" preview
if (Main::instance()->isFrontendEditView && isset($_GET['elementor-preview']) && $_GET['elementor-preview']) {
return;
}
if ( isset($_GET['wpacu_clean_load']) ) {
return;
}
$this->enqueuePublicStyles();
$this->enqueuePublicScripts();
}
/**
*
*/
private function enqueueAdminStyles()
{
wp_enqueue_style(
self::$ownAssets['styles']['style_core']['handle'],
plugins_url(self::$ownAssets['styles']['style_core']['rel_path'], WPACU_PLUGIN_FILE),
array(),
self::assetVer(self::$ownAssets['styles']['style_core']['rel_path'])
);
}
/**
*
*/
private function enqueueAdminScripts()
{
global $post, $pagenow;
$postId = 0; // default
$page = Misc::getVar('get', 'page');
$pageRequestFor = Misc::getVar('get', 'wpacu_for') ?: 'homepage';
// The admin is in a page such as /wp-admin/post.php?post=[POST_ID_HERE]&action=edit
$isPostIdFromEditPostPage = (isset($_GET['post'], $_GET['action']) && $_GET['action'] === 'edit' && $pagenow === 'post.php') ? (int)$_GET['post'] : '';
$isDashAssetsManagerPage = ($page === WPACU_PLUGIN_ID . '_assets_manager');
if ($isDashAssetsManagerPage) {
if ( $pageRequestFor === 'homepage' ) {
// Homepage tab / Check if the home page is one of the singular pages
$pageOnFront = (int) get_option( 'page_on_front' );
if ( $pageOnFront && $pageOnFront > 0 ) {
$postId = $pageOnFront;
}
} elseif ( in_array( $pageRequestFor, array( 'posts', 'pages', 'custom-post-types', 'media-attachment' ) ) && isset( $_GET['wpacu_post_id'] ) && $_GET['wpacu_post_id'] ) {
$postId = (int)Misc::getVar( 'get', 'wpacu_post_id' ) ?: 0;
}
} else {
$postId = isset($post->ID) ? $post->ID : 0;
if ($isPostIdFromEditPostPage > 0 && $isPostIdFromEditPostPage !== $postId) {
$postId = $isPostIdFromEditPostPage;
}
}
wp_register_script(
self::$ownAssets['scripts']['script_core']['handle'],
plugins_url(self::$ownAssets['scripts']['script_core']['rel_path'], WPACU_PLUGIN_FILE),
array('jquery'),
self::assetVer(self::$ownAssets['scripts']['script_core']['rel_path'])
);
if ($postId > 0) {
// It can also be the front page URL
$pageUrl = Misc::getPageUrl($postId);
} else {
$pageUrl = Misc::getPageUrl(0);
}
$svgReloadIcon = <<
Note: The checked URL returned an error when fetching the assets via AJAX call. This could be because of a firewall that is blocking the AJAX call, a redirect loop or an error in the script that is retrieving the output which could be due to an incompatibility between the plugin and the WordPress setup you are using.
Here is the response from the call:
Status Code Error: | {wpacu_status_code_error} * for more information about client and server errors, check this link |
Suggestion: | Select "WP Remote Post" as a method of retrieving the assets from the "Settings" page. If that doesn't fix the issue, just use "Manage in Front-end" option which should always work and submit a ticket about your problem. |
Output: | {wpacu_output} |
Note: The assets could not be fetched via the AJAX call. Here is the response:
Suggestion: | Select "WP Remote Post" as a method of retrieving the assets from the "Settings" page. If that doesn't fix the issue, just use "Manage in Front-end" option which should always work and submit a ticket about your problem. |
Output: | {wpacu_output} |