PluginProbe
Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates / 1.1.0
Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates v1.1.0
1.2.0 2.0.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9
templateberg / includes / class-templateberg-loader.php

class-templateberg-loader.php in Templateberg – Gutenberg Templates, WordPress Themes Template Kits & WordPress Templates 1.1.0, at includes/class-templateberg-loader.php

129 lines 4.8 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 * @link https://www.templateberg.com/
6 * @since 1.0.0
7 *
8 * @package Templateberg
9 */
10
11 /**
12 * Register all actions and filters for the plugin.
13 *
14 * Maintain a list of all hooks that are registered throughout
15 * the plugin, and register them with the WordPress API. Call the
16 * run function to execute the list of actions and filters.
17 *
18 * @package Templateberg
19 * @author Templateberg <info@templateberg.com>
20 */
21 class Templateberg_Loader
22 {
23
24 /**
25 * The array of actions registered with WordPress.
26 *
27 * @since 1.0.0
28 * @access protected
29 * @var array $actions The actions registered with WordPress to fire when the plugin loads.
30 */
31 protected $actions;
32
33 /**
34 * The array of filters registered with WordPress.
35 *
36 * @since 1.0.0
37 * @access protected
38 * @var array $filters The filters registered with WordPress to fire when the plugin loads.
39 */
40 protected $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 {
49
50 $this->actions = array();
51 $this->filters = array();
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 {
66 $this->actions = $this->add($this->actions, $hook, $component, $callback, $priority, $accepted_args);
67 }
68
69 /**
70 * Add a new filter to the collection to be registered with WordPress.
71 *
72 * @since 1.0.0
73 * @param string $hook The name of the WordPress filter that is being registered.
74 * @param object $component A reference to the instance of the object on which the filter is defined.
75 * @param string $callback The name of the function definition on the $component.
76 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
77 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1
78 */
79 public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1)
80 {
81 $this->filters = $this->add($this->filters, $hook, $component, $callback, $priority, $accepted_args);
82 }
83
84 /**
85 * A utility function that is used to register the actions and hooks into a single
86 * collection.
87 *
88 * @since 1.0.0
89 * @access private
90 * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
91 * @param string $hook The name of the WordPress filter that is being registered.
92 * @param object $component A reference to the instance of the object on which the filter is defined.
93 * @param string $callback The name of the function definition on the $component.
94 * @param int $priority The priority at which the function should be fired.
95 * @param int $accepted_args The number of arguments that should be passed to the $callback.
96 * @return array The collection of actions and filters registered with WordPress.
97 */
98 private function add($hooks, $hook, $component, $callback, $priority, $accepted_args)
99 {
100
101 $hooks[] = array(
102 'hook' => $hook,
103 'component' => $component,
104 'callback' => $callback,
105 'priority' => $priority,
106 'accepted_args' => $accepted_args,
107 );
108
109 return $hooks;
110 }
111
112 /**
113 * Register the filters and actions with WordPress.
114 *
115 * @since 1.0.0
116 */
117 public function run()
118 {
119
120 foreach ($this->filters as $hook) {
121 add_filter($hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args']);
122 }
123
124 foreach ($this->actions as $hook) {
125 add_action($hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args']);
126 }
127 }
128 }
129