Backtrace Deprecated
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: Better Deprecation Warnings | |
Author: scribu | |
Version: 1.0 | |
*/ | |
class Backtrace_Deprecated { | |
function init() { | |
if ( !defined('WP_DEBUG') || !WP_DEBUG ) | |
return; | |
add_action('deprecated_file_included', array(__CLASS__, 'deprecated_file'), 10, 3); | |
add_action('deprecated_function_run', array(__CLASS__, 'deprecated_function'), 10, 3); | |
add_action('deprecated_argument_run', array(__CLASS__, 'deprecated_argument'), 10, 3); | |
add_action('deprecated_file_trigger_error', '__return_false'); | |
add_action('deprecated_function_trigger_error', '__return_false'); | |
add_action('deprecated_argument_trigger_error', '__return_false'); | |
} | |
function deprecated_file($file, $replacement) { | |
self::output('file', $file, " Use $replacement instead."); | |
} | |
function deprecated_function($function, $replacement) { | |
self::output('function', "$function()", " Use $replacement instead."); | |
} | |
function deprecated_argument($argument, $message) { | |
self::output('argument', "for $argument()", $message); | |
} | |
function output($type, $target, $replacement) { | |
$full_backtrace = debug_backtrace(); | |
$backtrace = $full_backtrace[5]; | |
$caller = $backtrace['file']; | |
$line = $backtrace['line']; | |
$message = "\n<br><strong>Deprecated $type</strong> $target"; | |
if ( !empty($caller) ) | |
$message .= " used in <strong>$caller</strong> on line <strong>$line</strong>"; | |
$message .= ". $replacement<br>"; | |
if ( WP_DEBUG_LOG ) { | |
error_log($message); | |
return; | |
} | |
echo $message; | |
if ( empty($caller) ) { | |
echo "<pre>"; | |
print_r(array_slice($full_backtrace, 5)); | |
echo "</pre>"; | |
} | |
} | |
function test() { | |
include ABSPATH . '/wp-admin/upgrade-functions.php'; | |
_c(1); | |
add_option('foo', 'bar', 'oups'); | |
} | |
} | |
Backtrace_Deprecated::init(); | |
#add_action('admin_init', array('Backtrace_Deprecated', 'test')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment