Skip to content
Snippets Groups Projects
Verified Commit e2f03018 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat(wip): allow private hooks calls

parent f687d40d
No related branches found
No related tags found
2 merge requests!21.x,!1Draft: Basic implementation of plugin initializer
<?php
declare( strict_types=1 );
namespace WPDesk\Init;
/**
* Hooks trait.
*
* Allows protected and private methods to be used as hook callbacks in PHP <8.1. Since PHP 8.1
* you are able to take advantage of first class callable and register private methods in hooks
* without any workarounds.
*
* @author John P. Bloch
* @link https://github.com/johnpbloch/wordpress-dev/blob/master/src/Hooks.php
*/
trait HooksTrait {
/**
* Add a WordPress filter.
*
* @param callable $method
*
* @return true
*/
protected function add_filter(
string $hook,
$method,
int $priority = 10,
int $arg_count = 1
): bool {
return add_filter(
$hook,
$this->map_filter( $method, $arg_count ),
$priority,
$arg_count
);
}
/**
* Add a WordPress action.
*
* This is an alias of add_filter().
*
* @param callable $method
*
* @return true
*/
protected function add_action( string $hook, $method, int $priority = 10, int $arg_count = 1 ): bool {
return $this->add_filter( $hook, $method, $priority, $arg_count );
}
/**
* Remove a WordPress filter.
*
* @param callable $method
*
* @return bool Whether the function existed before it was removed.
*/
protected function remove_filter(
string $hook,
$method,
int $priority = 10,
int $arg_count = 1
): bool {
return remove_filter(
$hook,
$this->map_filter( $method, $arg_count ),
$priority
);
}
/**
* Remove a WordPress action.
*
* This is an alias of remove_filter().
*
* @param callable $method
*
* @return bool Whether the function is removed.
*/
protected function remove_action(
string $hook,
$method,
int $priority = 10,
int $arg_count = 1
): bool {
return $this->remove_filter( $hook, $method, $priority, $arg_count );
}
/**
* Map a filter to a closure that inherits the class' internal scope.
*
* This allows hooks to use protected and private methods.
*
* @param string $callable
* @param int $arg_count
*
* @return \Closure The callable actually attached to a WP hook
*/
private function map_filter( $callable, int $arg_count ): \Closure {
if ( is_string( $callable ) && method_exists( $this, $callable ) ) {
$object = $this;
$method = $callable;
}
if ( is_array( $callable ) ) {
[ $object, $method ] = $callable;
}
return static function () use ( $object, $method, $arg_count ) {
return $object->{$method}( ...array_slice( func_get_args(), 0, $arg_count ) );
};
}
}
<?php
declare( strict_types=1 );
namespace WPDesk\Init\Tests;
class HooksTraitTest extends \PHPUnit\Framework\TestCase {
/**
* How you can call a hook:
* 1. callable string: '__return_true'
* 2. closure: function () { echo "hello"; }
* 3. static call: [stdClass::class, 'hello']
* 4. class instance call: [ $this, 'hello' ]
* 5. invokable object: $this
* ! Since PHP 8.1
* 6. first-class callable: __return_true(...), $this->hello(...)
*/
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment