| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | if (!defined('ABSPATH')) { |
| 4 | 4 | exit; |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | +require_once __DIR__ . '/privacy.php'; | |
| 8 | + | |
| 7 | 9 | /** |
| 8 | 10 | * WordPress-Native Sentry Integration |
| 9 | 11 | * |
| 10 | 12 | * Uses WordPress HTTP API to send data directly to Sentry |
| @@ -41,14 +43,14 @@ | ||
| 41 | 43 | $this->host = null; // Proxy handles this |
| 42 | 44 | $this->scheme = 'proxy'; |
| 43 | 45 | } else { |
| 44 | 46 | // Legacy DSN format for backward compatibility |
| 45 | - $parsed = parse_url($this->dsn); | |
| 46 | - $this->public_key = isset($parsed['user']) ? $parsed['user'] : null; | |
| 47 | - $this->secret_key = isset($parsed['pass']) ? $parsed['pass'] : null; | |
| 48 | - $this->project_id = trim($parsed['path'], '/'); | |
| 49 | - $this->host = isset($parsed['host']) ? $parsed['host'] : null; | |
| 50 | - $this->scheme = isset($parsed['scheme']) ? $parsed['scheme'] : 'https'; | |
| 47 | + $parsed = wp_parse_url($this->dsn); | |
| 48 | + $this->public_key = is_array($parsed) && isset($parsed['user']) ? $parsed['user'] : null; | |
| 49 | + $this->secret_key = is_array($parsed) && isset($parsed['pass']) ? $parsed['pass'] : null; | |
| 50 | + $this->project_id = is_array($parsed) && isset($parsed['path']) ? trim($parsed['path'], '/') : null; | |
| 51 | + $this->host = is_array($parsed) && isset($parsed['host']) ? $parsed['host'] : null; | |
| 52 | + $this->scheme = is_array($parsed) && isset($parsed['scheme']) ? $parsed['scheme'] : 'https'; | |
| 51 | 53 | } |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | /** |
| @@ -95,8 +97,12 @@ | ||
| 95 | 97 | * @param array $extra Additional context. |
| 96 | 98 | * @return bool True if a request was dispatched, false if it could not be. |
| 97 | 99 | */ |
| 98 | 100 | public function captureMessageNonBlocking($message, $level = 'info', $extra = []) { |
| 101 | + if (metasync_telemetry_is_disabled()) { | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + | |
| 99 | 105 | # Mirrors sendToSentry()'s preconditions. |
| 100 | 106 | if ($this->isLocalhost()) { |
| 101 | 107 | return false; |
| 102 | 108 | } |
| @@ -387,9 +393,9 @@ | ||
| 387 | 393 | /** |
| 388 | 394 | * Check if the current environment is localhost/development |
| 389 | 395 | */ |
| 390 | 396 | private function isLocalhost() { |
| 391 | - $host = parse_url(home_url(), PHP_URL_HOST); | |
| 397 | + $host = wp_parse_url(home_url(), PHP_URL_HOST); | |
| 392 | 398 | |
| 393 | 399 | // Check for common localhost patterns |
| 394 | 400 | $localhost_patterns = [ |
| 395 | 401 | 'localhost', |
| @@ -402,9 +408,9 @@ | ||
| 402 | 408 | '.localhost' |
| 403 | 409 | ]; |
| 404 | 410 | |
| 405 | 411 | foreach ($localhost_patterns as $pattern) { |
| 406 | - if (strpos($host, $pattern) !== false) { | |
| 412 | + if ($host !== false && $host !== null && strpos($host, $pattern) !== false) { | |
| 407 | 413 | return true; |
| 408 | 414 | } |
| 409 | 415 | } |
| 410 | 416 | |
| @@ -432,8 +438,12 @@ | ||
| 432 | 438 | * @param array|null $attachment Optional attachment data |
| 433 | 439 | * @return bool|array Success status, or array with success and event_id |
| 434 | 440 | */ |
| 435 | 441 | private function sendToSentry($data, $item_type = 'event', $attachment = null) { |
| 442 | + if (metasync_telemetry_is_disabled()) { | |
| 443 | + return false; | |
| 444 | + } | |
| 445 | + | |
| 436 | 446 | // Skip sending to Sentry if running on localhost/development environment |
| 437 | 447 | if ($this->isLocalhost()) { |
| 438 | 448 | return false; |
| 439 | 449 | } |
| @@ -469,32 +479,23 @@ | ||
| 469 | 479 | 'X-Plugin-Version' => $plugin_version, |
| 470 | 480 | 'User-Agent' => 'WordPress MetaSync Plugin/' . $plugin_version |
| 471 | 481 | ]; |
| 472 | 482 | |
| 473 | - // Use cURL directly to ensure proper envelope format | |
| 474 | - $ch = curl_init(); | |
| 475 | - curl_setopt($ch, CURLOPT_URL, $url); | |
| 476 | - curl_setopt($ch, CURLOPT_POST, true); | |
| 477 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $envelope); | |
| 478 | - curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function($key, $value) { | |
| 479 | - return $key . ': ' . $value; | |
| 480 | - }, array_keys($headers), $headers)); | |
| 481 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 482 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); | |
| 483 | - curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5 second timeout as requested | |
| 484 | - curl_setopt($ch, CURLOPT_USERAGENT, 'WordPress MetaSync Plugin/' . $plugin_version); | |
| 485 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5 second connection timeout | |
| 486 | - | |
| 487 | - $response = curl_exec($ch); | |
| 488 | - $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
| 489 | - $error = curl_error($ch); | |
| 483 | + $response_data = wp_remote_post($url, [ | |
| 484 | + 'body' => $envelope, | |
| 485 | + 'headers' => $headers, | |
| 486 | + 'timeout' => 5, // 5 second timeout as requested | |
| 487 | + 'sslverify' => true, | |
| 488 | + 'user-agent' => 'WordPress MetaSync Plugin/' . $plugin_version, | |
| 489 | + ]); | |
| 490 | + $error = is_wp_error($response_data) ? $response_data->get_error_message() : ''; | |
| 491 | + $response_code = is_wp_error($response_data) ? 0 : (int) wp_remote_retrieve_response_code($response_data); | |
| 492 | + $response = is_wp_error($response_data) ? '' : wp_remote_retrieve_body($response_data); | |
| 490 | 493 | |
| 491 | - #curl_close($ch); | |
| 492 | - | |
| 493 | 494 | // Log errors in debug mode for troubleshooting |
| 494 | 495 | if (defined('WP_DEBUG') && WP_DEBUG && WP_DEBUG_LOG) { |
| 495 | 496 | if ($error) { |
| 496 | - error_log(sprintf( | |
| 497 | + error_log(sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets | |
| 497 | 498 | 'MetaSync Sentry Error (%s): %s | Response Code: %s | Item Type: %s', |
| 498 | 499 | $item_type, |
| 499 | 500 | $error, |
| 500 | 501 | $response_code, |
| @@ -500,13 +501,12 @@ | ||
| 500 | 501 | $response_code, |
| 501 | 502 | $item_type |
| 502 | 503 | )); |
| 503 | 504 | } elseif ($response_code < 200 || $response_code >= 300) { |
| 504 | - error_log(sprintf( | |
| 505 | - 'MetaSync Sentry HTTP Error (%s): Response Code: %s | Response: %s | Item Type: %s', | |
| 505 | + error_log(sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets | |
| 506 | + 'MetaSync Sentry HTTP Error (%s): Response Code: %s | Item Type: %s', | |
| 506 | 507 | $item_type, |
| 507 | 508 | $response_code, |
| 508 | - substr($response, 0, 200), | |
| 509 | 509 | $item_type |
| 510 | 510 | )); |
| 511 | 511 | } |
| 512 | 512 | } |
| @@ -710,9 +710,9 @@ | ||
| 710 | 710 | if ($cached_context !== false) { |
| 711 | 711 | // Add dynamic data that changes per request |
| 712 | 712 | $cached_context['memory_usage'] = memory_get_usage(true); |
| 713 | 713 | $cached_context['memory_peak'] = memory_get_peak_usage(true); |
| 714 | - $cached_context['request_uri'] = $_SERVER['REQUEST_URI'] ?? 'unknown'; | |
| 714 | + $cached_context['request_uri'] = metasync_telemetry_request_path(); | |
| 715 | 715 | return $cached_context; |
| 716 | 716 | } |
| 717 | 717 | |
| 718 | 718 | // Collect static system context (expensive operations) |
| @@ -729,14 +729,14 @@ | ||
| 729 | 729 | 'max_execution_time' => ini_get('max_execution_time'), |
| 730 | 730 | 'active_plugins' => count(get_option('active_plugins', [])), |
| 731 | 731 | 'active_theme' => get_template(), |
| 732 | 732 | 'multisite' => is_multisite(), |
| 733 | - 'mysql_version' => method_exists($wpdb, 'get_var') ? $wpdb->get_var('SELECT VERSION()') : 'unknown', | |
| 733 | + 'mysql_version' => method_exists($wpdb, 'get_var') ? $wpdb->get_var('SELECT VERSION()') : 'unknown', // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- diagnostic metadata — SELECT VERSION() has no cached WordPress API | |
| 734 | 734 | 'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'unknown', |
| 735 | 735 | // Dynamic data added per request |
| 736 | 736 | 'memory_usage' => memory_get_usage(true), |
| 737 | 737 | 'memory_peak' => memory_get_peak_usage(true), |
| 738 | - 'request_uri' => $_SERVER['REQUEST_URI'] ?? 'unknown' | |
| 738 | + 'request_uri' => metasync_telemetry_request_path() | |
| 739 | 739 | ]; |
| 740 | 740 | |
| 741 | 741 | // Cache static context for 1 hour |
| 742 | 742 | wp_cache_set($cache_key, $context, 'metasync', HOUR_IN_SECONDS); |
| @@ -812,10 +812,10 @@ | ||
| 812 | 812 | if ($this->isLocalhost()) { |
| 813 | 813 | return 'development'; |
| 814 | 814 | } |
| 815 | 815 | |
| 816 | - $host = parse_url(home_url(), PHP_URL_HOST); | |
| 817 | - if (strpos($host, 'staging') !== false || strpos($host, 'dev') !== false) { | |
| 816 | + $host = wp_parse_url(home_url(), PHP_URL_HOST); | |
| 817 | + if ($host !== false && $host !== null && (strpos($host, 'staging') !== false || strpos($host, 'dev') !== false)) { | |
| 818 | 818 | return 'staging'; |
| 819 | 819 | } |
| 820 | 820 | |
| 821 | 821 | return 'production'; |
| @@ -874,8 +874,12 @@ | ||
| 874 | 874 | * Initialize Sentry with DSN configuration |
| 875 | 875 | */ |
| 876 | 876 | function init_metasync_sentry_wordpress() { |
| 877 | 877 | global $metasync_sentry_wordpress; |
| 878 | + | |
| 879 | + if (metasync_telemetry_is_disabled()) { | |
| 880 | + return null; | |
| 881 | + } | |
| 878 | 882 | |
| 879 | 883 | $dsn = ''; |
| 880 | 884 | |
| 881 | 885 | // Use constants defined in metasync.php for configuration |
| @@ -905,8 +909,11 @@ | ||
| 905 | 909 | * Helper function to capture exceptions |
| 906 | 910 | */ |
| 907 | 911 | function metasync_sentry_capture_exception($exception, $extra = []) { |
| 908 | 912 | global $metasync_sentry_wordpress; |
| 913 | + if (metasync_telemetry_is_disabled()) { | |
| 914 | + return false; | |
| 915 | + } | |
| 909 | 916 | if (!$metasync_sentry_wordpress) { |
| 910 | 917 | $metasync_sentry_wordpress = init_metasync_sentry_wordpress(); |
| 911 | 918 | } |
| 912 | 919 | |
| @@ -920,8 +927,11 @@ | ||
| 920 | 927 | * Helper function to capture messages |
| 921 | 928 | */ |
| 922 | 929 | function metasync_sentry_capture_message($message, $level = 'info', $extra = [], $attachment = null) { |
| 923 | 930 | global $metasync_sentry_wordpress; |
| 931 | + if (metasync_telemetry_is_disabled()) { | |
| 932 | + return false; | |
| 933 | + } | |
| 924 | 934 | if (!$metasync_sentry_wordpress) { |
| 925 | 935 | $metasync_sentry_wordpress = init_metasync_sentry_wordpress(); |
| 926 | 936 | } |
| 927 | 937 | |
| @@ -940,8 +950,11 @@ | ||
| 940 | 950 | * MetaSync_Sentry_WordPress::captureMessageNonBlocking() for the trade-off. |
| 941 | 951 | */ |
| 942 | 952 | function metasync_sentry_capture_message_nonblocking($message, $level = 'info', $extra = []) { |
| 943 | 953 | global $metasync_sentry_wordpress; |
| 954 | + if (metasync_telemetry_is_disabled()) { | |
| 955 | + return false; | |
| 956 | + } | |
| 944 | 957 | if (!$metasync_sentry_wordpress) { |
| 945 | 958 | $metasync_sentry_wordpress = init_metasync_sentry_wordpress(); |
| 946 | 959 | } |
| 947 | 960 | |
| @@ -958,8 +971,11 @@ | ||
| 958 | 971 | * @return bool Success status |
| 959 | 972 | */ |
| 960 | 973 | function metasync_sentry_capture_feedback($feedback, $attachment = null) { |
| 961 | 974 | global $metasync_sentry_wordpress; |
| 975 | + if (metasync_telemetry_is_disabled()) { | |
| 976 | + return false; | |
| 977 | + } | |
| 962 | 978 | if (!$metasync_sentry_wordpress) { |
| 963 | 979 | $metasync_sentry_wordpress = init_metasync_sentry_wordpress(); |
| 964 | 980 | } |
| 965 | 981 | |