' . esc_html( $raw_debug ) . '', __( 'Viewing Raw Debug', 'debug-quick-look' ) ); } // Parse it. $parsed = parse_debug_file( $debug_file ); // And show the world. wp_die( $parsed['display'], __( 'View Your File', 'debug-quick-look' ), [ 'totals' => absint( $parsed['totals'] ) ] ); } /** * Handle parsing our logfile. * * @param string $logfile Which log file we want to debug. * @param string $order What order to display the entries. * * @return mixed */ function parse_debug_file( $logfile = '', $order = 'asc' ) { // Fetch the full lines. $lines = file( $logfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) ?: []; // Run a quick right trim on each line. $lines = array_map( 'rtrim', $lines ); // Run the lines through wp_kses_post, which should be enough to strip dangerous code and retain the formatting. $lines = array_map( 'wp_kses_post', $lines ); // Set our empty. $setup = []; // Set a marker for the proper lines. $index = 0; // Loop the lines. foreach ( $lines as $nm => $linestring ) { // Get our first character, which we test with. $first = substr( $linestring, 0, 1 ); // Starting with the date bracket. if ( '[' === esc_attr( $first ) ) { // Set the line index, in case we need to append it. $index = absint( $nm ); // Set the line as a new array element. $setup[ $nm ] = $linestring; } // Now handle the non-bracket lines. if ( '[' !== esc_attr( $first ) ) { // Get our current line data. $start = $setup[ $index ]; // Merge our current string with whatever we already had. $merge = $start . PHP_EOL . $linestring; // Add it to the merged string. $setup[ $index ] = $merge; } // Should be done here. } // Reset the array keys. $setup = array_values( $setup ); // Run our individual formatting. $setup = Formatting\format_parsed_lines( $setup ); // If we wanted descending, swap. if ( 'desc' === sanitize_text_field( $order ) ) { $setup = array_reverse( $setup ); } // Return an array of the markup and count. return [ 'totals' => count( $setup ), 'display' => implode( "\n", $setup ), ]; } /** * Return our custom wp_die handler. * * @param string $die_handler The current handler. * * @return string */ function die_handler( $die_handler ) { return apply_filters( Core\HOOK_PREFIX . 'die_handler', '\DebugQuickLook\Handler\build_handler', $die_handler ); }