PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / system / class-loader.php

class-loader.php in DecaLog 4.4.0, at includes/system/class-loader.php

119 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register all actions and filters for the plugin
4 *
5 * @package System
6 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
7 * @since 1.0.0
8 */
9
10 namespace Decalog\System;
11
12 /**
13 * Register all actions and filters for the plugin.
14 *
15 * Maintain a list of all hooks that are registered throughout
16 * the plugin, and register them with the WordPress API. Call the
17 * run function to execute the list of actions and filters.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class Loader {
24
25
26 /**
27 * The array of actions registered with WordPress.
28 *
29 * @since 1.0.0
30 * @var array $actions The actions registered with WordPress to fire when the plugin loads.
31 */
32 private $actions;
33
34 /**
35 * The array of filters registered with WordPress.
36 *
37 * @since 1.0.0
38 * @var array $filters The filters registered with WordPress to fire when the plugin loads.
39 */
40 private $filters;
41
42 /**
43 * Initialize the collections used to maintain the actions and filters.
44 *
45 * @since 1.0.0
46 */
47 public function __construct() {
48 $this->actions = [];
49 $this->filters = [];
50 }
51
52 /**
53 * Add a new action to the collection to be registered with WordPress.
54 *
55 * @since 1.0.0
56 * @param string $hook The name of the WordPress action that is being registered.
57 * @param object $component A reference to the instance of the object on which the action is defined.
58 * @param string $callback The name of the function definition on the $component.
59 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
60 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
61 */
62 public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
63 $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
64 }
65
66 /**
67 * Add a new filter to the collection to be registered with WordPress.
68 *
69 * @since 1.0.0
70 * @param string $hook The name of the WordPress filter that is being registered.
71 * @param object $component A reference to the instance of the object on which the filter is defined.
72 * @param string $callback The name of the function definition on the $component.
73 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
74 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
75 */
76 public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
77 $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
78 }
79
80 /**
81 * A utility function that is used to register the actions and hooks into a single
82 * collection.
83 *
84 * @since 1.0.0
85 * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
86 * @param string $hook The name of the WordPress filter that is being registered.
87 * @param object $component A reference to the instance of the object on which the filter is defined.
88 * @param string $callback The name of the function definition on the $component.
89 * @param int $priority The priority at which the function should be fired.
90 * @param int $accepted_args The number of arguments that should be passed to the $callback.
91 * @return array The collection of actions and filters registered with WordPress.
92 */
93 private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
94 $hooks[] = [
95 'hook' => $hook,
96 'component' => $component,
97 'callback' => $callback,
98 'priority' => $priority,
99 'accepted_args' => $accepted_args,
100 ];
101 return $hooks;
102 }
103
104 /**
105 * Register the filters and actions with WordPress.
106 *
107 * @since 1.0.0
108 */
109 public function run() {
110 foreach ( $this->filters as $hook ) {
111 add_filter( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
112 }
113 foreach ( $this->actions as $hook ) {
114 add_action( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
115 }
116 }
117
118 }
119