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
|
|
|
$this->validator = $validator; |
73
|
|
|
$this->event_args = $event_args; |
74
|
|
|
|
75
|
|
|
$this->validator->push_field( $this->slug ); |
76
|
|
|
$settings[ $this->slug ] = $this->{"validate_{$this->slug}"}( $settings[ $this->slug ] ); |
77
|
|
|
$this->validator->pop_field(); |
78
|
|
|
|
79
|
|
|
return $settings; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @since 1.0.0 |
84
|
|
|
*/ |
85
|
|
|
public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) { |
86
|
|
|
|
87
|
|
|
if ( isset( $settings[ $this->slug ] ) ) { |
88
|
|
|
$reaction->update_meta( $this->slug, $settings[ $this->slug ] ); |
89
|
|
|
} else { |
90
|
|
|
$reaction->delete_meta( $this->slug ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check whether this hook firing should hit the target. |
96
|
|
|
* |
97
|
|
|
* @since 1.0.0 |
98
|
|
|
* |
99
|
|
|
* @param WordPoints_Hook_ReactionI $reaction The reaction. |
100
|
|
|
* @param WordPoints_Hook_Event_Args $event_args The event args. |
101
|
|
|
* |
102
|
|
|
* @return bool Whether the target should be hit by this hook firing. |
103
|
|
|
*/ |
104
|
|
|
abstract public function should_hit( |
105
|
|
|
WordPoints_Hook_ReactionI $reaction, |
106
|
|
|
WordPoints_Hook_Event_Args $event_args |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* After a reaction has hit the target. |
111
|
|
|
* |
112
|
|
|
* @since 1.0.0 |
113
|
|
|
* |
114
|
|
|
* @param WordPoints_Hook_ReactionI $reaction The reaction. |
115
|
|
|
* @param WordPoints_Hook_Event_Args $event_args The event args. |
116
|
|
|
*/ |
117
|
|
|
public function after_hit( |
118
|
|
|
WordPoints_Hook_ReactionI $reaction, |
119
|
|
|
WordPoints_Hook_Event_Args $event_args |
120
|
|
|
) {} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Called after a reverse action is called. |
124
|
|
|
* |
125
|
|
|
* @since 1.0.0 |
126
|
|
|
* |
127
|
|
|
* @param string $event_slug The event slug. |
128
|
|
|
* @param WordPoints_Hook_Event_Args $event_args The event args. |
129
|
|
|
* @param WordPoints_Hook_Reactor $reactor The reactor object. |
130
|
|
|
*/ |
131
|
|
|
public function after_reverse( |
132
|
|
|
$event_slug, |
|
|
|
|
133
|
|
|
WordPoints_Hook_Event_Args $event_args, |
|
|
|
|
134
|
|
|
WordPoints_Hook_Reactor $reactor |
|
|
|
|
135
|
|
|
) {} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the data the scripts need for the UI. |
139
|
|
|
* |
140
|
|
|
* @since 1.0.0 |
141
|
|
|
* |
142
|
|
|
* @return array Any data that needs to be present for the scripts in the UI. |
143
|
|
|
*/ |
144
|
|
|
public function get_ui_script_data() { |
145
|
|
|
return array(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// EOF |
150
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.