| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our various parser functions. |
| 4 |
* |
| 5 |
* @package DebugQuickLook |
| 6 |
*/ |
| 7 |
|
| 8 |
// Call our namepsace. |
| 9 |
namespace DebugQuickLook\Parser; |
| 10 |
|
| 11 |
// Set our alias items. |
| 12 |
use DebugQuickLook as Core; |
| 13 |
use DebugQuickLook\Helpers as Helpers; |
| 14 |
use DebugQuickLook\Formatting as Formatting; |
| 15 |
|
| 16 |
/** |
| 17 |
* Kick off our parsing action. |
| 18 |
* |
| 19 |
* @param boolean $parse Whether to actually parse the file. |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
function run_parse( $parse = true ) { |
| 24 |
|
| 25 |
// Check to see if we have our constant. |
| 26 |
$hascon = Helpers\maybe_constant_set(); |
| 27 |
|
| 28 |
// If no constant is set, display that message. |
| 29 |
if ( ! $hascon ) { |
| 30 |
wp_die( __( 'You have not set the WP_DEBUG constant correctly.', 'debug-quick-look' ), __( 'Config Setup Error', 'debug-quick-look' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
// Add the new die handler. |
| 34 |
add_filter( 'wp_die_handler', __NAMESPACE__ . '\die_handler' ); |
| 35 |
|
| 36 |
// Set our debug log file. |
| 37 |
Helpers\check_debug_file(); |
| 38 |
$debug_file = Helpers\get_debug_file(); |
| 39 |
|
| 40 |
// If we didn't wanna parse the file, do it raw. |
| 41 |
if ( ! $parse ) { |
| 42 |
|
| 43 |
// Get our raw file. |
| 44 |
$raw_debug = file_get_contents( $debug_file ); |
| 45 |
|
| 46 |
// And die with the escaped raw. |
| 47 |
wp_die( '<pre class="debug-quick-look-raw">' . esc_html( $raw_debug ) . '</pre>', __( 'Viewing Raw Debug', 'debug-quick-look' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
// Parse it. |
| 51 |
$parsed = parse_debug_file( $debug_file ); |
| 52 |
|
| 53 |
// And show the world. |
| 54 |
wp_die( $parsed['display'], __( 'View Your File', 'debug-quick-look' ), [ 'totals' => absint( $parsed['totals'] ) ] ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Handle parsing our logfile. |
| 59 |
* |
| 60 |
* @param string $logfile Which log file we want to debug. |
| 61 |
* @param string $order What order to display the entries. |
| 62 |
* |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
function parse_debug_file( $logfile = '', $order = 'asc' ) { |
| 66 |
|
| 67 |
// Fetch the full lines. |
| 68 |
$lines = file( $logfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ?: []; |
| 69 |
|
| 70 |
// Run a quick right trim on each line. |
| 71 |
$lines = array_map( 'rtrim', $lines ); |
| 72 |
|
| 73 |
// Run the lines through wp_kses_post, which should be enough to strip dangerous code and retain the formatting. |
| 74 |
$lines = array_map( 'wp_kses_post', $lines ); |
| 75 |
|
| 76 |
// Set our empty. |
| 77 |
$setup = []; |
| 78 |
|
| 79 |
// Set a marker for the proper lines. |
| 80 |
$index = 0; |
| 81 |
|
| 82 |
// Loop the lines. |
| 83 |
foreach ( $lines as $nm => $linestring ) { |
| 84 |
|
| 85 |
// Get our first character, which we test with. |
| 86 |
$first = substr( $linestring, 0, 1 ); |
| 87 |
|
| 88 |
// Starting with the date bracket. |
| 89 |
if ( '[' === esc_attr( $first ) ) { |
| 90 |
|
| 91 |
// Set the line index, in case we need to append it. |
| 92 |
$index = absint( $nm ); |
| 93 |
|
| 94 |
// Set the line as a new array element. |
| 95 |
$setup[ $nm ] = $linestring; |
| 96 |
} |
| 97 |
|
| 98 |
// Now handle the non-bracket lines. |
| 99 |
if ( '[' !== esc_attr( $first ) ) { |
| 100 |
|
| 101 |
// Get our current line data. |
| 102 |
$start = $setup[ $index ]; |
| 103 |
|
| 104 |
// Merge our current string with whatever we already had. |
| 105 |
$merge = $start . PHP_EOL . $linestring; |
| 106 |
|
| 107 |
// Add it to the merged string. |
| 108 |
$setup[ $index ] = $merge; |
| 109 |
} |
| 110 |
|
| 111 |
// Should be done here. |
| 112 |
} |
| 113 |
|
| 114 |
// Reset the array keys. |
| 115 |
$setup = array_values( $setup ); |
| 116 |
|
| 117 |
// Run our individual formatting. |
| 118 |
$setup = Formatting\format_parsed_lines( $setup ); |
| 119 |
|
| 120 |
// If we wanted descending, swap. |
| 121 |
if ( 'desc' === sanitize_text_field( $order ) ) { |
| 122 |
$setup = array_reverse( $setup ); |
| 123 |
} |
| 124 |
|
| 125 |
// Return an array of the markup and count. |
| 126 |
return [ |
| 127 |
'totals' => count( $setup ), |
| 128 |
'display' => implode( "\n", $setup ), |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Return our custom wp_die handler. |
| 134 |
* |
| 135 |
* @param string $die_handler The current handler. |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
function die_handler( $die_handler ) { |
| 140 |
return apply_filters( Core\HOOK_PREFIX . 'die_handler', '\DebugQuickLook\Handler\build_handler', $die_handler ); |
| 141 |
} |
| 142 |
|