php 在WordPress上进行PHP调试的少数功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 在WordPress上进行PHP调试的少数功能相关的知识,希望对你有一定的参考价值。

<?php

/**
* Plugin Name: Debugging WordPress
* Plugin URI: https://premium.wpmudev.org/
* Description: Handy functions for PHP debugging on WordPress
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}



// See clearly what's going on and where is the debug data. Also it has to accommodate with the current styles and not fall behind or get mixed with other text etc.
// This snippet will provide real debug notices in a flashy table:
// - complete backtrace of calls on the left with line numbers
// - arguments content on the right, with the variable name and coloring per variable type
// Uses: error_log_full($arg1, $arg2,..);

if ( ! function_exists('error_log_full') ) {

	function error_log_full() {
		error_reporting(E_ALL);
		$args = func_get_args();
		$backtrace = debug_backtrace();
		$file = file($backtrace[0]['file']);
		$src  = $file[$backtrace[0]['line']-1];  // select debug($varname) line where it has been called
		$pat  = '#(.*)'.__FUNCTION__.' *?\( *?\$(.*) *?\)(.*)#i';  // search pattern for wtf(parameter)
		$arguments  = trim(preg_replace($pat, '$2', $src));  // extract arguments pattern
		$args_arr = array_map('trim', explode(',', $arguments));

		print '<style>
			div.debug {visible; clear: both; display: table; width: 100%; font-family: Courier,monospace; border: medium solid red; background-color: yellow; border-spacing: 5px; z-index: 999;}
			div.debug > div {display: unset; margin: 5px; border-spacing: 5px; padding: 5px;}
			div.debug .cell {display: inline-flex; padding: 5px; white-space: pre-wrap;}
			div.debug .left-cell {float: left; background-color: Violet;}
			div.debug .array {color: RebeccaPurple; background-color: Violet;}
			div.debug .object pre {color: DodgerBlue; background-color: PowderBlue;}
			div.debug .variable pre {color: RebeccaPurple; background-color: LightGoldenRodYellow;}
			div.debug pre {white-space: pre-wrap;}
		</style>'.PHP_EOL;

		print '<div class="debug">'.PHP_EOL;
			foreach ($args as $key => $arg) {
				print '<div><div class="left-cell cell"><b>';
				array_walk(debug_backtrace(),create_function('$a,$b','print "{$a[\'function\']}()(".basename($a[\'file\']).":{$a[\'line\']})<br> ";'));
				print '</b></div>'.PHP_EOL;
					if (is_array($arg)) {
						print '<div class="cell array"><b>'.$args_arr[$key].' = </b>';
							print_r(htmlspecialchars(print_r($arg)), ENT_COMPAT, 'UTF-8');
						print '</div>'.PHP_EOL;
					} elseif (is_object($arg)) {
						print '<div class="cell object"><pre><b>'.$args_arr[$key].' = </b>';
							print_r(htmlspecialchars(print_r(var_dump($arg))), ENT_COMPAT, 'UTF-8');
						print '</pre></div>'.PHP_EOL;
					} else {
						print '<div class="cell variable"><pre><b>'.$args_arr[$key].' = </b>&gt;';
							print_r(htmlspecialchars($arg, ENT_COMPAT, 'UTF-8').'&lt;');
						print '</pre></div>'.PHP_EOL;
					}
				print '</div>'.PHP_EOL;
			}
		print '</div>'.PHP_EOL;
	}

}



// Log something in the browser console.log
// Uses: console_log( $data );

if ( ! function_exists('console_log') ) {

	function console_log( $data ) {
		echo '<script>';
		echo 'console.log('. json_encode( $data ) .')';
		echo '</script>';
	}

}



// Log anything in the debug.log
// Uses: write_log( $data );

if ( ! function_exists('write_log') ) {

	function write_log( $data ) {
		error_log( print_r( $data, true ) );
	}

}




// Simple log view with enabling <pre> tag.
// Uses: debug_view( $data );

if ( ! function_exists('debug_view') ) {

	function debug_view( $data ) {
		echo '<pre>';
			if ( is_array( $data ) )  {
				print_r ( $data );
			} else {
				var_dump ( $data );
			}
		echo '</pre>';
	}

}

以上是关于php 在WordPress上进行PHP调试的少数功能的主要内容,如果未能解决你的问题,请参考以下文章

PhpStorm+Xdebug配置单步调试PHP

Centos 6.9中PHPmyadmin 的搭建,WordPress的搭建,Discuz的搭建

php Wordpress调试

php WordPress调试

php Wordpress调试和Profiler

PHP Wordpress调试配置