1
|
|
|
<?php |
2
|
|
|
namespace Intraxia\Jaxion\Core; |
3
|
|
|
|
4
|
|
|
use Intraxia\Jaxion\Contract\Core\HasActions; |
5
|
|
|
use Intraxia\Jaxion\Contract\Core\HasFilters; |
6
|
|
|
use Intraxia\Jaxion\Contract\Core\HasShortcode; |
7
|
|
|
use Intraxia\Jaxion\Contract\Core\Loader as LoaderContract; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Loader |
11
|
|
|
* |
12
|
|
|
* Iterates over a service container to register its services with their respective WordPres hooks. |
13
|
|
|
* |
14
|
|
|
* @package Intraxia\Jaxion |
15
|
|
|
* @subpackage Core |
16
|
|
|
*/ |
17
|
|
|
class Loader implements LoaderContract { |
18
|
|
|
/** |
19
|
|
|
* Array of action hooks to attach. |
20
|
|
|
* |
21
|
|
|
* @var array[] |
22
|
|
|
*/ |
23
|
|
|
protected $actions = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Array of filter hooks to attach. |
27
|
|
|
* |
28
|
|
|
* @var array[] |
29
|
|
|
*/ |
30
|
|
|
protected $filters = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
6 |
|
public function run() { |
36
|
6 |
|
foreach ( $this->actions as $action ) { |
37
|
3 |
|
add_action( |
38
|
3 |
|
$action['hook'], |
39
|
3 |
|
array( $action['service'], $action['method'] ), |
40
|
3 |
|
$action['priority'], |
41
|
3 |
|
$action['args'] |
42
|
2 |
|
); |
43
|
4 |
|
} |
44
|
|
|
|
45
|
6 |
|
foreach ( $this->filters as $filter ) { |
46
|
3 |
|
add_filter( |
47
|
3 |
|
$filter['hook'], |
48
|
3 |
|
array( $filter['service'], $filter['method'] ), |
49
|
3 |
|
$filter['priority'], |
50
|
3 |
|
$filter['args'] |
51
|
2 |
|
); |
52
|
4 |
|
} |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Register a service with the loader. |
57
|
|
|
* |
58
|
|
|
* @param mixed $service |
59
|
|
|
*/ |
60
|
|
|
public function register( $service ) { |
61
|
|
|
if ( $service instanceof HasActions ) { |
62
|
|
|
$this->register_actions( $service ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ( $service instanceof HasFilters ) { |
66
|
|
|
$this->register_filters( $service ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( $service instanceof HasShortcode ) { |
70
|
|
|
$this->register_shortcode( $service ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
* |
77
|
|
|
* @param HasActions $service |
78
|
|
|
*/ |
79
|
3 |
View Code Duplication |
public function register_actions( HasActions $service ) { |
|
|
|
|
80
|
3 |
|
foreach ( $service->action_hooks() as $action ) { |
81
|
3 |
|
$this->actions = $this->add( |
82
|
3 |
|
$this->actions, |
83
|
3 |
|
$action['hook'], |
84
|
2 |
|
$service, |
85
|
3 |
|
$action['method'], |
86
|
3 |
|
isset( $action['priority'] ) ? $action['priority'] : 10, |
87
|
3 |
|
isset( $action['args'] ) ? $action['args'] : 1 |
88
|
2 |
|
); |
89
|
2 |
|
} |
90
|
3 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
* |
95
|
|
|
* @param HasFilters $service |
96
|
|
|
*/ |
97
|
3 |
View Code Duplication |
public function register_filters( HasFilters $service ) { |
|
|
|
|
98
|
3 |
|
foreach ( $service->filter_hooks() as $filter ) { |
99
|
3 |
|
$this->filters = $this->add( |
100
|
3 |
|
$this->filters, |
101
|
3 |
|
$filter['hook'], |
102
|
2 |
|
$service, |
103
|
3 |
|
$filter['method'], |
104
|
3 |
|
isset( $filter['priority'] ) ? $filter['priority'] : 10, |
105
|
3 |
|
isset( $filter['args'] ) ? $filter['args'] : 1 |
106
|
2 |
|
); |
107
|
2 |
|
} |
108
|
3 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
* |
113
|
|
|
* @param HasShortcode $service |
114
|
|
|
*/ |
115
|
3 |
|
public function register_shortcode( HasShortcode $service ) { |
116
|
3 |
|
add_shortcode( $service->shortcode_name(), array( $service, 'do_shortcode' ) ); |
117
|
3 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Utility to register the actions and hooks into a single collection. |
121
|
|
|
* |
122
|
|
|
* @param array $hooks |
123
|
|
|
* @param string $hook |
124
|
|
|
* @param object $service |
125
|
|
|
* @param string $method |
126
|
|
|
* @param int $priority |
127
|
|
|
* @param int $accepted_args |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
6 |
|
protected function add( $hooks, $hook, $service, $method, $priority, $accepted_args ) { |
132
|
6 |
|
$hooks[] = array( |
133
|
6 |
|
'hook' => $hook, |
134
|
6 |
|
'service' => $service, |
135
|
6 |
|
'method' => $method, |
136
|
6 |
|
'priority' => $priority, |
137
|
6 |
|
'args' => $accepted_args, |
138
|
|
|
); |
139
|
|
|
|
140
|
6 |
|
return $hooks; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.