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_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));
 }
 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_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_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_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());
 }
Esempio n. 8
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));
 }