Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
37.50% |
3 / 8 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Debug | |
37.50% |
3 / 8 |
|
33.33% |
1 / 3 |
18.96 | |
0.00% |
0 / 1 |
| is_debug_mode | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| log | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| scrub | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Implement debug helper methods. |
| 4 | * |
| 5 | * @link https://automattic.com |
| 6 | * @since 1.0.0 |
| 7 | * @package automattic/jetpack-boost |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack_Boost\Lib; |
| 11 | |
| 12 | /** |
| 13 | * Class Debug |
| 14 | */ |
| 15 | class Debug { |
| 16 | /** |
| 17 | * Returns whether the debug mode has been triggered. |
| 18 | */ |
| 19 | public static function is_debug_mode() { |
| 20 | $script_debug = defined( 'SCRIPT_DEBUG' ) && \SCRIPT_DEBUG; |
| 21 | $manual_debug_mode = filter_input( INPUT_GET, 'jetpack-boost-debug' ); |
| 22 | |
| 23 | $debug = $script_debug || $manual_debug_mode; |
| 24 | |
| 25 | /** |
| 26 | * Filter debug status on/off |
| 27 | * |
| 28 | * @param bool $debug_status enable or disable debug mode. |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | return apply_filters( 'jetpack_boost_debug', $debug ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Write one line to the PHP error log, under WP_DEBUG only. |
| 37 | * |
| 38 | * The messages carry third-party text (exception messages, file paths, cache |
| 39 | * keys), so the line is scrubbed of CR/LF first: a message spanning two lines can |
| 40 | * otherwise forge a log entry of its own (CWE-117). |
| 41 | * |
| 42 | * @since 4.7.0 |
| 43 | * |
| 44 | * @param string $message What happened. Prefixed with "Jetpack Boost: ". |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public static function log( $message ) { |
| 49 | if ( ! defined( 'WP_DEBUG' ) || ! \WP_DEBUG ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 54 | error_log( self::scrub( 'Jetpack Boost: ' . $message ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Flatten a log line to one physical line. |
| 59 | * |
| 60 | * Separate from log(), and public, so a unit test can assert the rule without |
| 61 | * defining WP_DEBUG for the whole process. log() is the only caller. |
| 62 | * |
| 63 | * @since 4.7.0 |
| 64 | * |
| 65 | * @param mixed $message Text that may contain CR/LF. Cast rather than typed: a |
| 66 | * logging helper must not raise a TypeError on a path |
| 67 | * that is already handling a failure. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public static function scrub( $message ) { |
| 72 | return str_replace( array( "\r", "\n" ), ' ', (string) $message ); |
| 73 | } |
| 74 | } |