1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Points hook reactor class. |
5
|
|
|
* |
6
|
|
|
* @package wordpoints-hooks-api |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Hook reactor to award user points. |
12
|
|
|
* |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
*/ |
15
|
|
|
class WordPoints_Hook_Reactor_Points extends WordPoints_Hook_Reactor { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
protected $slug = 'points'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
protected $arg_types = 'user'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
*/ |
30
|
|
|
protected $action_types = array( 'fire', 'toggle_on', 'toggle_off' ); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
*/ |
35
|
|
|
protected $settings_fields = array( |
36
|
|
|
'description' => array( |
37
|
|
|
'type' => 'text', |
38
|
|
|
'required' => true, |
39
|
|
|
), |
40
|
|
|
'log_text' => array( |
41
|
|
|
'type' => 'text', |
42
|
|
|
'required' => true, |
43
|
|
|
), |
44
|
|
|
'points' => array( |
45
|
|
|
'default' => 0, |
46
|
|
|
'type' => 'number', |
47
|
|
|
'required' => true, |
48
|
|
|
), |
49
|
|
|
'points_type' => array( |
50
|
|
|
'default' => '', |
51
|
|
|
'type' => 'hidden', |
52
|
|
|
'required' => true, |
53
|
|
|
), |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @since 1.0.0 |
58
|
|
|
*/ |
59
|
|
|
public function get_settings_fields() { |
60
|
|
|
|
61
|
|
|
$this->settings_fields['points']['label'] = _x( 'Points', 'form label', 'wordpoints' ); |
62
|
|
|
$this->settings_fields['log_text']['label'] = _x( 'Log Text', 'form label', 'wordpoints' ); |
63
|
|
|
$this->settings_fields['description']['label'] = _x( 'Description', 'form label', 'wordpoints' ); |
64
|
|
|
|
65
|
|
|
return parent::get_settings_fields(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @since 1.0.0 |
70
|
|
|
*/ |
71
|
|
|
public function get_ui_script_data() { |
72
|
|
|
|
73
|
|
|
$data = parent::get_ui_script_data(); |
74
|
|
|
|
75
|
|
|
$data['target_label'] = __( 'Award To', 'wordpoints' ); |
76
|
|
|
$data['periods_label'] = __( 'Award each user no more than once per:', 'wordpoints' ); |
77
|
|
|
|
78
|
|
|
return $data; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @since 1.0.0 |
83
|
|
|
*/ |
84
|
|
|
public function validate_settings( |
85
|
|
|
array $settings, |
86
|
|
|
WordPoints_Hook_Reaction_Validator $validator, |
87
|
|
|
WordPoints_Hook_Event_Args $event_args |
88
|
|
|
) { |
89
|
|
|
|
90
|
|
|
if ( ! isset( $settings['points'] ) || false === wordpoints_int( $settings['points'] ) ) { |
91
|
|
|
$validator->add_error( __( 'Points must be an integer.', 'wordpoints' ), 'points' ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( ! isset( $settings['points_type'] ) || ! wordpoints_is_points_type( $settings['points_type'] ) ) { |
95
|
|
|
$validator->add_error( __( 'Invalid points type.', 'wordpoints' ), 'points_type' ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( ! isset( $settings['description'] ) ) { |
99
|
|
|
$validator->add_error( __( 'Description is required.', 'wordpoints' ), 'description' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( ! isset( $settings['log_text'] ) ) { |
103
|
|
|
$validator->add_error( __( 'Log Text is required.', 'wordpoints' ), 'log_text' ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return parent::validate_settings( $settings, $validator, $event_args ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @since 1.0.0 |
111
|
|
|
*/ |
112
|
|
|
public function update_settings( |
113
|
|
|
WordPoints_Hook_ReactionI $reaction, |
114
|
|
|
array $settings |
115
|
|
|
) { |
116
|
|
|
|
117
|
|
|
parent::update_settings( $reaction, $settings ); |
118
|
|
|
|
119
|
|
|
$reaction->update_meta( 'points', $settings['points'] ); |
120
|
|
|
$reaction->update_meta( 'points_type', $settings['points_type'] ); |
121
|
|
|
$reaction->update_meta( 'description', $settings['description'] ); |
122
|
|
|
$reaction->update_meta( 'log_text', $settings['log_text'] ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @since 1.0.0 |
127
|
|
|
*/ |
128
|
|
|
public function hit( WordPoints_Hook_Fire $fire ) { |
129
|
|
|
|
130
|
|
|
if ( 'toggle_off' === $fire->action_type ) { |
131
|
|
|
$this->reverse_hit( $fire ); |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$reaction = $fire->reaction; |
136
|
|
|
|
137
|
|
|
$target = $fire->event_args->get_from_hierarchy( |
138
|
|
|
$reaction->get_meta( 'target' ) |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
if ( ! $target instanceof WordPoints_Entity ) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$meta = array(); |
146
|
|
|
|
147
|
|
|
foreach ( $fire->event_args->get_entities() as $entity ) { |
148
|
|
|
$meta[ $entity->get_slug() ] = $entity->get_the_id(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
wordpoints_alter_points( |
152
|
|
|
$target->get_the_id() |
153
|
|
|
, $reaction->get_meta( 'points' ) |
154
|
|
|
, $reaction->get_meta( 'points_type' ) |
155
|
|
|
, $reaction->get_event_slug() |
156
|
|
|
, $meta |
157
|
|
|
, $reaction->get_meta( 'log_text' ) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @since 1.0.0 |
163
|
|
|
*/ |
164
|
|
|
public function reverse_hit( WordPoints_Hook_Fire $fire ) { |
165
|
|
|
|
166
|
|
|
$meta_queries = array( |
167
|
|
|
array( |
168
|
|
|
// This is needed for back-compat with the way the points hooks |
169
|
|
|
// reversed transactions, so we don't re-reverse them. |
170
|
|
|
'key' => 'auto_reversed', |
171
|
|
|
'compare' => 'NOT EXISTS', |
172
|
|
|
), |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
foreach ( $fire->event_args->get_entities() as $slug => $entity ) { |
176
|
|
|
|
177
|
|
|
$meta_queries[] = array( |
178
|
|
|
'key' => $slug, |
179
|
|
|
'value' => $entity->get_the_id(), |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$query = new WordPoints_Points_Logs_Query( |
184
|
|
|
array( |
185
|
|
|
'log_type' => $fire->reaction->get_event_slug(), |
186
|
|
|
'meta_query' => $meta_queries, |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$logs = $query->get(); |
191
|
|
|
|
192
|
|
|
if ( ! $logs ) { |
193
|
|
|
return; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
global $wpdb; |
197
|
|
|
|
198
|
|
|
add_filter( 'wordpoints_points_log', '__return_false' ); |
199
|
|
|
|
200
|
|
|
foreach ( $logs as $log ) { |
201
|
|
|
|
202
|
|
|
wordpoints_alter_points( |
203
|
|
|
$log->user_id |
204
|
|
|
, -$log->points |
205
|
|
|
, $log->points_type |
206
|
|
|
, "reverse-{$log->log_type}" |
207
|
|
|
, array( 'original_log_id' => $log->id ) |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
wordpoints_points_log_delete_all_metadata( $log->id ); |
211
|
|
|
|
212
|
|
|
// Now delete the log. |
213
|
|
|
$wpdb->delete( |
214
|
|
|
$wpdb->wordpoints_points_logs |
215
|
|
|
, array( 'id' => $log->id ) |
216
|
|
|
, '%d' |
217
|
|
|
); // WPCS: cache OK, cleaned by wordpoints_alter_points(). |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
remove_filter( 'wordpoints_points_log', '__return_false' ); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// EOF |
225
|
|
|
|