WordPress_Security_Txt_Loader::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

125
            /** @scrutinizer ignore-call */ 
126
            add_filter($hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'],
Loading history...
126
                        $hook['accepted_args']);
127
        }
128
129
        foreach ($this->actions as $hook) {
130
            add_action($hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'],
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

130
            /** @scrutinizer ignore-call */ 
131
            add_action($hook['hook'], [ $hook['component'], $hook['callback'] ], $hook['priority'],
Loading history...
131
                        $hook['accepted_args']);
132
        }
133
    }
134
}
135