diff --git a/src/Decorators/Hookable.php b/src/Decorators/Hookable.php index 8672140..794d3c5 100644 --- a/src/Decorators/Hookable.php +++ b/src/Decorators/Hookable.php @@ -15,12 +15,20 @@ */ #[Attribute( Attribute::TARGET_CLASS )] class Hookable { + /** + * Arguments for the hookable class constructor + * + * @var array + */ + public array $args; + /** * Constructor * * @param string $hook Hook name. * @param int $priority Hook priority. * @param callable $conditional Conditional callback function. + * @param mixed ...$args Arguments to pass to the hookable class constructor. */ public function __construct( /** @@ -41,6 +49,8 @@ public function __construct( * @var callable */ public $conditional = '__return_true', + mixed ...$args, ) { + $this->args = $args; } } diff --git a/src/Traits/Accessible_Hook_Methods.php b/src/Traits/Accessible_Hook_Methods.php index e222d7a..19b06c1 100644 --- a/src/Traits/Accessible_Hook_Methods.php +++ b/src/Traits/Accessible_Hook_Methods.php @@ -27,10 +27,12 @@ public function __call( string $name, array $arguments ) { $should_throw = static::check_access( $this, $name ); if ( false !== $should_throw ) { - throw new \BadMethodCallException( esc_html( $should_throw ) ); + throw new \BadMethodCallException( \esc_html( $should_throw ) ); } - return is_callable( array( 'parent', '__call' ) ) ? parent::__call( $name, $arguments ) : ( $this->$name )( ...$arguments ); + return \is_callable( array( 'parent', '__call' ) ) + ? parent::__call( $name, $arguments ) + : $this->$name( ...$arguments ); } /** @@ -46,7 +48,7 @@ public static function __callStatic( string $name, array $arguments ) { $should_throw = static::check_access( static::class, $name ); if ( false !== $should_throw ) { - throw new \BadMethodCallException( esc_html( $should_throw ) ); + throw new \BadMethodCallException( \esc_html( $should_throw ) ); } return static::$name( ...$arguments ); @@ -60,11 +62,17 @@ public static function __callStatic( string $name, array $arguments ) { * @return string|false */ private static function check_access( string|object $class_or_obj, string $method ): string|false { - $classname = is_object( $class_or_obj ) ? $class_or_obj::class : $class_or_obj; + $classname = \is_object( $class_or_obj ) ? $class_or_obj::class : $class_or_obj; return match ( true ) { - ! method_exists( $class_or_obj, $method ) => 'Call to undefined method ' . $classname . '::' . $method, - ! static::is_valid_method( $classname, $method ) => 'Call to private method ' . $classname . '::' . $method, + ! \method_exists( + $class_or_obj, + $method, + ) => 'Call to undefined method ' . $classname . '::' . $method, + ! static::is_valid_method( + $classname, + $method, + ) => 'Call to private method ' . $classname . '::' . $method, default => false, }; } @@ -77,10 +85,10 @@ private static function check_access( string|object $class_or_obj, string $metho * @return bool */ private static function is_valid_method( string $classname, string $method ): bool { - return array_reduce( + return \array_reduce( static::get_registered_hooks( $classname, $method ), - fn( bool $c, string $hook ) => $c || doing_action( $hook ) || doing_filter( $hook ), - false + static fn( bool $c, string $hook ) => $c || \doing_action( $hook ) || \doing_filter( $hook ), + false, ); } @@ -92,6 +100,6 @@ private static function is_valid_method( string $classname, string $method ): bo * @return array */ private static function get_registered_hooks( string $classname, string $method ): array { - return array_unique( wp_list_pluck( Filter::$registry[ $classname ][ $method ] ?? array(), 'tag' ) ); + return \array_unique( \wp_list_pluck( Filter::$registry[ $classname ][ $method ] ?? array(), 'tag' ) ); } } diff --git a/src/Utils/oblak-wp-hook-utils.php b/src/Utils/oblak-wp-hook-utils.php index 99f9c75..55bbb10 100644 --- a/src/Utils/oblak-wp-hook-utils.php +++ b/src/Utils/oblak-wp-hook-utils.php @@ -7,8 +7,6 @@ use Oblak\WP\Decorators\Base_Hook; -use function Oblak\WP\Utils\class_uses_deep; - /** * Invokes all the hooked methods for a class or object. *