| 1 |
<?php |
| 2 |
/** |
| 3 |
* MetaSync - Code Minification & Delivery Module Loader |
| 4 |
* |
| 5 |
* @package Search Atlas SEO |
| 6 |
* @copyright Copyright (C) 2021-2025, Search Atlas Group - support@searchatlas.com |
| 7 |
* @since 2.5.23 |
| 8 |
*/ |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
define('METASYNC_CODE_MIN_PATH', plugin_dir_path(__FILE__)); |
| 15 |
define('METASYNC_CODE_MIN_URL', plugin_dir_url(__FILE__)); |
| 16 |
|
| 17 |
// Load classes |
| 18 |
require_once METASYNC_CODE_MIN_PATH . 'class-minification-settings.php'; |
| 19 |
require_once METASYNC_CODE_MIN_PATH . 'class-minification-cache.php'; |
| 20 |
require_once METASYNC_CODE_MIN_PATH . 'class-compatibility-guard.php'; |
| 21 |
require_once METASYNC_CODE_MIN_PATH . 'class-css-minifier.php'; |
| 22 |
require_once METASYNC_CODE_MIN_PATH . 'class-js-minifier.php'; |
| 23 |
require_once METASYNC_CODE_MIN_PATH . 'class-js-defer-delay.php'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize code minification features based on settings. |
| 27 |
* Only activates when the plugin is active and settings are enabled. |
| 28 |
*/ |
| 29 |
function metasync_code_minification_init() { |
| 30 |
$settings = Metasync_Minification_Settings::get_settings(); |
| 31 |
$conflicts = Metasync_Compatibility_Guard::get_active_conflicts(); |
| 32 |
|
| 33 |
// CSS Minification |
| 34 |
if (!empty($settings['enable_css_minify']) && !isset($conflicts['css_minify'])) { |
| 35 |
new Metasync_CSS_Minifier($settings); |
| 36 |
} |
| 37 |
|
| 38 |
// JS Minification |
| 39 |
if (!empty($settings['enable_js_minify']) && !isset($conflicts['js_minify'])) { |
| 40 |
new Metasync_JS_Minifier($settings); |
| 41 |
} |
| 42 |
|
| 43 |
// JS Defer/Delay (defer check covers both defer and delay) |
| 44 |
$defer_enabled = !empty($settings['enable_js_defer']) || !empty($settings['enable_js_delay']); |
| 45 |
if ($defer_enabled && !isset($conflicts['js_defer'])) { |
| 46 |
new Metasync_JS_Defer_Delay($settings); |
| 47 |
} |
| 48 |
|
| 49 |
// Schedule cache cleanup cron |
| 50 |
Metasync_Minification_Cache::schedule_cleanup(); |
| 51 |
} |
| 52 |
add_action('init', 'metasync_code_minification_init'); |
| 53 |
|
| 54 |
/** |
| 55 |
* Cron handler for cache cleanup. |
| 56 |
*/ |
| 57 |
add_action(Metasync_Minification_Cache::CRON_HOOK, ['Metasync_Minification_Cache', 'cron_cleanup']); |
| 58 |
|
| 59 |
/** |
| 60 |
* Hook into MetaSync cache purge system. |
| 61 |
*/ |
| 62 |
add_action('metasync_cache_purge_all', function () { |
| 63 |
Metasync_Minification_Cache::purge_all(); |
| 64 |
}); |
| 65 |
|
| 66 |
/** |
| 67 |
* Purge minification cache on theme switch. |
| 68 |
*/ |
| 69 |
add_action('switch_theme', function () { |
| 70 |
Metasync_Minification_Cache::purge_all(); |
| 71 |
}); |
| 72 |
|
| 73 |
/** |
| 74 |
* Purge minification cache on plugin activation/deactivation. |
| 75 |
*/ |
| 76 |
add_action('activated_plugin', function () { |
| 77 |
Metasync_Minification_Cache::purge_all(); |
| 78 |
}); |
| 79 |
add_action('deactivated_plugin', function () { |
| 80 |
Metasync_Minification_Cache::purge_all(); |
| 81 |
}); |
| 82 |
|
| 83 |
/** |
| 84 |
* Enqueue code minification admin assets on the correct page only. |
| 85 |
* |
| 86 |
* @param string $hook_suffix The current admin page hook suffix. |
| 87 |
*/ |
| 88 |
function metasync_code_minification_enqueue_assets($hook_suffix) { |
| 89 |
if (strpos($hook_suffix, 'code-minification') === false) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$version = defined('METASYNC_VERSION') ? METASYNC_VERSION : '1.0.0'; |
| 94 |
$assets_url = METASYNC_CODE_MIN_URL . 'assets/'; |
| 95 |
|
| 96 |
wp_enqueue_style( |
| 97 |
'metasync-code-minification', |
| 98 |
$assets_url . 'css/code-minification.css', |
| 99 |
[], |
| 100 |
$version |
| 101 |
); |
| 102 |
|
| 103 |
wp_enqueue_script( |
| 104 |
'metasync-code-min-settings', |
| 105 |
$assets_url . 'js/code-minification-settings.js', |
| 106 |
[], |
| 107 |
$version, |
| 108 |
true |
| 109 |
); |
| 110 |
|
| 111 |
wp_localize_script('metasync-code-min-settings', 'metasyncCodeMin', [ |
| 112 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 113 |
'nonce' => wp_create_nonce('metasync_code_minification_nonce'), |
| 114 |
'purgeConfirm' => __('Are you sure you want to purge the minification cache? This cannot be undone.', 'metasync'), |
| 115 |
'resetConfirm' => __('Are you sure you want to reset all code minification settings to their defaults?', 'metasync'), |
| 116 |
]); |
| 117 |
} |
| 118 |
add_action('admin_enqueue_scripts', 'metasync_code_minification_enqueue_assets'); |
| 119 |
|
| 120 |
/** |
| 121 |
* AJAX handler: Purge minification cache. |
| 122 |
*/ |
| 123 |
add_action('wp_ajax_metasync_purge_minification_cache', function () { |
| 124 |
check_ajax_referer('metasync_code_minification_nonce', 'nonce'); |
| 125 |
|
| 126 |
if (!current_user_can('manage_options')) { |
| 127 |
wp_send_json_error(__('Permission denied.', 'metasync')); |
| 128 |
} |
| 129 |
|
| 130 |
Metasync_Minification_Cache::purge_all(); |
| 131 |
$stats = Metasync_Minification_Cache::get_cache_stats(); |
| 132 |
|
| 133 |
wp_send_json_success([ |
| 134 |
'message' => __('Minification cache purged successfully.', 'metasync'), |
| 135 |
'stats' => $stats, |
| 136 |
]); |
| 137 |
}); |
| 138 |
|