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
|
|
|
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 $action_type => $action_type_settings ) { |
88
|
|
|
|
89
|
|
|
$this->validator->push_field( $action_type ); |
90
|
|
|
|
91
|
|
|
$settings[ $this->slug ][ $action_type ] = $this->validate_action_type_settings( |
92
|
|
|
$action_type_settings |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->validator->pop_field(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->validator->pop_field(); |
99
|
|
|
|
100
|
|
|
return $settings; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @since 1.0.0 |
105
|
|
|
*/ |
106
|
|
|
public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) { |
107
|
|
|
|
108
|
|
|
if ( isset( $settings[ $this->slug ] ) ) { |
109
|
|
|
$reaction->update_meta( $this->slug, $settings[ $this->slug ] ); |
110
|
|
|
} else { |
111
|
|
|
$reaction->delete_meta( $this->slug ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Validate the settings for this extension for a particular action type. |
117
|
|
|
* |
118
|
|
|
* @since 1.0.0 |
119
|
|
|
* |
120
|
|
|
* @param mixed $settings The settings for a particular action type. |
121
|
|
|
* |
122
|
|
|
* @return mixed The validated settings. |
123
|
|
|
*/ |
124
|
|
|
protected function validate_action_type_settings( $settings ) { |
125
|
|
|
return $settings; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the extension settings from the fire object. |
130
|
|
|
* |
131
|
|
|
* By default the settings are stored per action type, so we offer this helper |
132
|
|
|
* method to get the settings that should be used based on the action type from |
133
|
|
|
* the fire object. |
134
|
|
|
* |
135
|
|
|
* @since 1.0.0 |
136
|
|
|
* |
137
|
|
|
* @param WordPoints_Hook_Fire $fire The hook fire object. |
138
|
|
|
* |
139
|
|
|
* @return mixed The settings for the extension, or false if none. |
140
|
|
|
*/ |
141
|
|
|
protected function get_settings_from_fire( WordPoints_Hook_Fire $fire ) { |
142
|
|
|
|
143
|
|
|
$settings = $fire->reaction->get_meta( $this->slug ); |
144
|
|
|
|
145
|
|
|
if ( ! is_array( $settings ) ) { |
146
|
|
|
return $settings; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( isset( $settings[ $fire->action_type ] ) ) { |
150
|
|
|
return $settings[ $fire->action_type ]; |
151
|
|
|
} else { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Check whether this hook firing should hit the target. |
158
|
|
|
* |
159
|
|
|
* @since 1.0.0 |
160
|
|
|
* |
161
|
|
|
* @param WordPoints_Hook_Fire $fire The hook fire object. |
162
|
|
|
* |
163
|
|
|
* @return bool Whether the target should be hit by this hook firing. |
164
|
|
|
*/ |
165
|
|
|
abstract public function should_hit( WordPoints_Hook_Fire $fire ); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the data the scripts need for the UI. |
169
|
|
|
* |
170
|
|
|
* @since 1.0.0 |
171
|
|
|
* |
172
|
|
|
* @return array Any data that needs to be present for the scripts in the UI. |
173
|
|
|
*/ |
174
|
|
|
public function get_ui_script_data() { |
175
|
|
|
return array(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// EOF |
180
|
|
|
|
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.