";
echo "Igny8 CRON: Request started
";
error_log("Igny8 CRON: Request started - " . date('Y-m-d H:i:s'));
// Force load Igny8 plugin if not already loaded
echo "Igny8 CRON: Checking if plugin is active
";
if (!function_exists('igny8_get_ai_setting')) {
echo "Igny8 CRON: Plugin not loaded, attempting to load
";
// Try to load the plugin manually - check multiple possible locations
$possible_paths = [
'igny8-ai-seo/igny8.php',
'igny8/igny8.php',
'igny8-ai-seo.php'
];
$plugin_loaded = false;
foreach ($possible_paths as $plugin_file) {
$full_path = WP_PLUGIN_DIR . '/' . $plugin_file;
echo "Igny8 CRON: Checking path: " . $full_path . "
";
if (file_exists($full_path)) {
echo "Igny8 CRON: Plugin file found at: " . $full_path . ", loading
";
include_once $full_path;
$plugin_loaded = true;
break;
}
}
if (!$plugin_loaded) {
echo "Igny8 CRON: Plugin file not found in any expected location
";
echo "Igny8 CRON: WP_PLUGIN_DIR: " . WP_PLUGIN_DIR . "
";
echo "Igny8 CRON: Available plugins: " . implode(', ', array_diff(scandir(WP_PLUGIN_DIR), array('.', '..'))) . "
";
}
// Check again after manual load
if (!function_exists('igny8_get_ai_setting')) {
echo "Igny8 CRON: Plugin still not active after manual load, trying WordPress plugin loading
";
// Try to trigger WordPress plugin loading
if (function_exists('do_action')) {
echo "Igny8 CRON: Triggering plugins_loaded action
";
do_action('plugins_loaded');
}
// Manually load cron handlers if still not found
if (!function_exists('igny8_auto_cluster_cron_handler')) {
echo "Igny8 CRON: Manually loading cron handlers
";
$cron_handlers_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/cron/igny8-cron-handlers.php';
if (file_exists($cron_handlers_path)) {
echo "Igny8 CRON: Loading cron handlers from: " . $cron_handlers_path . "
";
include_once $cron_handlers_path;
} else {
echo "Igny8 CRON: Cron handlers file not found at: " . $cron_handlers_path . "
";
}
}
// Final check
if (!function_exists('igny8_get_ai_setting')) {
echo "Igny8 CRON: Plugin still not active after all attempts
";
error_log("Igny8 CRON: Plugin not active - igny8_get_ai_setting function not found");
http_response_code(500);
die(json_encode(['error' => 'Igny8 plugin not active']));
}
}
}
echo "Igny8 CRON: Plugin is active
";
// Ensure cron handlers are loaded
if (!function_exists('igny8_auto_cluster_cron_handler')) {
echo "Igny8 CRON: Loading cron handlers manually
";
$cron_handlers_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/cron/igny8-cron-handlers.php';
if (file_exists($cron_handlers_path)) {
echo "Igny8 CRON: Loading cron handlers from: " . $cron_handlers_path . "
";
include_once $cron_handlers_path;
} else {
echo "Igny8 CRON: Cron handlers file not found at: " . $cron_handlers_path . "
";
}
}
echo "Igny8 CRON: Cron handlers loaded
";
// Load global helpers for sector options function
if (!function_exists('igny8_get_sector_options')) {
echo "Igny8 CRON: Loading global helpers for sector options
";
$global_helpers_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/admin/global-helpers.php';
if (file_exists($global_helpers_path)) {
echo "Igny8 CRON: Loading global helpers from: " . $global_helpers_path . "
";
include_once $global_helpers_path;
} else {
echo "Igny8 CRON: Global helpers file not found at: " . $global_helpers_path . "
";
}
}
echo "Igny8 CRON: Global helpers loaded
";
// Load AJAX handlers for clustering function
if (!function_exists('igny8_ajax_ai_cluster_keywords')) {
echo "Igny8 CRON: Loading AJAX handlers for clustering
";
$ajax_handlers_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/admin/ajax.php';
if (file_exists($ajax_handlers_path)) {
echo "Igny8 CRON: Loading AJAX handlers from: " . $ajax_handlers_path . "
";
include_once $ajax_handlers_path;
} else {
echo "Igny8 CRON: AJAX handlers file not found at: " . $ajax_handlers_path . "
";
}
}
echo "Igny8 CRON: AJAX handlers loaded
";
// Load model rates configuration to prevent PHP warnings
echo "Igny8 CRON: Loading model rates configuration
";
$model_rates_path = WP_PLUGIN_DIR . '/igny8-ai-seo/ai/model-rates-config.php';
if (file_exists($model_rates_path)) {
echo "Igny8 CRON: Loading model rates from: " . $model_rates_path . "
";
include_once $model_rates_path;
// Verify the global variable is set
if (isset($GLOBALS['IGNY8_MODEL_RATES'])) {
echo "Igny8 CRON: Model rates global variable loaded successfully
";
} else {
echo "Igny8 CRON: WARNING - Model rates global variable not set
";
}
} else {
echo "Igny8 CRON: Model rates file not found at: " . $model_rates_path . "
";
}
echo "Igny8 CRON: Model rates loaded
";
// Load database functions for taxonomy registration
if (!function_exists('igny8_register_taxonomies')) {
echo "Igny8 CRON: Loading database functions for taxonomy registration
";
$db_functions_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/db/db.php';
if (file_exists($db_functions_path)) {
echo "Igny8 CRON: Loading database functions from: " . $db_functions_path . "
";
include_once $db_functions_path;
} else {
echo "Igny8 CRON: Database functions file not found at: " . $db_functions_path . "
";
}
}
echo "Igny8 CRON: Database functions loaded
";
// Load master dispatcher for smart automation
if (!function_exists('igny8_master_dispatcher_run')) {
echo "Igny8 CRON: Loading master dispatcher
";
$master_dispatcher_path = WP_PLUGIN_DIR . '/igny8-ai-seo/core/cron/igny8-cron-master-dispatcher.php';
if (file_exists($master_dispatcher_path)) {
echo "Igny8 CRON: Loading master dispatcher from: " . $master_dispatcher_path . "
";
include_once $master_dispatcher_path;
} else {
echo "Igny8 CRON: Master dispatcher file not found at: " . $master_dispatcher_path . "
";
}
}
echo "Igny8 CRON: Master dispatcher loaded
";
// 🔧 PATCH: Enable full WordPress taxonomy and rewrite support in CRON context
echo "Igny8 CRON: Initializing WordPress rewrite and taxonomy system
";
// Set globals for rewrite + taxonomy system
global $wp_rewrite, $wp_taxonomies;
// ✅ Fix: Initialize rewrite system (prevents "add_rewrite_tag() on null" error)
if (!isset($wp_rewrite)) {
echo "Igny8 CRON: Initializing WP_Rewrite system
";
$wp_rewrite = new WP_Rewrite();
$wp_rewrite->init();
echo "Igny8 CRON: WP_Rewrite system initialized
";
} else {
echo "Igny8 CRON: WP_Rewrite system already initialized
";
}
// ✅ Trigger key WP hooks (needed for taxonomy registration) - but be selective
echo "Igny8 CRON: Triggering WordPress hooks for taxonomy support
";
// Only trigger plugins_loaded (safest for external cron)
if (!did_action('plugins_loaded')) {
do_action('plugins_loaded');
echo "Igny8 CRON: plugins_loaded hook triggered
";
} else {
echo "Igny8 CRON: plugins_loaded hook already executed
";
}
// Skip after_setup_theme and init to avoid widget system issues
echo "Igny8 CRON: Skipping after_setup_theme and init hooks to avoid widget system conflicts
";
// ✅ Load and register Igny8 taxonomies manually
echo "Igny8 CRON: Registering Igny8 taxonomies
";
if (function_exists('igny8_register_taxonomies')) {
// Ensure WordPress global taxonomies array exists
global $wp_taxonomies;
if (!is_array($wp_taxonomies)) {
$wp_taxonomies = [];
echo "Igny8 CRON: Initialized wp_taxonomies global array
";
}
igny8_register_taxonomies();
echo "Igny8 CRON: Igny8 taxonomies registered
";
} else {
echo "Igny8 CRON: WARNING - igny8_register_taxonomies function not found
";
}
// ✅ Verify taxonomy registration
if (!taxonomy_exists('clusters')) {
echo "Igny8 CRON: ❌ Taxonomy 'clusters' not registered
";
error_log('[CRON] ❌ Taxonomy "clusters" not registered');
// Fallback: Try to register clusters taxonomy directly
echo "Igny8 CRON: Attempting direct taxonomy registration as fallback
";
if (function_exists('register_taxonomy')) {
register_taxonomy('clusters', ['post', 'page', 'product'], [
'hierarchical' => true,
'labels' => [
'name' => 'Content Clusters',
'singular_name' => 'Cluster',
],
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
]);
echo "Igny8 CRON: Direct clusters taxonomy registration attempted
";
}
} else {
echo "Igny8 CRON: ✅ Taxonomy 'clusters' registered successfully
";
error_log('[CRON] ✅ Taxonomy "clusters" registered successfully');
}
if (!taxonomy_exists('sectors')) {
echo "Igny8 CRON: ❌ Taxonomy 'sectors' not registered
";
error_log('[CRON] ❌ Taxonomy "sectors" not registered');
// Fallback: Try to register sectors taxonomy directly
if (function_exists('register_taxonomy')) {
register_taxonomy('sectors', ['post', 'page', 'product'], [
'hierarchical' => true,
'labels' => [
'name' => 'Sectors',
'singular_name' => 'Sector',
],
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
]);
echo "Igny8 CRON: Direct sectors taxonomy registration attempted
";
}
} else {
echo "Igny8 CRON: ✅ Taxonomy 'sectors' registered successfully
";
error_log('[CRON] ✅ Taxonomy "sectors" registered successfully');
}
// === STEP 1: Validate Security Key ===
echo "Igny8 CRON: Validating security key
";
$provided_key = isset($_GET['import_key']) ? sanitize_text_field($_GET['import_key']) : '';
$stored_key = get_option('igny8_secure_cron_key');
echo "Igny8 CRON: Provided key: " . substr($provided_key, 0, 8) . "...
";
echo "Igny8 CRON: Stored key: " . substr($stored_key, 0, 8) . "...
";
if (empty($stored_key) || $provided_key !== $stored_key) {
echo "Igny8 CRON: Security key validation failed
";
error_log("Igny8 CRON: Security key validation failed - provided: " . substr($provided_key, 0, 8) . ", stored: " . substr($stored_key, 0, 8));
status_header(403);
echo json_encode(['error' => 'Invalid or missing security key']);
exit;
}
echo "Igny8 CRON: Security key validated
";
// === STEP 2: Capture Action ===
echo "Igny8 CRON: Capturing action parameter
";
$action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';
echo "Igny8 CRON: Action: " . $action . "
";
if (empty($action)) {
echo "Igny8 CRON: Missing action parameter
";
error_log("Igny8 CRON: Missing action parameter");
status_header(400);
echo json_encode(['error' => 'Missing action parameter']);
exit;
}
// === STEP 3: Execute CRON Function ===
echo "Igny8 CRON: Building allowed actions list
";
$allowed_actions = [
'master_scheduler' => 'igny8_master_dispatcher_run',
'auto_cluster' => 'igny8_auto_cluster_cron',
'auto_ideas' => 'igny8_auto_generate_ideas_cron',
'auto_queue' => 'igny8_auto_queue_cron',
'auto_content' => 'igny8_auto_generate_content_cron',
'auto_images' => 'igny8_auto_generate_images_cron',
'auto_publish' => 'igny8_auto_publish_drafts_cron',
'auto_optimizer' => 'igny8_auto_optimizer_cron',
'auto_recalc' => 'igny8_auto_recalc_cron',
'health_check' => 'igny8_health_check_cron',
'test' => 'igny8_test_cron_endpoint',
];
echo "Igny8 CRON: Allowed actions: " . implode(', ', array_keys($allowed_actions)) . "
";
if (!array_key_exists($action, $allowed_actions)) {
echo "Igny8 CRON: Invalid action name: " . $action . "
";
error_log("Igny8 CRON: Invalid action name: " . $action);
status_header(400);
echo json_encode(['error' => 'Invalid action name']);
exit;
}
echo "Igny8 CRON: Action validated: " . $action . "
";
// Execute safely and catch errors
echo "Igny8 CRON: Starting execution
";
try {
// Handle test action specially
if ($action === 'test') {
echo "Igny8 CRON: Executing test action
";
status_header(200);
echo json_encode([
'success' => true,
'message' => 'Igny8 CRON endpoint is working via wp-load.php',
'timestamp' => date('Y-m-d H:i:s'),
'server' => $_SERVER['SERVER_NAME'] ?? 'unknown',
'url_structure' => 'wp-load.php?import_key=[KEY]&import_id=igny8_cron&action=[ACTION]'
]);
exit;
}
echo "Igny8 CRON: Executing action: " . $action . "
";
echo "Igny8 CRON: Hook to execute: " . $allowed_actions[$action] . "
";
// Check if the hook function exists
$hook_function = $allowed_actions[$action];
$handler_function = $hook_function . '_handler';
echo "Igny8 CRON: Checking hook function: " . $hook_function . "
";
echo "Igny8 CRON: Checking handler function: " . $handler_function . "
";
if (!function_exists($hook_function) && !function_exists($handler_function)) {
echo "Igny8 CRON: Neither hook nor handler function found
";
echo "Igny8 CRON: Available functions containing 'igny8_auto_cluster':
";
$functions = get_defined_functions()['user'];
foreach ($functions as $func) {
if (strpos($func, 'igny8_auto_cluster') !== false) {
echo "- " . $func . "
";
}
}
error_log("Igny8 CRON: Hook function not found: " . $hook_function);
status_header(500);
echo json_encode(['error' => 'Hook function not found', 'hook' => $hook_function]);
exit;
}
echo "Igny8 CRON: Hook function exists: " . $allowed_actions[$action] . "
";
// Execute the hook
echo "Igny8 CRON: Calling do_action(" . $allowed_actions[$action] . ")
";
do_action($allowed_actions[$action]);
echo "Igny8 CRON: do_action completed successfully
";
status_header(200);
echo json_encode(['success' => true, 'message' => "CRON action '{$action}' executed successfully."]);
} catch (Throwable $e) {
echo "Igny8 CRON: Exception caught: " . $e->getMessage() . "
";
error_log("Igny8 CRON: Exception caught: " . $e->getMessage() . " in " . $e->getFile() . " line " . $e->getLine());
status_header(500);
echo json_encode(['error' => 'Execution failed', 'details' => $e->getMessage()]);
}
echo "Igny8 CRON: Request completed
";
echo ""; // Close the main div
exit;
}