public function test_has_filter_with_wrong_callback()
 {
     $callback = '__return_null';
     $hook = new WP_Hook();
     $tag = __FUNCTION__;
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $hook->add_filter($tag, $callback, $priority, $accepted_args);
     $this->assertFalse($hook->has_filter($tag, '__return_false'));
 }
 public function test_not_has_filter_with_directly_removed_callback()
 {
     $callback = '__return_null';
     $hook = new WP_Hook();
     $tag = __FUNCTION__;
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $hook->add_filter($tag, $callback, $priority, $accepted_args);
     $function_key = _wp_filter_build_unique_id($tag, $callback, $priority);
     unset($hook->callbacks[$priority][$function_key]);
     $this->assertFalse($hook->has_filters());
 }
 public function test_remove_filter_with_another_at_different_priority()
 {
     $callback_one = '__return_null';
     $callback_two = '__return_false';
     $hook = new WP_Hook();
     $tag = rand_str();
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $hook->add_filter($tag, $callback_one, $priority, $accepted_args);
     $hook->add_filter($tag, $callback_two, $priority + 1, $accepted_args);
     $hook->remove_filter($tag, $callback_one, $priority);
     $this->assertFalse(isset($hook->callbacks[$priority]));
     $this->assertCount(1, $hook->callbacks[$priority + 1]);
 }
 public function test_do_all_hook_with_multiple_calls()
 {
     $a = new MockAction();
     $callback = array($a, 'action');
     $hook = new WP_Hook();
     $tag = 'all';
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $arg = rand_str();
     $hook->add_filter($tag, $callback, $priority, $accepted_args);
     $args = array($arg);
     $hook->do_all_hook($args);
     $hook->do_all_hook($args);
     $this->assertEquals(2, $a->get_call_count());
 }
 public function test_apply_filters_with_multiple_calls()
 {
     $a = new MockAction();
     $callback = array($a, 'filter');
     $hook = new WP_Hook();
     $tag = rand_str();
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $arg = rand_str();
     $hook->add_filter($tag, $callback, $priority, $accepted_args);
     $returned_one = $hook->apply_filters($arg, array($arg));
     $returned_two = $hook->apply_filters($returned_one, array($returned_one));
     $this->assertEquals($returned_two, $arg);
     $this->assertEquals(2, $a->get_call_count());
 }
 public function test_array_to_hooks()
 {
     $tag1 = rand_str();
     $priority1 = rand(1, 100);
     $tag2 = rand_str();
     $priority2 = rand(1, 100);
     $filters = array($tag1 => array($priority1 => array('test1' => array('function' => '__return_false', 'accepted_args' => 2))), $tag2 => array($priority2 => array('test1' => array('function' => '__return_null', 'accepted_args' => 1))));
     $hooks = WP_Hook::build_preinitialized_hooks($filters);
     $this->assertEquals($priority1, $hooks[$tag1]->has_filter($tag1, '__return_false'));
     $this->assertEquals($priority2, $hooks[$tag2]->has_filter($tag2, '__return_null'));
 }
 public function test_foreach()
 {
     $callback_one = '__return_null';
     $callback_two = '__return_false';
     $hook = new WP_Hook();
     $tag = __FUNCTION__;
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $hook->add_filter($tag, $callback_one, $priority, $accepted_args);
     $hook->add_filter($tag, $callback_two, $priority + 1, $accepted_args);
     $functions = array();
     $priorities = array();
     foreach ($hook as $key => $callbacks) {
         $priorities[] = $key;
         foreach ($callbacks as $function_index => $the_) {
             $functions[] = $the_['function'];
         }
     }
     $this->assertEqualSets(array($priority, $priority + 1), $priorities);
     $this->assertEqualSets(array($callback_one, $callback_two), $functions);
 }
 public function test_remove_all_filters_with_priority()
 {
     $callback_one = '__return_null';
     $callback_two = '__return_false';
     $hook = new WP_Hook();
     $tag = rand_str();
     $priority = rand(1, 100);
     $accepted_args = rand(1, 100);
     $hook->add_filter($tag, $callback_one, $priority, $accepted_args);
     $hook->add_filter($tag, $callback_two, $priority + 1, $accepted_args);
     $hook->remove_all_filters($priority);
     $this->assertFalse($hook->has_filter($tag, $callback_one));
     $this->assertTrue($hook->has_filters());
     $this->assertEquals($priority + 1, $hook->has_filter($tag, $callback_two));
 }
示例#9
0
 /**
  * Normalizes filters set up before WordPress has initialized to WP_Hook objects.
  *
  * @since 4.7.0
  * @access public
  * @static
  *
  * @param array $filters Filters to normalize.
  * @return WP_Hook[] Array of normalized filters.
  */
 public static function build_preinitialized_hooks($filters)
 {
     /** @var WP_Hook[] $normalized */
     $normalized = array();
     foreach ($filters as $tag => $callback_groups) {
         if (is_object($callback_groups) && $callback_groups instanceof WP_Hook) {
             $normalized[$tag] = $callback_groups;
             continue;
         }
         $hook = new WP_Hook();
         // Loop through callback groups.
         foreach ($callback_groups as $priority => $callbacks) {
             // Loop through callbacks.
             foreach ($callbacks as $cb) {
                 $hook->add_filter($tag, $cb['function'], $priority, $cb['accepted_args']);
             }
         }
         $normalized[$tag] = $hook;
     }
     return $normalized;
 }
 public function test_do_action_with_more_accepted_args()
 {
     $callback = array($this, '_action_callback');
     $hook = new WP_Hook();
     $tag = __FUNCTION__;
     $priority = rand(1, 100);
     $accepted_args = 1000;
     $arg = __FUNCTION__ . '_arg';
     $hook->add_filter($tag, $callback, $priority, $accepted_args);
     $hook->do_action(array($arg));
     $this->assertCount(1, $this->events[0]['args']);
 }
 public function test_sort_after_add_filter()
 {
     $a = new MockAction();
     $b = new MockAction();
     $c = new MockAction();
     $hook = new WP_Hook();
     $tag = rand_str();
     $hook->add_filter($tag, array($a, 'action'), 10, 1);
     $hook->add_filter($tag, array($b, 'action'), 5, 1);
     $hook->add_filter($tag, array($c, 'action'), 8, 1);
     $this->assertEquals(array(5, 8, 10), array_keys($hook->callbacks));
 }
示例#12
0
 *
 * Also see the {@link https://codex.wordpress.org/Plugin_API Plugin API} for
 * more information and examples on how to use a lot of these functions.
 *
 * This file should have no external dependencies.
 *
 * @package WordPress
 * @subpackage Plugin
 * @since 1.5.0
 */
// Initialize the filter globals.
require dirname(__FILE__) . '/class-wp-hook.php';
/** @var WP_Hook[] $wp_filter */
global $wp_filter, $wp_actions, $wp_current_filter;
if ($wp_filter) {
    $wp_filter = WP_Hook::build_preinitialized_hooks($wp_filter);
} else {
    $wp_filter = array();
}
if (!isset($wp_actions)) {
    $wp_actions = array();
}
if (!isset($wp_current_filter)) {
    $wp_current_filter = array();
}
/**
 * Hook a function or method to a specific filter action.
 *
 * WordPress offers filter hooks to allow plugins to modify
 * various types of internal data at runtime.
 *
示例#13
0
/**
 * Start the WordPress object cache.
 *
 * If an object-cache.php file exists in the wp-content directory,
 * it uses that drop-in as an external object cache.
 *
 * @since 3.0.0
 * @access private
 */
function wp_start_object_cache()
{
    global $wp_filter;
    $first_init = false;
    if (!function_exists('wp_cache_init')) {
        if (file_exists(WP_CONTENT_DIR . '/object-cache.php')) {
            require_once WP_CONTENT_DIR . '/object-cache.php';
            if (function_exists('wp_cache_init')) {
                wp_using_ext_object_cache(true);
            }
            // Re-initialize any hooks added manually by object-cache.php
            if ($wp_filter) {
                $wp_filter = WP_Hook::build_preinitialized_hooks($wp_filter);
            }
        }
        $first_init = true;
    } elseif (!wp_using_ext_object_cache() && file_exists(WP_CONTENT_DIR . '/object-cache.php')) {
        /*
         * Sometimes advanced-cache.php can load object-cache.php before
         * it is loaded here. This breaks the function_exists check above
         * and can result in `$_wp_using_ext_object_cache` being set
         * incorrectly. Double check if an external cache exists.
         */
        wp_using_ext_object_cache(true);
    }
    if (!wp_using_ext_object_cache()) {
        require_once ABSPATH . WPINC . '/cache.php';
    }
    /*
     * If cache supports reset, reset instead of init if already
     * initialized. Reset signals to the cache that global IDs
     * have changed and it may need to update keys and cleanup caches.
     */
    if (!$first_init && function_exists('wp_cache_switch_to_blog')) {
        wp_cache_switch_to_blog(get_current_blog_id());
    } elseif (function_exists('wp_cache_init')) {
        wp_cache_init();
    }
    if (function_exists('wp_cache_add_global_groups')) {
        wp_cache_add_global_groups(array('users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites'));
        wp_cache_add_non_persistent_groups(array('counts', 'plugins'));
    }
}