Skip to content

Commit 6cf1f7a

Browse files
committed
feat: Added hookable constructor args
1 parent aacc759 commit 6cf1f7a

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/Decorators/Hookable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@
1515
*/
1616
#[Attribute( Attribute::TARGET_CLASS )]
1717
class Hookable {
18+
/**
19+
* Arguments for the hookable class constructor
20+
*
21+
* @var array
22+
*/
23+
public array $args;
24+
1825
/**
1926
* Constructor
2027
*
2128
* @param string $hook Hook name.
2229
* @param int $priority Hook priority.
2330
* @param callable $conditional Conditional callback function.
31+
* @param mixed ...$args Arguments to pass to the hookable class constructor.
2432
*/
2533
public function __construct(
2634
/**
@@ -41,6 +49,8 @@ public function __construct(
4149
* @var callable
4250
*/
4351
public $conditional = '__return_true',
52+
mixed ...$args,
4453
) {
54+
$this->args = $args;
4555
}
4656
}

src/Traits/Accessible_Hook_Methods.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ public function __call( string $name, array $arguments ) {
2727
$should_throw = static::check_access( $this, $name );
2828

2929
if ( false !== $should_throw ) {
30-
throw new \BadMethodCallException( esc_html( $should_throw ) );
30+
throw new \BadMethodCallException( \esc_html( $should_throw ) );
3131
}
3232

33-
return is_callable( array( 'parent', '__call' ) ) ? parent::__call( $name, $arguments ) : ( $this->$name )( ...$arguments );
33+
return \is_callable( array( 'parent', '__call' ) )
34+
? parent::__call( $name, $arguments )
35+
: $this->$name( ...$arguments );
3436
}
3537

3638
/**
@@ -46,7 +48,7 @@ public static function __callStatic( string $name, array $arguments ) {
4648
$should_throw = static::check_access( static::class, $name );
4749

4850
if ( false !== $should_throw ) {
49-
throw new \BadMethodCallException( esc_html( $should_throw ) );
51+
throw new \BadMethodCallException( \esc_html( $should_throw ) );
5052
}
5153

5254
return static::$name( ...$arguments );
@@ -60,11 +62,17 @@ public static function __callStatic( string $name, array $arguments ) {
6062
* @return string|false
6163
*/
6264
private static function check_access( string|object $class_or_obj, string $method ): string|false {
63-
$classname = is_object( $class_or_obj ) ? $class_or_obj::class : $class_or_obj;
65+
$classname = \is_object( $class_or_obj ) ? $class_or_obj::class : $class_or_obj;
6466

6567
return match ( true ) {
66-
! method_exists( $class_or_obj, $method ) => 'Call to undefined method ' . $classname . '::' . $method,
67-
! static::is_valid_method( $classname, $method ) => 'Call to private method ' . $classname . '::' . $method,
68+
! \method_exists(
69+
$class_or_obj,
70+
$method,
71+
) => 'Call to undefined method ' . $classname . '::' . $method,
72+
! static::is_valid_method(
73+
$classname,
74+
$method,
75+
) => 'Call to private method ' . $classname . '::' . $method,
6876
default => false,
6977
};
7078
}
@@ -77,10 +85,10 @@ private static function check_access( string|object $class_or_obj, string $metho
7785
* @return bool
7886
*/
7987
private static function is_valid_method( string $classname, string $method ): bool {
80-
return array_reduce(
88+
return \array_reduce(
8189
static::get_registered_hooks( $classname, $method ),
82-
fn( bool $c, string $hook ) => $c || doing_action( $hook ) || doing_filter( $hook ),
83-
false
90+
static fn( bool $c, string $hook ) => $c || \doing_action( $hook ) || \doing_filter( $hook ),
91+
false,
8492
);
8593
}
8694

@@ -92,6 +100,6 @@ private static function is_valid_method( string $classname, string $method ): bo
92100
* @return array
93101
*/
94102
private static function get_registered_hooks( string $classname, string $method ): array {
95-
return array_unique( wp_list_pluck( Filter::$registry[ $classname ][ $method ] ?? array(), 'tag' ) );
103+
return \array_unique( \wp_list_pluck( Filter::$registry[ $classname ][ $method ] ?? array(), 'tag' ) );
96104
}
97105
}

src/Utils/oblak-wp-hook-utils.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
use Oblak\WP\Decorators\Base_Hook;
99

10-
use function Oblak\WP\Utils\class_uses_deep;
11-
1210
/**
1311
* Invokes all the hooked methods for a class or object.
1412
*

0 commit comments

Comments
 (0)