Skip to content

Instantly share code, notes, and snippets.

@iandunn
Created April 22, 2014 04:33
Show Gist options
  • Save iandunn/11165430 to your computer and use it in GitHub Desktop.
Save iandunn/11165430 to your computer and use it in GitHub Desktop.
This was in a repo, but moving to a Gist and deleting the repo because it's no longer useful.
<?php
/*
* Outputs the type, length and contents of a variable in a readable format, which can be useful for debugging.
* Output can be sent to echo(), die() / wp_die(), returned, a WordPress transient or a WordPress admin notice
* (via the IDAdminNotice class, not included). There are extra output methods for use in a WordPress environment,
* but it will also work on it's own.
*/
/*
* Copyright 2011 Ian Dunn (email : ian@iandunn.name)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( $_SERVER['SCRIPT_FILENAME'] == __FILE__ )
die( 'Access denied.' );
if( !class_exists( 'IDDescribeVar' ) )
{
/**
* @package IDDescribeVar
* @author Ian Dunn <ian@iandunn.name>
*/
class IDDescribeVar
{
// Declare variables and constants
const NAME = 'ID Describe Var';
const VERSION = '0.1';
const PREFIX = 'iddv_';
/**
* Prints the input in various ways for debugging.
* @author Ian Dunn <ian@iandunn.name>
* @param mixed $data
* @param string $output 'message' will be sent to an IDAdminNotice; 'die' will be output inside wp_die() or die(); 'transient' will create a transient in the database (WordPress only); 'return' will be returned;
* @param string $message Optional message to output before description
* @return mixed
*/
public static function describe( $data, $output = 'die', $message = '' )
{
$type = gettype( $data );
// Build description
switch( $type )
{
case 'array':
case 'object':
$length = count( $data );
$data = print_r( $data, true );
break;
case 'string';
$length = strlen( $data );
break;
default:
$length = count( $data );
ob_start();
var_dump( $data );
$data = ob_get_contents();
ob_end_clean();
$data = print_r( $data, true );
break;
}
$description = sprintf('
<p>
%s
Type: %s<br />
Length: %s<br />
Content: <br /><blockquote><pre>%s</pre></blockquote>
</p>',
( $message ? 'Message: '. $message .'<br />' : '' ),
$type,
$length,
htmlspecialchars( $data )
);
// Output description
switch( $output )
{
case 'notice':
if( class_exists( 'IDAdminNotices' ) )
IDAdminNotices::cGetSingleton()->mEnqueue( $description, 'update' ); // @todo store a private var w/ the instance so you're not calling the singleton method multiple times when this function is called multiple times
else
throw new Exception( self::NAME .' error: IDAdminNotices class not available.' );
break;
case 'return':
return $description;
break;
case 'transient':
if( function_exists( 'set_transient' ) )
{
$uniqueKey = $message ? str_replace( array( ' ', '-', '/', '\\', '.' ), '_', $message ) : mt_rand(); // removes characters that are invalid in MySQL column names
set_transient( self::PREFIX . 'describe_' . $uniqueKey, $description, 60 * 60 );
}
else
throw new Exception( self::NAME ." error: WordPress' set_transient() function not available." );
break;
case 'echo':
echo $description; // @todo - want to esc_html on message, but not entire description. can't do to $message above because don't want to escape for other switch cases
break;
case 'die':
default:
if( function_exists( 'wp_die' ) )
wp_die( $description );
else
die( $description );
break;
}
}
} // end IDDescribeVar
}
if( class_exists( 'IDAdminNotices' ) )
IDAdminNotices::cGetSingleton(); // initialize the singleton instance as early as possible to make sure hook callbacks are registered in time
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment