WPB_Loader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

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

117
            /** @scrutinizer ignore-call */ 
118
            add_filter($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
Loading history...
118
        }
119
120
        foreach ($this->actions as $hook) {
121
            add_action($hook['hook'], [$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

121
            /** @scrutinizer ignore-call */ 
122
            add_action($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
Loading history...
122
        }
123
    }
124
}
125