PluginProbe
Simple CPT / trunk
Simple CPT vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1
simple-cpt / includes / class-simple-cpt-loader.php

class-simple-cpt-loader.php in Simple CPT trunk, at includes/class-simple-cpt-loader.php

151 lines 5.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 * @link https://profiles.wordpress.org/pattihis/
6 * @since 1.0.0
7 *
8 * @package Simple_Cpt
9 * @subpackage Simple_Cpt/includes
10 */
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 Simple_Cpt
20 * @subpackage Simple_Cpt/includes
21 * @author George Pattichis <gpattihis@gmail.com>
22 */
23 class Simple_Cpt_Loader {
24
25 /**
26 * The array of actions registered with WordPress.
27 *
28 * @since 1.0.0
29 * @access protected
30 * @var array $actions The actions registered with WordPress to fire when the plugin loads.
31 */
32 protected $actions;
33
34 /**
35 * The array of filters registered with WordPress.
36 *
37 * @since 1.0.0
38 * @access protected
39 * @var array $filters The filters registered with WordPress to fire when the plugin loads.
40 */
41 protected $filters;
42
43 /**
44 * The array of shortcodes registered with WordPress.
45 *
46 * @since 1.0.0
47 * @access protected
48 * @var array $shortcodes The shortcodes registered with WordPress to fire when the plugin loads.
49 */
50 protected $shortcodes;
51
52 /**
53 * Initialize the collections used to maintain the actions and filters.
54 *
55 * @since 1.0.0
56 */
57 public function __construct() {
58
59 $this->actions = array();
60 $this->filters = array();
61 $this->shortcodes = array();
62 }
63
64 /**
65 * Add a new action to the collection to be registered with WordPress.
66 *
67 * @since 1.0.0
68 * @param string $hook The name of the WordPress action that is being registered.
69 * @param object $component A reference to the instance of the object on which the action is defined.
70 * @param string $callback The name of the function definition on the $component.
71 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
72 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
73 */
74 public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
75 $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
76 }
77
78 /**
79 * Add a new filter to the collection to be registered with WordPress.
80 *
81 * @since 1.0.0
82 * @param string $hook The name of the WordPress filter that is being registered.
83 * @param object $component A reference to the instance of the object on which the filter is defined.
84 * @param string $callback The name of the function definition on the $component.
85 * @param int $priority Optional. The priority at which the function should be fired. Default is 10.
86 * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1
87 */
88 public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
89 $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
90 }
91
92 /**
93 * Add a new shortcode to the collection to be registered with WordPress
94 *
95 * @since 1.0.2
96 * @param string $tag The name of the new shortcode.
97 * @param object $component A reference to the instance of the object on which the shortcode is defined.
98 * @param string $callback The name of the function that defines the shortcode.
99 */
100 public function add_shortcode( $tag, $component, $callback, $priority = 10, $accepted_args = 2 ) {
101 $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
102 }
103
104 /**
105 * A utility function that is used to register the actions and hooks into a single
106 * collection.
107 *
108 * @since 1.0.0
109 * @access private
110 * @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
111 * @param string $hook The name of the WordPress filter that is being registered.
112 * @param object $component A reference to the instance of the object on which the filter is defined.
113 * @param string $callback The name of the function definition on the $component.
114 * @param int $priority The priority at which the function should be fired.
115 * @param int $accepted_args The number of arguments that should be passed to the $callback.
116 * @return array The collection of actions and filters registered with WordPress.
117 */
118 private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
119
120 $hooks[] = array(
121 'hook' => $hook,
122 'component' => $component,
123 'callback' => $callback,
124 'priority' => $priority,
125 'accepted_args' => $accepted_args,
126 );
127
128 return $hooks;
129 }
130
131 /**
132 * Register the filters and actions with WordPress.
133 *
134 * @since 1.0.0
135 */
136 public function run() {
137
138 foreach ( $this->filters as $hook ) {
139 add_filter( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
140 }
141
142 foreach ( $this->actions as $hook ) {
143 add_action( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
144 }
145
146 foreach ( $this->shortcodes as $hook ) {
147 add_shortcode( $hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'], $hook['accepted_args'] );
148 }
149 }
150 }
151