PluginProbe
Sessions / 2.2.0
Sessions v2.2.0
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-loader.php

class-loader.php in Sessions 2.2.0, at includes/system/class-loader.php

122 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 POSessions\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 * @access private
31 * @var array $actions The actions registered with WordPress to fire when the plugin loads.
32 */
33 private $actions;
34
35 /**
36 * The array of filters registered with WordPress.
37 *
38 * @since 1.0.0
39 * @access private
40 * @var array $filters The filters registered with WordPress to fire when the plugin loads.
41 */
42 private $filters;
43
44 /**
45 * Initialize the collections used to maintain the actions and filters.
46 *
47 * @since 1.0.0
48 */
49 public function __construct() {
50 $this->actions = [];
51 $this->filters = [];
52 }
53
54 /**
55 * Add a new action to the collection to be registered with WordPress.
56 *
57 * @since 1.0.0
58 * @param string $hook The name of the WordPress action that is being registered.
59 * @param object $component A reference to the instance of the object on which the action is defined.
60 * @param string $callback The name of the function definition on the $component.
61 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
62 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
63 */
64 public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
65 $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
66 }
67
68 /**
69 * Add a new filter to the collection to be registered with WordPress.
70 *
71 * @since 1.0.0
72 * @param string $hook The name of the WordPress filter that is being registered.
73 * @param object $component A reference to the instance of the object on which the filter is defined.
74 * @param string $callback The name of the function definition on the $component.
75 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
76 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
77 */
78 public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
79 $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
80 }
81
82 /**
83 * A utility function that is used to register the actions and hooks into a single
84 * collection.
85 *
86 * @since 1.0.0
87 * @access private
88 * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
89 * @param string $hook The name of the WordPress filter that is being registered.
90 * @param object $component A reference to the instance of the object on which the filter is defined.
91 * @param string $callback The name of the function definition on the $component.
92 * @param int $priority The priority at which the function should be fired.
93 * @param int $accepted_args The number of arguments that should be passed to the $callback.
94 * @return array The collection of actions and filters registered with WordPress.
95 */
96 private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
97 $hooks[] = [
98 'hook' => $hook,
99 'component' => $component,
100 'callback' => $callback,
101 'priority' => $priority,
102 'accepted_args' => $accepted_args,
103 ];
104 return $hooks;
105 }
106
107 /**
108 * Register the filters and actions with WordPress.
109 *
110 * @since 1.0.0
111 */
112 public function run() {
113 foreach ( $this->filters as $hook ) {
114 add_filter( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
115 }
116 foreach ( $this->actions as $hook ) {
117 add_action( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
118 }
119 }
120
121 }
122