bobbingwide / oik-bwtrace

debug trace for WordPress
https://www.oik-plugins.com/oik-plugins/oik-bwtrace-debug-trace-for-wordpress/
GNU General Public License v2.0
6 stars 1 forks source link

bw_trace_inspect_current no longer determines the current priority #63

Closed bobbingwide closed 4 years ago

bobbingwide commented 6 years ago

Since WordPress 4.7.0 filters and hooks are now stored in the global $wp_filter as instances of class WP_hook. The logic used to determine the current priority when tracing filter results no longer works.

Expected output

when filter results to trace contains

the_content:1,the_content:8,the_content:9,the_content

we trace the results of the the_content filter at priority 1, 8, 9 and 9999 (default when value not set) This priority should be shown in the trace record that precedes the results.

Actual output

It shows callbacks, which is the name of the first field in the object.

Proposed solution

WP_hook is implemented as a final class. The current_priority is a protected field. We can get the value of a protected field by using Reflection functions.

/**
 *
 https://stackoverflow.com/questions/1762135/accessing-private-variable-from-member-function-in-php
 */
function bw_trace_get_private_property( $obj, $prop ) {
       $myClassReflection = new ReflectionClass(get_class($obj));
  $secret = $myClassReflection->getProperty( $prop );
  $secret->setAccessible(true);
  $value = $secret->getValue($obj);
return $value;
}

We need to change bw_trace_inspect_current() to obtain the value of the current hooks current_priority field, which is an array,

 function bw_trace_inspect_current() {
        global $wp_filter;
        $tag = current_filter();
       $current = $wp_filter[ $tag ];
       $priority = bw_trace_get_private_property( $current, "current_priority" );
       $priority = $priority[0];
        return $priority;
 }

It would have been easier to have a method to allow this inspection or just make the field public.

The solution does not need to check the WordPress version, as it's documented that it requires WordPress 4.9

bobbingwide commented 6 years ago

Silly me. WP_Hooks provides the current_priority method that I need! It also means that WordPress TRAC 33886 can be marked as complete, rather than invalid.

bobbingwide commented 4 years ago

Closing this now that I've written a test for it.