Skip to content

Instantly share code, notes, and snippets.

@johnalarcon
Last active November 26, 2020 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnalarcon/24941c48b513370053bb738daacd891a to your computer and use it in GitHub Desktop.
Save johnalarcon/24941c48b513370053bb738daacd891a to your computer and use it in GitHub Desktop.
A couple of simple functions to help with debugging ClassicPress plugins in development.
<?php
// Prevent direct access.
if (strpos($_SERVER['PHP_SELF'], 'debug.php')) {
die();
}
/**
* Debug - to expose data in variables, arrays, objects.
*
* @author John Alarcon
*
* @param mixed $var Required. The variable, array, object to debug.
* @param boolean $name Optional. Add a name for the item being debugged.
* @param integer $pad Optional. Move debug display to the right by this many pixels.
* @param boolean $exit Optional. Stop script execution.
*/
function debug($var, $name=false, $pad=0, $exit=false) {
$prefix = 'codepotent-debug-mixed';
$unique = rand(123456, 456789);
echo "\n\n";
echo '<style>';
echo 'div.'.$prefix.'-container-'.$unique.' {clear:both;margin:25px 0 0 !important;padding:0 !important;}';
echo 'div.'.$prefix.'-container-'.$unique.' pre.'.$prefix.'-code {overflow-x:scroll;margin:0 0 0 '.(int)$pad.'px !important;padding:10px !important;border:1px solid #ccc;background:#f2f2f2;font-family:monospace;font-size:11pt;line-height:1.25em !important;}';
echo 'div.'.$prefix.'-container-'.$unique.' h2.'.$prefix.'-heading {font-size:11pt;padding:0 !important;margin:0 !important;}';
echo 'div.'.$prefix.'-container-'.$unique.' .'.$prefix.'-red-text {color:#f00;}';
echo '</style>'."\n";
echo '<div class="'.$prefix.'-container-'.$unique.'">'."\n";
echo '<pre class="'.$prefix.'-code">'."\n";
echo '<h2 class="'.$prefix.'-heading '.$prefix.'-red-text">'.(($name)?'$'.$name.' ['.gettype($var).']':gettype($var)).'</h2>'."\n";
print_r($var)."\n";
if ($exit) {
echo '<br />&lt;-----[ <strong class="'.$prefix.'-red-text">script terminated</strong> ]-----&gt;'."\n";
}
echo ' </pre>'."\n";
echo '</div>'."\n";
echo "\n\n";
if ($exit) {
die();
}
}
/**
* Debug - to expose data in single, basic variables.
*
* A lightweight function to expose data within basic variables; best used in loops.
*
* @author John Alarcon
*
* @param mixed $var Required. The variable to debug.
* @param boolean $name Optional. Add a name for the item being debugged.
* @param integer $pad Optional. Move debug display to the right by this many pixels.
* @param boolean $exit Optional. Stop script execution.
*/
function line($var, $name=false, $pad=0, $exit=false) {
$prefix = 'codepotent-debug-line';
$unique = rand(123456, 456789);
echo "\n\n";
echo '<style>';
echo 'div.'.$prefix.'-container-'.$unique.' {clear:both;margin:0 !important;padding:0 !important;font-family:monospace !important;'.(($pad!==0)?'margin-left:'.(int)$pad.'px !important;' : '').'}';
echo 'div.'.$prefix.'-container-'.$unique.' span {font-size:11pt;}';
echo 'div.'.$prefix.'-container-'.$unique.' .'.$prefix.'-red-text {color:#f00;}';
echo 'div.'.$prefix.'-container-'.$unique.' .'.$prefix.'-black-text {color:#000;}';
echo 'div.'.$prefix.'-container-'.$unique.' .'.$prefix.'-gray-text {color:#999;}';
echo '</style>'."\n";
echo '<div class="'.$prefix.'-container-'.$unique.'">'."\n";
echo '<span class="'.$prefix.'-black-text"><strong>&gt;</strong></span> ';
if ($name) {
echo '<span class="'.$prefix.'-red-text"><strong>$'.$name.'</strong></span> <span class="'.$prefix.'-gray-text">=</span> ';
}
echo '<span class="'.$prefix.'-black-text">'.$var.' <span class="'.$prefix.'-gray-text">['.gettype($var).']</span></span>';
if ($exit) {
echo '<br />> &lt;-----[ <strong class="'.$prefix.'-red-text">script terminated</strong> ]-----&gt;'."\n";
}
echo '</div>'."\n";
echo "\n\n";
if ($exit) {
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment