| 1 |
<?php |
| 2 |
require_once __DIR__ . "/vendor/autoload.php"; |
| 3 |
|
| 4 |
use Lws\Classes\LwsOptimize; |
| 5 |
use Lws\Classes\LwsOptimizeWpCli; |
| 6 |
|
| 7 |
/** |
| 8 |
* Plugin Name: LWS Optimize - All-in-One Speed Booster & Cache Tools |
| 9 |
* Plugin URI: https://www.lws.fr/ |
| 10 |
* Description: Reach better speed and performances with Optimize! Minification, Combination, Media convertion... Everything you need for a better website |
| 11 |
* Version: 4.0 |
| 12 |
* Author: LWS |
| 13 |
* Author URI: https://www.lws.fr |
| 14 |
* Tested up to: 7.0 |
| 15 |
* Domain Path: /languages |
| 16 |
* License: GPL-2.0-or-later |
| 17 |
* |
| 18 |
* @link https://www.lws.fr/ |
| 19 |
* @since 1.0 |
| 20 |
* @package lws-optimize |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
if (!defined('ABSPATH')) { |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
// Polyfills of useful PHP8+ functions for PHP < 8 |
| 30 |
if (!function_exists('str_starts_with')) { |
| 31 |
function str_starts_with($haystack, $needle) |
| 32 |
{ |
| 33 |
return strlen($needle) === 0 || strpos($haystack, $needle) === 0; |
| 34 |
} |
| 35 |
} |
| 36 |
if (!function_exists('str_contains')) { |
| 37 |
function str_contains($haystack, $needle) |
| 38 |
{ |
| 39 |
return strlen($needle) === 0 || strpos($haystack, $needle) !== false; |
| 40 |
} |
| 41 |
} |
| 42 |
if (!function_exists('str_ends_with')) { |
| 43 |
function str_ends_with($haystack, $needle) |
| 44 |
{ |
| 45 |
return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
// https://example.fr/wp-content/plugins/lws-optimize/ |
| 51 |
if (!defined('LWS_OP_URL')) { |
| 52 |
define('LWS_OP_URL', plugin_dir_url(__FILE__)); |
| 53 |
} |
| 54 |
|
| 55 |
// ABSPATH/wp-content/plugins/lws-optimize/ |
| 56 |
if (!defined('LWS_OP_DIR')) { |
| 57 |
define('LWS_OP_DIR', plugin_dir_path(__FILE__)); |
| 58 |
} |
| 59 |
|
| 60 |
// ABSPATH/wp-content/cache/lwsoptimize/ |
| 61 |
if (!defined('LWS_OP_UPLOADS')) { |
| 62 |
define('LWS_OP_UPLOADS', WP_CONTENT_DIR . '/cache/lwsoptimize/'); |
| 63 |
} |
| 64 |
|
| 65 |
// lws-optimize/lws-optimize.php |
| 66 |
if (!defined('LWS_OP_BASENAME')) { |
| 67 |
define('LWS_OP_BASENAME', plugin_basename(__FILE__)); |
| 68 |
} |
| 69 |
|
| 70 |
// Path to the object-cache file (for Memcached) |
| 71 |
if (!defined('LWSOP_OBJECTCACHE_PATH')) { |
| 72 |
define('LWSOP_OBJECTCACHE_PATH', WP_CONTENT_DIR . '/object-cache.php'); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
// Function declarations for hook callbacks |
| 77 |
function lws_optimize_activation_callback() { |
| 78 |
delete_option('lws_optimize_preload_is_ongoing'); |
| 79 |
|
| 80 |
$optimize_options = get_option('lws_optimize_config_array', []); |
| 81 |
|
| 82 |
$GLOBALS['lws_optimize']->lws_optimize_set_cache_htaccess(); |
| 83 |
$GLOBALS['lws_optimize']->lws_optimize_reset_header_htaccess(); |
| 84 |
|
| 85 |
// Deactivate the preloading on plugin activation to prevent issues |
| 86 |
if (isset($optimize_options['filebased_cache']) && $optimize_options['filebased_cache']['state'] == "true") { |
| 87 |
if (wp_next_scheduled("lws_optimize_start_filebased_preload")) { |
| 88 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload'); |
| 89 |
} |
| 90 |
|
| 91 |
$optimize_options['filebased_cache']['preload'] = "false"; |
| 92 |
$optimize_options['filebased_cache']['preload_done'] = 0; |
| 93 |
$optimize_options['filebased_cache']['preload_ongoing'] = "true"; |
| 94 |
|
| 95 |
update_option('lws_optimize_config_array', $optimize_options); |
| 96 |
} |
| 97 |
|
| 98 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_convert_media_cron'), 'lws_optimize_convert_media_cron'); |
| 99 |
wp_unschedule_event(wp_next_scheduled('lwsop_revertOptimization'), 'lwsop_revertOptimization'); |
| 100 |
|
| 101 |
if (isset($optimize_options['maintenance_db']) && $optimize_options['maintenance_db']['state'] == "true") { |
| 102 |
if (wp_next_scheduled("lws_optimize_maintenance_db_weekly")) { |
| 103 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly'); |
| 104 |
} |
| 105 |
wp_schedule_event(time() + 604800, 'weekly', "lws_optimize_maintenance_db_weekly"); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
function lws_optimize_deactivation_callback() { |
| 110 |
delete_option('lws_optimize_preload_is_ongoing'); |
| 111 |
|
| 112 |
// Deactivate all crons |
| 113 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload'); |
| 114 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_convert_media_cron'), 'lws_optimize_convert_media_cron'); |
| 115 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly'); |
| 116 |
wp_unschedule_event(wp_next_scheduled("lwsop_revertOptimization"), "lwsop_revertOptimization"); |
| 117 |
|
| 118 |
// Remove .htaccess content |
| 119 |
$htaccess_file = ABSPATH . '/.htaccess'; |
| 120 |
|
| 121 |
if (file_exists($htaccess_file) && is_writable($htaccess_file)) { |
| 122 |
$htaccess_content = file_get_contents($htaccess_file); |
| 123 |
|
| 124 |
// Remove LWS OPTIMIZE - CACHING section |
| 125 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - CACHING.*?#END LWS OPTIMIZE - CACHING\s*/s', "\n", $htaccess_content); |
| 126 |
|
| 127 |
// Remove LWS OPTIMIZE - EXPIRE HEADER section |
| 128 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - EXPIRE HEADER.*?#END LWS OPTIMIZE - EXPIRE HEADER\s*/s', "\n", $htaccess_content); |
| 129 |
|
| 130 |
// Remove LWS OPTIMIZE - GZIP COMPRESSION section |
| 131 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - GZIP COMPRESSION.*?#END LWS OPTIMIZE - GZIP COMPRESSION\s*/s', "\n", $htaccess_content); |
| 132 |
|
| 133 |
// Write the modified content back to the file |
| 134 |
file_put_contents($htaccess_file, $htaccess_content); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
function lws_optimize_uninstall_callback() { |
| 139 |
// Remove the cache folder |
| 140 |
$cache_dir = WP_CONTENT_DIR . '/cache/lwsoptimize/'; |
| 141 |
$upload_dir = WP_CONTENT_DIR . '/uploads/lwsoptimize/'; |
| 142 |
|
| 143 |
WP_Filesystem(); |
| 144 |
global $wp_filesystem; |
| 145 |
|
| 146 |
if (file_exists($cache_dir)) { |
| 147 |
$wp_filesystem->rmdir($cache_dir, true); |
| 148 |
} |
| 149 |
|
| 150 |
if (file_exists($upload_dir)) { |
| 151 |
$wp_filesystem->rmdir($upload_dir, true); |
| 152 |
} |
| 153 |
|
| 154 |
// Deactivate all crons |
| 155 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_start_filebased_preload'), 'lws_optimize_start_filebased_preload'); |
| 156 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_convert_media_cron'), 'lws_optimize_convert_media_cron'); |
| 157 |
wp_unschedule_event(wp_next_scheduled('lws_optimize_maintenance_db_weekly'), 'lws_optimize_maintenance_db_weekly'); |
| 158 |
wp_unschedule_event(wp_next_scheduled("lwsop_revertOptimization"), "lwsop_revertOptimization"); |
| 159 |
|
| 160 |
// Remove .htaccess content |
| 161 |
$htaccess_file = ABSPATH . '/.htaccess'; |
| 162 |
|
| 163 |
if (file_exists($htaccess_file) && is_writable($htaccess_file)) { |
| 164 |
$htaccess_content = file_get_contents($htaccess_file); |
| 165 |
|
| 166 |
// Remove LWS OPTIMIZE - CACHING section |
| 167 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - CACHING.*?#END LWS OPTIMIZE - CACHING\s*/s', "\n", $htaccess_content); |
| 168 |
|
| 169 |
// Remove LWS OPTIMIZE - EXPIRE HEADER section |
| 170 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - EXPIRE HEADER.*?#END LWS OPTIMIZE - EXPIRE HEADER\s*/s', "\n", $htaccess_content); |
| 171 |
|
| 172 |
// Remove LWS OPTIMIZE - GZIP COMPRESSION section |
| 173 |
$htaccess_content = preg_replace('/\s*#LWS OPTIMIZE - GZIP COMPRESSION.*?#END LWS OPTIMIZE - GZIP COMPRESSION\s*/s', "\n", $htaccess_content); |
| 174 |
|
| 175 |
// Write the modified content back to the file |
| 176 |
file_put_contents($htaccess_file, $htaccess_content); |
| 177 |
} |
| 178 |
|
| 179 |
// Remove all options on delete |
| 180 |
delete_option('lws_optimize_config_array'); |
| 181 |
delete_option('lws_optimize_preload_is_ongoing'); |
| 182 |
delete_option('lws_optimize_image_api_key'); |
| 183 |
delete_option('lws_optimize_cache_statistics'); |
| 184 |
delete_option('lws_optimize_image_conversion_options'); |
| 185 |
delete_option('lwsop_plugin_version'); |
| 186 |
} |
| 187 |
|
| 188 |
// Actions to execute when the plugin is activated / deactivated / deleted / upgraded |
| 189 |
register_activation_hook(__FILE__, 'lws_optimize_activation_callback'); |
| 190 |
register_deactivation_hook(__FILE__, 'lws_optimize_deactivation_callback'); |
| 191 |
register_uninstall_hook(__FILE__, 'lws_optimize_uninstall_callback'); |
| 192 |
|
| 193 |
add_action('plugins_loaded', function() { |
| 194 |
$ancienne_version = get_option('lwsop_plugin_version', 0); |
| 195 |
$nouvelle_version = '3.3.11'; // Remplacez par la version actuelle du plugin. |
| 196 |
|
| 197 |
if ($ancienne_version !== $nouvelle_version) { |
| 198 |
add_option( 'wp_lwsoptimize_post_update', 1); |
| 199 |
|
| 200 |
// Mettre à jour la version en base. |
| 201 |
update_option('lwsop_plugin_version', $nouvelle_version); |
| 202 |
} |
| 203 |
}); |
| 204 |
|
| 205 |
// Manage the "Our plugins" tab, allowing users to install our plugins |
| 206 |
add_action("wp_ajax_lws_op_downloadPlugin", "wp_ajax_install_plugin"); |
| 207 |
add_action("wp_ajax_lws_op_activatePlugin", function() |
| 208 |
{ |
| 209 |
check_ajax_referer('activate_plugin', '_ajax_nonce'); |
| 210 |
if (isset($_POST['ajax_slug'])) { |
| 211 |
switch (sanitize_textarea_field($_POST['ajax_slug'])) { |
| 212 |
case 'lws-hide-login': |
| 213 |
activate_plugin('lws-hide-login/lws-hide-login.php'); |
| 214 |
break; |
| 215 |
case 'lws-tools': |
| 216 |
activate_plugin('lws-tools/lws-tools.php'); |
| 217 |
break; |
| 218 |
case 'lws-affiliation': |
| 219 |
activate_plugin('lws-affiliation/lws-affiliation.php'); |
| 220 |
break; |
| 221 |
case 'lws-cleaner': |
| 222 |
activate_plugin('lws-cleaner/lws-cleaner.php'); |
| 223 |
break; |
| 224 |
default: |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
}); |
| 229 |
|
| 230 |
$deactivated = get_option('lws_optimize_deactivate_temporarily', false); |
| 231 |
if ($deactivated) { |
| 232 |
if (time() > $deactivated) { |
| 233 |
delete_option('lws_optimize_deactivate_temporarily'); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$GLOBALS['lws_optimize'] = new LwsOptimize(); |
| 238 |
|
| 239 |
// Register WP-CLI commands once the plugin is loaded |
| 240 |
if (defined('WP_CLI') && WP_CLI) { |
| 241 |
LwsOptimizeWpCli::register_commands(); |
| 242 |
} |
| 243 |
|