Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Manage Debug Log
<?php
/**
* Plugin Name: Manage Debug Log
* Plugin URI: http://brasofilo.com/manage-debug-log
* Description: Adds a settings page to view and clear the Debug Log (/wp-content/debug.log)
* Version: 1.0
* Author: Rodolfo Buaiz
* Network: true
* Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
* Licence: GPLv2 or later
*/
!defined( 'ABSPATH' ) AND exit(
"<pre>Hi there! I'm just part of a plugin, <h1>&iquest;what exactly are you looking for?"
);
add_action(
'plugins_loaded',
array( BL_Default_Post_Meta_Boxes::get_instance(), 'plugin_setup' )
);
class BL_Default_Post_Meta_Boxes
{
protected static $instance = NULL;
private $no_log_img;
private $logfile = 'debug.log';
private $logpath;
public function __construct() { }
public static function get_instance()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
public function plugin_setup()
{
$this->no_log_img = 'http://f.cl.ly/items/1r1J1j2t2E291h0E0i0n/nothing-here.jpg';
$this->logpath = WP_CONTENT_DIR . '/' . $this->logfile;
$hook = is_multisite() ? 'network_' : '';
add_action( "{$hook}admin_menu", array( $this, 'make_menu' ) );
}
public function make_menu()
{
$hook = is_multisite() ? 'settings.php' : 'options-general.php';
add_submenu_page(
$hook,
'Debug Log',
'Debug Log',
'add_users',
'debug-log',
array( $this, 'render_debug' )
);
}
public function render_debug()
{
# Security check and clear log
if (
isset( $_POST['b5f_debug_log'] )
&& wp_verify_nonce( $_POST['b5f_debug_log'], plugin_basename( __FILE__ ) )
)
{
unlink( $this->logpath );
}
# Read log
if ( is_readable( $this->logpath ) ) {
$handle = fopen ( $this->logpath, 'r' );
$content = stream_get_contents( $handle );
fclose( $handle );
}
# Prepare content
$nonce = wp_nonce_field( plugin_basename( __FILE__ ), 'b5f_debug_log', true, false );
$submit = !empty( $content ) ? '<input type="submit" class="button-secondary" name="submit" value="Delete log" />' : '';
$content = !empty( $content ) ? '<pre><code>'.print_r($content,true).'</code></pre>' : "<div id='no-cont'>Nothing here, captain!<br /><img src='{$this->no_log_img}' style='margin-top:15px' /></div>";
# Do it
echo <<<HTML
<style>
.wrap { margin-left: .5em; width: 80%; }
#no-cont { margin: 1em 0; font-size: 2em; font-weight:bold; color:#009 }
#no-cont img {max-width: 330px;}
code { line-height: 2em; }
footer { margin-top:2em; opacity: .5 }
</style>
<div class="wrap">
<div id="icon-tools" class="icon32"></div>
<h2>Debug Log</h2>
<div id="poststuff">
<form action="" method="post" id="notes_form">
$nonce
$submit
$content
$submit
</form>
<footer>&hearts; <a href="http://brasofilo.com">Rodolfo Buaiz</a> &middot; <a href="https://github.com/brasofilo">Github</a></footer>
</div>
</div>
HTML;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment