1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Hook extension class. |
5
|
|
|
* |
6
|
|
|
* @package wordpoints-hooks-api |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Represents a hook extension. |
12
|
|
|
* |
13
|
|
|
* Hook extensions extend the basic hooks API, and can modify whether a particular |
14
|
|
|
* hook firing should hit the target. Each extension makes this decision based on |
15
|
|
|
* custom settings it offers for each reaction. |
16
|
|
|
* |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
abstract class WordPoints_Hook_Extension implements WordPoints_Hook_SettingsI { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The unique slug for identifying this extension. |
23
|
|
|
* |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $slug; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The validator for the current reaction. |
32
|
|
|
* |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
* |
35
|
|
|
* @var WordPoints_Hook_Reaction_Validator |
36
|
|
|
*/ |
37
|
|
|
protected $validator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The args for the current event. |
41
|
|
|
* |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
* |
44
|
|
|
* @var WordPoints_Hook_Event_Args |
45
|
|
|
*/ |
46
|
|
|
protected $event_args; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the slug of this extension. |
50
|
|
|
* |
51
|
|
|
* @since 1.0.0 |
52
|
|
|
* |
53
|
|
|
* @return string The extension's slug. |
54
|
|
|
*/ |
55
|
|
|
public function get_slug() { |
56
|
|
|
return $this->slug; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @since 1.0.0 |
61
|
|
|
*/ |
62
|
|
|
public function validate_settings( |
63
|
|
|
array $settings, |
64
|
|
|
WordPoints_Hook_Reaction_Validator $validator, |
65
|
|
|
WordPoints_Hook_Event_Args $event_args |
66
|
|
|
) { |
67
|
|
|
|
68
|
|
|
if ( ! isset( $settings[ $this->slug ] ) ) { |
69
|
|
|
return $settings; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
if ( ! is_array( $settings[ $this->slug ] ) ) { |
|
|
|
|
73
|
|
|
|
74
|
|
|
$validator->add_error( |
75
|
|
|
__( 'Invalid settings format.', 'wordpoints' ) |
76
|
|
|
, $this->slug |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $settings; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->validator = $validator; |
83
|
|
|
$this->event_args = $event_args; |
84
|
|
|
|
85
|
|
|
$this->validator->push_field( $this->slug ); |
86
|
|
|
|
87
|
|
|
foreach ( $settings[ $this->slug ] as $firer => $firer_settings ) { |
88
|
|
|
|
89
|
|
|
if ( ! wordpoints_hooks()->firers->is_registered( $firer ) ) { |
90
|
|
|
$this->validator->add_error( __( 'Unknown hook firer.', 'wordpoints' ), $firer ); |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->validator->push_field( $firer ); |
95
|
|
|
|
96
|
|
|
$settings[ $this->slug ][ $firer ] = $this->validate_firer_settings( |
97
|
|
|
$firer_settings |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->validator->pop_field(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->validator->pop_field(); |
104
|
|
|
|
105
|
|
|
return $settings; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @since 1.0.0 |
110
|
|
|
*/ |
111
|
|
|
public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) { |
112
|
|
|
|
113
|
|
|
if ( isset( $settings[ $this->slug ] ) ) { |
114
|
|
|
$reaction->update_meta( $this->slug, $settings[ $this->slug ] ); |
115
|
|
|
} else { |
116
|
|
|
$reaction->delete_meta( $this->slug ); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Validate the settings for this extension for a particular firer. |
122
|
|
|
* |
123
|
|
|
* @since 1.0.0 |
124
|
|
|
* |
125
|
|
|
* @param mixed $settings The settings for a particular firer. |
126
|
|
|
* |
127
|
|
|
* @return mixed The validated settings. |
128
|
|
|
*/ |
129
|
|
|
protected function validate_firer_settings( $settings ) {} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the extension settings from the fire object. |
133
|
|
|
* |
134
|
|
|
* By default the settings are stored per firer, so we offer this helper method |
135
|
|
|
* to get the settings that should be used based on the firer from the fire |
136
|
|
|
* object. |
137
|
|
|
* |
138
|
|
|
* @since 1.0.0 |
139
|
|
|
* |
140
|
|
|
* @param WordPoints_Hook_Fire $fire The hook fire object. |
141
|
|
|
* |
142
|
|
|
* @return mixed The settings for the extension, or false if none. |
143
|
|
|
*/ |
144
|
|
|
protected function get_settings_from_fire( WordPoints_Hook_Fire $fire ) { |
145
|
|
|
|
146
|
|
|
$firer_slug = $fire->firer->get_slug(); |
147
|
|
|
$settings = $fire->reaction->get_meta( $this->slug ); |
148
|
|
|
|
149
|
|
|
if ( ! is_array( $settings ) ) { |
150
|
|
|
return $settings; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( isset( $settings[ $firer_slug ] ) ) { |
154
|
|
|
return $settings[ $firer_slug ]; |
155
|
|
|
} else { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Check whether this hook firing should hit the target. |
162
|
|
|
* |
163
|
|
|
* @since 1.0.0 |
164
|
|
|
* |
165
|
|
|
* @param WordPoints_Hook_Fire $fire The hook fire object. |
166
|
|
|
* |
167
|
|
|
* @return bool Whether the target should be hit by this hook firing. |
168
|
|
|
*/ |
169
|
|
|
abstract public function should_hit( WordPoints_Hook_Fire $fire ); |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* After a reaction has hit the target. |
173
|
|
|
* |
174
|
|
|
* @since 1.0.0 |
175
|
|
|
* |
176
|
|
|
* @param WordPoints_Hook_Fire $fire The hook fire object. |
177
|
|
|
*/ |
178
|
|
|
public function after_hit( WordPoints_Hook_Fire $fire ) {} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the data the scripts need for the UI. |
182
|
|
|
* |
183
|
|
|
* @since 1.0.0 |
184
|
|
|
* |
185
|
|
|
* @return array Any data that needs to be present for the scripts in the UI. |
186
|
|
|
*/ |
187
|
|
|
public function get_ui_script_data() { |
188
|
|
|
return array(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// EOF |
193
|
|
|
|
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.