Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bw_trace_inspect_current no longer determines the current priority #63

Closed
bobbingwide opened this issue Feb 3, 2018 · 2 comments
Closed
Assignees

Comments

@bobbingwide
Copy link
Owner

@bobbingwide bobbingwide commented Feb 3, 2018

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

This comment has been minimized.

Copy link
Owner Author

@bobbingwide bobbingwide commented Feb 3, 2018

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

This comment has been minimized.

Copy link
Owner Author

@bobbingwide bobbingwide commented Nov 29, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.