class-code-manager-i18n.php
5 years ago
class-code-manager-loader.php
5 years ago
class-code-manager-switch.php
5 years ago
class-code-manager.php
5 years ago
class-code-manager-loader.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Code_Manager_Loader |
| 5 | * |
| 6 | * Adds and activates plugin filters and actions. |
| 7 | * |
| 8 | * @author Peter Schulz |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | class Code_Manager_Loader { |
| 12 | |
| 13 | /** |
| 14 | * Registered plugin actions |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $actions = []; |
| 19 | |
| 20 | /** |
| 21 | * Registered plugin filters |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $filters = []; |
| 26 | |
| 27 | /** |
| 28 | * Adds an action to the action array |
| 29 | * |
| 30 | * Calls method add the create array element to be added to action array. |
| 31 | * |
| 32 | * @param string $hook Action name to be registered. |
| 33 | * @param object $component Reference to object to which the action will be applied. |
| 34 | * @param string $callback Callback function: method of $component object. |
| 35 | * @param int $priority Priority of the action (default = 10). |
| 36 | * @param int $accepted_args Number of accepted arguments of callback (default = 1). |
| 37 | * |
| 38 | * @see Code_Manager_Loader::add() |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 43 | $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Create action or filter element |
| 48 | * |
| 49 | * Generic creation of an element that can be added to the action array or filter array. |
| 50 | * |
| 51 | * @param array $hooks Action or filter array to which the hook is added. |
| 52 | * @param string $hook Action or filter name to be registered. |
| 53 | * @param object $component Reference to object to which the action or filter will be applied. |
| 54 | * @param string $callback Callback function: method of $component object. |
| 55 | * @param int $priority Priority of the action or action. |
| 56 | * @param int $accepted_args Number of accepted arguments of callback. |
| 57 | * |
| 58 | * @return array |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | protected function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { |
| 62 | $hooks[] = [ |
| 63 | 'hook' => $hook, |
| 64 | 'component' => $component, |
| 65 | 'callback' => $callback, |
| 66 | 'priority' => $priority, |
| 67 | 'accepted_args' => $accepted_args, |
| 68 | ]; |
| 69 | |
| 70 | return $hooks; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Adds a filter to the filter array |
| 75 | * |
| 76 | * Calls method add the create array element to be added to filter array. |
| 77 | * |
| 78 | * @param string $hook Filter name to be registered. |
| 79 | * @param object $component Reference to object to which the filter will be applied. |
| 80 | * @param string $callback Callback function: method of $component object. |
| 81 | * @param int $priority Priority of the filter (default = 10). |
| 82 | * @param int $accepted_args Number of accepted arguments of callback (default = 1). |
| 83 | * |
| 84 | * @see WP_Data_Access_Loader::add() |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { |
| 89 | $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Registration of filters and actions |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | */ |
| 97 | public function run() { |
| 98 | foreach ( $this->filters as $hook ) { |
| 99 | add_filter( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] ); |
| 100 | } |
| 101 | |
| 102 | foreach ( $this->actions as $hook ) { |
| 103 | add_action( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |