| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sentry Configuration Helper |
| 4 |
* |
| 5 |
* Helper functions to configure and test Sentry integration |
| 6 |
* |
| 7 |
* @package Search Engine Labs SEO |
| 8 |
* @subpackage Telemetry |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// If this file is called directly, abort. |
| 13 |
if (!defined('WPINC')) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Configure Sentry DSN |
| 19 |
* |
| 20 |
* @param string $dsn Your Sentry DSN |
| 21 |
* @return bool Success status |
| 22 |
*/ |
| 23 |
function metasync_set_sentry_dsn($dsn) { |
| 24 |
$options = get_option('metasync_options', array()); |
| 25 |
$options['sentry_dsn'] = sanitize_text_field($dsn); |
| 26 |
return update_option('metasync_options', $options); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Test Sentry connection |
| 31 |
* |
| 32 |
* @return array Test results |
| 33 |
*/ |
| 34 |
function metasync_test_sentry() { |
| 35 |
$telemetry = metasync_telemetry(); |
| 36 |
return $telemetry->test_sentry_connection(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Send test error to Sentry |
| 41 |
* |
| 42 |
* @param string $message Optional test message |
| 43 |
* @return array Results |
| 44 |
*/ |
| 45 |
function metasync_send_test_error($message = 'Test error from MetaSync Plugin') { |
| 46 |
$telemetry = metasync_telemetry(); |
| 47 |
|
| 48 |
try { |
| 49 |
// Create a test exception |
| 50 |
throw new Exception($message . ' - Test Exception at ' . date('Y-m-d H:i:s')); |
| 51 |
} catch (Exception $e) { |
| 52 |
$telemetry->send_exception($e, array( |
| 53 |
'test' => true, |
| 54 |
'source' => 'manual_test', |
| 55 |
'timestamp' => time() |
| 56 |
)); |
| 57 |
|
| 58 |
return array( |
| 59 |
'success' => true, |
| 60 |
'message' => 'Test exception sent to both custom backend and Sentry' |
| 61 |
); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Send test message to Sentry |
| 67 |
* |
| 68 |
* @param string $message Test message |
| 69 |
* @param string $level Log level |
| 70 |
* @return array Results |
| 71 |
*/ |
| 72 |
function metasync_send_test_message($message = 'Test message from MetaSync Plugin', $level = 'warning') { |
| 73 |
$telemetry = metasync_telemetry(); |
| 74 |
|
| 75 |
$telemetry->send_message($message, $level, array( |
| 76 |
'test' => true, |
| 77 |
'source' => 'manual_test', |
| 78 |
'timestamp' => time() |
| 79 |
)); |
| 80 |
|
| 81 |
return array( |
| 82 |
'success' => true, |
| 83 |
'message' => "Test {$level} message sent to both backends" |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get Sentry configuration status |
| 89 |
* |
| 90 |
* @return array Configuration status |
| 91 |
*/ |
| 92 |
function metasync_get_sentry_status() { |
| 93 |
$options = get_option('metasync_options', array()); |
| 94 |
$dsn = isset($options['sentry_dsn']) ? $options['sentry_dsn'] : ''; |
| 95 |
|
| 96 |
// Check if DSN is set via wp-config.php |
| 97 |
$wp_config_dsn = defined('METASYNC_SENTRY_DSN') ? METASYNC_SENTRY_DSN : ''; |
| 98 |
|
| 99 |
$telemetry_stats = metasync_telemetry()->get_telemetry_stats(); |
| 100 |
|
| 101 |
return array( |
| 102 |
'dsn_in_options' => !empty($dsn), |
| 103 |
'dsn_in_wp_config' => !empty($wp_config_dsn), |
| 104 |
'active_dsn' => !empty($wp_config_dsn) ? $wp_config_dsn : $dsn, |
| 105 |
'sentry_enabled' => isset($telemetry_stats['sentry_enabled']) ? $telemetry_stats['sentry_enabled'] : false, |
| 106 |
'telemetry_enabled' => isset($telemetry_stats['enabled']) ? $telemetry_stats['enabled'] : false, |
| 107 |
'environment' => Metasync_Telemetry_Config::detect_environment(), |
| 108 |
'plugin_version' => defined('METASYNC_VERSION') ? METASYNC_VERSION : '1.0.0' |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Display Sentry setup instructions |
| 114 |
*/ |
| 115 |
function metasync_show_sentry_instructions() { |
| 116 |
echo "<div style='background: #f9f9f9; border: 1px solid #ddd; padding: 20px; margin: 20px 0; border-radius: 5px;'>"; |
| 117 |
echo "<h3>🔧 Sentry Integration Setup</h3>"; |
| 118 |
|
| 119 |
$status = metasync_get_sentry_status(); |
| 120 |
|
| 121 |
if (!$status['sentry_enabled']) { |
| 122 |
echo "<div style='background: #fff3cd; border: 1px solid #ffeaa7; padding: 10px; border-radius: 3px; margin-bottom: 15px;'>"; |
| 123 |
echo "<strong>⚠️ Sentry Not Configured</strong><br>"; |
| 124 |
echo "Follow the steps below to connect your Sentry dashboard."; |
| 125 |
echo "</div>"; |
| 126 |
} else { |
| 127 |
echo "<div style='background: #d1edff; border: 1px solid #74b9ff; padding: 10px; border-radius: 3px; margin-bottom: 15px;'>"; |
| 128 |
echo "<strong>� |
| 129 |
Sentry Configured</strong><br>"; |
| 130 |
echo "Active DSN: " . substr($status['active_dsn'], 0, 30) . "..."; |
| 131 |
echo "</div>"; |
| 132 |
} |
| 133 |
|
| 134 |
echo "<h4>Step 1: Get Your Sentry DSN</h4>"; |
| 135 |
echo "<ol>"; |
| 136 |
echo "<li>Go to your <a href='https://sentry.io/projects/' target='_blank'>Sentry Dashboard</a></li>"; |
| 137 |
echo "<li>Select your project (or create a new one)</li>"; |
| 138 |
echo "<li>Go to Settings → Projects → [Your Project] → Client Keys (DSN)</li>"; |
| 139 |
echo "<li>Copy the DSN (looks like: <code>https://[key]@[org].ingest.sentry.io/[project]</code>)</li>"; |
| 140 |
echo "</ol>"; |
| 141 |
|
| 142 |
echo "<h4>Step 2: Configure DSN (Choose One Method)</h4>"; |
| 143 |
echo "<p><strong>Method A: Add to wp-config.php (Recommended)</strong></p>"; |
| 144 |
echo "<pre style='background: #f1f1f1; padding: 10px; border-radius: 3px;'>"; |
| 145 |
echo "// Add this line to your wp-config.php file\n"; |
| 146 |
echo "define('METASYNC_SENTRY_DSN', 'YOUR_SENTRY_DSN_HERE');\n"; |
| 147 |
echo "</pre>"; |
| 148 |
|
| 149 |
echo "<p><strong>Method B: Using PHP function</strong></p>"; |
| 150 |
echo "<pre style='background: #f1f1f1; padding: 10px; border-radius: 3px;'>"; |
| 151 |
echo "// Add this to your theme's functions.php or a custom plugin\n"; |
| 152 |
echo "metasync_set_sentry_dsn('YOUR_SENTRY_DSN_HERE');\n"; |
| 153 |
echo "</pre>"; |
| 154 |
|
| 155 |
echo "<h4>Step 3: Test the Connection</h4>"; |
| 156 |
echo "<p>Run these commands to test your Sentry integration:</p>"; |
| 157 |
echo "<pre style='background: #f1f1f1; padding: 10px; border-radius: 3px;'>"; |
| 158 |
echo "// Test connection\n"; |
| 159 |
echo "\$result = metasync_test_sentry();\n"; |
| 160 |
echo "var_dump(\$result);\n\n"; |
| 161 |
echo "// Send test error\n"; |
| 162 |
echo "\$result = metasync_send_test_error('Test error message');\n"; |
| 163 |
echo "var_dump(\$result);\n\n"; |
| 164 |
echo "// Send test warning\n"; |
| 165 |
echo "\$result = metasync_send_test_message('Test warning message', 'warning');\n"; |
| 166 |
echo "var_dump(\$result);\n"; |
| 167 |
echo "</pre>"; |
| 168 |
|
| 169 |
echo "<h4>Current Status</h4>"; |
| 170 |
echo "<table style='border-collapse: collapse; width: 100%;'>"; |
| 171 |
echo "<tr><td style='border: 1px solid #ddd; padding: 8px;'><strong>Telemetry Enabled:</strong></td><td style='border: 1px solid #ddd; padding: 8px;'>" . ($status['telemetry_enabled'] ? '� |
| 172 |
Yes' : '❌ No') . "</td></tr>"; |
| 173 |
echo "<tr><td style='border: 1px solid #ddd; padding: 8px;'><strong>Sentry Enabled:</strong></td><td style='border: 1px solid #ddd; padding: 8px;'>" . ($status['sentry_enabled'] ? '� |
| 174 |
Yes' : '❌ No') . "</td></tr>"; |
| 175 |
echo "<tr><td style='border: 1px solid #ddd; padding: 8px;'><strong>Environment:</strong></td><td style='border: 1px solid #ddd; padding: 8px;'>" . $status['environment'] . "</td></tr>"; |
| 176 |
echo "<tr><td style='border: 1px solid #ddd; padding: 8px;'><strong>Plugin Version:</strong></td><td style='border: 1px solid #ddd; padding: 8px;'>" . $status['plugin_version'] . "</td></tr>"; |
| 177 |
echo "</table>"; |
| 178 |
|
| 179 |
echo "</div>"; |
| 180 |
} |
| 181 |
|
| 182 |
// Auto-display instructions if called directly via admin |
| 183 |
if (isset($_GET['metasync_sentry_setup'])) { |
| 184 |
add_action('admin_notices', 'metasync_show_sentry_instructions'); |
| 185 |
} |
| 186 |
|