Massive refactoring to improve code organization, maintainability, and follow SOLID principles. Main plugin file reduced by 80%! 🏗️ New Architecture: Created 4 new specialized classes following Single Responsibility Principle: 1️⃣ class-admin.php (118 lines) - Admin menu management - Settings registration - Admin scripts/styles enqueuing - Settings link on plugins page 2️⃣ class-ajax-handler.php (105 lines) - Clear cache AJAX - Optimize database AJAX - Export settings AJAX - Create cache directory AJAX 3️⃣ class-frontend-optimizer.php (237 lines) - Disable Emojis (complete) - Disable Dashicons (complete) - Disable Embeds (complete) - Remove Query Strings - Enable GZIP Compression 4️⃣ class-activator.php (68 lines) - Plugin activation logic - Plugin deactivation logic - Default settings setup - Cache directory creation 📊 Impact: - opticore.php: 583 → 160 lines (-423 lines, -73%) - Better separation of concerns - Easier to maintain and test - Each class has single responsibility - Cleaner code organization 🎯 Main Plugin File Now: - Only orchestration/initialization - Loads dependencies - Initializes components - Delegates to specialized classes ✨ Benefits: - ✅ SOLID principles applied - ✅ Single Responsibility Principle - ✅ Better code organization - ✅ Easier to test - ✅ Easier to extend - ✅ More maintainable - ✅ Professional structure All functionality remains identical - this is pure refactoring with no changes to features or behavior.
104 lines
3.1 KiB
PHP
104 lines
3.1 KiB
PHP
<?php
|
|
/**
|
|
* AJAX Handler
|
|
* Handles all AJAX requests
|
|
*
|
|
* @package OptiCore
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class OptiCore_AJAX_Handler {
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
$this->init_hooks();
|
|
}
|
|
|
|
/**
|
|
* Initialize AJAX hooks
|
|
*/
|
|
private function init_hooks() {
|
|
add_action('wp_ajax_opticore_clear_cache', array($this, 'clear_cache'));
|
|
add_action('wp_ajax_opticore_optimize_database', array($this, 'optimize_database'));
|
|
add_action('wp_ajax_opticore_export_settings', array($this, 'export_settings'));
|
|
add_action('wp_ajax_opticore_create_cache_dir', array($this, 'create_cache_dir'));
|
|
}
|
|
|
|
/**
|
|
* AJAX: Clear cache
|
|
*/
|
|
public function clear_cache() {
|
|
check_ajax_referer('opticore_ajax_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(array('message' => __('Permission denied.', 'opticore')));
|
|
}
|
|
|
|
$cache_manager = OptiCore_Cache_Manager::get_instance();
|
|
$cache_manager->clear_page_cache();
|
|
|
|
wp_send_json_success(array('message' => __('Cache cleared successfully!', 'opticore')));
|
|
}
|
|
|
|
/**
|
|
* AJAX: Optimize database
|
|
*/
|
|
public function optimize_database() {
|
|
check_ajax_referer('opticore_ajax_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(array('message' => __('Permission denied.', 'opticore')));
|
|
}
|
|
|
|
$db_optimizer = OptiCore_Database_Optimizer::get_instance();
|
|
$db_optimizer->run_optimization();
|
|
|
|
wp_send_json_success(array('message' => __('Database optimized successfully!', 'opticore')));
|
|
}
|
|
|
|
/**
|
|
* AJAX: Export settings
|
|
*/
|
|
public function export_settings() {
|
|
check_ajax_referer('opticore_ajax_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(array('message' => __('Permission denied.', 'opticore')));
|
|
}
|
|
|
|
$settings = get_option('opticore_settings');
|
|
|
|
wp_send_json_success(array('settings' => $settings));
|
|
}
|
|
|
|
/**
|
|
* AJAX: Create cache directory
|
|
*/
|
|
public function create_cache_dir() {
|
|
check_ajax_referer('opticore_cache_nonce', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(array('message' => __('Permission denied.', 'opticore')));
|
|
}
|
|
|
|
$cache_dir = WP_CONTENT_DIR . '/cache/opticore';
|
|
|
|
if (wp_mkdir_p($cache_dir)) {
|
|
wp_send_json_success(array('message' => __('Cache directory created successfully!', 'opticore')));
|
|
} else {
|
|
wp_send_json_error(array('message' => __('Failed to create cache directory. Check permissions.', 'opticore')));
|
|
}
|
|
}
|
|
}
|
|
|