($memory_limit * 0.8)) { // NEW: Structured error logging with category and code if (class_exists('Metasync_Error_Logger')) { $memory_used_mb = round($memory_after_options / 1024 / 1024, 2); $memory_limit_mb = round($memory_limit / 1024 / 1024, 2); $memory_percent = round(($memory_after_options / $memory_limit) * 100, 1); Metasync_Error_Logger::log( Metasync_Error_Logger::CATEGORY_MEMORY_EXHAUSTED, Metasync_Error_Logger::SEVERITY_CRITICAL, 'Memory limit nearly exhausted - telemetry initialization skipped', [ 'memory_used_mb' => $memory_used_mb, 'memory_limit_mb' => $memory_limit_mb, 'memory_percent' => $memory_percent, 'threshold' => '80%', 'operation' => 'telemetry_init', 'action' => 'skipped_initialization' ] ); } // error_log('MetaSync Telemetry: Skipping manager initialization due to memory usage'); return; } Metasync_Telemetry_Manager::get_instance(); // API request monitoring removed - creates excessive overhead // Class kept for backward compatibility but not instantiated // Already handled by log-sync.php } catch (Exception $e) { // Fail silently to prevent site crashes // error_log('MetaSync Telemetry: Initialization failed: ' . $e->getMessage()); } } // Only initialize telemetry if not explicitly disabled if (!defined('METASYNC_DISABLE_TELEMETRY') || !constant('METASYNC_DISABLE_TELEMETRY')) { // Add hook - memory check is now handled inside the function for efficiency add_action('plugins_loaded', 'init_metasync_telemetry', 5); } /** * Helper function to get telemetry manager instance * * @return Metasync_Telemetry_Manager */ function metasync_telemetry() { return Metasync_Telemetry_Manager::get_instance(); } /** * Cleanup scheduled cron jobs from old telemetry system */ function cleanup_metasync_legacy_cron_jobs() { // Remove old cron jobs $timestamp = wp_next_scheduled('metasync_process_background_telemetry'); if ($timestamp) { wp_unschedule_event($timestamp, 'metasync_process_background_telemetry'); } $timestamp = wp_next_scheduled('metasync_process_file_queue'); if ($timestamp) { wp_unschedule_event($timestamp, 'metasync_process_file_queue'); } $timestamp = wp_next_scheduled('metasync_telemetry_queue_flush'); if ($timestamp) { wp_unschedule_event($timestamp, 'metasync_telemetry_queue_flush'); } } add_action('init', 'cleanup_metasync_legacy_cron_jobs', 1);