| @@ -43,14 +43,14 @@ | ||
| 43 | 43 | $this->host = null; // Proxy handles this |
| 44 | 44 | $this->scheme = 'proxy'; |
| 45 | 45 | } else { |
| 46 | 46 | // Legacy DSN format for backward compatibility |
| 47 | - $parsed = parse_url($this->dsn); | |
| 48 | - $this->public_key = isset($parsed['user']) ? $parsed['user'] : null; | |
| 49 | - $this->secret_key = isset($parsed['pass']) ? $parsed['pass'] : null; | |
| 50 | - $this->project_id = trim($parsed['path'], '/'); | |
| 51 | - $this->host = isset($parsed['host']) ? $parsed['host'] : null; | |
| 52 | - $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'; | |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
| @@ -393,9 +393,9 @@ | ||
| 393 | 393 | /** |
| 394 | 394 | * Check if the current environment is localhost/development |
| 395 | 395 | */ |
| 396 | 396 | private function isLocalhost() { |
| 397 | - $host = parse_url(home_url(), PHP_URL_HOST); | |
| 397 | + $host = wp_parse_url(home_url(), PHP_URL_HOST); | |
| 398 | 398 | |
| 399 | 399 | // Check for common localhost patterns |
| 400 | 400 | $localhost_patterns = [ |
| 401 | 401 | 'localhost', |
| @@ -408,9 +408,9 @@ | ||
| 408 | 408 | '.localhost' |
| 409 | 409 | ]; |
| 410 | 410 | |
| 411 | 411 | foreach ($localhost_patterns as $pattern) { |
| 412 | - if (strpos($host, $pattern) !== false) { | |
| 412 | + if ($host !== false && $host !== null && strpos($host, $pattern) !== false) { | |
| 413 | 413 | return true; |
| 414 | 414 | } |
| 415 | 415 | } |
| 416 | 416 | |
| @@ -479,32 +479,23 @@ | ||
| 479 | 479 | 'X-Plugin-Version' => $plugin_version, |
| 480 | 480 | 'User-Agent' => 'WordPress MetaSync Plugin/' . $plugin_version |
| 481 | 481 | ]; |
| 482 | 482 | |
| 483 | - // Use cURL directly to ensure proper envelope format | |
| 484 | - $ch = curl_init(); | |
| 485 | - curl_setopt($ch, CURLOPT_URL, $url); | |
| 486 | - curl_setopt($ch, CURLOPT_POST, true); | |
| 487 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $envelope); | |
| 488 | - curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(function($key, $value) { | |
| 489 | - return $key . ': ' . $value; | |
| 490 | - }, array_keys($headers), $headers)); | |
| 491 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 492 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); | |
| 493 | - curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5 second timeout as requested | |
| 494 | - curl_setopt($ch, CURLOPT_USERAGENT, 'WordPress MetaSync Plugin/' . $plugin_version); | |
| 495 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5 second connection timeout | |
| 496 | - | |
| 497 | - $response = curl_exec($ch); | |
| 498 | - $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
| 499 | - $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); | |
| 500 | 493 | |
| 501 | - #curl_close($ch); | |
| 502 | - | |
| 503 | 494 | // Log errors in debug mode for troubleshooting |
| 504 | 495 | if (defined('WP_DEBUG') && WP_DEBUG && WP_DEBUG_LOG) { |
| 505 | 496 | if ($error) { |
| 506 | - error_log(sprintf( | |
| 497 | + error_log(sprintf( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets | |
| 507 | 498 | 'MetaSync Sentry Error (%s): %s | Response Code: %s | Item Type: %s', |
| 508 | 499 | $item_type, |
| 509 | 500 | $error, |
| 510 | 501 | $response_code, |
| @@ -510,13 +501,12 @@ | ||
| 510 | 501 | $response_code, |
| 511 | 502 | $item_type |
| 512 | 503 | )); |
| 513 | 504 | } elseif ($response_code < 200 || $response_code >= 300) { |
| 514 | - error_log(sprintf( | |
| 515 | - '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', | |
| 516 | 507 | $item_type, |
| 517 | 508 | $response_code, |
| 518 | - substr($response, 0, 200), | |
| 519 | 509 | $item_type |
| 520 | 510 | )); |
| 521 | 511 | } |
| 522 | 512 | } |
| @@ -739,9 +729,9 @@ | ||
| 739 | 729 | 'max_execution_time' => ini_get('max_execution_time'), |
| 740 | 730 | 'active_plugins' => count(get_option('active_plugins', [])), |
| 741 | 731 | 'active_theme' => get_template(), |
| 742 | 732 | 'multisite' => is_multisite(), |
| 743 | - '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 | |
| 744 | 734 | 'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'unknown', |
| 745 | 735 | // Dynamic data added per request |
| 746 | 736 | 'memory_usage' => memory_get_usage(true), |
| 747 | 737 | 'memory_peak' => memory_get_peak_usage(true), |
| @@ -822,10 +812,10 @@ | ||
| 822 | 812 | if ($this->isLocalhost()) { |
| 823 | 813 | return 'development'; |
| 824 | 814 | } |
| 825 | 815 | |
| 826 | - $host = parse_url(home_url(), PHP_URL_HOST); | |
| 827 | - 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)) { | |
| 828 | 818 | return 'staging'; |
| 829 | 819 | } |
| 830 | 820 | |
| 831 | 821 | return 'production'; |