| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ordered WCPOS bootstrap hook installation. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS; |
| 9 |
|
| 10 |
/** Installs rows declared by Init::hook_rows(); registrars keep their own internals. */ |
| 11 |
final class Hook_Manifest { |
| 12 |
/** |
| 13 |
* Install named hooks, or invoke null-hook registrar callbacks immediately. |
| 14 |
* |
| 15 |
* @param array $rows Ordered hook rows. |
| 16 |
*/ |
| 17 |
public static function install( array $rows ): void { |
| 18 |
self::validate( $rows ); |
| 19 |
foreach ( $rows as $row ) { |
| 20 |
if ( null === $row['hook'] ) { |
| 21 |
( $row['callback'] )(); |
| 22 |
} else { |
| 23 |
// WordPress actions and filters share the same registration primitive. |
| 24 |
add_filter( $row['hook'], $row['callback'], $row['priority'], $row['args'] ); |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Require the declared metadata, without executing any callbacks. |
| 31 |
* |
| 32 |
* @param array $rows Ordered hook rows. |
| 33 |
* @throws \InvalidArgumentException When required metadata is absent or invalid. |
| 34 |
*/ |
| 35 |
public static function validate( array $rows ): void { |
| 36 |
foreach ( $rows as $index => $row ) { |
| 37 |
foreach ( array( 'hook', 'callback', 'priority', 'args', 'reason', 'phase' ) as $field ) { |
| 38 |
if ( ! array_key_exists( $field, $row ) ) { |
| 39 |
throw new \InvalidArgumentException( esc_html( 'Hook manifest row ' . $index . ' is missing ' . $field . '.' ) ); |
| 40 |
} |
| 41 |
} |
| 42 |
if ( ! \in_array( $row['phase'], array( 'pre-latch', 'sync-latched', 'post-latch' ), true ) ) { |
| 43 |
throw new \InvalidArgumentException( esc_html( 'Hook manifest row ' . $index . ' requires a valid phase.' ) ); |
| 44 |
} |
| 45 |
foreach ( array( 'priority', 'args' ) as $field ) { |
| 46 |
if ( ! \is_int( $row[ $field ] ) ) { |
| 47 |
throw new \InvalidArgumentException( esc_html( 'Hook manifest row ' . $index . ' requires an integer ' . $field . '.' ) ); |
| 48 |
} |
| 49 |
} |
| 50 |
if ( ! \is_callable( $row['callback'] ) ) { |
| 51 |
throw new \InvalidArgumentException( esc_html( 'Hook manifest row ' . $index . ' requires a callable callback.' ) ); |
| 52 |
} |
| 53 |
if ( ! \is_string( $row['reason'] ) || '' === trim( $row['reason'] ) ) { |
| 54 |
throw new \InvalidArgumentException( esc_html( 'Hook manifest row ' . $index . ' requires a non-empty reason.' ) ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|