Completed
Push — master ( 353fd1...eae0f6 )
by Benjamin
14:14
created

Hook_Loader::add_action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
eloc 1
nc 1
nop 5
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
 * @package WPSiteMonitor
10
 * @link https://github.com/BWibrew/WP-Site-Monitor/
11
 * @author Benjamin Wibrew <[email protected]>
12
 * @since 1.0.0
13
 */
14
15
namespace WPSiteMonitor;
16
17
/**
18
 * Class Hook_Loader
19
 *
20
 * @package WPSiteMonitor
21
 */
22
class Hook_Loader {
23
24
	/**
25
	 * The array of actions registered with WordPress.
26
	 *
27
	 * @access   protected
28
	 * @var      array    $actions    The actions registered with WordPress to fire when the plugin loads.
29
	 * @since    1.0.0
30
	 */
31
	protected $actions;
32
33
	/**
34
	 * The array of filters registered with WordPress.
35
	 *
36
	 * @access   protected
37
	 * @var      array    $filters    The filters registered with WordPress to fire when the plugin loads.
38
	 * @since    1.0.0
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
		$this->actions = array();
49
		$this->filters = array();
50
	}
51
52
	/**
53
	 * Add a new action to the collection to be registered with WordPress.
54
	 *
55
	 * @param    string $hook             The name of the WordPress action that is being registered.
56
	 * @param    object $component        A reference to the instance of the object on which the action is defined.
57
	 * @param    string $callback         The name of the function definition on the $component.
58
	 * @param    int    $priority         Optional. The priority at which the function should be fired. Default is 10.
59
	 * @param    int    $accepted_args    Optional. The number of arguments that should be passed to the $callback. Default is 1.
60
	 * @since    1.0.0
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
	 * @param    string $hook             The name of the WordPress filter that is being registered.
70
	 * @param    object $component        A reference to the instance of the object on which the filter is defined.
71
	 * @param    string $callback         The name of the function definition on the $component.
72
	 * @param    int    $priority         Optional. The priority at which the function should be fired. Default is 10.
73
	 * @param    int    $accepted_args    Optional. The number of arguments that should be passed to the $callback. Default is 1.
74
	 * @since    1.0.0
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
	 * @access   private
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
	 * @since    1.0.0
93
	 */
94
	private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
95
		$hooks[] = array(
96
			'hook'          => $hook,
97
			'component'     => $component,
98
			'callback'      => $callback,
99
			'priority'      => $priority,
100
			'accepted_args' => $accepted_args,
101
		);
102
103
		return $hooks;
104
	}
105
106
	/**
107
	 * Register the filters and actions with WordPress.
108
	 *
109
	 * @since    1.0.0
110
	 */
111
	public function run() {
112 View Code Duplication
		foreach ( $this->filters as $hook ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
			add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
			/** @scrutinizer ignore-call */ 
114
   add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
Loading history...
114
		}
115
116 View Code Duplication
		foreach ( $this->actions as $hook ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
			add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
			/** @scrutinizer ignore-call */ 
118
   add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
Loading history...
118
		}
119
	}
120
}
121