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

160 lines 5.8 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 Metasync_Telemetry_Manager::get_instance();
113
114 // API request monitoring removed - creates excessive overhead
115 // Class kept for backward compatibility but not instantiated
116 // Already handled by log-sync.php
117
118 } catch (Exception $e) {
119 // Fail silently to prevent site crashes
120 // error_log('MetaSync Telemetry: Initialization failed: ' . $e->getMessage());
121 }
122 }
123
124 // Only initialize telemetry if not explicitly disabled
125 if (!defined('METASYNC_DISABLE_TELEMETRY') || !constant('METASYNC_DISABLE_TELEMETRY')) {
126 // Add hook - memory check is now handled inside the function for efficiency
127 add_action('plugins_loaded', 'init_metasync_telemetry', 5);
128 }
129
130 /**
131 * Helper function to get telemetry manager instance
132 *
133 * @return Metasync_Telemetry_Manager
134 */
135 function metasync_telemetry() {
136 return Metasync_Telemetry_Manager::get_instance();
137 }
138
139 /**
140 * Cleanup scheduled cron jobs from old telemetry system
141 */
142 function cleanup_metasync_legacy_cron_jobs() {
143 // Remove old cron jobs
144 $timestamp = wp_next_scheduled('metasync_process_background_telemetry');
145 if ($timestamp) {
146 wp_unschedule_event($timestamp, 'metasync_process_background_telemetry');
147 }
148
149 $timestamp = wp_next_scheduled('metasync_process_file_queue');
150 if ($timestamp) {
151 wp_unschedule_event($timestamp, 'metasync_process_file_queue');
152 }
153
154 $timestamp = wp_next_scheduled('metasync_telemetry_queue_flush');
155 if ($timestamp) {
156 wp_unschedule_event($timestamp, 'metasync_telemetry_queue_flush');
157 }
158 }
159 add_action('init', 'cleanup_metasync_legacy_cron_jobs', 1);
160