Skip to content

Commit

Permalink
feat: Added hookable constructor args
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Mar 16, 2024
1 parent aacc759 commit 6cf1f7a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/Decorators/Hookable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
/**
Expand All @@ -41,6 +49,8 @@ public function __construct(
* @var callable
*/
public $conditional = '__return_true',
mixed ...$args,
) {
$this->args = $args;
}
}
28 changes: 18 additions & 10 deletions src/Traits/Accessible_Hook_Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -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 );
Expand All @@ -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,
};
}
Expand All @@ -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,
);
}

Expand All @@ -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' ) );
}
}
2 changes: 0 additions & 2 deletions src/Utils/oblak-wp-hook-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 6cf1f7a

Please sign in to comment.