($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; } // Defensive load: this class is normally provided by the Composer // autoloader, but on some installs that can miss it at runtime (stale // OPcache after an update, or an autoloader collision with another // plugin). Load it explicitly so telemetry never fatals the page. if (!class_exists('Metasync_Telemetry_Manager')) { $manager_file = plugin_dir_path(__FILE__) . 'class-telemetry-manager.php'; if (is_readable($manager_file)) { require_once $manager_file; } } if (class_exists('Metasync_Telemetry_Manager')) { 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 (\Throwable $e) { // Fail silently to prevent site crashes. Catch \Throwable (not just // Exception) so PHP 7+ Errors — e.g. a "class not found" from a missed // autoload — are swallowed here instead of fataling the request. // 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|null Null when the class cannot be loaded. */ function metasync_telemetry() { if (!class_exists('Metasync_Telemetry_Manager')) { $manager_file = plugin_dir_path(__FILE__) . 'class-telemetry-manager.php'; if (is_readable($manager_file)) { require_once $manager_file; } } if (!class_exists('Metasync_Telemetry_Manager')) { return null; } 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);