load_includes(); $this->init_hooks(); } /** * Load class files. */ private function load_includes() { $includes = array( 'class-lazy-load', 'class-minify-html', 'class-defer-js', 'class-bloat-remover', 'class-db-cleanup', 'class-performance-score', 'class-asset-counter', 'class-admin-page', 'class-pro-bridge', ); foreach ( $includes as $file ) { require_once ASMITH_SO_PLUGIN_DIR . 'includes/' . $file . '.php'; } } /** * Register WordPress hooks. */ private function init_hooks() { register_activation_hook( ASMITH_SO_PLUGIN_FILE, array( $this, 'activate' ) ); register_deactivation_hook( ASMITH_SO_PLUGIN_FILE, array( $this, 'deactivate' ) ); add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'activated_plugin', array( $this, 'redirect_after_activation' ) ); } /** * Runs after all plugins are loaded. */ public function on_plugins_loaded() { Asmith_SO_Lazy_Load::instance(); Asmith_SO_Minify_HTML::instance(); Asmith_SO_Defer_JS::instance(); Asmith_SO_Bloat_Remover::instance(); Asmith_SO_DB_Cleanup::instance(); Asmith_SO_Performance_Score::instance(); Asmith_SO_Asset_Counter::instance(); Asmith_SO_Admin_Page::instance(); Asmith_SO_Pro_Bridge::instance(); } /** * Enqueue admin CSS and JS on our pages. * * @param string $hook_suffix The current admin page hook. */ public function enqueue_admin_assets( $hook_suffix ) { $our_pages = array( 'toplevel_page_asmith-so', 'speed-optimizer_page_asmith-so-settings' ); if ( ! in_array( $hook_suffix, $our_pages, true ) ) { return; } wp_enqueue_style( 'asmith-so-admin', ASMITH_SO_PLUGIN_URL . 'assets/css/admin.css', array(), ASMITH_SO_VERSION ); wp_enqueue_script( 'asmith-so-admin', ASMITH_SO_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery' ), ASMITH_SO_VERSION, true ); wp_localize_script( 'asmith-so-admin', 'asmith_so', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'asmith_so_nonce' ), 'i18n' => array( 'scanning' => esc_html__( 'Scanning...', 'a-smith-speed-optimizer' ), 'scan_done' => esc_html__( 'Scan complete!', 'a-smith-speed-optimizer' ), 'scan_failed' => esc_html__( 'Scan failed. Please try again.', 'a-smith-speed-optimizer' ), 'cleaning' => esc_html__( 'Cleaning...', 'a-smith-speed-optimizer' ), 'cleaned' => esc_html__( 'Cleaned!', 'a-smith-speed-optimizer' ), 'confirm_cleanup' => esc_html__( 'Are you sure you want to clean up these items? This cannot be undone.', 'a-smith-speed-optimizer' ), ), ) ); // On the dashboard page, if this is the first run, enqueue the first-scan animation. if ( 'toplevel_page_asmith-so' === $hook_suffix && empty( get_option( 'asmith_so_last_scan' ) ) ) { wp_enqueue_script( 'asmith-so-firstrun', ASMITH_SO_PLUGIN_URL . 'assets/js/first-scan.js', array( 'jquery', 'asmith-so-admin' ), ASMITH_SO_VERSION, true ); wp_localize_script( 'asmith-so-firstrun', 'asmith_so_firstrun', array( 'dashboard_url' => admin_url( 'admin.php?page=asmith-so' ), 'i18n' => array( 'analyzing' => __( 'Analyzing current optimizations...', 'a-smith-speed-optimizer' ), 'optimizations_checked' => __( 'Optimization status checked', 'a-smith-speed-optimizer' ), 'counting_assets' => __( 'Counting scripts and styles...', 'a-smith-speed-optimizer' ), 'assets_inventoried' => __( 'Assets inventoried', 'a-smith-speed-optimizer' ), 'checking_bloat' => __( 'Checking for bloat items...', 'a-smith-speed-optimizer' ), 'bloat_complete' => __( 'Bloat analysis complete', 'a-smith-speed-optimizer' ), 'measuring' => __( 'Measuring performance metrics...', 'a-smith-speed-optimizer' ), 'metrics_collected' => __( 'Metrics collected', 'a-smith-speed-optimizer' ), 'calculating' => __( 'Calculating performance score...', 'a-smith-speed-optimizer' ), 'score_calculated' => __( 'Score calculated', 'a-smith-speed-optimizer' ), 'scan_complete' => __( 'Scan complete!', 'a-smith-speed-optimizer' ), 'scan_errors' => __( 'Scan complete with some errors. View dashboard.', 'a-smith-speed-optimizer' ), 'headline_fast' => __( 'Your site is lightning fast!', 'a-smith-speed-optimizer' ), 'msg_fast' => __( 'All optimizations are in great shape.', 'a-smith-speed-optimizer' ), 'headline_good' => __( 'Good speed, with room to improve.', 'a-smith-speed-optimizer' ), 'msg_good' => __( 'Check your dashboard for optimization opportunities.', 'a-smith-speed-optimizer' ), 'headline_attention' => __( 'Some optimizations need attention.', 'a-smith-speed-optimizer' ), 'msg_attention' => __( 'Enable more features to boost your speed score.', 'a-smith-speed-optimizer' ), 'headline_slow' => __( 'Your site needs speed optimization.', 'a-smith-speed-optimizer' ), 'msg_slow' => __( 'View your dashboard to start optimizing.', 'a-smith-speed-optimizer' ), ), ) ); } } /** * Plugin activation. */ public function activate() { // Schedule weekly performance scan cron. if ( ! wp_next_scheduled( 'asmith_so_weekly_scan' ) ) { wp_schedule_event( time(), 'weekly', 'asmith_so_weekly_scan' ); } // Set default options. $defaults = array( 'asmith_so_lazy_load' => true, 'asmith_so_minify_html' => false, 'asmith_so_defer_js' => false, 'asmith_so_defer_js_exclusions' => '', 'asmith_so_lazy_load_exclusions' => '', 'asmith_so_remove_emoji' => true, 'asmith_so_remove_embeds' => true, 'asmith_so_remove_query_strings' => true, 'asmith_so_remove_dashicons' => true, 'asmith_so_remove_xmlrpc' => true, 'asmith_so_remove_rest_links' => true, 'asmith_so_remove_shortlinks' => true, 'asmith_so_remove_wlw' => true, 'asmith_so_remove_rsd' => true, 'asmith_so_remove_rss' => false, 'asmith_so_email_frequency' => 'weekly', 'asmith_so_email_recipients' => get_option( 'admin_email' ), 'asmith_so_auto_scan' => true, ); foreach ( $defaults as $key => $value ) { if ( false === get_option( $key ) ) { update_option( $key, $value ); } } // Store activation time for first-run scan. update_option( 'asmith_so_activated', time() ); // Flush rewrite rules. flush_rewrite_rules(); } /** * Redirect to Speed Optimizer dashboard immediately after activation. * * @param string $plugin The plugin file that was activated. */ public function redirect_after_activation( $plugin ) { if ( $plugin !== ASMITH_SO_PLUGIN_BASENAME ) { return; } // Don't redirect on bulk activation or WP-CLI. if ( isset( $_GET['activate-multi'] ) || wp_doing_ajax() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WP core activation redirect pattern, no form data processed. return; } wp_safe_redirect( admin_url( 'admin.php?page=asmith-so' ) ); exit; } /** * Plugin deactivation. */ public function deactivate() { wp_clear_scheduled_hook( 'asmith_so_weekly_scan' ); wp_clear_scheduled_hook( 'asmith_so_daily_scan' ); wp_clear_scheduled_hook( 'asmith_so_email_report' ); } } /** * Returns the main plugin instance. * * @return Asmith_Speed_Optimizer */ function asmith_so() { return Asmith_Speed_Optimizer::instance(); } // Boot the plugin. asmith_so();