| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Loader { |
| 8 |
protected array $actions; |
| 9 |
protected array $filters; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->actions = array(); |
| 13 |
$this->filters = array(); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* @param string $hook |
| 18 |
* @param mixed $component |
| 19 |
* @param string $callback |
| 20 |
* @param int $priority |
| 21 |
* @param int $accepted_args |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function add_action( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ) { |
| 26 |
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @param string $hook |
| 31 |
* @param mixed $component |
| 32 |
* @param string $callback |
| 33 |
* @param int $priority |
| 34 |
* @param int $accepted_args |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function add_filter( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ) { |
| 39 |
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @param array $hooks |
| 44 |
* @param string $hook |
| 45 |
* @param mixed $component |
| 46 |
* @param string $callback |
| 47 |
* @param int $priority |
| 48 |
* @param int $accepted_args |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
private function add( |
| 53 |
array $hooks, |
| 54 |
string $hook, |
| 55 |
$component, |
| 56 |
string $callback, |
| 57 |
int $priority, |
| 58 |
int $accepted_args |
| 59 |
): array { |
| 60 |
$hooks[] = array( |
| 61 |
'hook' => $hook, |
| 62 |
'component' => $component, |
| 63 |
'callback' => $callback, |
| 64 |
'priority' => $priority, |
| 65 |
'accepted_args' => $accepted_args, |
| 66 |
); |
| 67 |
|
| 68 |
return $hooks; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function run(): void { |
| 75 |
foreach ( $this->filters as $hook ) { |
| 76 |
add_filter( |
| 77 |
$hook['hook'], |
| 78 |
array( $hook['component'], $hook['callback'] ), |
| 79 |
$hook['priority'], |
| 80 |
$hook['accepted_args'] |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ( $this->actions as $hook ) { |
| 85 |
add_action( |
| 86 |
$hook['hook'], |
| 87 |
array( $hook['component'], $hook['callback'] ), |
| 88 |
$hook['priority'], |
| 89 |
$hook['accepted_args'] |
| 90 |
); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|