Loader::add_action()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 5
crap 2
1
<?php
2
3
/**
4
 * Register all actions and filters for the plugin
5
 *
6
 * @link       http://example.com
7
 * @since      1.0.0
8
 *
9
 * @package    PluginName
10
 * @subpackage PluginName/includes
11
 */
12
13
namespace logoscon\WP\RedmineEmbed;
14
15
/**
16
 * Register all actions and filters for the plugin.
17
 *
18
 * Maintain a list of all hooks that are registered throughout
19
 * the plugin, and register them with the WordPress API. Call the
20
 * run function to execute the list of actions and filters.
21
 *
22
 * @package    PluginName
23
 * @subpackage PluginName/includes
24
 * @author     Your Name <[email protected]>
25
 */
26
class Loader {
27
28
	/**
29
	 * The array of actions registered with WordPress.
30
	 *
31
	 * @since    1.0.0
32
	 * @access   protected
33
	 * @var      array    $actions    The actions registered with WordPress to fire when the plugin loads.
34
	 */
35
	protected $actions;
36
37
	/**
38
	 * The array of filters registered with WordPress.
39
	 *
40
	 * @since    1.0.0
41
	 * @access   protected
42
	 * @var      array    $filters    The filters registered with WordPress to fire when the plugin loads.
43
	 */
44
	protected $filters;
45
46
	/**
47
	 * Initialize the collections used to maintain the actions and filters.
48
	 *
49
	 * @since    1.0.0
50
	 */
51
	public function __construct() {
52
53
		$this->actions = array();
54
		$this->filters = array();
55
56
	}
57
58
	/**
59
	 * Add a new action to the collection to be registered with WordPress.
60
	 *
61
	 * @since    1.0.0
62
	 * @param      string               $hook             The name of the WordPress action that is being registered.
63
	 * @param      object               $component        A reference to the instance of the object on which the action is defined.
64
	 * @param      string               $callback         The name of the function definition on the $component.
65
	 * @param      int      Optional    $priority         The priority at which the function should be fired.
66
	 * @param      int      Optional    $accepted_args    The number of arguments that should be passed to the $callback.
67
	 */
68
	public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
69
		$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
70
	}
71
72
	/**
73
	 * Add a new filter to the collection to be registered with WordPress.
74
	 *
75
	 * @since    1.0.0
76
	 * @param      string               $hook             The name of the WordPress filter that is being registered.
77
	 * @param      object               $component        A reference to the instance of the object on which the filter is defined.
78
	 * @param      string               $callback         The name of the function definition on the $component.
79
	 * @param      int      Optional    $priority         The priority at which the function should be fired.
80
	 * @param      int      Optional    $accepted_args    The number of arguments that should be passed to the $callback.
81
	 */
82
	public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
83
		$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
84
	}
85
86
	/**
87
	 * A utility function that is used to register the actions and hooks into a single
88
	 * collection.
89
	 *
90
	 * @since    1.0.0
91
	 * @access   private
92
	 * @param      array                $hooks            The collection of hooks that is being registered (that is, actions or filters).
93
	 * @param      string               $hook             The name of the WordPress filter that is being registered.
94
	 * @param      object               $component        A reference to the instance of the object on which the filter is defined.
95
	 * @param      string               $callback         The name of the function definition on the $component.
96
	 * @param      int      Optional    $priority         The priority at which the function should be fired.
97
	 * @param      int      Optional    $accepted_args    The number of arguments that should be passed to the $callback.
98
	 * @return   type                                   The collection of actions and filters registered with WordPress.
0 ignored issues
show
Documentation introduced by
Should the return type not be array[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
	 */
100
	private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
101
102
		$hooks[] = array(
103
			'hook'          => $hook,
104
			'component'     => $component,
105
			'callback'      => $callback,
106
			'priority'      => $priority,
107
			'accepted_args' => $accepted_args
108
		);
109
110
		return $hooks;
111
112
	}
113
114
	/**
115
	 * Register the filters and actions with WordPress.
116
	 *
117
	 * @since    1.0.0
118
	 */
119
	public function run() {
120
121 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...
122
			\add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
123
		}
124
125 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...
126
			\add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
127
		}
128
129
	}
130
131
}
132