PluginProbe
Plugin Notes Plus / 1.2.1
Plugin Notes Plus v1.2.1
trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9
plugin-notes-plus / includes / class-plugin-notes-plus-loader.php

class-plugin-notes-plus-loader.php in Plugin Notes Plus 1.2.1, at includes/class-plugin-notes-plus-loader.php

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