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