| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for debug data. |
| 4 |
* |
| 5 |
* @package Cache-Warmer |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Cache_Warmer; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
/** |
| 13 |
* Debug class. |
| 14 |
*/ |
| 15 |
final class Debug { |
| 16 |
|
| 17 |
/** |
| 18 |
* Maybe returns the debug array. |
| 19 |
* |
| 20 |
* @return array Debug array. |
| 21 |
* |
| 22 |
* @throws Exception Exception. |
| 23 |
*/ |
| 24 |
public static function maybe_get_debug_array() { |
| 25 |
return self::debug_mode_enabled() |
| 26 |
&& |
| 27 |
( |
| 28 |
// Return only 1 / 10th (each 50s on average) due to the potentially big size. |
| 29 |
self::debug_each_request() || 1 === wp_rand( 1, 10 ) |
| 30 |
) |
| 31 |
? self::get_debug_array() : []; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the debug array. |
| 36 |
* |
| 37 |
* @return array Debug array. |
| 38 |
* |
| 39 |
* @throws Exception Exception. |
| 40 |
*/ |
| 41 |
public static function get_debug_array() { |
| 42 |
$data = [ |
| 43 |
'current-server-ip-address' => Server_IP_Detection::get_current_server_ip(), |
| 44 |
'last-processing-link' => Cache_Warmer::$options->get( 'last-processing-link' ), |
| 45 |
'last-failed-to-retrieve-link' => Cache_Warmer::$options->get( 'last-failed-to-retrieve-link' ), |
| 46 |
'last-retrieved-link' => Cache_Warmer::$options->get( 'last-retrieved-link' ), |
| 47 |
]; |
| 48 |
|
| 49 |
$options_to_inspect = [ |
| 50 |
'links-tree-leftovers', |
| 51 |
'retrieved-links', |
| 52 |
'failed-to-retrieve-links', |
| 53 |
'unscheduled-links-tree-leftovers', |
| 54 |
'unscheduled-retrieved-links', |
| 55 |
'unscheduled-failed-to-retrieve-links', |
| 56 |
]; |
| 57 |
|
| 58 |
foreach ( $options_to_inspect as $option ) { |
| 59 |
$option_name = "cache-warmer-$option"; |
| 60 |
$data[ $option ] = [ |
| 61 |
'fromObjectCache' => Cache_Warmer::$options->use_object_cache_for_option( $option_name ), |
| 62 |
'value' => Cache_Warmer::$options->get( $option_name ), |
| 63 |
'optionValue' => get_option( $option_name ), |
| 64 |
'cacheValue' => wp_cache_get( $option_name ), |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if the plugin debug mode is enabled. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
private static function debug_mode_enabled() { |
| 77 |
return isset( $_ENV['CACHE_WARMER_DEBUG'] ) && in_array( $_ENV['CACHE_WARMER_DEBUG'], [ 'true', '1', 'yes' ], true ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Check if needed to debug each request. |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
private static function debug_each_request() { |
| 86 |
return isset( $_ENV['CACHE_WARMER_DEBUG_EACH_REQUEST'] ) && in_array( $_ENV['CACHE_WARMER_DEBUG_EACH_REQUEST'], [ 'true', '1', 'yes' ], true ); |
| 87 |
} |
| 88 |
} |
| 89 |
|