| @@ -29,13 +29,14 @@ | ||
| 29 | 29 | * @var string $key |
| 30 | 30 | * @var array $options |
| 31 | 31 | */ |
| 32 | 32 | |
| 33 | -$api = new Logtivity_Api(); | |
| 34 | -$apiKey = $api->getApiKey(); | |
| 35 | -$latestResponse = $api->getLatestResponse(); | |
| 36 | -$status = $api->getConnectionStatus(); | |
| 33 | +$api = new Logtivity_Api(); | |
| 34 | +$apiKey = $api->getApiKey(); | |
| 35 | +$connectionStatus = $api->getConnectionStatus(); | |
| 36 | +$debugLogEntries = (new Logtivity_Debug_Log())->recent(); | |
| 37 | 37 | |
| 38 | +// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- logtivity_view() renders trusted plugin templates. | |
| 38 | 39 | echo logtivity_view('_admin-header', compact('options')); |
| 39 | 40 | ?> |
| 40 | 41 | |
| 41 | 42 | <div class="postbox logtivity-settings"> |
| @@ -51,22 +52,36 @@ | ||
| 51 | 52 | </div> |
| 52 | 53 | </div> |
| 53 | 54 | <?php endif; |
| 54 | 55 | |
| 55 | - if (logtivity_is_production() == false) : ?> | |
| 56 | + if (logtivity_url_environment_mismatch()) : ?> | |
| 57 | + <div class="logtivity-notice logtivity-notice-danger"> | |
| 58 | + <h2>Logtivity is misconfigured and logging has stopped.</h2> | |
| 59 | + <div> | |
| 60 | + Your App URL (<?php echo esc_html(logtivity_get_app_url()); ?>) | |
| 61 | + and API URL (<?php echo esc_html(logtivity_get_api_url()); ?>) | |
| 62 | + point to different environments. Neither | |
| 63 | + <code>LOGTIVITY_APP_URL</code> or <code>LOGTIVITY_API_URL</code> | |
| 64 | + constant should be set, or both must be set to the same | |
| 65 | + non-production environment. No logs will be sent until this is | |
| 66 | + corrected. | |
| 67 | + </div> | |
| 68 | + </div> | |
| 69 | + <?php elseif (logtivity_is_production() == false) : ?> | |
| 56 | 70 | <div class="logtivity-notice logtivity-notice-warning"> |
| 57 | 71 | <h2>This site is not sending logs to the production application.</h2> |
| 58 | 72 | <div> |
| 59 | - See your dashboard at | |
| 60 | - <a target="_blank" | |
| 61 | - href="<?php echo logtivity_get_app_url(); ?>"> | |
| 62 | - <?php esc_html_e('Logtivity dashboard', 'logtivity'); ?> | |
| 63 | - </a> | |
| 73 | + See your dashboard at | |
| 74 | + <a target="_blank" | |
| 75 | + rel="noopener noreferrer" | |
| 76 | + href="<?php echo esc_url(logtivity_get_app_url()); ?>"> | |
| 77 | + <?php esc_html_e('Logtivity dashboard', 'logtivity'); ?> | |
| 78 | + </a> | |
| 64 | 79 | </div> |
| 65 | 80 | </div> |
| 66 | 81 | <?php endif; ?> |
| 67 | 82 | |
| 68 | - <form action="<?php echo admin_url('admin-ajax.php'); ?>?action=logtivity_update_settings" method="post"> | |
| 83 | + <form action="<?php echo esc_url(admin_url('admin-ajax.php?action=logtivity_update_settings')); ?>" method="post"> | |
| 69 | 84 | |
| 70 | 85 | <?php wp_nonce_field('logtivity_update_settings', 'logtivity_update_settings') ?> |
| 71 | 86 | |
| 72 | 87 | <table class="form-table"> |
| @@ -83,14 +98,14 @@ | ||
| 83 | 98 | <input id="logtivity_site_api_key" |
| 84 | 99 | name="logtivity_site_api_key" |
| 85 | 100 | type="text" |
| 86 | 101 | <?php echo has_filter('logtivity_site_api_key') ? 'readonly' : ''; ?> |
| 87 | - value="<?php echo sanitize_text_field($apiKey); ?>" | |
| 102 | + value="<?php echo esc_attr($apiKey); ?>" | |
| 88 | 103 | class="regular-text"> |
| 89 | 104 | <p> |
| 90 | 105 | Status: |
| 91 | 106 | <?php |
| 92 | - switch ($status) { | |
| 107 | + switch ($connectionStatus) { | |
| 93 | 108 | case 'success': |
| 94 | 109 | echo '<span style="color: #4caf50; font-weight: bold;">Connected</span>'; |
| 95 | 110 | break; |
| 96 | 111 | |
| @@ -95,17 +110,17 @@ | ||
| 95 | 110 | break; |
| 96 | 111 | |
| 97 | 112 | case 'paused': |
| 98 | 113 | echo '<span style="color: #dbbf24; font-weight: bold;">Logging is paused</span>'; |
| 99 | - echo '<br>' . $api->getConnectionMessage(); | |
| 114 | + echo '<br>' . esc_html($api->getConnectionMessage()); | |
| 100 | 115 | break; |
| 101 | 116 | |
| 102 | 117 | case 'staging': |
| 103 | - echo '<span style="color: #dbbf24; font-weight: bold;">' . $api->getConnectionMessage() . '</span>'; | |
| 118 | + echo '<span style="color: #dbbf24; font-weight: bold;">' . esc_html($api->getConnectionMessage()) . '</span>'; | |
| 104 | 119 | break; |
| 105 | 120 | |
| 106 | 121 | case 'fail': |
| 107 | - echo '<span style="color: #ff3232; font-weight: bold;">' . $api->getConnectionMessage() . '</span>'; | |
| 122 | + echo '<span style="color: #ff3232; font-weight: bold;">' . esc_html($api->getConnectionMessage()) . '</span>'; | |
| 108 | 123 | break; |
| 109 | 124 | |
| 110 | 125 | default: |
| 111 | 126 | if ($apiKey) { |
| @@ -305,13 +320,14 @@ | ||
| 305 | 320 | </td> |
| 306 | 321 | |
| 307 | 322 | <td> |
| 308 | 323 | <span class="description"> |
| 309 | - This will log the latest response from the API. | |
| 310 | - This can be useful for debugging the result from an API call when storing a log. | |
| 311 | - We <strong>recommend setting this to off by default</strong> as this will allow | |
| 312 | - us to send logs asynchronously and not wait for a response from the API. | |
| 313 | - This will be more performant. | |
| 324 | + This will record a history of request and response data for API calls | |
| 325 | + to a debug log file (<code>wp-content/uploads/logtivity-logs/debug.log</code>), | |
| 326 | + viewable below. Response details are only captured for calls that already | |
| 327 | + wait for a response (periodic settings check-ins, roughly every 10 minutes); | |
| 328 | + other calls are still sent without waiting and only the outgoing request is | |
| 329 | + logged. Enabling this does not affect performance. | |
| 314 | 330 | </span> |
| 315 | 331 | </td> |
| 316 | 332 | </tr> |
| 317 | 333 | |
| @@ -350,12 +366,12 @@ | ||
| 350 | 366 | ?> |
| 351 | 367 | If you have multiple sites on Logtivity and would rather control disabled |
| 352 | 368 | logs globally you can go to the |
| 353 | 369 | <?php |
| 354 | - echo sprintf( | |
| 370 | + printf( | |
| 355 | 371 | '<a href="%s" target="_blank" rel="nofollow">%s</a>', |
| 356 | - logtivity_get_app_url() . '/team-settings/activity-log-settings', | |
| 357 | - 'Activity Log Settings page' | |
| 372 | + esc_url(logtivity_get_app_url() . '/team-settings/activity-log-settings'), | |
| 373 | + esc_html__('Activity Log Settings page', 'logtivity') | |
| 358 | 374 | ); |
| 359 | 375 | ?> |
| 360 | 376 | in your Logtivity dashboard. |
| 361 | 377 | <?php endif; ?> |
| @@ -381,42 +397,76 @@ | ||
| 381 | 397 | |
| 382 | 398 | <div class="postbox"> |
| 383 | 399 | <div class="inside"> |
| 384 | 400 | |
| 385 | - <h3>Latest Response</h3> | |
| 401 | + <h3>Debug Log</h3> | |
| 386 | 402 | |
| 387 | - <?php | |
| 388 | - if ($latestResponse) : | |
| 389 | - $date = $latestResponse['date'] ?? null; | |
| 390 | - $code = $latestResponse['code'] ?? null; | |
| 391 | - $message = $latestResponse['message'] ?? null; | |
| 392 | - $body = $latestResponse['body'] ?? null; | |
| 403 | + <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="display: inline-block; margin-bottom: 15px;"> | |
| 404 | + <input type="hidden" name="action" value="logtivity_clear_debug_log"> | |
| 405 | + <?php wp_nonce_field('logtivity_clear_debug_log', 'logtivity_clear_debug_log'); ?> | |
| 406 | + <input id="logtivity-clear-debug-log" | |
| 407 | + type="submit" | |
| 408 | + class="button" | |
| 409 | + value="Clear Debug Log" | |
| 410 | + onclick="return confirm('Are you sure you want to clear the debug log?');"> | |
| 411 | + </form> | |
| 393 | 412 | |
| 394 | - if ($date) : ?> | |
| 395 | - <h4>Date: <?php echo $date; ?></h4> | |
| 396 | - <?php | |
| 397 | - endif; | |
| 413 | + <?php if ($debugLogEntries) : ?> | |
| 414 | + <a id="logtivity-download-debug-log" | |
| 415 | + class="button" | |
| 416 | + style="margin-bottom: 15px;" | |
| 417 | + href="<?php | |
| 418 | + echo esc_url(wp_nonce_url( | |
| 419 | + admin_url('admin-post.php?action=logtivity_download_debug_log'), | |
| 420 | + 'logtivity_download_debug_log', | |
| 421 | + 'logtivity_download_debug_log' | |
| 422 | + )); | |
| 423 | + ?>"> | |
| 424 | + Download Debug Log | |
| 425 | + </a> | |
| 426 | + <?php endif; ?> | |
| 398 | 427 | |
| 399 | - if ($code || $message) : ?> | |
| 400 | - <h4> | |
| 401 | - Response: <?php echo sprintf('%s - %s', $code ?: 'NA', $message ?: 'No Message'); ?> | |
| 402 | - </h4> | |
| 403 | - <?php | |
| 404 | - endif; | |
| 405 | - | |
| 406 | - if ($body) : ?> | |
| 407 | - <code style="display: block; padding: 20px; overflow-x: auto;"> | |
| 408 | - <?php echo json_encode($body); ?> | |
| 409 | - </code> | |
| 410 | - <?php | |
| 411 | - endif; | |
| 412 | - | |
| 413 | - else : ?> | |
| 414 | - <p>The latest logging response will appear here after an event has been logged.</p> | |
| 415 | - | |
| 416 | - <?php endif ?> | |
| 428 | + <?php if ($debugLogEntries) : ?> | |
| 429 | + <?php foreach ($debugLogEntries as $entry) : ?> | |
| 430 | + <div style="border-bottom: 1px solid #ddd; padding: 10px 0;"> | |
| 431 | + <h4> | |
| 432 | + <?php echo esc_html($entry['timestamp'] ?? ''); ?> | |
| 433 | + — | |
| 434 | + <?php echo esc_html($entry['method'] ?? ''); ?> | |
| 435 | + <?php echo esc_html($entry['endpoint'] ?? ''); ?> | |
| 436 | + </h4> | |
| 437 | + <?php $responseCode = $entry['response_code'] ?? null; ?> | |
| 438 | + <h4> | |
| 439 | + Response: | |
| 440 | + <?php | |
| 441 | + echo $responseCode === null | |
| 442 | + ? 'not captured (sent without waiting for a response)' | |
| 443 | + : esc_html(sprintf( | |
| 444 | + '%s - %s (%sms)', | |
| 445 | + $responseCode, | |
| 446 | + $entry['response_message'] ?? 'No Message', | |
| 447 | + $entry['response_time_ms'] ?? 'NA' | |
| 448 | + )); | |
| 449 | + ?> | |
| 450 | + </h4> | |
| 451 | + <code style="display: block; padding: 20px; overflow-x: auto;"> | |
| 452 | + Request: <?php echo esc_html(wp_json_encode($entry['request_body'] ?? null)); ?> | |
| 453 | + </code> | |
| 454 | + <?php if ($responseCode !== null) : ?> | |
| 455 | + <code style="display: block; padding: 20px; overflow-x: auto;"> | |
| 456 | + Response: <?php echo esc_html(wp_json_encode($entry['response_body'] ?? null)); ?> | |
| 457 | + </code> | |
| 458 | + <?php endif; ?> | |
| 459 | + </div> | |
| 460 | + <?php endforeach; ?> | |
| 461 | + <?php else : ?> | |
| 462 | + <p>The debug log will appear here after an API call has been made.</p> | |
| 463 | + <?php endif; ?> | |
| 417 | 464 | </div> |
| 418 | 465 | </div> |
| 419 | 466 | |
| 420 | 467 | <?php endif ?> |
| 421 | 468 | |
| 422 | -<?php echo logtivity_view('_admin-footer', compact('options')); ?> | |
| 469 | +<?php | |
| 470 | +// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- logtivity_view() renders trusted plugin templates. | |
| 471 | +echo logtivity_view('_admin-footer', compact('options')); | |
| 472 | +?> | |