PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.21
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.21
2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 2.6.4 2.6.3 2.5.23 All 138 releases
metasync / telemetry / telemetry-init.php

telemetry-init.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.6.21, at telemetry/telemetry-init.php

186 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telemetry System Initialization
4 *
5 * Initializes the telemetry system and hooks into WordPress plugin lifecycle
6 * Compatible with PHP 7.1+
7 *
8 * @package Search Engine Labs SEO
9 * @subpackage Telemetry
10 * @since 1.0.0
11 */
12
13 // If this file is called directly, abort.
14 if (!defined('WPINC')) {
15 die;
16 }
17
18 // Class files are autoloaded by Composer. Procedural/side-effect files stay explicit.
19 require_once plugin_dir_path(__FILE__) . 'sentry-wordpress-integration.php';
20 require_once plugin_dir_path(__FILE__) . 'wordpress-error-handler.php';
21 require_once plugin_dir_path(__FILE__) . 'sentry-helper.php';
22
23
24 /**
25 * Initialize telemetry system
26 */
27 function init_metasync_telemetry() {
28 // Optimized memory check with caching
29 static $memory_check_done = false;
30 static $memory_safe = true;
31
32 // Only check memory once per request to reduce overhead
33 if (!$memory_check_done) {
34 $current_memory = memory_get_usage(true);
35 $memory_limit_str = ini_get('memory_limit');
36
37 // Parse memory limit manually since wp_convert_hr_to_bytes might not be available
38 if ($memory_limit_str === '-1' || $memory_limit_str === -1) {
39 $memory_limit = PHP_INT_MAX; // Unlimited
40 } else {
41 $memory_limit = trim($memory_limit_str);
42 $last = strtolower($memory_limit[strlen($memory_limit) - 1]);
43 $value = (int) $memory_limit;
44
45 switch ($last) {
46 case 'g':
47 $value *= 1024;
48 case 'm':
49 $value *= 1024;
50 case 'k':
51 $value *= 1024;
52 }
53 $memory_limit = $value;
54 }
55
56 // If we're using more than 70% of memory, skip telemetry entirely
57 $memory_safe = $current_memory <= ($memory_limit * 0.7);
58 $memory_check_done = true;
59
60 if (!$memory_safe) {
61 // error_log('MetaSync Telemetry: Disabled due to high memory usage (' . round($current_memory / 1024 / 1024, 2) . 'MB / ' . round($memory_limit / 1024 / 1024, 2) . 'MB)');
62 return;
63 }
64 } else if (!$memory_safe) {
65 return; // Skip if memory was already determined to be unsafe
66 }
67
68 // Check if telemetry is explicitly disabled
69 if (defined('METASYNC_DISABLE_TELEMETRY') && constant('METASYNC_DISABLE_TELEMETRY')) {
70 return;
71 }
72
73 try {
74 // Clean up old database options if they exist
75 delete_option('metasync_sentry_dsn');
76 delete_option('metasync_sentry_project_id');
77 delete_option('metasync_sentry_environment');
78 delete_option('metasync_sentry_release');
79 delete_option('metasync_sentry_sample_rate');
80
81 // Telemetry configuration is now handled by constants defined in metasync.php
82 // METASYNC_SENTRY_PROJECT_ID, METASYNC_SENTRY_ENVIRONMENT, etc.
83
84 // Only initialize if memory is still safe
85 $memory_after_options = memory_get_usage(true);
86 if ($memory_after_options > ($memory_limit * 0.8)) {
87 // NEW: Structured error logging with category and code
88 if (class_exists('Metasync_Error_Logger')) {
89 $memory_used_mb = round($memory_after_options / 1024 / 1024, 2);
90 $memory_limit_mb = round($memory_limit / 1024 / 1024, 2);
91 $memory_percent = round(($memory_after_options / $memory_limit) * 100, 1);
92
93 Metasync_Error_Logger::log(
94 Metasync_Error_Logger::CATEGORY_MEMORY_EXHAUSTED,
95 Metasync_Error_Logger::SEVERITY_CRITICAL,
96 'Memory limit nearly exhausted - telemetry initialization skipped',
97 [
98 'memory_used_mb' => $memory_used_mb,
99 'memory_limit_mb' => $memory_limit_mb,
100 'memory_percent' => $memory_percent,
101 'threshold' => '80%',
102 'operation' => 'telemetry_init',
103 'action' => 'skipped_initialization'
104 ]
105 );
106 }
107
108 // error_log('MetaSync Telemetry: Skipping manager initialization due to memory usage');
109 return;
110 }
111
112 // Defensive load: this class is normally provided by the Composer
113 // autoloader, but on some installs that can miss it at runtime (stale
114 // OPcache after an update, or an autoloader collision with another
115 // plugin). Load it explicitly so telemetry never fatals the page.
116 if (!class_exists('Metasync_Telemetry_Manager')) {
117 $manager_file = plugin_dir_path(__FILE__) . 'class-telemetry-manager.php';
118 if (is_readable($manager_file)) {
119 require_once $manager_file;
120 }
121 }
122
123 if (class_exists('Metasync_Telemetry_Manager')) {
124 Metasync_Telemetry_Manager::get_instance();
125 }
126
127 // API request monitoring removed - creates excessive overhead
128 // Class kept for backward compatibility but not instantiated
129 // Already handled by log-sync.php
130
131 } catch (\Throwable $e) {
132 // Fail silently to prevent site crashes. Catch \Throwable (not just
133 // Exception) so PHP 7+ Errors — e.g. a "class not found" from a missed
134 // autoload — are swallowed here instead of fataling the request.
135 // error_log('MetaSync Telemetry: Initialization failed: ' . $e->getMessage());
136 }
137 }
138
139 // Only initialize telemetry if not explicitly disabled
140 if (!defined('METASYNC_DISABLE_TELEMETRY') || !constant('METASYNC_DISABLE_TELEMETRY')) {
141 // Add hook - memory check is now handled inside the function for efficiency
142 add_action('plugins_loaded', 'init_metasync_telemetry', 5);
143 }
144
145 /**
146 * Helper function to get telemetry manager instance
147 *
148 * @return Metasync_Telemetry_Manager|null Null when the class cannot be loaded.
149 */
150 function metasync_telemetry() {
151 if (!class_exists('Metasync_Telemetry_Manager')) {
152 $manager_file = plugin_dir_path(__FILE__) . 'class-telemetry-manager.php';
153 if (is_readable($manager_file)) {
154 require_once $manager_file;
155 }
156 }
157
158 if (!class_exists('Metasync_Telemetry_Manager')) {
159 return null;
160 }
161
162 return Metasync_Telemetry_Manager::get_instance();
163 }
164
165 /**
166 * Cleanup scheduled cron jobs from old telemetry system
167 */
168 function cleanup_metasync_legacy_cron_jobs() {
169 // Remove old cron jobs
170 $timestamp = wp_next_scheduled('metasync_process_background_telemetry');
171 if ($timestamp) {
172 wp_unschedule_event($timestamp, 'metasync_process_background_telemetry');
173 }
174
175 $timestamp = wp_next_scheduled('metasync_process_file_queue');
176 if ($timestamp) {
177 wp_unschedule_event($timestamp, 'metasync_process_file_queue');
178 }
179
180 $timestamp = wp_next_scheduled('metasync_telemetry_queue_flush');
181 if ($timestamp) {
182 wp_unschedule_event($timestamp, 'metasync_telemetry_queue_flush');
183 }
184 }
185 add_action('init', 'cleanup_metasync_legacy_cron_jobs', 1);
186