| 1 |
<?php |
| 2 |
/** |
| 3 |
* Telemetry Configuration |
| 4 |
* |
| 5 |
* Configuration constants and settings for the telemetry system |
| 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 |
/** |
| 19 |
* Telemetry Configuration Class |
| 20 |
*/ |
| 21 |
class Metasync_Telemetry_Config { |
| 22 |
|
| 23 |
/** |
| 24 |
* Default API endpoint base URL |
| 25 |
*/ |
| 26 |
const DEFAULT_API_BASE = 'https://dash-api-telemetry.searchatlas.com'; |
| 27 |
|
| 28 |
/** |
| 29 |
* API endpoint path |
| 30 |
*/ |
| 31 |
const API_ENDPOINT_PATH = '/collect'; |
| 32 |
|
| 33 |
/** |
| 34 |
* JWT token expiration time (1 hour) |
| 35 |
*/ |
| 36 |
const JWT_EXPIRATION_TIME = 3600; |
| 37 |
|
| 38 |
/** |
| 39 |
* Maximum retry attempts for failed requests |
| 40 |
*/ |
| 41 |
const MAX_RETRY_ATTEMPTS = 3; |
| 42 |
|
| 43 |
/** |
| 44 |
* Maximum queue size before auto-flush |
| 45 |
* Increased from 10 to 25 to reduce database load |
| 46 |
*/ |
| 47 |
const MAX_QUEUE_SIZE = 25; |
| 48 |
|
| 49 |
/** |
| 50 |
* Batch size for queue processing |
| 51 |
* Increased from 5 to 15 to reduce database load |
| 52 |
*/ |
| 53 |
const QUEUE_BATCH_SIZE = 15; |
| 54 |
|
| 55 |
/** |
| 56 |
* API request timeout (seconds) |
| 57 |
*/ |
| 58 |
const REQUEST_TIMEOUT = 30; |
| 59 |
|
| 60 |
/** |
| 61 |
* Maximum query execution time to consider "slow" (seconds) |
| 62 |
*/ |
| 63 |
const SLOW_QUERY_THRESHOLD = 1.0; |
| 64 |
|
| 65 |
/** |
| 66 |
* Maximum number of slow queries to log per request |
| 67 |
*/ |
| 68 |
const MAX_SLOW_QUERIES_LOGGED = 5; |
| 69 |
|
| 70 |
/** |
| 71 |
* Maximum SQL query length for logging (characters) |
| 72 |
*/ |
| 73 |
const MAX_QUERY_LOG_LENGTH = 500; |
| 74 |
|
| 75 |
/** |
| 76 |
* Queue flush interval (cron) |
| 77 |
* Changed from hourly to every 2 hours to reduce database load |
| 78 |
*/ |
| 79 |
const QUEUE_FLUSH_INTERVAL = 'metasync_every_2_hours'; |
| 80 |
|
| 81 |
/** |
| 82 |
* JWT secret key option name |
| 83 |
*/ |
| 84 |
const JWT_SECRET_OPTION_NAME = 'metasync_telemetry_jwt_secret'; |
| 85 |
|
| 86 |
/** |
| 87 |
* Telemetry settings option name |
| 88 |
*/ |
| 89 |
const TELEMETRY_SETTINGS_OPTION_NAME = 'metasync_telemetry_settings'; |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the full API endpoint URL for a site |
| 93 |
* |
| 94 |
* @param string $site_url Optional site URL, uses current site if not provided |
| 95 |
* @return string Full API endpoint URL |
| 96 |
*/ |
| 97 |
public static function get_api_endpoint($site_url = '') { |
| 98 |
if (empty($site_url)) { |
| 99 |
$site_url = preg_replace('#^https?://#', '', home_url()); |
| 100 |
} |
| 101 |
|
| 102 |
return self::DEFAULT_API_BASE . self::API_ENDPOINT_PATH . '/' . $site_url; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get telemetry configuration settings |
| 107 |
* |
| 108 |
* @return array Configuration settings |
| 109 |
*/ |
| 110 |
public static function get_settings() { |
| 111 |
$default_settings = array( |
| 112 |
'enabled' => true, |
| 113 |
'api_endpoint' => self::get_api_endpoint(), |
| 114 |
'jwt_expiration' => self::JWT_EXPIRATION_TIME, |
| 115 |
'max_retries' => self::MAX_RETRY_ATTEMPTS, |
| 116 |
'max_queue_size' => self::MAX_QUEUE_SIZE, |
| 117 |
'request_timeout' => self::REQUEST_TIMEOUT, |
| 118 |
'slow_query_threshold' => self::SLOW_QUERY_THRESHOLD, |
| 119 |
'debug_mode' => defined('WP_DEBUG') && WP_DEBUG, |
| 120 |
'environment' => self::detect_environment() |
| 121 |
); |
| 122 |
|
| 123 |
$saved_settings = get_option(self::TELEMETRY_SETTINGS_OPTION_NAME, array()); |
| 124 |
|
| 125 |
return array_merge($default_settings, $saved_settings); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Update telemetry settings |
| 130 |
* |
| 131 |
* @param array $settings New settings |
| 132 |
* @return bool Success status |
| 133 |
*/ |
| 134 |
public static function update_settings($settings) { |
| 135 |
$current_settings = self::get_settings(); |
| 136 |
$updated_settings = array_merge($current_settings, $settings); |
| 137 |
|
| 138 |
return update_option(self::TELEMETRY_SETTINGS_OPTION_NAME, $updated_settings); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if telemetry is enabled |
| 143 |
* |
| 144 |
* @return bool True if enabled |
| 145 |
*/ |
| 146 |
public static function is_enabled() { |
| 147 |
// Check for global disable constant |
| 148 |
if (defined('METASYNC_DISABLE_TELEMETRY') && constant('METASYNC_DISABLE_TELEMETRY')) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// Check for local development mode |
| 153 |
if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_LOCAL_DEV') && constant('WP_LOCAL_DEV')) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
// Check plugin settings |
| 158 |
$main_options = get_option('metasync_options', array()); |
| 159 |
if (isset($main_options['disable_telemetry']) && $main_options['disable_telemetry']) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
// Check telemetry-specific settings |
| 164 |
$settings = self::get_settings(); |
| 165 |
return $settings['enabled']; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Detect current environment |
| 170 |
* |
| 171 |
* @return string Environment name |
| 172 |
*/ |
| 173 |
public static function detect_environment() { |
| 174 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 175 |
return 'development'; |
| 176 |
} |
| 177 |
|
| 178 |
$host = parse_url(home_url(), PHP_URL_HOST); |
| 179 |
if (strpos($host, 'staging') !== false || strpos($host, 'dev') !== false) { |
| 180 |
return 'staging'; |
| 181 |
} |
| 182 |
|
| 183 |
return 'production'; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get site hash for identification |
| 188 |
* |
| 189 |
* @return string Site hash |
| 190 |
*/ |
| 191 |
public static function get_site_hash() { |
| 192 |
$site_data = home_url() . get_bloginfo('name'); |
| 193 |
return substr(md5($site_data), 0, 16); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Get plugin information for telemetry |
| 198 |
* |
| 199 |
* @return array Plugin info |
| 200 |
*/ |
| 201 |
public static function get_plugin_info() { |
| 202 |
return array( |
| 203 |
'name' => 'Search Engine Labs SEO', |
| 204 |
'identifier' => 'metasync', |
| 205 |
'version' => defined('METASYNC_VERSION') ? METASYNC_VERSION : '1.0.0', |
| 206 |
'file' => plugin_basename(__FILE__) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get system context information |
| 212 |
* |
| 213 |
* @return array System context |
| 214 |
*/ |
| 215 |
public static function get_system_context() { |
| 216 |
global $wpdb; |
| 217 |
|
| 218 |
return array( |
| 219 |
'wordpress_version' => get_bloginfo('version'), |
| 220 |
'php_version' => PHP_VERSION, |
| 221 |
'php_os' => PHP_OS_FAMILY, |
| 222 |
'mysql_version' => method_exists($wpdb, 'get_var') ? $wpdb->get_var('SELECT VERSION()') : 'unknown', |
| 223 |
'server_software' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'unknown', |
| 224 |
'active_plugins' => count(get_option('active_plugins', array())), |
| 225 |
'active_theme' => get_template(), |
| 226 |
'multisite' => is_multisite(), |
| 227 |
'memory_limit' => ini_get('memory_limit'), |
| 228 |
'max_execution_time' => ini_get('max_execution_time'), |
| 229 |
'upload_max_filesize' => ini_get('upload_max_filesize'), |
| 230 |
'post_max_size' => ini_get('post_max_size'), |
| 231 |
'max_input_vars' => ini_get('max_input_vars') |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get error level mapping |
| 237 |
* |
| 238 |
* @return array Error level mapping |
| 239 |
*/ |
| 240 |
public static function get_error_level_map() { |
| 241 |
return array( |
| 242 |
E_ERROR => 'error', |
| 243 |
E_WARNING => 'warning', |
| 244 |
E_PARSE => 'error', |
| 245 |
E_NOTICE => 'info', |
| 246 |
E_CORE_ERROR => 'error', |
| 247 |
E_CORE_WARNING => 'warning', |
| 248 |
E_COMPILE_ERROR => 'error', |
| 249 |
E_COMPILE_WARNING => 'warning', |
| 250 |
E_USER_ERROR => 'error', |
| 251 |
E_USER_WARNING => 'warning', |
| 252 |
E_USER_NOTICE => 'info', |
| 253 |
E_STRICT => 'info', |
| 254 |
E_RECOVERABLE_ERROR => 'error', |
| 255 |
E_DEPRECATED => 'warning', |
| 256 |
E_USER_DEPRECATED => 'warning' |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get allowed log levels |
| 262 |
* |
| 263 |
* @return array Allowed log levels |
| 264 |
*/ |
| 265 |
public static function get_allowed_log_levels() { |
| 266 |
return array('debug', 'info', 'warning', 'error'); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Validate log level |
| 271 |
* |
| 272 |
* @param string $level Log level to validate |
| 273 |
* @return string Valid log level |
| 274 |
*/ |
| 275 |
public static function validate_log_level($level) { |
| 276 |
$allowed_levels = self::get_allowed_log_levels(); |
| 277 |
return in_array($level, $allowed_levels) ? $level : 'info'; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Get JWT configuration |
| 282 |
* |
| 283 |
* @return array JWT configuration |
| 284 |
*/ |
| 285 |
public static function get_jwt_config() { |
| 286 |
return array( |
| 287 |
'algorithm' => 'HS256', |
| 288 |
'expiration_time' => self::JWT_EXPIRATION_TIME, |
| 289 |
'issuer' => home_url(), |
| 290 |
'audience' => 'dash-api-telemetry' |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Get request headers for API calls |
| 296 |
* |
| 297 |
* @return array Default headers |
| 298 |
*/ |
| 299 |
public static function get_default_headers() { |
| 300 |
$plugin_info = self::get_plugin_info(); |
| 301 |
|
| 302 |
return array( |
| 303 |
'Content-Type' => 'application/json', |
| 304 |
'User-Agent' => 'MetaSyncTelemetry/' . $plugin_info['version'], |
| 305 |
'X-Plugin-Version' => $plugin_info['version'], |
| 306 |
'X-Site-Hash' => self::get_site_hash(), |
| 307 |
'X-WordPress-Version' => get_bloginfo('version'), |
| 308 |
'X-PHP-Version' => PHP_VERSION |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Sanitize configuration value |
| 314 |
* |
| 315 |
* @param string $key Configuration key |
| 316 |
* @param mixed $value Value to sanitize |
| 317 |
* @return mixed Sanitized value |
| 318 |
*/ |
| 319 |
public static function sanitize_config_value($key, $value) { |
| 320 |
switch ($key) { |
| 321 |
case 'enabled': |
| 322 |
return (bool) $value; |
| 323 |
case 'api_endpoint': |
| 324 |
return esc_url_raw($value); |
| 325 |
case 'jwt_expiration': |
| 326 |
case 'max_retries': |
| 327 |
case 'max_queue_size': |
| 328 |
case 'request_timeout': |
| 329 |
return absint($value); |
| 330 |
case 'slow_query_threshold': |
| 331 |
return floatval($value); |
| 332 |
case 'environment': |
| 333 |
$allowed = array('development', 'staging', 'production'); |
| 334 |
return in_array($value, $allowed) ? $value : 'production'; |
| 335 |
default: |
| 336 |
return sanitize_text_field($value); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Reset telemetry configuration to defaults |
| 342 |
* |
| 343 |
* @return bool Success status |
| 344 |
*/ |
| 345 |
public static function reset_to_defaults() { |
| 346 |
delete_option(self::TELEMETRY_SETTINGS_OPTION_NAME); |
| 347 |
delete_option(self::JWT_SECRET_OPTION_NAME); |
| 348 |
return true; |
| 349 |
} |
| 350 |
} |
| 351 |
|