check_and_run_migrations(); // AGENTS.MD ENHANCEMENT: Initialize REST API with error handling try { AICOAGAC_Rest_Api::get_instance(); aicoagac_debug_log('Main Plugin: REST API singleton instance initialized'); } catch (Exception $e) { aicoagac_debug_log('Main Plugin: ERROR - Failed to initialize REST API: ' . $e->getMessage()); } // UNIFIED AUTOMATION FIX: Simple Automation is initialized in aicoagac_load_automation_system() // Removed duplicate initialization to prevent conflicts // AGENTS.MD CRITICAL FIX: Initialize only the selected automation system // This logic is now handled by aicoagac_load_automation_system // Add admin menu add_action('admin_menu', array($this, 'add_admin_menu')); // Enqueue admin scripts add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); // Admin initialization (moved from redundant handler) if (is_admin()) { // Lower priority to run after OAuth callbacks and other critical tasks add_action('admin_init', array($this, 'admin_init_tasks'), 15); } aicoagac_debug_log("Main Plugin: Initialization completed successfully in context: {$context}"); } catch (Exception $e) { aicoagac_debug_log("Main Plugin: CRITICAL ERROR during initialization: " . $e->getMessage()); // Continue execution but log the error for debugging } } /** * Get unified automation system status */ public function get_automation_system_status() { return array( 'current_system' => 'unified', 'simple_available' => false, 'system_status' => __('Unified & Reliable', 'ai-content-agent') ); } /** * Admin initialization tasks (moved from redundant handler) */ public function admin_init_tasks() { // Handle database updates (has its own frequency control) aicoagac_check_database_updates(); } /** * Check and run database migrations if needed */ private function check_and_run_migrations() { // Only run migrations in admin context to avoid performance issues if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) { return; } // Check if migrations have been checked recently to avoid repeated checks if (get_transient('aicoagac_migration_check_done')) { return; } require_once AICOAGAC_PLUGIN_PATH . 'includes/class-aicoagac-migration-manager.php'; $migration_manager = new AICOAGAC_Migration_Manager(); $result = $migration_manager->run_migrations(); if (is_wp_error($result)) { aicoagac_debug_log('Migration error: ' . $result->get_error_message()); // Set a shorter transient on error to retry sooner set_transient('aicoagac_migration_check_done', time(), 300); // 5 minutes } else { // Set transient to avoid repeated checks for 1 hour set_transient('aicoagac_migration_check_done', time(), 3600); } } /** * Add admin menu */ public function add_admin_menu() { add_menu_page( __( 'AI Content Agent (ACA)', 'ai-content-agent' ), __( 'AI Content Agent (ACA)', 'ai-content-agent' ), 'manage_options', 'ai-content-agent', array($this, 'admin_page'), 'dashicons-welcome-write-blog', 30 ); } /** * Admin page callback */ public function admin_page() { // Dependencies installer removed for WordPress.org compliance echo '
'; } /** * Enqueue admin scripts and styles */ public function enqueue_admin_scripts($hook) { // Only load on our plugin page if ('toplevel_page_ai-content-agent' != $hook) { return; } // Find the latest built JS file in admin/assets $assets_dir = AICOAGAC_PLUGIN_PATH . 'admin/assets/'; $js_files = glob($assets_dir . 'index-*.js'); if (!empty($js_files)) { // Get the most recent JS file by modification time $latest_file = ''; $latest_time = 0; foreach ($js_files as $file) { $file_time = filemtime($file); if ($file_time > $latest_time) { $latest_time = $file_time; $latest_file = $file; } } if ($latest_file) { $js_filename = basename($latest_file); $js_version = AICOAGAC_VERSION . '-' . $latest_time; $script_handle = 'aicoagac-app-' . md5($js_filename . $latest_time); // Enqueue the compiled React app (CSS is inlined in JS) wp_enqueue_script($script_handle, AICOAGAC_PLUGIN_URL . 'admin/assets/' . $js_filename, array(), $js_version, true); // Pass data to React app wp_localize_script($script_handle, 'aicoagacData', array( 'nonce' => wp_create_nonce('wp_rest'), 'api_url' => rest_url('aicoagac/v1/'), 'admin_url' => admin_url(), 'plugin_url' => AICOAGAC_PLUGIN_URL, 'is_pro' => false, // Pro features removed 'i18n' => array( // Pagination and UI strings 'Page' => __('Page', 'ai-content-agent'), 'of' => __('of', 'ai-content-agent'), 'Prev' => __('Prev', 'ai-content-agent'), 'Next' => __('Next', 'ai-content-agent'), // Optional related labels used around pagination/header 'Published Articles' => __('Published Articles', 'ai-content-agent'), 'Your Published Content' => __('Your Published Content', 'ai-content-agent'), 'No Published Posts Yet' => __('No Published Posts Yet', 'ai-content-agent'), 'Go to Drafts' => __('Go to Drafts', 'ai-content-agent'), 'View Live Post' => __('View Live Post', 'ai-content-agent'), 'Published Posts' => __('Published Posts', 'ai-content-agent'), 'Your successful content publications' => __('Your successful content publications', 'ai-content-agent'), ), )); } } else { // No assets found (should not happen in distributed build) return; } } } // Initialize the plugin (singleton) - delayed to ensure WordPress is ready add_action('plugins_loaded', function() { global $aicoagac_content_agent; $aicoagac_content_agent = AICOAGAC_Content_Agent::get_instance(); }, 1); // Legacy cron hooks removed - using unified automation system only // Admin initialization is handled in the main init method to avoid redundancy // Admin notices - only show on admin pages, not during REST API requests // Post view count tracking (optimized to reduce database load) // Removed aicoagac_track_post_views and its hook to avoid default tracking for privacy // Admin initialization functions moved to main init method to avoid redundancy function aicoagac_check_database_updates() { // Only run for admins and not during plugin activation or AJAX if (!is_admin() || !current_user_can('activate_plugins') || wp_doing_ajax() || defined('DOING_AUTOSAVE')) { return; } // Don't run on every admin page load - use transient to limit checks if (get_transient('aicoagac_migration_check_done')) { return; } // Include migration manager $migration_file = AICOAGAC_PLUGIN_PATH . 'includes/class-aicoagac-migration-manager.php'; if (file_exists($migration_file)) { require_once $migration_file; $migration_manager = new AICOAGAC_Migration_Manager(); $result = $migration_manager->run_migrations(); if (is_wp_error($result)) { add_action('admin_notices', function() use ($result) { // Don't show notices during REST API requests or AJAX calls if (defined('REST_REQUEST') && REST_REQUEST) { return; } if (defined('DOING_AJAX') && DOING_AJAX) { return; } echo '' . esc_html__( 'ACA Database Update Failed:', 'ai-content-agent' ) . ' ' . esc_html($result->get_error_message()) . '