public
Created

Backtrace Deprecated

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?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'));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.