Make WordPress Core


Ignore:
Timestamp:
09/08/2016 03:54:13 AM (9 years ago)
Author:
pento
Message:

Hooks: Add the new class WP_Hook, and modify hook handling to make use of it.

Filters and actions have been the basis of WordPress' plugin functionality since time immemorial, they've always been a reliable method for acting upon the current state of WordPress, and will continue to be so.

Over the years, however, edge cases have cropped up. Particularly when it comes to recursively executing hooks, or a hook adding and removing itself, the existing implementation struggled to keep up with more complex use cases.

And so, we introduce WP_Hook. By changing $wp_filter from an array of arrays, to an array of objects, we reduce the complexity of the hook handling code, as the processing code (see ::apply_filters()) only needs to be aware of itself, rather than the state of all hooks. At the same time, we're able te handle more complex use cases, as the object can more easily keep track of its own state than an array ever could.

Props jbrinley for the original architecture and design of this patch.
Props SergeyBiryukov, cheeserolls, Denis-de-Bernardy, leewillis77, wonderboymusic, nacin, jorbin, DrewAPicture, ocean90, dougwollison, khag7, pento, noplanman and aaroncampbell for their testing, suggestions, contributions, patch maintenance, cajoling and patience as we got through this.
Fixes #17817.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/functions.php

    r38445 r38571  
    2222// For adding hooks before loading WP
    2323function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    24     global $wp_filter, $merged_filters;
     24    global $wp_filter;
    2525
    2626    $idx = _test_filter_build_unique_id($tag, $function_to_add, $priority);
    2727    $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    28     unset( $merged_filters[ $tag ] );
    2928    return true;
    3029}
    3130
    3231function _test_filter_build_unique_id($tag, $function, $priority) {
    33     global $wp_filter;
    34     static $filter_id_count = 0;
    35 
    3632    if ( is_string($function) )
    3733        return $function;
Note: See TracChangeset for help on using the changeset viewer.