PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.26
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.26
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.26, at telemetry/telemetry-init.php

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