-
-
Save alexpos/5685190 to your computer and use it in GitHub Desktop.
WP: console debug
This file contains hidden or 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: JavaScript Console Debug | |
Description: Debug your PHP in the console. | |
Version: 1.0 | |
Author: DRSK | |
*/ | |
/** | |
// Standard debugging | |
// Simple message to console | |
$console->debug("A very simple message"); | |
// Variable to console | |
$x = 3; | |
$y = 5; | |
$z = $x/$y; | |
$console->debug("Variable Z: ", $z); | |
// A warning | |
$console->debug("A simple Warning", null, WARN); | |
// Info | |
$console->debug("A simple Info message", null, INFO); | |
// An error | |
$console->debug("A simple error messsage", null, ERROR); | |
// Array in console | |
$fruits = array("banana", "apple", "strawberry", "pineaple"); | |
$fruits = array_reverse($fruits); | |
$console->debug("Fruits array", $fruits); | |
// Object to console | |
$book = new stdClass; | |
$book->title = "Harry Potter and the Prisoner of Azkaban"; | |
$book->author = "J. K. Rowling"; | |
$book->publisher = "Arthur A. Levine Books"; | |
$book->amazon_link = "http://www.amazon.com/dp/0439136369/"; | |
$console->debug("Object", $book); | |
// WordPress debugging | |
// note: nothing that 'echoes' such as the_permalink() works, use get_permalink() etc instead | |
$console->debug($post); | |
$console->debug(get_the_id()); | |
$console->debug("Post name:", $post->post_name); | |
$console->debug("Post meta:", get_post_meta( $post->ID )); | |
*/ | |
defined('WP_PLUGIN_URL') or die('Restricted access'); | |
// class to handle the debugging | |
class WP_console_debug { | |
function __construct() { | |
// define some variables for the console output | |
if (!defined('LOG')) define('LOG',1); | |
if (!defined('INFO')) define('INFO',2); | |
if (!defined('WARN')) define('WARN',3); | |
if (!defined('ERROR')) define('ERROR',4); | |
// new line | |
define('NL',"\r\n"); | |
// apply debugging to footer | |
add_action( 'wp_footer', array( $this, 'print_console' ) ); | |
} | |
// debugging to console | |
public function debug( $name, $var = null, $type = LOG ) { | |
global $console_debug, $wpdb, $template; | |
if( !$name ) | |
return; | |
// debugging template files | |
if( $name == TEMPLATE ) | |
$name = basename( $template ); | |
// start output | |
$output = '<script type="text/javascript">'.NL; | |
switch($type) { | |
case LOG: | |
$output .= 'console.log("'.$name.'");'.NL; | |
break; | |
case INFO: | |
$output .= 'console.info("'.$name.'");'.NL; | |
break; | |
case WARN: | |
$output .= 'console.warn("'.$name.'");'.NL; | |
break; | |
case ERROR: | |
$output .= 'console.error("'.$name.'");'.NL; | |
break; | |
} | |
if (!empty($var)) { | |
if (is_object($var) || is_array($var)) { | |
$object = json_encode($var); | |
$output .= 'var object'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' = \''.str_replace( array("'", '\"', '\r\n'), "\'", $object).'\';'.NL; | |
$output .= 'var val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' = eval("(" + object'.preg_replace('~[^A-Z|0-9]~i',"_",$name).' + ")" );'.NL; | |
switch($type) { | |
case LOG: | |
$output .= 'console.debug(val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).');'.NL; | |
break; | |
case INFO: | |
$output .= 'console.info(val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).');'.NL; | |
break; | |
case WARN: | |
$output .= 'console.warn(val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).');'.NL; | |
break; | |
case ERROR: | |
$output .= 'console.error(val'.preg_replace('~[^A-Z|0-9]~i',"_",$name).');'.NL; | |
break; | |
} | |
} else { | |
switch($type) { | |
case LOG: | |
$output .= 'console.debug("'.str_replace('"','\\"',$var).'");'.NL; | |
break; | |
case INFO: | |
$output .= 'console.info("'.str_replace('"','\\"',$var).'");'.NL; | |
break; | |
case WARN: | |
$output .= 'console.warn("'.str_replace('"','\\"',$var).'");'.NL; | |
break; | |
case ERROR: | |
$output .= 'console.error("'.str_replace('"','\\"',$var).'");'.NL; | |
break; | |
} | |
} | |
} | |
$output .= '</script>'.NL; | |
// assign the output to the globally stored variable | |
$console_debug = $output; | |
} | |
// function to print to footer | |
public function print_console() { | |
global $console_debug; | |
echo $console_debug; | |
} | |
} | |
// initiation | |
if( !is_admin() ) { | |
$console = new WP_console_debug(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment