Created
October 13, 2016 15:23
-
-
Save antonorlov/a90d8d6b549413b302eb1029540877f6 to your computer and use it in GitHub Desktop.
Debugger for wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Wordpress debugger | |
Plugin URI: mailto:[email protected] | |
Description: Debug tools for wordpress | |
Author: Anton Orlov | |
Author URI: mailto:[email protected] | |
Version: 1.0 | |
*/ | |
// Exit if accessed directly | |
if (!defined('ABSPATH')) exit; | |
class Debugger { | |
public static function print_r($data, $filename = null, $level = 5) { | |
$result = self::print_r_level($data, $level = 5); | |
if ($filename) { | |
self::log($result, $filename); | |
} else { | |
return $result; | |
} | |
} | |
public static function print_r_level($data, $level = 5) | |
{ | |
static $innerLevel = 1; | |
static $tabLevel = 1; | |
static $cache = array(); | |
$type = gettype($data); | |
$tabs = str_repeat(' ', $tabLevel); | |
$quoteTabes = str_repeat(' ', $tabLevel - 1); | |
$recrusiveType = array('object', 'array'); | |
$output = $data; | |
// Recrusive | |
if (in_array($type, $recrusiveType)) { | |
// If type is object, try to get properties by Reflection. | |
if ($type == 'object') { | |
if (in_array($data, $cache)) { | |
return "\n{$quoteTabes}*RECURSION*\n"; | |
} | |
// Cache the data | |
$cache[] = $data; | |
$output = get_class($data) . ' ' . ucfirst($type); | |
$ref = new \ReflectionObject($data); | |
$properties = $ref->getProperties(); | |
$elements = array(); | |
foreach ($properties as $property) { | |
$property->setAccessible(true); | |
$pType = $property->getName(); | |
if ($property->isProtected()) { | |
$pType .= ":protected"; | |
} | |
elseif ($property->isPrivate()) { | |
$pType .= ":" . $property->class . ":private"; | |
} | |
if ($property->isStatic()) { | |
$pType .= ":static"; | |
} | |
$elements[$pType] = $property->getValue($data); | |
} | |
} | |
// If type is array, just retun it's value. | |
elseif ($type == 'array') { | |
$output = ucfirst($type); | |
$elements = $data; | |
} | |
// Start dumping datas | |
if ($level == 0 || $innerLevel < $level) { | |
// Start recrusive print | |
$output .= "\n{$quoteTabes}("; | |
foreach ($elements as $key => $element) { | |
$output .= "\n{$tabs}[{$key}] => "; | |
// Increment level | |
$tabLevel = $tabLevel + 2; | |
$innerLevel++; | |
$output .= in_array(gettype($element), $recrusiveType) ? self::print_r_level($element, $level) : $element; | |
// Decrement level | |
$tabLevel = $tabLevel - 2; | |
$innerLevel--; | |
} | |
$output .= "\n{$quoteTabes})\n"; | |
} | |
else { | |
$output .= "\n{$quoteTabes}*MAX LEVEL*\n"; | |
} | |
} | |
// Clean cache | |
if ($innerLevel == 1) { | |
$cache = array(); | |
} | |
return $output; | |
} | |
public static function log($data, $filename = 'default', $flag = FILE_APPEND) | |
{ | |
$fullpath = dirname(__FILE__) . '/log/' . $filename . '.txt'; | |
file_put_contents($fullpath, date("H:i:s d-m-Y") . " " . $data . "\n", $flag); | |
chmod($fullpath, 0777); | |
} | |
public static function callTrace($filename = null) | |
{ | |
$e = new Exception(); | |
$trace = explode("\n", $e->getTraceAsString()); | |
// reverse array to make steps line up chronologically | |
$trace = array_reverse($trace); | |
array_shift($trace); // remove {main} | |
array_pop($trace); // remove call to this method | |
$length = count($trace); | |
$result = array(); | |
for ($i = 0; $i < $length; $i++) { | |
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering | |
} | |
$_result = "\t" . implode("\n\t", $result); | |
if ($filename) { | |
self::log($_result, $filename); | |
} else { | |
return $_result; | |
} | |
} | |
} | |
add_action('shutdown', function() { | |
$log = ''; | |
foreach($GLOBALS['wp_actions'] as $action => $count) { | |
$log .= $action . " (" . $count . ")\n"; | |
} | |
Debugger::log($log, 'wp_actions'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment