plugin = $plugin; } /** * Register AJAX hooks. * * @return void */ public function register() { add_action( 'wp_ajax_acelora_site_accelerator_purge_cache', array( $this, 'purge_cache' ) ); add_action( 'wp_ajax_acelora_site_accelerator_preload_cache', array( $this, 'preload_cache' ) ); add_action( 'wp_ajax_acelora_site_accelerator_preload_status', array( $this, 'preload_status' ) ); add_action( 'wp_ajax_acelora_site_accelerator_optimize_database', array( $this, 'optimize_database' ) ); add_action( 'wp_ajax_acelora_site_accelerator_optimize_images', array( $this, 'optimize_images' ) ); add_action( 'wp_ajax_acelora_site_accelerator_run_optimization', array( $this, 'run_optimization' ) ); add_action( 'wp_ajax_acelora_site_accelerator_apply_browser_rules', array( $this, 'apply_browser_rules' ) ); } /** * Purge all cache. * * @return void */ public function purge_cache() { $this->verify(); $deleted = $this->plugin->purge_all_caches(); wp_send_json_success( array( 'message' => sprintf( /* translators: %d: deleted files */ __( 'Cache purged. %d cached files removed.', 'acelora-site-accelerator' ), absint( $deleted ) ), ) ); } /** * Start preload. * * @return void */ public function preload_cache() { $this->verify(); $preloader = $this->plugin->module( 'preloader' ); $status = $preloader ? $preloader->start( true ) : array(); wp_send_json_success( array( 'message' => __( 'URL warmup started.', 'acelora-site-accelerator' ), 'status' => $status, ) ); } /** * Get preload status. * * @return void */ public function preload_status() { $this->verify(); $preloader = $this->plugin->module( 'preloader' ); wp_send_json_success( array( 'status' => $preloader ? $preloader->status() : array(), ) ); } /** * Optimize database. * * @return void */ public function optimize_database() { $this->verify(); $database = $this->plugin->module( 'database' ); $result = $database ? $database->cleanup() : array(); wp_send_json_success( array( 'message' => __( 'Database optimization complete.', 'acelora-site-accelerator' ), 'result' => $result, ) ); } /** * Optimize a batch of images. * * @return void */ public function optimize_images() { $this->verify(); $images = $this->plugin->module( 'images' ); $result = $images ? $images->optimize_batch() : array(); $message = __( 'Image optimization batch complete. No pending images were found.', 'acelora-site-accelerator' ); if ( ! empty( $result['processed'] ) || ! empty( $result['files'] ) || ! empty( $result['webp'] ) ) { $message = sprintf( /* translators: 1: attachment count, 2: file count, 3: generated WebP count, 4: saved file size */ __( 'Image optimization complete. %1$d attachments, %2$d files, %3$d WebP files, %4$s saved.', 'acelora-site-accelerator' ), absint( $result['processed'] ?? 0 ), absint( $result['files'] ?? 0 ), absint( $result['webp'] ?? 0 ), size_format( absint( $result['saved'] ?? 0 ) ) ); } elseif ( ! empty( $result['errors'] ) && is_array( $result['errors'] ) ) { $message = __( 'Image optimization could not process the pending images. Check file permissions and PHP image support.', 'acelora-site-accelerator' ); } wp_send_json_success( array( 'message' => $message, 'result' => $result, ) ); } /** * Apply browser cache rules. * * @return void */ public function apply_browser_rules() { $this->verify(); $browser = $this->plugin->module( 'browser' ); $result = $browser ? $browser->apply_rules() : false; if ( $result ) { wp_send_json_success( array( 'message' => __( 'Browser cache rules applied to .htaccess.', 'acelora-site-accelerator' ) ) ); } wp_send_json_error( array( 'message' => __( 'Unable to write .htaccess rules. Check file permissions or apply the displayed rules manually.', 'acelora-site-accelerator' ) ) ); } /** * Run one-click optimization. * * @return void */ public function run_optimization() { $this->verify(); wp_send_json_success( $this->run_full_optimization( true ) ); } /** * Run full optimization, optionally returning user-friendly data. * * @param bool $interactive Whether user initiated this action. * @return array */ public function run_full_optimization( $interactive = true ) { $results = array(); $cache = $this->plugin->module( 'page_cache' ); if ( $cache ) { $results['cache_purged'] = $this->plugin->purge_all_caches(); $cache->cleanup_expired(); } $database = $this->plugin->module( 'database' ); if ( $database ) { $results['database'] = $database->cleanup(); } $images = $this->plugin->module( 'images' ); if ( $images ) { $results['images'] = $images->optimize_batch( 10 ); } $preloader = $this->plugin->module( 'preloader' ); if ( $preloader ) { $results['preload'] = $preloader->start( $interactive ); } $this->plugin->logger()->log( 'info', 'One-click optimization completed.', $results ); return array( 'message' => __( 'Optimize Website workflow complete. Cache cleanup ran, database cleanup ran, images were processed, and URL warmup was queued.', 'acelora-site-accelerator' ), 'result' => $results, ); } /** * Validate request. * * @return void */ private function verify() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Insufficient permissions.', 'acelora-site-accelerator' ) ), 403 ); } check_ajax_referer( 'acelora_site_accelerator_admin', 'nonce' ); } }