@@ -14,702 +14,702 @@ |
||
14 | 14 | */ |
15 | 15 | class WordPoints_CubePoints_Importer extends WordPoints_Importer { |
16 | 16 | |
17 | - /** |
|
18 | - * Reversible log types indexed by the reversing log type. |
|
19 | - * |
|
20 | - * This information is used to set the `auto_reversed` and `original_log_id` |
|
21 | - * points log metadata for the imported logs. |
|
22 | - * |
|
23 | - * @since 1.2.0 |
|
24 | - * |
|
25 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $reversible_log_types = array( |
|
30 | - 'comment_remove' => 'comment', |
|
31 | - 'post_comment_remove' => 'post_comment', |
|
32 | - ); |
|
33 | - |
|
34 | - /** |
|
35 | - * Primary entity slugs for each imported log type. |
|
36 | - * |
|
37 | - * These are points log meta keys under which to save the entity ID in the |
|
38 | - * CubePoints `data` log field. |
|
39 | - * |
|
40 | - * @since 1.2.0 |
|
41 | - * |
|
42 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $log_type_entities = array( |
|
47 | - 'comment' => 'comment', |
|
48 | - 'comment_remove' => 'comment', |
|
49 | - 'post' => 'post', |
|
50 | - 'post_comment' => 'comment', |
|
51 | - 'post_comment_remove' => 'comment', |
|
52 | - 'register' => 'user', |
|
53 | - ); |
|
54 | - |
|
55 | - /** |
|
56 | - * IDs of reversible logs, indexed by CubePoints log type and object ID. |
|
57 | - * |
|
58 | - * @since 1.2.0 |
|
59 | - * |
|
60 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
61 | - * |
|
62 | - * @var int[][] |
|
63 | - */ |
|
64 | - protected $reversible_log_ids; |
|
65 | - |
|
66 | - /** |
|
67 | - * @since 1.0.0 |
|
68 | - */ |
|
69 | - public function __construct( $name ) { |
|
70 | - |
|
71 | - parent::__construct( $name ); |
|
72 | - |
|
73 | - $this->components = array( |
|
74 | - 'points' => array( |
|
75 | - 'excluded_users' => array( |
|
76 | - 'label' => __( 'Excluded users', 'wordpoints-importer' ), |
|
77 | - 'function' => array( $this, 'import_excluded_users' ), |
|
78 | - ), |
|
79 | - 'settings' => array( |
|
80 | - 'label' => __( 'Points Reactions', 'wordpoints-importer' ), |
|
81 | - 'description' => __( 'If checked, the settings for the number of points to award for posts, comments, etc. are imported.', 'wordpoints-importer' ), |
|
82 | - 'function' => array( $this, 'import_points_settings' ), |
|
83 | - ), |
|
84 | - 'user_points' => array( |
|
85 | - 'label' => __( 'User points', 'wordpoints-importer' ), |
|
86 | - 'function' => array( $this, 'import_user_points' ), |
|
87 | - ), |
|
88 | - 'logs' => array( |
|
89 | - 'label' => __( 'Points logs', 'wordpoints-importer' ), |
|
90 | - 'function' => array( $this, 'import_points_logs' ), |
|
91 | - 'can_import' => array( $this, 'can_import_points_logs' ), |
|
92 | - ), |
|
93 | - ), |
|
94 | - 'ranks' => array( |
|
95 | - 'ranks' => array( |
|
96 | - 'label' => __( 'Rank settings', 'wordpoints-importer' ), |
|
97 | - 'description' => __( 'If checked, the list of ranks is imported, and users will have the correct ranks assigned to them.', 'wordpoints-importer' ), |
|
98 | - 'function' => array( $this, 'import_ranks' ), |
|
99 | - ), |
|
100 | - ), |
|
101 | - ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @since 1.0.0 |
|
106 | - */ |
|
107 | - public function is_available() { |
|
108 | - |
|
109 | - if ( ! $this->is_cubepoints_installed() ) { |
|
110 | - return new WP_Error( |
|
111 | - 'cubepoints_not_installed' |
|
112 | - , __( 'CubePoints is not installed', 'wordpoints-importer' ) |
|
113 | - ); |
|
114 | - } |
|
115 | - |
|
116 | - return true; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Check if CubePoints is installed. |
|
121 | - * |
|
122 | - * @since 1.0.0 |
|
123 | - */ |
|
124 | - public function is_cubepoints_installed() { |
|
125 | - |
|
126 | - return (bool) get_option( 'cp_db_version' ); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Check if CubePoints is active. |
|
131 | - * |
|
132 | - * @since 1.0.0 |
|
133 | - */ |
|
134 | - public function is_cubepoints_active() { |
|
135 | - |
|
136 | - return function_exists( 'cp_ready' ); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Import the excluded users. |
|
141 | - * |
|
142 | - * @since 1.0.0 |
|
143 | - */ |
|
144 | - protected function import_excluded_users() { |
|
145 | - |
|
146 | - $this->feedback->info( __( 'Importing excluded users…', 'wordpoints-importer' ) ); |
|
147 | - |
|
148 | - $excluded_users = get_option( 'cp_topfilter' ); |
|
149 | - |
|
150 | - if ( ! is_array( $excluded_users ) ) { |
|
151 | - $this->feedback->warning( __( 'No excluded users found.', 'wordpoints-importer' ) ); |
|
152 | - return; |
|
153 | - } |
|
154 | - |
|
155 | - $user_ids = array(); |
|
156 | - |
|
157 | - foreach ( $excluded_users as $user_login ) { |
|
158 | - |
|
159 | - $user = get_user_by( 'login', $user_login ); |
|
160 | - |
|
161 | - if ( $user instanceof WP_User ) { |
|
162 | - $user_ids[] = $user->ID; |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - $excluded_user_ids = wordpoints_get_maybe_network_array_option( |
|
167 | - 'wordpoints_excluded_users' |
|
168 | - ); |
|
169 | - |
|
170 | - $excluded_user_ids = array_unique( |
|
171 | - array_merge( $excluded_user_ids, $user_ids ) |
|
172 | - ); |
|
173 | - |
|
174 | - wordpoints_update_maybe_network_option( |
|
175 | - 'wordpoints_excluded_users' |
|
176 | - , $excluded_user_ids |
|
177 | - ); |
|
178 | - |
|
179 | - $this->feedback->success( |
|
180 | - sprintf( |
|
181 | - __( 'Imported %s excluded users.', 'wordpoints-importer' ) |
|
182 | - , count( $user_ids ) |
|
183 | - ) |
|
184 | - ); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Import the points settings to the points hooks. |
|
189 | - * |
|
190 | - * @since 1.1.0 |
|
191 | - * |
|
192 | - * @param array $settings The settings for the points component import. |
|
193 | - */ |
|
194 | - protected function import_points_settings( $settings ) { |
|
195 | - |
|
196 | - $this->feedback->info( __( 'Importing points reactions…', 'wordpoints-importer' ) ); |
|
197 | - |
|
198 | - $options = array( |
|
199 | - 'cp_comment_points' => array( |
|
200 | - 'event' => 'comment_leave\post', |
|
201 | - 'target' => array( 'comment\post', 'author', 'user' ), |
|
202 | - /* translators: The post type name */ |
|
203 | - 'log_text' => __( 'Comment on a %s.', 'wordpoints-importer' ), |
|
204 | - /* translators: The post type name */ |
|
205 | - 'description' => __( 'Commenting on a %s.', 'wordpoints-importer' ), |
|
206 | - 'legacy_log_type' => 'cubepoints-comment', |
|
207 | - 'legacy_meta_key' => 'comment', |
|
208 | - ), |
|
209 | - 'cp_post_points' => array( |
|
210 | - 'event' => 'post_publish\post', |
|
211 | - 'target' => array( 'post\post', 'author', 'user' ), |
|
212 | - /* translators: The post type name */ |
|
213 | - 'log_text' => __( 'Published a Post.', 'wordpoints-importer' ), |
|
214 | - /* translators: The post type name */ |
|
215 | - 'description' => __( 'Publishing a Post.', 'wordpoints-importer' ), |
|
216 | - 'legacy_log_type' => 'cubepoints-post', |
|
217 | - 'legacy_meta_key' => 'post', |
|
218 | - // CubePoints doesn't remove points when a post is deleted. |
|
219 | - 'blocker' => array( 'toggle_off' => true ), |
|
220 | - 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
221 | - ), |
|
222 | - 'cp_reg_points' => array( |
|
223 | - 'event' => 'user_register', |
|
224 | - 'log_text' => __( 'Registration.', 'wordpoints-importer' ), |
|
225 | - 'description' => __( 'Registration.', 'wordpoints-importer' ), |
|
226 | - 'legacy_log_type' => 'cubepoints-register', |
|
227 | - ), |
|
228 | - 'cp_post_author_points' => array( |
|
229 | - 'event' => 'comment_leave\post', |
|
230 | - 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
231 | - /* translators: The post type name */ |
|
232 | - 'log_text' => __( 'Received a comment on a %s.', 'wordpoints-importer' ), |
|
233 | - /* translators: The post type name */ |
|
234 | - 'description' => __( 'Receiving a comment on a %s.', 'wordpoints-importer' ), |
|
235 | - 'legacy_log_type' => 'cubepoints-post_author', |
|
236 | - 'legacy_meta_key' => 'comment', |
|
237 | - ), |
|
238 | - ); |
|
239 | - |
|
240 | - // Don't import this module setting if the module isn't active. |
|
241 | - if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'post_author_points' ) ) { |
|
242 | - unset( $options['cp_post_author_points'] ); |
|
243 | - } |
|
244 | - |
|
245 | - $imported = 0; |
|
246 | - |
|
247 | - foreach ( $options as $option => $hook_settings ) { |
|
248 | - |
|
249 | - $points = get_option( $option ); |
|
250 | - |
|
251 | - if ( wordpoints_posint( $points ) ) { |
|
252 | - |
|
253 | - $hook_settings['points'] = $points; |
|
254 | - $hook_settings['points_type'] = $settings['points_type']; |
|
255 | - |
|
256 | - if ( |
|
257 | - // The CubePoints post points were only awarded for Posts. |
|
258 | - 'post_publish\post' !== $hook_settings['event'] |
|
259 | - && strpos( $hook_settings['event'], '\post' ) |
|
260 | - ) { |
|
261 | - |
|
262 | - $post_type_slugs = get_post_types( array( 'public' => true ) ); |
|
263 | - |
|
264 | - foreach ( $post_type_slugs as $post_type_slug ) { |
|
265 | - |
|
266 | - $added = $this->add_points_hook( |
|
267 | - $this->format_settings_for_post_type( |
|
268 | - $post_type_slug |
|
269 | - , $hook_settings |
|
270 | - ) |
|
271 | - ); |
|
272 | - |
|
273 | - if ( $added ) { |
|
274 | - $imported++; |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - } else { |
|
279 | - if ( $this->add_points_hook( $hook_settings ) ) { |
|
280 | - $imported++; |
|
281 | - } |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - } // End foreach ( $options ). |
|
286 | - |
|
287 | - if ( $this->import_daily_points_hook( $settings ) ) { |
|
288 | - $imported++; |
|
289 | - } |
|
290 | - |
|
291 | - $this->feedback->success( |
|
292 | - sprintf( __( 'Imported %s points reactions.', 'wordpoints-importer' ), $imported ) |
|
293 | - ); |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Import the settings for the Daily Points module to a points hook. |
|
298 | - * |
|
299 | - * @since 1.1.0 |
|
300 | - * |
|
301 | - * @param array $settings The settings for the points component import. |
|
302 | - * |
|
303 | - * @return bool True if the settings were imported, false otherwise. |
|
304 | - */ |
|
305 | - protected function import_daily_points_hook( $settings ) { |
|
306 | - |
|
307 | - // Don't import this module setting if the module isn't active. |
|
308 | - if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'dailypoints' ) ) { |
|
309 | - return false; |
|
310 | - } |
|
311 | - |
|
312 | - $points = get_option( 'cp_module_dailypoints_points' ); |
|
313 | - $period = get_option( 'cp_module_dailypoints_time' ); |
|
314 | - |
|
315 | - if ( ! wordpoints_int( $points ) || ! wordpoints_posint( $period ) ) { |
|
316 | - return false; |
|
317 | - } |
|
318 | - |
|
319 | - return $this->add_points_hook( |
|
320 | - array( |
|
321 | - 'event' => 'user_visit', |
|
322 | - 'target' => array( 'current:user' ), |
|
323 | - 'reactor' => 'points_legacy', |
|
324 | - 'points' => $points, |
|
325 | - 'points_type' => $settings['points_type'], |
|
326 | - 'points_legacy_periods' => array( |
|
327 | - 'fire' => array( |
|
328 | - array( |
|
329 | - 'length' => $period, |
|
330 | - 'args' => array( array( 'current:user' ) ), |
|
331 | - 'relative' => true, |
|
332 | - ), |
|
333 | - ), |
|
334 | - ), |
|
335 | - 'log_text' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
336 | - 'description' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
337 | - 'points_legacy_reversals' => array(), |
|
338 | - 'legacy_log_type' => 'cubepoints-dailypoints', |
|
339 | - ) |
|
340 | - ); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Programmatically create a new instance of a points hook. |
|
345 | - * |
|
346 | - * @since 1.0.0 |
|
347 | - * @since 1.2.0 Now just accepts a single argument, $settings. |
|
348 | - * |
|
349 | - * @param array $settings The settings for this hook. |
|
350 | - * |
|
351 | - * @return bool True if added successfully, or false on failure. |
|
352 | - */ |
|
353 | - private function add_points_hook( $settings = array() ) { |
|
354 | - |
|
355 | - $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
356 | - |
|
357 | - $settings = array_merge( |
|
358 | - array( |
|
359 | - 'target' => array( 'user' ), |
|
360 | - 'reactor' => 'points_legacy', |
|
361 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
362 | - ) |
|
363 | - , $settings |
|
364 | - ); |
|
365 | - |
|
366 | - $reaction = $reaction_store->create_reaction( $settings ); |
|
367 | - |
|
368 | - if ( ! $reaction instanceof WordPoints_Hook_ReactionI ) { |
|
369 | - return false; |
|
370 | - } |
|
371 | - |
|
372 | - return true; |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Format the settings for a reaction for a particular post type. |
|
377 | - * |
|
378 | - * @since 1.2.0 |
|
379 | - * |
|
380 | - * @param string $post_type The slug of the post type to format the settings for. |
|
381 | - * @param array $settings The reaction settings. |
|
382 | - * |
|
383 | - * @return array The settings modified for this particular post type. |
|
384 | - */ |
|
385 | - protected function format_settings_for_post_type( $post_type, $settings ) { |
|
386 | - |
|
387 | - $settings['event'] = str_replace( |
|
388 | - '\post' |
|
389 | - , '\\' . $post_type |
|
390 | - , $settings['event'] |
|
391 | - ); |
|
392 | - |
|
393 | - $settings['target'] = str_replace( |
|
394 | - '\post' |
|
395 | - , '\\' . $post_type |
|
396 | - , $settings['target'] |
|
397 | - ); |
|
398 | - |
|
399 | - $labels = get_post_type_labels( get_post_type_object( $post_type ) ); |
|
400 | - |
|
401 | - $settings['log_text'] = sprintf( $settings['log_text'], $labels->singular_name ); |
|
402 | - $settings['description'] = sprintf( $settings['description'], $labels->singular_name ); |
|
403 | - |
|
404 | - return $settings; |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Import the user points. |
|
409 | - * |
|
410 | - * @since 1.0.0 |
|
411 | - * |
|
412 | - * @param array $settings The settings for the points component import. |
|
413 | - */ |
|
414 | - protected function import_user_points( $settings ) { |
|
415 | - |
|
416 | - $this->feedback->info( __( 'Importing users' points…', 'wordpoints-importer' ) ); |
|
417 | - |
|
418 | - // We don't log the import transactions. |
|
419 | - add_filter( 'wordpoints_points_log', '__return_false' ); |
|
420 | - |
|
421 | - $start = 0; |
|
422 | - |
|
423 | - // We do the import in batches. |
|
424 | - while ( $rows = $this->get_next_user_points_batch( $start ) ) { |
|
425 | - |
|
426 | - $start += count( $rows ); |
|
427 | - |
|
428 | - foreach ( $rows as $row ) { |
|
429 | - |
|
430 | - wordpoints_alter_points( |
|
431 | - $row->user_id |
|
432 | - , $row->points |
|
433 | - , $settings['points_type'] |
|
434 | - , 'cubepoints_import' // This is only for the hooks, it isn't being logged. |
|
435 | - ); |
|
436 | - } |
|
437 | - |
|
438 | - unset( $rows ); |
|
439 | - } |
|
440 | - |
|
441 | - remove_filter( 'wordpoints_points_log', '__return_false' ); |
|
442 | - |
|
443 | - $this->feedback->success( sprintf( __( 'Imported points for %s users…', 'wordpoints-importer' ), $start ) ); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Get a batch of user points to import. |
|
448 | - * |
|
449 | - * @since 1.0.0 |
|
450 | - * |
|
451 | - * @param int $start The offset number to begin counting at. |
|
452 | - * |
|
453 | - * @return object[]|false The rows, or false. |
|
454 | - */ |
|
455 | - protected function get_next_user_points_batch( $start ) { |
|
456 | - |
|
457 | - global $wpdb; |
|
458 | - |
|
459 | - $rows = $wpdb->get_results( |
|
460 | - $wpdb->prepare( |
|
461 | - " |
|
17 | + /** |
|
18 | + * Reversible log types indexed by the reversing log type. |
|
19 | + * |
|
20 | + * This information is used to set the `auto_reversed` and `original_log_id` |
|
21 | + * points log metadata for the imported logs. |
|
22 | + * |
|
23 | + * @since 1.2.0 |
|
24 | + * |
|
25 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $reversible_log_types = array( |
|
30 | + 'comment_remove' => 'comment', |
|
31 | + 'post_comment_remove' => 'post_comment', |
|
32 | + ); |
|
33 | + |
|
34 | + /** |
|
35 | + * Primary entity slugs for each imported log type. |
|
36 | + * |
|
37 | + * These are points log meta keys under which to save the entity ID in the |
|
38 | + * CubePoints `data` log field. |
|
39 | + * |
|
40 | + * @since 1.2.0 |
|
41 | + * |
|
42 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $log_type_entities = array( |
|
47 | + 'comment' => 'comment', |
|
48 | + 'comment_remove' => 'comment', |
|
49 | + 'post' => 'post', |
|
50 | + 'post_comment' => 'comment', |
|
51 | + 'post_comment_remove' => 'comment', |
|
52 | + 'register' => 'user', |
|
53 | + ); |
|
54 | + |
|
55 | + /** |
|
56 | + * IDs of reversible logs, indexed by CubePoints log type and object ID. |
|
57 | + * |
|
58 | + * @since 1.2.0 |
|
59 | + * |
|
60 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
61 | + * |
|
62 | + * @var int[][] |
|
63 | + */ |
|
64 | + protected $reversible_log_ids; |
|
65 | + |
|
66 | + /** |
|
67 | + * @since 1.0.0 |
|
68 | + */ |
|
69 | + public function __construct( $name ) { |
|
70 | + |
|
71 | + parent::__construct( $name ); |
|
72 | + |
|
73 | + $this->components = array( |
|
74 | + 'points' => array( |
|
75 | + 'excluded_users' => array( |
|
76 | + 'label' => __( 'Excluded users', 'wordpoints-importer' ), |
|
77 | + 'function' => array( $this, 'import_excluded_users' ), |
|
78 | + ), |
|
79 | + 'settings' => array( |
|
80 | + 'label' => __( 'Points Reactions', 'wordpoints-importer' ), |
|
81 | + 'description' => __( 'If checked, the settings for the number of points to award for posts, comments, etc. are imported.', 'wordpoints-importer' ), |
|
82 | + 'function' => array( $this, 'import_points_settings' ), |
|
83 | + ), |
|
84 | + 'user_points' => array( |
|
85 | + 'label' => __( 'User points', 'wordpoints-importer' ), |
|
86 | + 'function' => array( $this, 'import_user_points' ), |
|
87 | + ), |
|
88 | + 'logs' => array( |
|
89 | + 'label' => __( 'Points logs', 'wordpoints-importer' ), |
|
90 | + 'function' => array( $this, 'import_points_logs' ), |
|
91 | + 'can_import' => array( $this, 'can_import_points_logs' ), |
|
92 | + ), |
|
93 | + ), |
|
94 | + 'ranks' => array( |
|
95 | + 'ranks' => array( |
|
96 | + 'label' => __( 'Rank settings', 'wordpoints-importer' ), |
|
97 | + 'description' => __( 'If checked, the list of ranks is imported, and users will have the correct ranks assigned to them.', 'wordpoints-importer' ), |
|
98 | + 'function' => array( $this, 'import_ranks' ), |
|
99 | + ), |
|
100 | + ), |
|
101 | + ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @since 1.0.0 |
|
106 | + */ |
|
107 | + public function is_available() { |
|
108 | + |
|
109 | + if ( ! $this->is_cubepoints_installed() ) { |
|
110 | + return new WP_Error( |
|
111 | + 'cubepoints_not_installed' |
|
112 | + , __( 'CubePoints is not installed', 'wordpoints-importer' ) |
|
113 | + ); |
|
114 | + } |
|
115 | + |
|
116 | + return true; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Check if CubePoints is installed. |
|
121 | + * |
|
122 | + * @since 1.0.0 |
|
123 | + */ |
|
124 | + public function is_cubepoints_installed() { |
|
125 | + |
|
126 | + return (bool) get_option( 'cp_db_version' ); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Check if CubePoints is active. |
|
131 | + * |
|
132 | + * @since 1.0.0 |
|
133 | + */ |
|
134 | + public function is_cubepoints_active() { |
|
135 | + |
|
136 | + return function_exists( 'cp_ready' ); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Import the excluded users. |
|
141 | + * |
|
142 | + * @since 1.0.0 |
|
143 | + */ |
|
144 | + protected function import_excluded_users() { |
|
145 | + |
|
146 | + $this->feedback->info( __( 'Importing excluded users…', 'wordpoints-importer' ) ); |
|
147 | + |
|
148 | + $excluded_users = get_option( 'cp_topfilter' ); |
|
149 | + |
|
150 | + if ( ! is_array( $excluded_users ) ) { |
|
151 | + $this->feedback->warning( __( 'No excluded users found.', 'wordpoints-importer' ) ); |
|
152 | + return; |
|
153 | + } |
|
154 | + |
|
155 | + $user_ids = array(); |
|
156 | + |
|
157 | + foreach ( $excluded_users as $user_login ) { |
|
158 | + |
|
159 | + $user = get_user_by( 'login', $user_login ); |
|
160 | + |
|
161 | + if ( $user instanceof WP_User ) { |
|
162 | + $user_ids[] = $user->ID; |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + $excluded_user_ids = wordpoints_get_maybe_network_array_option( |
|
167 | + 'wordpoints_excluded_users' |
|
168 | + ); |
|
169 | + |
|
170 | + $excluded_user_ids = array_unique( |
|
171 | + array_merge( $excluded_user_ids, $user_ids ) |
|
172 | + ); |
|
173 | + |
|
174 | + wordpoints_update_maybe_network_option( |
|
175 | + 'wordpoints_excluded_users' |
|
176 | + , $excluded_user_ids |
|
177 | + ); |
|
178 | + |
|
179 | + $this->feedback->success( |
|
180 | + sprintf( |
|
181 | + __( 'Imported %s excluded users.', 'wordpoints-importer' ) |
|
182 | + , count( $user_ids ) |
|
183 | + ) |
|
184 | + ); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Import the points settings to the points hooks. |
|
189 | + * |
|
190 | + * @since 1.1.0 |
|
191 | + * |
|
192 | + * @param array $settings The settings for the points component import. |
|
193 | + */ |
|
194 | + protected function import_points_settings( $settings ) { |
|
195 | + |
|
196 | + $this->feedback->info( __( 'Importing points reactions…', 'wordpoints-importer' ) ); |
|
197 | + |
|
198 | + $options = array( |
|
199 | + 'cp_comment_points' => array( |
|
200 | + 'event' => 'comment_leave\post', |
|
201 | + 'target' => array( 'comment\post', 'author', 'user' ), |
|
202 | + /* translators: The post type name */ |
|
203 | + 'log_text' => __( 'Comment on a %s.', 'wordpoints-importer' ), |
|
204 | + /* translators: The post type name */ |
|
205 | + 'description' => __( 'Commenting on a %s.', 'wordpoints-importer' ), |
|
206 | + 'legacy_log_type' => 'cubepoints-comment', |
|
207 | + 'legacy_meta_key' => 'comment', |
|
208 | + ), |
|
209 | + 'cp_post_points' => array( |
|
210 | + 'event' => 'post_publish\post', |
|
211 | + 'target' => array( 'post\post', 'author', 'user' ), |
|
212 | + /* translators: The post type name */ |
|
213 | + 'log_text' => __( 'Published a Post.', 'wordpoints-importer' ), |
|
214 | + /* translators: The post type name */ |
|
215 | + 'description' => __( 'Publishing a Post.', 'wordpoints-importer' ), |
|
216 | + 'legacy_log_type' => 'cubepoints-post', |
|
217 | + 'legacy_meta_key' => 'post', |
|
218 | + // CubePoints doesn't remove points when a post is deleted. |
|
219 | + 'blocker' => array( 'toggle_off' => true ), |
|
220 | + 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
221 | + ), |
|
222 | + 'cp_reg_points' => array( |
|
223 | + 'event' => 'user_register', |
|
224 | + 'log_text' => __( 'Registration.', 'wordpoints-importer' ), |
|
225 | + 'description' => __( 'Registration.', 'wordpoints-importer' ), |
|
226 | + 'legacy_log_type' => 'cubepoints-register', |
|
227 | + ), |
|
228 | + 'cp_post_author_points' => array( |
|
229 | + 'event' => 'comment_leave\post', |
|
230 | + 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
231 | + /* translators: The post type name */ |
|
232 | + 'log_text' => __( 'Received a comment on a %s.', 'wordpoints-importer' ), |
|
233 | + /* translators: The post type name */ |
|
234 | + 'description' => __( 'Receiving a comment on a %s.', 'wordpoints-importer' ), |
|
235 | + 'legacy_log_type' => 'cubepoints-post_author', |
|
236 | + 'legacy_meta_key' => 'comment', |
|
237 | + ), |
|
238 | + ); |
|
239 | + |
|
240 | + // Don't import this module setting if the module isn't active. |
|
241 | + if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'post_author_points' ) ) { |
|
242 | + unset( $options['cp_post_author_points'] ); |
|
243 | + } |
|
244 | + |
|
245 | + $imported = 0; |
|
246 | + |
|
247 | + foreach ( $options as $option => $hook_settings ) { |
|
248 | + |
|
249 | + $points = get_option( $option ); |
|
250 | + |
|
251 | + if ( wordpoints_posint( $points ) ) { |
|
252 | + |
|
253 | + $hook_settings['points'] = $points; |
|
254 | + $hook_settings['points_type'] = $settings['points_type']; |
|
255 | + |
|
256 | + if ( |
|
257 | + // The CubePoints post points were only awarded for Posts. |
|
258 | + 'post_publish\post' !== $hook_settings['event'] |
|
259 | + && strpos( $hook_settings['event'], '\post' ) |
|
260 | + ) { |
|
261 | + |
|
262 | + $post_type_slugs = get_post_types( array( 'public' => true ) ); |
|
263 | + |
|
264 | + foreach ( $post_type_slugs as $post_type_slug ) { |
|
265 | + |
|
266 | + $added = $this->add_points_hook( |
|
267 | + $this->format_settings_for_post_type( |
|
268 | + $post_type_slug |
|
269 | + , $hook_settings |
|
270 | + ) |
|
271 | + ); |
|
272 | + |
|
273 | + if ( $added ) { |
|
274 | + $imported++; |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + } else { |
|
279 | + if ( $this->add_points_hook( $hook_settings ) ) { |
|
280 | + $imported++; |
|
281 | + } |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + } // End foreach ( $options ). |
|
286 | + |
|
287 | + if ( $this->import_daily_points_hook( $settings ) ) { |
|
288 | + $imported++; |
|
289 | + } |
|
290 | + |
|
291 | + $this->feedback->success( |
|
292 | + sprintf( __( 'Imported %s points reactions.', 'wordpoints-importer' ), $imported ) |
|
293 | + ); |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Import the settings for the Daily Points module to a points hook. |
|
298 | + * |
|
299 | + * @since 1.1.0 |
|
300 | + * |
|
301 | + * @param array $settings The settings for the points component import. |
|
302 | + * |
|
303 | + * @return bool True if the settings were imported, false otherwise. |
|
304 | + */ |
|
305 | + protected function import_daily_points_hook( $settings ) { |
|
306 | + |
|
307 | + // Don't import this module setting if the module isn't active. |
|
308 | + if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'dailypoints' ) ) { |
|
309 | + return false; |
|
310 | + } |
|
311 | + |
|
312 | + $points = get_option( 'cp_module_dailypoints_points' ); |
|
313 | + $period = get_option( 'cp_module_dailypoints_time' ); |
|
314 | + |
|
315 | + if ( ! wordpoints_int( $points ) || ! wordpoints_posint( $period ) ) { |
|
316 | + return false; |
|
317 | + } |
|
318 | + |
|
319 | + return $this->add_points_hook( |
|
320 | + array( |
|
321 | + 'event' => 'user_visit', |
|
322 | + 'target' => array( 'current:user' ), |
|
323 | + 'reactor' => 'points_legacy', |
|
324 | + 'points' => $points, |
|
325 | + 'points_type' => $settings['points_type'], |
|
326 | + 'points_legacy_periods' => array( |
|
327 | + 'fire' => array( |
|
328 | + array( |
|
329 | + 'length' => $period, |
|
330 | + 'args' => array( array( 'current:user' ) ), |
|
331 | + 'relative' => true, |
|
332 | + ), |
|
333 | + ), |
|
334 | + ), |
|
335 | + 'log_text' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
336 | + 'description' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
337 | + 'points_legacy_reversals' => array(), |
|
338 | + 'legacy_log_type' => 'cubepoints-dailypoints', |
|
339 | + ) |
|
340 | + ); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Programmatically create a new instance of a points hook. |
|
345 | + * |
|
346 | + * @since 1.0.0 |
|
347 | + * @since 1.2.0 Now just accepts a single argument, $settings. |
|
348 | + * |
|
349 | + * @param array $settings The settings for this hook. |
|
350 | + * |
|
351 | + * @return bool True if added successfully, or false on failure. |
|
352 | + */ |
|
353 | + private function add_points_hook( $settings = array() ) { |
|
354 | + |
|
355 | + $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
356 | + |
|
357 | + $settings = array_merge( |
|
358 | + array( |
|
359 | + 'target' => array( 'user' ), |
|
360 | + 'reactor' => 'points_legacy', |
|
361 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
362 | + ) |
|
363 | + , $settings |
|
364 | + ); |
|
365 | + |
|
366 | + $reaction = $reaction_store->create_reaction( $settings ); |
|
367 | + |
|
368 | + if ( ! $reaction instanceof WordPoints_Hook_ReactionI ) { |
|
369 | + return false; |
|
370 | + } |
|
371 | + |
|
372 | + return true; |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Format the settings for a reaction for a particular post type. |
|
377 | + * |
|
378 | + * @since 1.2.0 |
|
379 | + * |
|
380 | + * @param string $post_type The slug of the post type to format the settings for. |
|
381 | + * @param array $settings The reaction settings. |
|
382 | + * |
|
383 | + * @return array The settings modified for this particular post type. |
|
384 | + */ |
|
385 | + protected function format_settings_for_post_type( $post_type, $settings ) { |
|
386 | + |
|
387 | + $settings['event'] = str_replace( |
|
388 | + '\post' |
|
389 | + , '\\' . $post_type |
|
390 | + , $settings['event'] |
|
391 | + ); |
|
392 | + |
|
393 | + $settings['target'] = str_replace( |
|
394 | + '\post' |
|
395 | + , '\\' . $post_type |
|
396 | + , $settings['target'] |
|
397 | + ); |
|
398 | + |
|
399 | + $labels = get_post_type_labels( get_post_type_object( $post_type ) ); |
|
400 | + |
|
401 | + $settings['log_text'] = sprintf( $settings['log_text'], $labels->singular_name ); |
|
402 | + $settings['description'] = sprintf( $settings['description'], $labels->singular_name ); |
|
403 | + |
|
404 | + return $settings; |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Import the user points. |
|
409 | + * |
|
410 | + * @since 1.0.0 |
|
411 | + * |
|
412 | + * @param array $settings The settings for the points component import. |
|
413 | + */ |
|
414 | + protected function import_user_points( $settings ) { |
|
415 | + |
|
416 | + $this->feedback->info( __( 'Importing users' points…', 'wordpoints-importer' ) ); |
|
417 | + |
|
418 | + // We don't log the import transactions. |
|
419 | + add_filter( 'wordpoints_points_log', '__return_false' ); |
|
420 | + |
|
421 | + $start = 0; |
|
422 | + |
|
423 | + // We do the import in batches. |
|
424 | + while ( $rows = $this->get_next_user_points_batch( $start ) ) { |
|
425 | + |
|
426 | + $start += count( $rows ); |
|
427 | + |
|
428 | + foreach ( $rows as $row ) { |
|
429 | + |
|
430 | + wordpoints_alter_points( |
|
431 | + $row->user_id |
|
432 | + , $row->points |
|
433 | + , $settings['points_type'] |
|
434 | + , 'cubepoints_import' // This is only for the hooks, it isn't being logged. |
|
435 | + ); |
|
436 | + } |
|
437 | + |
|
438 | + unset( $rows ); |
|
439 | + } |
|
440 | + |
|
441 | + remove_filter( 'wordpoints_points_log', '__return_false' ); |
|
442 | + |
|
443 | + $this->feedback->success( sprintf( __( 'Imported points for %s users…', 'wordpoints-importer' ), $start ) ); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Get a batch of user points to import. |
|
448 | + * |
|
449 | + * @since 1.0.0 |
|
450 | + * |
|
451 | + * @param int $start The offset number to begin counting at. |
|
452 | + * |
|
453 | + * @return object[]|false The rows, or false. |
|
454 | + */ |
|
455 | + protected function get_next_user_points_batch( $start ) { |
|
456 | + |
|
457 | + global $wpdb; |
|
458 | + |
|
459 | + $rows = $wpdb->get_results( |
|
460 | + $wpdb->prepare( |
|
461 | + " |
|
462 | 462 | SELECT `user_id`, `meta_value` as points |
463 | 463 | FROM {$wpdb->usermeta} |
464 | 464 | WHERE `meta_key` = 'cpoints' |
465 | 465 | ORDER BY `umeta_id` |
466 | 466 | LIMIT %d,500 |
467 | 467 | " |
468 | - , $start |
|
469 | - ) |
|
470 | - ); // WPCS: cache OK. |
|
471 | - |
|
472 | - if ( ! is_array( $rows ) ) { |
|
473 | - return false; |
|
474 | - } |
|
475 | - |
|
476 | - return $rows; |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Check if the points logs can be imported. |
|
481 | - * |
|
482 | - * @since 1.0.0 |
|
483 | - * |
|
484 | - * @return true|WP_Error True on success or a WP_Error on failure. |
|
485 | - */ |
|
486 | - public function can_import_points_logs() { |
|
487 | - |
|
488 | - if ( ! $this->is_cubepoints_active() ) { |
|
489 | - |
|
490 | - return new WP_Error( |
|
491 | - 'wordpoints_import_points_logs_cubepoints_inactive' |
|
492 | - , __( 'CubePoints must be active.', 'wordpoints-importer' ) |
|
493 | - ); |
|
494 | - } |
|
495 | - |
|
496 | - return true; |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * Import the points logs. |
|
501 | - * |
|
502 | - * @since 1.0.0 |
|
503 | - * |
|
504 | - * @param array $settings The import settings for the points component. |
|
505 | - */ |
|
506 | - protected function import_points_logs( $settings ) { |
|
507 | - |
|
508 | - $this->feedback->info( __( 'Importing points logs…', 'wordpoints-importer' ) ); |
|
509 | - |
|
510 | - $start = 0; |
|
511 | - |
|
512 | - while ( $logs = $this->get_next_points_logs_batch( $start ) ) { |
|
513 | - |
|
514 | - $start += count( $logs ); |
|
515 | - |
|
516 | - foreach ( $logs as $log ) { |
|
517 | - |
|
518 | - $this->import_points_log( |
|
519 | - $log->uid |
|
520 | - , $log->points |
|
521 | - , $settings['points_type'] |
|
522 | - , "cubepoints-{$log->type}" |
|
523 | - , array( |
|
524 | - 'cubepoints_type' => $log->type, |
|
525 | - 'cubepoints_data' => $log->data, |
|
526 | - ) |
|
527 | - , date( 'Y-m-d H:i:s', $log->timestamp ) |
|
528 | - ); |
|
529 | - } |
|
530 | - |
|
531 | - unset( $logs ); |
|
532 | - } |
|
533 | - |
|
534 | - $this->feedback->success( sprintf( __( 'Imported %s points log entries.', 'wordpoints-importer' ), $start ) ); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Import a points log. |
|
539 | - * |
|
540 | - * @since 1.0.0 |
|
541 | - * |
|
542 | - * @param int $user_id The ID of the user the log is for. |
|
543 | - * @param int $points The number of points awarded. |
|
544 | - * @param string $points_type The points type. |
|
545 | - * @param string $log_type The log type. |
|
546 | - * @param array $meta The metadata for this log. |
|
547 | - * @param int $date The date the transaction took place. |
|
548 | - */ |
|
549 | - protected function import_points_log( $user_id, $points, $points_type, $log_type, $meta, $date ) { |
|
550 | - |
|
551 | - global $wpdb; |
|
552 | - |
|
553 | - $result = $wpdb->insert( |
|
554 | - $wpdb->wordpoints_points_logs, |
|
555 | - array( |
|
556 | - 'user_id' => $user_id, |
|
557 | - 'points' => $points, |
|
558 | - 'points_type' => $points_type, |
|
559 | - 'log_type' => $log_type, |
|
560 | - 'text' => $this->render_points_log_text( '', $user_id, $points, $points_type, $log_type, $meta ), |
|
561 | - 'date' => $date, |
|
562 | - 'site_id' => $wpdb->siteid, |
|
563 | - 'blog_id' => $wpdb->blogid, |
|
564 | - ), |
|
565 | - array( '%d', '%d', '%s', '%s', '%s', '%s', '%d', '%d' ) |
|
566 | - ); |
|
567 | - |
|
568 | - if ( false !== $result ) { |
|
569 | - |
|
570 | - $log_id = (int) $wpdb->insert_id; |
|
571 | - |
|
572 | - // Set auto_reversed and original_log_id metadata for reversed logs. |
|
573 | - foreach ( $this->reversible_log_types as $reverse_type => $type ) { |
|
574 | - |
|
575 | - if ( $meta['cubepoints_type'] === $type ) { |
|
576 | - |
|
577 | - // Save this log ID for later, in case this log was reversed. |
|
578 | - // cubepoints_data will contain the entity ID. |
|
579 | - $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] = $log_id; |
|
580 | - |
|
581 | - } elseif ( |
|
582 | - $meta['cubepoints_type'] === $reverse_type |
|
583 | - && isset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ) |
|
584 | - ) { |
|
585 | - |
|
586 | - // This log was reverses another one. Set the original log ID. |
|
587 | - $meta['original_log_id'] = $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ]; |
|
588 | - |
|
589 | - // And mark the original as auto_reversed. |
|
590 | - wordpoints_add_points_log_meta( $meta['original_log_id'], 'auto_reversed', $log_id ); |
|
591 | - |
|
592 | - // No need to keep this info anymore. |
|
593 | - unset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ); |
|
594 | - } |
|
595 | - } |
|
596 | - |
|
597 | - // Set the entity IDs to their own meta keys, for the sake of reversals. |
|
598 | - if ( isset( $this->log_type_entities[ $meta['cubepoints_type'] ] ) ) { |
|
599 | - $meta[ $this->log_type_entities[ $meta['cubepoints_type'] ] ] = $meta['cubepoints_data']; |
|
600 | - } |
|
601 | - |
|
602 | - foreach ( $meta as $meta_key => $meta_value ) { |
|
603 | - |
|
604 | - wordpoints_add_points_log_meta( $log_id, $meta_key, $meta_value ); |
|
605 | - } |
|
606 | - |
|
607 | - do_action( 'wordpoints_points_log', $user_id, $points, $points_type, $log_type, $meta, $log_id ); |
|
608 | - |
|
609 | - } // End if ( inserted successfully ). |
|
610 | - } |
|
611 | - |
|
612 | - /** |
|
613 | - * Generate the log text when importing a points log. |
|
614 | - * |
|
615 | - * @since 1.0.0 |
|
616 | - */ |
|
617 | - public function render_points_log_text( $text, $user_id, $points, $points_type, $log_type, $meta ) { |
|
618 | - |
|
619 | - ob_start(); |
|
620 | - do_action( 'cp_logs_description', $meta['cubepoints_type'], $user_id, $points, $meta['cubepoints_data'] ); |
|
621 | - return ob_get_clean(); |
|
622 | - } |
|
623 | - |
|
624 | - /** |
|
625 | - * Get the next 500 points logs from CubePoints. |
|
626 | - * |
|
627 | - * @since 1.0.0 |
|
628 | - * |
|
629 | - * @param int $start The offset number to begin counting the 500 at. |
|
630 | - * |
|
631 | - * @return object[]|false The rows from the database. |
|
632 | - */ |
|
633 | - protected function get_next_points_logs_batch( $start ) { |
|
634 | - |
|
635 | - global $wpdb; |
|
636 | - |
|
637 | - $logs = $wpdb->get_results( // WPCS: unprepared SQL OK. |
|
638 | - $wpdb->prepare( // WPCS: unprepared SQL OK. |
|
639 | - ' |
|
468 | + , $start |
|
469 | + ) |
|
470 | + ); // WPCS: cache OK. |
|
471 | + |
|
472 | + if ( ! is_array( $rows ) ) { |
|
473 | + return false; |
|
474 | + } |
|
475 | + |
|
476 | + return $rows; |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Check if the points logs can be imported. |
|
481 | + * |
|
482 | + * @since 1.0.0 |
|
483 | + * |
|
484 | + * @return true|WP_Error True on success or a WP_Error on failure. |
|
485 | + */ |
|
486 | + public function can_import_points_logs() { |
|
487 | + |
|
488 | + if ( ! $this->is_cubepoints_active() ) { |
|
489 | + |
|
490 | + return new WP_Error( |
|
491 | + 'wordpoints_import_points_logs_cubepoints_inactive' |
|
492 | + , __( 'CubePoints must be active.', 'wordpoints-importer' ) |
|
493 | + ); |
|
494 | + } |
|
495 | + |
|
496 | + return true; |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * Import the points logs. |
|
501 | + * |
|
502 | + * @since 1.0.0 |
|
503 | + * |
|
504 | + * @param array $settings The import settings for the points component. |
|
505 | + */ |
|
506 | + protected function import_points_logs( $settings ) { |
|
507 | + |
|
508 | + $this->feedback->info( __( 'Importing points logs…', 'wordpoints-importer' ) ); |
|
509 | + |
|
510 | + $start = 0; |
|
511 | + |
|
512 | + while ( $logs = $this->get_next_points_logs_batch( $start ) ) { |
|
513 | + |
|
514 | + $start += count( $logs ); |
|
515 | + |
|
516 | + foreach ( $logs as $log ) { |
|
517 | + |
|
518 | + $this->import_points_log( |
|
519 | + $log->uid |
|
520 | + , $log->points |
|
521 | + , $settings['points_type'] |
|
522 | + , "cubepoints-{$log->type}" |
|
523 | + , array( |
|
524 | + 'cubepoints_type' => $log->type, |
|
525 | + 'cubepoints_data' => $log->data, |
|
526 | + ) |
|
527 | + , date( 'Y-m-d H:i:s', $log->timestamp ) |
|
528 | + ); |
|
529 | + } |
|
530 | + |
|
531 | + unset( $logs ); |
|
532 | + } |
|
533 | + |
|
534 | + $this->feedback->success( sprintf( __( 'Imported %s points log entries.', 'wordpoints-importer' ), $start ) ); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Import a points log. |
|
539 | + * |
|
540 | + * @since 1.0.0 |
|
541 | + * |
|
542 | + * @param int $user_id The ID of the user the log is for. |
|
543 | + * @param int $points The number of points awarded. |
|
544 | + * @param string $points_type The points type. |
|
545 | + * @param string $log_type The log type. |
|
546 | + * @param array $meta The metadata for this log. |
|
547 | + * @param int $date The date the transaction took place. |
|
548 | + */ |
|
549 | + protected function import_points_log( $user_id, $points, $points_type, $log_type, $meta, $date ) { |
|
550 | + |
|
551 | + global $wpdb; |
|
552 | + |
|
553 | + $result = $wpdb->insert( |
|
554 | + $wpdb->wordpoints_points_logs, |
|
555 | + array( |
|
556 | + 'user_id' => $user_id, |
|
557 | + 'points' => $points, |
|
558 | + 'points_type' => $points_type, |
|
559 | + 'log_type' => $log_type, |
|
560 | + 'text' => $this->render_points_log_text( '', $user_id, $points, $points_type, $log_type, $meta ), |
|
561 | + 'date' => $date, |
|
562 | + 'site_id' => $wpdb->siteid, |
|
563 | + 'blog_id' => $wpdb->blogid, |
|
564 | + ), |
|
565 | + array( '%d', '%d', '%s', '%s', '%s', '%s', '%d', '%d' ) |
|
566 | + ); |
|
567 | + |
|
568 | + if ( false !== $result ) { |
|
569 | + |
|
570 | + $log_id = (int) $wpdb->insert_id; |
|
571 | + |
|
572 | + // Set auto_reversed and original_log_id metadata for reversed logs. |
|
573 | + foreach ( $this->reversible_log_types as $reverse_type => $type ) { |
|
574 | + |
|
575 | + if ( $meta['cubepoints_type'] === $type ) { |
|
576 | + |
|
577 | + // Save this log ID for later, in case this log was reversed. |
|
578 | + // cubepoints_data will contain the entity ID. |
|
579 | + $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] = $log_id; |
|
580 | + |
|
581 | + } elseif ( |
|
582 | + $meta['cubepoints_type'] === $reverse_type |
|
583 | + && isset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ) |
|
584 | + ) { |
|
585 | + |
|
586 | + // This log was reverses another one. Set the original log ID. |
|
587 | + $meta['original_log_id'] = $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ]; |
|
588 | + |
|
589 | + // And mark the original as auto_reversed. |
|
590 | + wordpoints_add_points_log_meta( $meta['original_log_id'], 'auto_reversed', $log_id ); |
|
591 | + |
|
592 | + // No need to keep this info anymore. |
|
593 | + unset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ); |
|
594 | + } |
|
595 | + } |
|
596 | + |
|
597 | + // Set the entity IDs to their own meta keys, for the sake of reversals. |
|
598 | + if ( isset( $this->log_type_entities[ $meta['cubepoints_type'] ] ) ) { |
|
599 | + $meta[ $this->log_type_entities[ $meta['cubepoints_type'] ] ] = $meta['cubepoints_data']; |
|
600 | + } |
|
601 | + |
|
602 | + foreach ( $meta as $meta_key => $meta_value ) { |
|
603 | + |
|
604 | + wordpoints_add_points_log_meta( $log_id, $meta_key, $meta_value ); |
|
605 | + } |
|
606 | + |
|
607 | + do_action( 'wordpoints_points_log', $user_id, $points, $points_type, $log_type, $meta, $log_id ); |
|
608 | + |
|
609 | + } // End if ( inserted successfully ). |
|
610 | + } |
|
611 | + |
|
612 | + /** |
|
613 | + * Generate the log text when importing a points log. |
|
614 | + * |
|
615 | + * @since 1.0.0 |
|
616 | + */ |
|
617 | + public function render_points_log_text( $text, $user_id, $points, $points_type, $log_type, $meta ) { |
|
618 | + |
|
619 | + ob_start(); |
|
620 | + do_action( 'cp_logs_description', $meta['cubepoints_type'], $user_id, $points, $meta['cubepoints_data'] ); |
|
621 | + return ob_get_clean(); |
|
622 | + } |
|
623 | + |
|
624 | + /** |
|
625 | + * Get the next 500 points logs from CubePoints. |
|
626 | + * |
|
627 | + * @since 1.0.0 |
|
628 | + * |
|
629 | + * @param int $start The offset number to begin counting the 500 at. |
|
630 | + * |
|
631 | + * @return object[]|false The rows from the database. |
|
632 | + */ |
|
633 | + protected function get_next_points_logs_batch( $start ) { |
|
634 | + |
|
635 | + global $wpdb; |
|
636 | + |
|
637 | + $logs = $wpdb->get_results( // WPCS: unprepared SQL OK. |
|
638 | + $wpdb->prepare( // WPCS: unprepared SQL OK. |
|
639 | + ' |
|
640 | 640 | SELECT * |
641 | 641 | FROM `' . CP_DB . '` |
642 | 642 | ORDER BY `id` |
643 | 643 | LIMIT %d,500 |
644 | 644 | ' |
645 | - , $start |
|
646 | - ) |
|
647 | - ); // WPCS: cache OK. |
|
645 | + , $start |
|
646 | + ) |
|
647 | + ); // WPCS: cache OK. |
|
648 | 648 | |
649 | - if ( ! is_array( $logs ) ) { |
|
650 | - $this->feedback->error( __( 'Unable to retrieve the logs from CubePoints…', 'wordpoints-importer' ) ); |
|
651 | - return false; |
|
652 | - } |
|
649 | + if ( ! is_array( $logs ) ) { |
|
650 | + $this->feedback->error( __( 'Unable to retrieve the logs from CubePoints…', 'wordpoints-importer' ) ); |
|
651 | + return false; |
|
652 | + } |
|
653 | 653 | |
654 | - return $logs; |
|
655 | - } |
|
654 | + return $logs; |
|
655 | + } |
|
656 | 656 | |
657 | - /** |
|
658 | - * Import ranks. |
|
659 | - * |
|
660 | - * @since 1.1.0 |
|
661 | - * |
|
662 | - * @param array $settings The import settings for the ranks component. |
|
663 | - */ |
|
664 | - public function import_ranks( $settings ) { |
|
657 | + /** |
|
658 | + * Import ranks. |
|
659 | + * |
|
660 | + * @since 1.1.0 |
|
661 | + * |
|
662 | + * @param array $settings The import settings for the ranks component. |
|
663 | + */ |
|
664 | + public function import_ranks( $settings ) { |
|
665 | 665 | |
666 | - $this->feedback->info( __( 'Importing ranks…', 'wordpoints-importer' ) ); |
|
666 | + $this->feedback->info( __( 'Importing ranks…', 'wordpoints-importer' ) ); |
|
667 | 667 | |
668 | - $ranks_data = get_option( 'cp_module_ranks_data' ); |
|
668 | + $ranks_data = get_option( 'cp_module_ranks_data' ); |
|
669 | 669 | |
670 | - if ( empty( $ranks_data ) || ! is_array( $ranks_data ) ) { |
|
671 | - $this->feedback->error( __( 'No ranks found.', 'wordpoints-importer' ) ); |
|
672 | - return; |
|
673 | - } |
|
670 | + if ( empty( $ranks_data ) || ! is_array( $ranks_data ) ) { |
|
671 | + $this->feedback->error( __( 'No ranks found.', 'wordpoints-importer' ) ); |
|
672 | + return; |
|
673 | + } |
|
674 | 674 | |
675 | - $i = 0; |
|
675 | + $i = 0; |
|
676 | 676 | |
677 | - // The base rank already exists, so we just update it. |
|
678 | - if ( isset( $ranks_data[0] ) ) { |
|
677 | + // The base rank already exists, so we just update it. |
|
678 | + if ( isset( $ranks_data[0] ) ) { |
|
679 | 679 | |
680 | - wordpoints_update_rank( |
|
681 | - WordPoints_Rank_Groups::get_group( $settings['rank_group'] )->get_rank( 0 ) |
|
682 | - , $ranks_data[0] |
|
683 | - , 'base' |
|
684 | - , $settings['rank_group'] |
|
685 | - , 0 |
|
686 | - ); |
|
680 | + wordpoints_update_rank( |
|
681 | + WordPoints_Rank_Groups::get_group( $settings['rank_group'] )->get_rank( 0 ) |
|
682 | + , $ranks_data[0] |
|
683 | + , 'base' |
|
684 | + , $settings['rank_group'] |
|
685 | + , 0 |
|
686 | + ); |
|
687 | 687 | |
688 | - $i++; |
|
688 | + $i++; |
|
689 | 689 | |
690 | - unset( $ranks_data[0] ); |
|
691 | - } |
|
690 | + unset( $ranks_data[0] ); |
|
691 | + } |
|
692 | 692 | |
693 | - $points_type = substr( $settings['rank_group'], strlen( 'points_type-' ) ); |
|
694 | - $rank_type = 'points-' . $points_type; |
|
693 | + $points_type = substr( $settings['rank_group'], strlen( 'points_type-' ) ); |
|
694 | + $rank_type = 'points-' . $points_type; |
|
695 | 695 | |
696 | - ksort( $ranks_data ); |
|
696 | + ksort( $ranks_data ); |
|
697 | 697 | |
698 | - foreach ( $ranks_data as $points => $rank_name ) { |
|
698 | + foreach ( $ranks_data as $points => $rank_name ) { |
|
699 | 699 | |
700 | - wordpoints_add_rank( |
|
701 | - $rank_name |
|
702 | - , $rank_type |
|
703 | - , $settings['rank_group'] |
|
704 | - , $i |
|
705 | - , array( 'points' => $points, 'points_type' => $points_type ) |
|
706 | - ); |
|
700 | + wordpoints_add_rank( |
|
701 | + $rank_name |
|
702 | + , $rank_type |
|
703 | + , $settings['rank_group'] |
|
704 | + , $i |
|
705 | + , array( 'points' => $points, 'points_type' => $points_type ) |
|
706 | + ); |
|
707 | 707 | |
708 | - $i++; |
|
709 | - } |
|
708 | + $i++; |
|
709 | + } |
|
710 | 710 | |
711 | - $this->feedback->success( sprintf( __( 'Imported %s ranks.', 'wordpoints-importer' ), $i ) ); |
|
712 | - } |
|
711 | + $this->feedback->success( sprintf( __( 'Imported %s ranks.', 'wordpoints-importer' ), $i ) ); |
|
712 | + } |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | // EOF |
@@ -14,138 +14,138 @@ discard block |
||
14 | 14 | */ |
15 | 15 | final class WordPoints_Importers { |
16 | 16 | |
17 | - // |
|
18 | - // Private Vars. |
|
19 | - // |
|
20 | - |
|
21 | - /** |
|
22 | - * The registered importers. |
|
23 | - * |
|
24 | - * @since 1.0.0 |
|
25 | - * |
|
26 | - * @type array $importers |
|
27 | - */ |
|
28 | - private static $importers = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * Whether the class has been initialized yet. |
|
32 | - * |
|
33 | - * @since 1.0.0 |
|
34 | - * |
|
35 | - * @type bool $initialized |
|
36 | - */ |
|
37 | - private static $initialized = false; |
|
38 | - |
|
39 | - // |
|
40 | - // Private Functions. |
|
41 | - // |
|
42 | - |
|
43 | - /** |
|
44 | - * Initialize the class. |
|
45 | - * |
|
46 | - * @since 1.0.0 |
|
47 | - */ |
|
48 | - private static function init() { |
|
49 | - |
|
50 | - // We do this first so we avoid infinite loops if this class is called by a |
|
51 | - // function hooked to the below action. |
|
52 | - self::$initialized = true; |
|
53 | - |
|
54 | - /** |
|
55 | - * Register importers. |
|
56 | - * |
|
57 | - * @since 1.0.0 |
|
58 | - */ |
|
59 | - do_action( 'wordpoints_register_importers' ); |
|
60 | - } |
|
61 | - |
|
62 | - // |
|
63 | - // Public Functions. |
|
64 | - // |
|
65 | - |
|
66 | - /** |
|
67 | - * Get all of the registered importers. |
|
68 | - * |
|
69 | - * @since 1.0.0 |
|
70 | - * |
|
71 | - * @return array All of the registered importers. |
|
72 | - */ |
|
73 | - public static function get() { |
|
74 | - |
|
75 | - if ( ! self::$initialized ) { |
|
76 | - self::init(); |
|
77 | - } |
|
78 | - |
|
79 | - return self::$importers; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Register an importer. |
|
84 | - * |
|
85 | - * If the importer is already registered, it will be overwritten. |
|
86 | - * |
|
87 | - * @since 1.0.0 |
|
88 | - * |
|
89 | - * @param string $slug The unique identifier for this importer. |
|
90 | - * @param array $args { |
|
91 | - * Other importer arguments. |
|
92 | - * |
|
93 | - * @type string $class The Importer class. |
|
94 | - * @type string $name The name of this importer. |
|
95 | - * } |
|
96 | - */ |
|
97 | - public static function register( $slug, array $args ) { |
|
98 | - self::$importers[ $slug ] = $args; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Deregister an importer. |
|
103 | - * |
|
104 | - * @since 1.0.0 |
|
105 | - * |
|
106 | - * @param string $slug The slug of the importer to deregister. |
|
107 | - */ |
|
108 | - public static function deregister( $slug ) { |
|
109 | - unset( self::$importers[ $slug ] ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Check if an importer is registered. |
|
114 | - * |
|
115 | - * @since 1.0.0 |
|
116 | - * |
|
117 | - * @param string $slug The slug of the importer. |
|
118 | - * |
|
119 | - * @return bool True if the importer is registered, otherwise false. |
|
120 | - */ |
|
121 | - public static function is_registered( $slug ) { |
|
122 | - |
|
123 | - if ( ! self::$initialized ) { |
|
124 | - self::init(); |
|
125 | - } |
|
126 | - |
|
127 | - return isset( self::$importers[ $slug ] ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get an instance of an importer. |
|
132 | - * |
|
133 | - * @since 1.0.0 |
|
134 | - * |
|
135 | - * @param string $slug The slug of the importer to get an instance of. |
|
136 | - * |
|
137 | - * @return WordPoints_Importer|false The importer, or false if it isn't registered. |
|
138 | - */ |
|
139 | - public static function get_importer( $slug ) { |
|
140 | - |
|
141 | - if ( ! self::is_registered( $slug ) ) { |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - $importer = self::$importers[ $slug ]; |
|
146 | - |
|
147 | - return new $importer['class']( $importer['name'] ); |
|
148 | - } |
|
17 | + // |
|
18 | + // Private Vars. |
|
19 | + // |
|
20 | + |
|
21 | + /** |
|
22 | + * The registered importers. |
|
23 | + * |
|
24 | + * @since 1.0.0 |
|
25 | + * |
|
26 | + * @type array $importers |
|
27 | + */ |
|
28 | + private static $importers = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * Whether the class has been initialized yet. |
|
32 | + * |
|
33 | + * @since 1.0.0 |
|
34 | + * |
|
35 | + * @type bool $initialized |
|
36 | + */ |
|
37 | + private static $initialized = false; |
|
38 | + |
|
39 | + // |
|
40 | + // Private Functions. |
|
41 | + // |
|
42 | + |
|
43 | + /** |
|
44 | + * Initialize the class. |
|
45 | + * |
|
46 | + * @since 1.0.0 |
|
47 | + */ |
|
48 | + private static function init() { |
|
49 | + |
|
50 | + // We do this first so we avoid infinite loops if this class is called by a |
|
51 | + // function hooked to the below action. |
|
52 | + self::$initialized = true; |
|
53 | + |
|
54 | + /** |
|
55 | + * Register importers. |
|
56 | + * |
|
57 | + * @since 1.0.0 |
|
58 | + */ |
|
59 | + do_action( 'wordpoints_register_importers' ); |
|
60 | + } |
|
61 | + |
|
62 | + // |
|
63 | + // Public Functions. |
|
64 | + // |
|
65 | + |
|
66 | + /** |
|
67 | + * Get all of the registered importers. |
|
68 | + * |
|
69 | + * @since 1.0.0 |
|
70 | + * |
|
71 | + * @return array All of the registered importers. |
|
72 | + */ |
|
73 | + public static function get() { |
|
74 | + |
|
75 | + if ( ! self::$initialized ) { |
|
76 | + self::init(); |
|
77 | + } |
|
78 | + |
|
79 | + return self::$importers; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Register an importer. |
|
84 | + * |
|
85 | + * If the importer is already registered, it will be overwritten. |
|
86 | + * |
|
87 | + * @since 1.0.0 |
|
88 | + * |
|
89 | + * @param string $slug The unique identifier for this importer. |
|
90 | + * @param array $args { |
|
91 | + * Other importer arguments. |
|
92 | + * |
|
93 | + * @type string $class The Importer class. |
|
94 | + * @type string $name The name of this importer. |
|
95 | + * } |
|
96 | + */ |
|
97 | + public static function register( $slug, array $args ) { |
|
98 | + self::$importers[ $slug ] = $args; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Deregister an importer. |
|
103 | + * |
|
104 | + * @since 1.0.0 |
|
105 | + * |
|
106 | + * @param string $slug The slug of the importer to deregister. |
|
107 | + */ |
|
108 | + public static function deregister( $slug ) { |
|
109 | + unset( self::$importers[ $slug ] ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Check if an importer is registered. |
|
114 | + * |
|
115 | + * @since 1.0.0 |
|
116 | + * |
|
117 | + * @param string $slug The slug of the importer. |
|
118 | + * |
|
119 | + * @return bool True if the importer is registered, otherwise false. |
|
120 | + */ |
|
121 | + public static function is_registered( $slug ) { |
|
122 | + |
|
123 | + if ( ! self::$initialized ) { |
|
124 | + self::init(); |
|
125 | + } |
|
126 | + |
|
127 | + return isset( self::$importers[ $slug ] ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get an instance of an importer. |
|
132 | + * |
|
133 | + * @since 1.0.0 |
|
134 | + * |
|
135 | + * @param string $slug The slug of the importer to get an instance of. |
|
136 | + * |
|
137 | + * @return WordPoints_Importer|false The importer, or false if it isn't registered. |
|
138 | + */ |
|
139 | + public static function get_importer( $slug ) { |
|
140 | + |
|
141 | + if ( ! self::is_registered( $slug ) ) { |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + $importer = self::$importers[ $slug ]; |
|
146 | + |
|
147 | + return new $importer['class']( $importer['name'] ); |
|
148 | + } |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | /** |
@@ -155,239 +155,239 @@ discard block |
||
155 | 155 | */ |
156 | 156 | abstract class WordPoints_Importer { |
157 | 157 | |
158 | - /** |
|
159 | - * The name of the importer. |
|
160 | - * |
|
161 | - * @since 1.0.0 |
|
162 | - * |
|
163 | - * @type string $name |
|
164 | - */ |
|
165 | - protected $name; |
|
166 | - |
|
167 | - /** |
|
168 | - * The components supported by this importer. |
|
169 | - * |
|
170 | - * The keys are the component slugs, the values arrays of options for importing |
|
171 | - * to that component. |
|
172 | - * |
|
173 | - * @since 1.0.0 |
|
174 | - * |
|
175 | - * @type array[] $components |
|
176 | - */ |
|
177 | - protected $components = array(); |
|
178 | - |
|
179 | - /** |
|
180 | - * The feedback provider object. |
|
181 | - * |
|
182 | - * This is only set by self::do_import(). |
|
183 | - * |
|
184 | - * @since 1.0.0 |
|
185 | - * |
|
186 | - * @type WordPoints_Importer_Feedback $feedback |
|
187 | - */ |
|
188 | - protected $feedback; |
|
189 | - |
|
190 | - /** |
|
191 | - * Check if this importer is available. |
|
192 | - * |
|
193 | - * @since 1.0.0 |
|
194 | - * |
|
195 | - * @return true|WP_Error A WP_Error if the importer is not available. |
|
196 | - */ |
|
197 | - abstract public function is_available(); |
|
198 | - |
|
199 | - /** |
|
200 | - * Construct the importer. |
|
201 | - * |
|
202 | - * @since 1.0.0 |
|
203 | - * |
|
204 | - * @param string $name The name of the importer. |
|
205 | - */ |
|
206 | - public function __construct( $name ) { |
|
207 | - |
|
208 | - $this->name = $name; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Check if this importer supports a specific component. |
|
213 | - * |
|
214 | - * @since 1.0.0 |
|
215 | - * |
|
216 | - * @param string $component The slug of a component. |
|
217 | - * |
|
218 | - * @return bool True if the component is supported, otherwise false. |
|
219 | - */ |
|
220 | - public function supports_component( $component ) { |
|
221 | - |
|
222 | - return isset( $this->components[ $component ] ); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Get the import options for a component. |
|
227 | - * |
|
228 | - * @since 1.0.0 |
|
229 | - * |
|
230 | - * @param string $component The slug of a component. |
|
231 | - * |
|
232 | - * @return array[] The options for this component. |
|
233 | - */ |
|
234 | - public function get_options_for_component( $component ) { |
|
235 | - |
|
236 | - if ( ! $this->supports_component( $component ) ) { |
|
237 | - return array(); |
|
238 | - } |
|
239 | - |
|
240 | - return $this->components[ $component ]; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Run the import. |
|
245 | - * |
|
246 | - * @since 1.0.0 |
|
247 | - * |
|
248 | - * @param array $args The settings for the import. |
|
249 | - * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
250 | - */ |
|
251 | - public function do_import( array $args, $feedback = null ) { |
|
252 | - |
|
253 | - if ( ! ( $feedback instanceof WordPoints_Importer_Feedback ) ) { |
|
254 | - $feedback = new WordPoints_Importer_Feedback; |
|
255 | - } |
|
256 | - |
|
257 | - $this->feedback = $feedback; |
|
258 | - |
|
259 | - $this->feedback->info( sprintf( __( 'Importing from %s…', 'wordpoints-importer' ), $this->name ) ); |
|
260 | - |
|
261 | - $this->no_interruptions(); |
|
262 | - |
|
263 | - foreach ( $args as $component => $options ) { |
|
264 | - $this->do_import_for_component( $component, $options ); |
|
265 | - } |
|
266 | - |
|
267 | - $this->feedback->info( __( 'Import complete.', 'wordpoints-importer' ) ); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Prevent any interruptions from occurring during the import. |
|
272 | - * |
|
273 | - * @since 1.2.1 |
|
274 | - */ |
|
275 | - protected function no_interruptions() { |
|
276 | - |
|
277 | - ignore_user_abort( true ); |
|
278 | - |
|
279 | - if ( |
|
280 | - // Back-compat with WordPoints 2.1. |
|
281 | - function_exists( 'wordpoints_is_function_disabled' ) |
|
282 | - && ! wordpoints_is_function_disabled( 'set_time_limit' ) |
|
283 | - ) { |
|
284 | - set_time_limit( 0 ); |
|
285 | - } |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Validate the import settings for a component. |
|
290 | - * |
|
291 | - * @since 1.0.0 |
|
292 | - * |
|
293 | - * @param string $component The slug of the component. |
|
294 | - * @param array $settings The settings supplied for this component. |
|
295 | - * |
|
296 | - * @return bool Whether the settings are valid. |
|
297 | - */ |
|
298 | - protected function validate_import_settings( $component, $settings ) { |
|
299 | - |
|
300 | - /** |
|
301 | - * Filter whether the settings are valid before importing. |
|
302 | - * |
|
303 | - * @since 1.0.0 |
|
304 | - * |
|
305 | - * @param bool $valid Whether the settings are valid. |
|
306 | - * @param array $settings The settings for this component. |
|
307 | - * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
308 | - */ |
|
309 | - return apply_filters( "wordpoints_import_settings_valid-{$component}", true, $settings, $this->feedback ); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Run the import for a component. |
|
314 | - * |
|
315 | - * @since 1.0.0 |
|
316 | - * |
|
317 | - * @param string $component The component to run the import for. |
|
318 | - * @param array $options The selected options of what to import. |
|
319 | - */ |
|
320 | - protected function do_import_for_component( $component, $options ) { |
|
321 | - |
|
322 | - $component_data = WordPoints_Components::instance()->get_component( |
|
323 | - $component |
|
324 | - ); |
|
325 | - |
|
326 | - if ( false === $component_data ) { |
|
327 | - $this->feedback->warning( sprintf( __( 'Skipping %s component—not installed.', 'wordpoints-importer' ), esc_html( $component ) ) ); |
|
328 | - return; |
|
329 | - } |
|
330 | - |
|
331 | - if ( true !== $this->supports_component( $component ) ) { |
|
332 | - $this->feedback->warning( sprintf( __( 'Skipping the %s component—not supported.', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
333 | - return; |
|
334 | - } |
|
335 | - |
|
336 | - $settings = array(); |
|
337 | - |
|
338 | - if ( isset( $options['_data'] ) ) { |
|
339 | - $settings = $options['_data']; |
|
340 | - unset( $options['_data'] ); |
|
341 | - } |
|
342 | - |
|
343 | - if ( empty( $options ) || ! $this->validate_import_settings( $component, $settings ) ) { |
|
344 | - return; |
|
345 | - } |
|
346 | - |
|
347 | - $this->feedback->info( sprintf( __( 'Importing data to the %s component…', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
348 | - |
|
349 | - foreach ( $options as $option => $unused ) { |
|
350 | - $this->do_import_for_option( $option, $component, $settings ); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Run the import for an option. |
|
356 | - * |
|
357 | - * The import is split up into different options which the user can select (these |
|
358 | - * are displayed to the user as checkboxes in the form). This handles the import |
|
359 | - * for each of the individual things the user has selected to import. These are |
|
360 | - * all optional, so each is just termed an import "option" here. |
|
361 | - * |
|
362 | - * @since 1.0.0 |
|
363 | - * |
|
364 | - * @param string $option An import option that has been selected. |
|
365 | - * @param string $component The component this option is for. |
|
366 | - * @param array $settings Other settings for this component. |
|
367 | - */ |
|
368 | - protected function do_import_for_option( $option, $component, $settings ) { |
|
369 | - |
|
370 | - if ( ! isset( $this->components[ $component ][ $option ] ) ) { |
|
371 | - $this->feedback->warning( sprintf( __( 'Skipping unrecognized import option “%s”…', 'wordpoints-importer' ), $option ) ); |
|
372 | - return; |
|
373 | - } |
|
374 | - |
|
375 | - $option_data = $this->components[ $component ][ $option ]; |
|
376 | - |
|
377 | - // Check if we can actually run this option. |
|
378 | - if ( isset( $option_data['can_import'] ) ) { |
|
379 | - |
|
380 | - $cant_import = call_user_func( $option_data['can_import'], $settings ); |
|
381 | - |
|
382 | - if ( is_wp_error( $cant_import ) ) { |
|
383 | - $this->feedback->warning( sprintf( __( 'Skipping importing %1$s. Reason: %2$s', 'wordpoints-importer' ), $option_data['label'], $cant_import->get_error_message() ) ); |
|
384 | - return; |
|
385 | - } |
|
386 | - } |
|
387 | - |
|
388 | - // OK, we can run the import method for this option. |
|
389 | - call_user_func( $option_data['function'], $settings ); |
|
390 | - } |
|
158 | + /** |
|
159 | + * The name of the importer. |
|
160 | + * |
|
161 | + * @since 1.0.0 |
|
162 | + * |
|
163 | + * @type string $name |
|
164 | + */ |
|
165 | + protected $name; |
|
166 | + |
|
167 | + /** |
|
168 | + * The components supported by this importer. |
|
169 | + * |
|
170 | + * The keys are the component slugs, the values arrays of options for importing |
|
171 | + * to that component. |
|
172 | + * |
|
173 | + * @since 1.0.0 |
|
174 | + * |
|
175 | + * @type array[] $components |
|
176 | + */ |
|
177 | + protected $components = array(); |
|
178 | + |
|
179 | + /** |
|
180 | + * The feedback provider object. |
|
181 | + * |
|
182 | + * This is only set by self::do_import(). |
|
183 | + * |
|
184 | + * @since 1.0.0 |
|
185 | + * |
|
186 | + * @type WordPoints_Importer_Feedback $feedback |
|
187 | + */ |
|
188 | + protected $feedback; |
|
189 | + |
|
190 | + /** |
|
191 | + * Check if this importer is available. |
|
192 | + * |
|
193 | + * @since 1.0.0 |
|
194 | + * |
|
195 | + * @return true|WP_Error A WP_Error if the importer is not available. |
|
196 | + */ |
|
197 | + abstract public function is_available(); |
|
198 | + |
|
199 | + /** |
|
200 | + * Construct the importer. |
|
201 | + * |
|
202 | + * @since 1.0.0 |
|
203 | + * |
|
204 | + * @param string $name The name of the importer. |
|
205 | + */ |
|
206 | + public function __construct( $name ) { |
|
207 | + |
|
208 | + $this->name = $name; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Check if this importer supports a specific component. |
|
213 | + * |
|
214 | + * @since 1.0.0 |
|
215 | + * |
|
216 | + * @param string $component The slug of a component. |
|
217 | + * |
|
218 | + * @return bool True if the component is supported, otherwise false. |
|
219 | + */ |
|
220 | + public function supports_component( $component ) { |
|
221 | + |
|
222 | + return isset( $this->components[ $component ] ); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Get the import options for a component. |
|
227 | + * |
|
228 | + * @since 1.0.0 |
|
229 | + * |
|
230 | + * @param string $component The slug of a component. |
|
231 | + * |
|
232 | + * @return array[] The options for this component. |
|
233 | + */ |
|
234 | + public function get_options_for_component( $component ) { |
|
235 | + |
|
236 | + if ( ! $this->supports_component( $component ) ) { |
|
237 | + return array(); |
|
238 | + } |
|
239 | + |
|
240 | + return $this->components[ $component ]; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Run the import. |
|
245 | + * |
|
246 | + * @since 1.0.0 |
|
247 | + * |
|
248 | + * @param array $args The settings for the import. |
|
249 | + * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
250 | + */ |
|
251 | + public function do_import( array $args, $feedback = null ) { |
|
252 | + |
|
253 | + if ( ! ( $feedback instanceof WordPoints_Importer_Feedback ) ) { |
|
254 | + $feedback = new WordPoints_Importer_Feedback; |
|
255 | + } |
|
256 | + |
|
257 | + $this->feedback = $feedback; |
|
258 | + |
|
259 | + $this->feedback->info( sprintf( __( 'Importing from %s…', 'wordpoints-importer' ), $this->name ) ); |
|
260 | + |
|
261 | + $this->no_interruptions(); |
|
262 | + |
|
263 | + foreach ( $args as $component => $options ) { |
|
264 | + $this->do_import_for_component( $component, $options ); |
|
265 | + } |
|
266 | + |
|
267 | + $this->feedback->info( __( 'Import complete.', 'wordpoints-importer' ) ); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Prevent any interruptions from occurring during the import. |
|
272 | + * |
|
273 | + * @since 1.2.1 |
|
274 | + */ |
|
275 | + protected function no_interruptions() { |
|
276 | + |
|
277 | + ignore_user_abort( true ); |
|
278 | + |
|
279 | + if ( |
|
280 | + // Back-compat with WordPoints 2.1. |
|
281 | + function_exists( 'wordpoints_is_function_disabled' ) |
|
282 | + && ! wordpoints_is_function_disabled( 'set_time_limit' ) |
|
283 | + ) { |
|
284 | + set_time_limit( 0 ); |
|
285 | + } |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Validate the import settings for a component. |
|
290 | + * |
|
291 | + * @since 1.0.0 |
|
292 | + * |
|
293 | + * @param string $component The slug of the component. |
|
294 | + * @param array $settings The settings supplied for this component. |
|
295 | + * |
|
296 | + * @return bool Whether the settings are valid. |
|
297 | + */ |
|
298 | + protected function validate_import_settings( $component, $settings ) { |
|
299 | + |
|
300 | + /** |
|
301 | + * Filter whether the settings are valid before importing. |
|
302 | + * |
|
303 | + * @since 1.0.0 |
|
304 | + * |
|
305 | + * @param bool $valid Whether the settings are valid. |
|
306 | + * @param array $settings The settings for this component. |
|
307 | + * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
308 | + */ |
|
309 | + return apply_filters( "wordpoints_import_settings_valid-{$component}", true, $settings, $this->feedback ); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Run the import for a component. |
|
314 | + * |
|
315 | + * @since 1.0.0 |
|
316 | + * |
|
317 | + * @param string $component The component to run the import for. |
|
318 | + * @param array $options The selected options of what to import. |
|
319 | + */ |
|
320 | + protected function do_import_for_component( $component, $options ) { |
|
321 | + |
|
322 | + $component_data = WordPoints_Components::instance()->get_component( |
|
323 | + $component |
|
324 | + ); |
|
325 | + |
|
326 | + if ( false === $component_data ) { |
|
327 | + $this->feedback->warning( sprintf( __( 'Skipping %s component—not installed.', 'wordpoints-importer' ), esc_html( $component ) ) ); |
|
328 | + return; |
|
329 | + } |
|
330 | + |
|
331 | + if ( true !== $this->supports_component( $component ) ) { |
|
332 | + $this->feedback->warning( sprintf( __( 'Skipping the %s component—not supported.', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
333 | + return; |
|
334 | + } |
|
335 | + |
|
336 | + $settings = array(); |
|
337 | + |
|
338 | + if ( isset( $options['_data'] ) ) { |
|
339 | + $settings = $options['_data']; |
|
340 | + unset( $options['_data'] ); |
|
341 | + } |
|
342 | + |
|
343 | + if ( empty( $options ) || ! $this->validate_import_settings( $component, $settings ) ) { |
|
344 | + return; |
|
345 | + } |
|
346 | + |
|
347 | + $this->feedback->info( sprintf( __( 'Importing data to the %s component…', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
348 | + |
|
349 | + foreach ( $options as $option => $unused ) { |
|
350 | + $this->do_import_for_option( $option, $component, $settings ); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Run the import for an option. |
|
356 | + * |
|
357 | + * The import is split up into different options which the user can select (these |
|
358 | + * are displayed to the user as checkboxes in the form). This handles the import |
|
359 | + * for each of the individual things the user has selected to import. These are |
|
360 | + * all optional, so each is just termed an import "option" here. |
|
361 | + * |
|
362 | + * @since 1.0.0 |
|
363 | + * |
|
364 | + * @param string $option An import option that has been selected. |
|
365 | + * @param string $component The component this option is for. |
|
366 | + * @param array $settings Other settings for this component. |
|
367 | + */ |
|
368 | + protected function do_import_for_option( $option, $component, $settings ) { |
|
369 | + |
|
370 | + if ( ! isset( $this->components[ $component ][ $option ] ) ) { |
|
371 | + $this->feedback->warning( sprintf( __( 'Skipping unrecognized import option “%s”…', 'wordpoints-importer' ), $option ) ); |
|
372 | + return; |
|
373 | + } |
|
374 | + |
|
375 | + $option_data = $this->components[ $component ][ $option ]; |
|
376 | + |
|
377 | + // Check if we can actually run this option. |
|
378 | + if ( isset( $option_data['can_import'] ) ) { |
|
379 | + |
|
380 | + $cant_import = call_user_func( $option_data['can_import'], $settings ); |
|
381 | + |
|
382 | + if ( is_wp_error( $cant_import ) ) { |
|
383 | + $this->feedback->warning( sprintf( __( 'Skipping importing %1$s. Reason: %2$s', 'wordpoints-importer' ), $option_data['label'], $cant_import->get_error_message() ) ); |
|
384 | + return; |
|
385 | + } |
|
386 | + } |
|
387 | + |
|
388 | + // OK, we can run the import method for this option. |
|
389 | + call_user_func( $option_data['function'], $settings ); |
|
390 | + } |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | // EOF |
@@ -14,23 +14,23 @@ discard block |
||
14 | 14 | */ |
15 | 15 | function wordpoints_importer_admin_menu() { |
16 | 16 | |
17 | - add_submenu_page( |
|
18 | - wordpoints_get_main_admin_menu() |
|
19 | - ,__( 'WordPoints — Import', 'wordpoints-importer' ) |
|
20 | - ,__( 'Import', 'wordpoints-importer' ) |
|
21 | - ,'manage_options' |
|
22 | - ,'wordpoints_import' |
|
23 | - ,'wordpoints_import_admin_screen' |
|
24 | - ); |
|
25 | - |
|
26 | - add_submenu_page( |
|
27 | - '_wordpoints_import' // Fake. |
|
28 | - ,__( 'WordPoints — Importing', 'wordpoints-importer' ) |
|
29 | - ,__( 'Importing', 'wordpoints-importer' ) |
|
30 | - ,'manage_options' |
|
31 | - ,'wordpoints_importing' |
|
32 | - ,'wordpoints_importing_admin_screen' |
|
33 | - ); |
|
17 | + add_submenu_page( |
|
18 | + wordpoints_get_main_admin_menu() |
|
19 | + ,__( 'WordPoints — Import', 'wordpoints-importer' ) |
|
20 | + ,__( 'Import', 'wordpoints-importer' ) |
|
21 | + ,'manage_options' |
|
22 | + ,'wordpoints_import' |
|
23 | + ,'wordpoints_import_admin_screen' |
|
24 | + ); |
|
25 | + |
|
26 | + add_submenu_page( |
|
27 | + '_wordpoints_import' // Fake. |
|
28 | + ,__( 'WordPoints — Importing', 'wordpoints-importer' ) |
|
29 | + ,__( 'Importing', 'wordpoints-importer' ) |
|
30 | + ,'manage_options' |
|
31 | + ,'wordpoints_importing' |
|
32 | + ,'wordpoints_importing_admin_screen' |
|
33 | + ); |
|
34 | 34 | } |
35 | 35 | add_action( 'admin_menu', 'wordpoints_importer_admin_menu' ); |
36 | 36 | add_action( 'network_admin_menu', 'wordpoints_importer_admin_menu' ); |
@@ -42,12 +42,12 @@ discard block |
||
42 | 42 | */ |
43 | 43 | function wordpoints_import_admin_screen() { |
44 | 44 | |
45 | - /** |
|
46 | - * The importer admin screen. |
|
47 | - * |
|
48 | - * @since 1.0.0 |
|
49 | - */ |
|
50 | - require( dirname( __FILE__ ) . '/screens/import.php' ); |
|
45 | + /** |
|
46 | + * The importer admin screen. |
|
47 | + * |
|
48 | + * @since 1.0.0 |
|
49 | + */ |
|
50 | + require( dirname( __FILE__ ) . '/screens/import.php' ); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | */ |
58 | 58 | function wordpoints_importing_admin_screen() { |
59 | 59 | |
60 | - /** |
|
61 | - * The importer admin screen. |
|
62 | - * |
|
63 | - * @since 1.0.0 |
|
64 | - */ |
|
65 | - require( dirname( __FILE__ ) . '/screens/importing.php' ); |
|
60 | + /** |
|
61 | + * The importer admin screen. |
|
62 | + * |
|
63 | + * @since 1.0.0 |
|
64 | + */ |
|
65 | + require( dirname( __FILE__ ) . '/screens/importing.php' ); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -72,15 +72,15 @@ discard block |
||
72 | 72 | */ |
73 | 73 | function wordpoints_importer_register_admin_scripts() { |
74 | 74 | |
75 | - $assets_url = wordpoints_modules_url( |
|
76 | - 'admin/assets' |
|
77 | - , dirname( dirname( __FILE__ ) ) . '/importer.php' |
|
78 | - ); |
|
75 | + $assets_url = wordpoints_modules_url( |
|
76 | + 'admin/assets' |
|
77 | + , dirname( dirname( __FILE__ ) ) . '/importer.php' |
|
78 | + ); |
|
79 | 79 | |
80 | - wp_register_style( |
|
81 | - 'wordpoints-importer-feedback' |
|
82 | - , $assets_url . '/css/feedback.css' |
|
83 | - ); |
|
80 | + wp_register_style( |
|
81 | + 'wordpoints-importer-feedback' |
|
82 | + , $assets_url . '/css/feedback.css' |
|
83 | + ); |
|
84 | 84 | } |
85 | 85 | add_action( 'init', 'wordpoints_importer_register_admin_scripts' ); |
86 | 86 | |
@@ -91,41 +91,41 @@ discard block |
||
91 | 91 | */ |
92 | 92 | function wordpoints_importer_do_import() { |
93 | 93 | |
94 | - if ( ! defined( 'IFRAME_REQUEST' ) ) { |
|
95 | - define( 'IFRAME_REQUEST', true ); |
|
96 | - } |
|
94 | + if ( ! defined( 'IFRAME_REQUEST' ) ) { |
|
95 | + define( 'IFRAME_REQUEST', true ); |
|
96 | + } |
|
97 | 97 | |
98 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
99 | - wp_die( esc_html__( 'Sorry, you are not allowed to import to WordPoints.', 'wordpoints-importer' ) ); |
|
100 | - } |
|
98 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
99 | + wp_die( esc_html__( 'Sorry, you are not allowed to import to WordPoints.', 'wordpoints-importer' ) ); |
|
100 | + } |
|
101 | 101 | |
102 | - check_admin_referer( 'wordpoints_import' ); |
|
102 | + check_admin_referer( 'wordpoints_import' ); |
|
103 | 103 | |
104 | - if ( ! isset( $_GET['importer'] ) ) { |
|
105 | - wp_die( esc_html__( 'No importer selected.', 'wordpoints-importer' ) ); |
|
106 | - } |
|
104 | + if ( ! isset( $_GET['importer'] ) ) { |
|
105 | + wp_die( esc_html__( 'No importer selected.', 'wordpoints-importer' ) ); |
|
106 | + } |
|
107 | 107 | |
108 | - $importer = WordPoints_Importers::get_importer( |
|
109 | - sanitize_key( $_GET['importer'] ) |
|
110 | - ); |
|
108 | + $importer = WordPoints_Importers::get_importer( |
|
109 | + sanitize_key( $_GET['importer'] ) |
|
110 | + ); |
|
111 | 111 | |
112 | - if ( ! ( $importer instanceof WordPoints_Importer ) ) { |
|
113 | - wp_die( esc_html__( 'Importer not installed.', 'wordpoints-importer' ) ); |
|
114 | - } |
|
112 | + if ( ! ( $importer instanceof WordPoints_Importer ) ) { |
|
113 | + wp_die( esc_html__( 'Importer not installed.', 'wordpoints-importer' ) ); |
|
114 | + } |
|
115 | 115 | |
116 | - wp_enqueue_style( 'wordpoints-importer-feedback' ); |
|
116 | + wp_enqueue_style( 'wordpoints-importer-feedback' ); |
|
117 | 117 | |
118 | - $args = array(); |
|
118 | + $args = array(); |
|
119 | 119 | |
120 | - if ( isset( $_GET['wordpoints_import'] ) && is_array( $_GET['wordpoints_import'] ) ) { |
|
121 | - $args = wp_unslash( $_GET['wordpoints_import'] ); // WPCS: sanitization OK. |
|
122 | - } |
|
120 | + if ( isset( $_GET['wordpoints_import'] ) && is_array( $_GET['wordpoints_import'] ) ) { |
|
121 | + $args = wp_unslash( $_GET['wordpoints_import'] ); // WPCS: sanitization OK. |
|
122 | + } |
|
123 | 123 | |
124 | - iframe_header(); |
|
124 | + iframe_header(); |
|
125 | 125 | |
126 | - $importer->do_import( $args ); |
|
126 | + $importer->do_import( $args ); |
|
127 | 127 | |
128 | - iframe_footer(); |
|
128 | + iframe_footer(); |
|
129 | 129 | } |
130 | 130 | add_action( 'update-custom_wordpoints_import', 'wordpoints_importer_do_import' ); |
131 | 131 | |
@@ -136,9 +136,9 @@ discard block |
||
136 | 136 | */ |
137 | 137 | function wordpoints_importer_admin_screen_points_type_select() { |
138 | 138 | |
139 | - $args = array( 'name' => 'wordpoints_import[points][_data][points_type]' ); |
|
139 | + $args = array( 'name' => 'wordpoints_import[points][_data][points_type]' ); |
|
140 | 140 | |
141 | - ?> |
|
141 | + ?> |
|
142 | 142 | |
143 | 143 | <p> |
144 | 144 | <label for="wordpoints_import[points][_data][points_type]"> |
@@ -150,8 +150,8 @@ discard block |
||
150 | 150 | <?php |
151 | 151 | } |
152 | 152 | add_action( |
153 | - 'wordpoints_importer_before_component_options-points' |
|
154 | - , 'wordpoints_importer_admin_screen_points_type_select' |
|
153 | + 'wordpoints_importer_before_component_options-points' |
|
154 | + , 'wordpoints_importer_admin_screen_points_type_select' |
|
155 | 155 | ); |
156 | 156 | |
157 | 157 | /** |
@@ -167,24 +167,24 @@ discard block |
||
167 | 167 | */ |
168 | 168 | function wordpoints_importer_validate_points_type_setting( $valid, $settings, $feedback ) { |
169 | 169 | |
170 | - if ( $valid ) { |
|
170 | + if ( $valid ) { |
|
171 | 171 | |
172 | - if ( ! isset( $settings['points_type'] ) ) { |
|
173 | - $feedback->warning( __( 'Skipping Points component—no points type specified.', 'wordpoints-importer' ) ); |
|
174 | - $valid = false; |
|
175 | - } elseif ( ! wordpoints_is_points_type( $settings['points_type'] ) ) { |
|
176 | - $feedback->warning( __( 'Skipping Points component—invalid points type selected.', 'wordpoints-importer' ) ); |
|
177 | - $valid = false; |
|
178 | - } |
|
179 | - } |
|
172 | + if ( ! isset( $settings['points_type'] ) ) { |
|
173 | + $feedback->warning( __( 'Skipping Points component—no points type specified.', 'wordpoints-importer' ) ); |
|
174 | + $valid = false; |
|
175 | + } elseif ( ! wordpoints_is_points_type( $settings['points_type'] ) ) { |
|
176 | + $feedback->warning( __( 'Skipping Points component—invalid points type selected.', 'wordpoints-importer' ) ); |
|
177 | + $valid = false; |
|
178 | + } |
|
179 | + } |
|
180 | 180 | |
181 | - return $valid; |
|
181 | + return $valid; |
|
182 | 182 | } |
183 | 183 | add_action( |
184 | - 'wordpoints_import_settings_valid-points' |
|
185 | - , 'wordpoints_importer_validate_points_type_setting' |
|
186 | - , 10 |
|
187 | - , 3 |
|
184 | + 'wordpoints_import_settings_valid-points' |
|
185 | + , 'wordpoints_importer_validate_points_type_setting' |
|
186 | + , 10 |
|
187 | + , 3 |
|
188 | 188 | ); |
189 | 189 | |
190 | 190 | /** |
@@ -194,20 +194,20 @@ discard block |
||
194 | 194 | */ |
195 | 195 | function wordpoints_importer_admin_screen_rank_group_select() { |
196 | 196 | |
197 | - $rank_groups = WordPoints_Rank_Groups::get(); |
|
197 | + $rank_groups = WordPoints_Rank_Groups::get(); |
|
198 | 198 | |
199 | - // See https://github.com/WordPoints/wordpoints/issues/310. |
|
200 | - $options = array(); |
|
201 | - foreach ( $rank_groups as $rank_group ) { |
|
202 | - $options[ $rank_group->slug ] = $rank_group->name; |
|
203 | - } |
|
199 | + // See https://github.com/WordPoints/wordpoints/issues/310. |
|
200 | + $options = array(); |
|
201 | + foreach ( $rank_groups as $rank_group ) { |
|
202 | + $options[ $rank_group->slug ] = $rank_group->name; |
|
203 | + } |
|
204 | 204 | |
205 | - $dropdown = new WordPoints_Dropdown_Builder( |
|
206 | - $options |
|
207 | - , array( 'name' => 'wordpoints_import[ranks][_data][rank_group]' ) |
|
208 | - ); |
|
205 | + $dropdown = new WordPoints_Dropdown_Builder( |
|
206 | + $options |
|
207 | + , array( 'name' => 'wordpoints_import[ranks][_data][rank_group]' ) |
|
208 | + ); |
|
209 | 209 | |
210 | - ?> |
|
210 | + ?> |
|
211 | 211 | |
212 | 212 | <p> |
213 | 213 | <label for="wordpoints_import[ranks][_data][rank_group]"> |
@@ -219,8 +219,8 @@ discard block |
||
219 | 219 | <?php |
220 | 220 | } |
221 | 221 | add_action( |
222 | - 'wordpoints_importer_before_component_options-ranks' |
|
223 | - , 'wordpoints_importer_admin_screen_rank_group_select' |
|
222 | + 'wordpoints_importer_before_component_options-ranks' |
|
223 | + , 'wordpoints_importer_admin_screen_rank_group_select' |
|
224 | 224 | ); |
225 | 225 | |
226 | 226 | /** |
@@ -236,24 +236,24 @@ discard block |
||
236 | 236 | */ |
237 | 237 | function wordpoints_importer_validate_rank_group_setting( $valid, $settings, $feedback ) { |
238 | 238 | |
239 | - if ( $valid ) { |
|
239 | + if ( $valid ) { |
|
240 | 240 | |
241 | - if ( ! isset( $settings['rank_group'] ) ) { |
|
242 | - $feedback->warning( __( 'Skipping Ranks component—no rank group specified.', 'wordpoints-importer' ) ); |
|
243 | - $valid = false; |
|
244 | - } elseif ( ! WordPoints_Rank_Groups::is_group_registered( $settings['rank_group'] ) ) { |
|
245 | - $feedback->warning( __( 'Skipping Ranks component—invalid rank group selected.', 'wordpoints-importer' ) ); |
|
246 | - $valid = false; |
|
247 | - } |
|
248 | - } |
|
241 | + if ( ! isset( $settings['rank_group'] ) ) { |
|
242 | + $feedback->warning( __( 'Skipping Ranks component—no rank group specified.', 'wordpoints-importer' ) ); |
|
243 | + $valid = false; |
|
244 | + } elseif ( ! WordPoints_Rank_Groups::is_group_registered( $settings['rank_group'] ) ) { |
|
245 | + $feedback->warning( __( 'Skipping Ranks component—invalid rank group selected.', 'wordpoints-importer' ) ); |
|
246 | + $valid = false; |
|
247 | + } |
|
248 | + } |
|
249 | 249 | |
250 | - return $valid; |
|
250 | + return $valid; |
|
251 | 251 | } |
252 | 252 | add_action( |
253 | - 'wordpoints_import_settings_valid-ranks' |
|
254 | - , 'wordpoints_importer_validate_rank_group_setting' |
|
255 | - , 10 |
|
256 | - , 3 |
|
253 | + 'wordpoints_import_settings_valid-ranks' |
|
254 | + , 'wordpoints_importer_validate_rank_group_setting' |
|
255 | + , 10 |
|
256 | + , 3 |
|
257 | 257 | ); |
258 | 258 | |
259 | 259 | // EOF |
@@ -19,22 +19,22 @@ discard block |
||
19 | 19 | // Back-compat with WordPoints 2.1. |
20 | 20 | if ( class_exists( 'WordPoints_PHPUnit_Bootstrap_Loader' ) ) { |
21 | 21 | |
22 | - $loader = WordPoints_PHPUnit_Bootstrap_Loader::instance(); |
|
23 | - $loader->add_plugin( 'cubepoints/cubepoints.php' ); |
|
24 | - $loader->add_php_file( |
|
25 | - WORDPOINTS_IMPORTER_TESTS_DIR . '/includes/activate-cubepoints-components.php' |
|
26 | - , 'after' |
|
27 | - , array( 'dailypoints', 'post_author_points' ) |
|
28 | - ); |
|
22 | + $loader = WordPoints_PHPUnit_Bootstrap_Loader::instance(); |
|
23 | + $loader->add_plugin( 'cubepoints/cubepoints.php' ); |
|
24 | + $loader->add_php_file( |
|
25 | + WORDPOINTS_IMPORTER_TESTS_DIR . '/includes/activate-cubepoints-components.php' |
|
26 | + , 'after' |
|
27 | + , array( 'dailypoints', 'post_author_points' ) |
|
28 | + ); |
|
29 | 29 | |
30 | 30 | } elseif ( ! defined( 'WORDPOINTS_MODULE_TESTS_LOADER' ) ) { |
31 | 31 | |
32 | - /** |
|
33 | - * The function that loads the module for the tests. |
|
34 | - * |
|
35 | - * @since 1.0.0 |
|
36 | - */ |
|
37 | - define( 'WORDPOINTS_MODULE_TESTS_LOADER', 'wordpoints_importer_tests_manually_load_module' ); |
|
32 | + /** |
|
33 | + * The function that loads the module for the tests. |
|
34 | + * |
|
35 | + * @since 1.0.0 |
|
36 | + */ |
|
37 | + define( 'WORDPOINTS_MODULE_TESTS_LOADER', 'wordpoints_importer_tests_manually_load_module' ); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -45,10 +45,10 @@ discard block |
||
45 | 45 | */ |
46 | 46 | function wordpoints_importer_tests_manually_load_module() { |
47 | 47 | |
48 | - require( WORDPOINTS_IMPORTER_TESTS_DIR . '/../../src/importer.php' ); |
|
49 | - require( WORDPOINTS_IMPORTER_TESTS_DIR . '/../../src/admin/admin.php' ); |
|
48 | + require( WORDPOINTS_IMPORTER_TESTS_DIR . '/../../src/importer.php' ); |
|
49 | + require( WORDPOINTS_IMPORTER_TESTS_DIR . '/../../src/admin/admin.php' ); |
|
50 | 50 | |
51 | - wordpoints_importer_tests_manually_load_cubepoints(); |
|
51 | + wordpoints_importer_tests_manually_load_cubepoints(); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -59,25 +59,25 @@ discard block |
||
59 | 59 | */ |
60 | 60 | function wordpoints_importer_tests_manually_load_cubepoints() { |
61 | 61 | |
62 | - require( WP_PLUGIN_DIR . '/cubepoints/cubepoints.php' ); |
|
62 | + require( WP_PLUGIN_DIR . '/cubepoints/cubepoints.php' ); |
|
63 | 63 | |
64 | - cp_activate(); |
|
64 | + cp_activate(); |
|
65 | 65 | |
66 | - // We activate these now so that they will be fully loaded. Otherwise only part |
|
67 | - // of their functions will be loaded, as the rest are defined in a conditional. |
|
68 | - // Because of some of the functions are defined outside of the conditional, there |
|
69 | - // is no way for us to load the functions later without a fatal error from |
|
70 | - // "already defined function". |
|
71 | - cp_module_activation_set( 'dailypoints', 'active' ); |
|
72 | - cp_module_activation_set( 'post_author_points', 'active' ); |
|
66 | + // We activate these now so that they will be fully loaded. Otherwise only part |
|
67 | + // of their functions will be loaded, as the rest are defined in a conditional. |
|
68 | + // Because of some of the functions are defined outside of the conditional, there |
|
69 | + // is no way for us to load the functions later without a fatal error from |
|
70 | + // "already defined function". |
|
71 | + cp_module_activation_set( 'dailypoints', 'active' ); |
|
72 | + cp_module_activation_set( 'post_author_points', 'active' ); |
|
73 | 73 | |
74 | - // We have to do this manually here after WordPress 4.7. |
|
75 | - // https://core.trac.wordpress.org/ticket/38011#comment:3 |
|
76 | - global $wp_version; |
|
74 | + // We have to do this manually here after WordPress 4.7. |
|
75 | + // https://core.trac.wordpress.org/ticket/38011#comment:3 |
|
76 | + global $wp_version; |
|
77 | 77 | |
78 | - if ( version_compare( $wp_version, '4.6', '>' ) ) { |
|
79 | - cp_modules_include(); |
|
80 | - } |
|
78 | + if ( version_compare( $wp_version, '4.6', '>' ) ) { |
|
79 | + cp_modules_include(); |
|
80 | + } |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | // EOF |
@@ -8,7 +8,7 @@ |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | foreach ( $data as $module_slug ) { |
11 | - cp_module_activation_set( $module_slug, 'active' ); |
|
11 | + cp_module_activation_set( $module_slug, 'active' ); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | // EOF |
@@ -17,856 +17,856 @@ |
||
17 | 17 | */ |
18 | 18 | class WordPoints_CubePoints_Importer_Test extends WordPoints_Points_UnitTestCase { |
19 | 19 | |
20 | - /** |
|
21 | - * The importer used in the tests. |
|
22 | - * |
|
23 | - * @since 1.0.0 |
|
24 | - * |
|
25 | - * @var WordPoints_CubePoints_Importer |
|
26 | - */ |
|
27 | - protected $importer; |
|
28 | - |
|
29 | - /** |
|
30 | - * @since 1.0.0 |
|
31 | - */ |
|
32 | - public function setUp() { |
|
33 | - |
|
34 | - parent::setUp(); |
|
35 | - |
|
36 | - // These are usually inactive by default. We activate them in the tests |
|
37 | - // bootstrap so that they will be fully loaded, but deactivate them here to |
|
38 | - // restore default behavior. |
|
39 | - cp_module_activation_set( 'post_author_points', false ); |
|
40 | - cp_module_activation_set( 'dailypoints', false ); |
|
41 | - |
|
42 | - $this->importer = new WordPoints_CubePoints_Importer( 'Test CubePoints' ); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @since 1.1.0 |
|
47 | - */ |
|
48 | - public function tearDown() { |
|
49 | - |
|
50 | - WordPoints_Rank_Groups::deregister_group( 'points_type-points' ); |
|
51 | - WordPoints_Rank_Types::deregister_type( 'points-points' ); |
|
52 | - |
|
53 | - parent::tearDown(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Test that it returns true when CubePoints is installed. |
|
58 | - * |
|
59 | - * @since 1.0.0 |
|
60 | - * |
|
61 | - * @covers WordPoints_CubePoints_Importer::is_cubepoints_installed |
|
62 | - */ |
|
63 | - public function test_is_cubepoints_installed() { |
|
64 | - |
|
65 | - $this->assertTrue( $this->importer->is_cubepoints_installed() ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Test that it returns false when CubePoints is not installed. |
|
70 | - * |
|
71 | - * @since 1.0.0 |
|
72 | - * |
|
73 | - * @covers WordPoints_CubePoints_Importer::is_cubepoints_installed |
|
74 | - */ |
|
75 | - public function test_is_cubepoints_not_installed() { |
|
76 | - |
|
77 | - delete_option( 'cp_db_version' ); |
|
78 | - |
|
79 | - $this->assertFalse( $this->importer->is_cubepoints_installed() ); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Test that it returns true when CubePoints is installed. |
|
84 | - * |
|
85 | - * @since 1.0.0 |
|
86 | - * |
|
87 | - * @covers WordPoints_CubePoints_Importer::is_available |
|
88 | - */ |
|
89 | - public function test_is_cubepoints_is_available() { |
|
90 | - |
|
91 | - $this->assertTrue( $this->importer->is_available() ); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Test that it returns a WP_Error when CubePoints is not installed. |
|
96 | - * |
|
97 | - * @since 1.0.0 |
|
98 | - * |
|
99 | - * @covers WordPoints_CubePoints_Importer::is_available |
|
100 | - */ |
|
101 | - public function test_is_cubepoints_not_available() { |
|
102 | - |
|
103 | - delete_option( 'cp_db_version' ); |
|
104 | - |
|
105 | - $this->assertWPError( $this->importer->is_available() ); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Test that it returns true when CubePoints is active. |
|
110 | - * |
|
111 | - * @since 1.0.0 |
|
112 | - * |
|
113 | - * @covers WordPoints_CubePoints_Importer::is_cubepoints_active |
|
114 | - */ |
|
115 | - public function test_is_cubepoints_active() { |
|
116 | - |
|
117 | - $this->assertEquals( |
|
118 | - function_exists( 'cp_ready' ) |
|
119 | - , $this->importer->is_cubepoints_active() |
|
120 | - ); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Test importing the excluded users. |
|
125 | - * |
|
126 | - * @since 1.0.0 |
|
127 | - * |
|
128 | - * @covers WordPoints_CubePoints_Importer::import_excluded_users |
|
129 | - */ |
|
130 | - public function test_import_excluded_users() { |
|
131 | - |
|
132 | - $user_ids = $this->factory->user->create_many( 3 ); |
|
133 | - $user_logins = array(); |
|
134 | - |
|
135 | - foreach ( $user_ids as $user_id ) { |
|
136 | - $user_logins[] = get_userdata( $user_id )->user_login; |
|
137 | - } |
|
138 | - |
|
139 | - update_option( 'cp_topfilter', $user_logins ); |
|
140 | - |
|
141 | - $this->do_points_import( 'excluded_users' ); |
|
142 | - |
|
143 | - $this->assertEquals( |
|
144 | - $user_ids |
|
145 | - , wordpoints_get_excluded_users( 'tests' ) |
|
146 | - ); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Test importing the excluded users gives a warning if there are none. |
|
151 | - * |
|
152 | - * @since 1.0.0 |
|
153 | - * |
|
154 | - * @covers WordPoints_CubePoints_Importer::import_excluded_users |
|
155 | - */ |
|
156 | - public function test_import_excluded_users_none() { |
|
157 | - |
|
158 | - delete_option( 'cp_topfilter' ); |
|
159 | - |
|
160 | - $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
161 | - |
|
162 | - $this->importer->do_import( |
|
163 | - array( |
|
164 | - 'points' => array( |
|
165 | - 'excluded_users' => '1', |
|
166 | - '_data' => array( 'points_type' => 'points' ), |
|
167 | - ), |
|
168 | - ) |
|
169 | - , $feedback |
|
170 | - ); |
|
171 | - |
|
172 | - $this->assertCount( 1, $feedback->messages['warning'] ); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Test importing the points settings to points hooks. |
|
177 | - * |
|
178 | - * @since 1.1.0 |
|
179 | - * |
|
180 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
181 | - * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
182 | - * @covers WordPoints_CubePoints_Importer::format_settings_for_post_type |
|
183 | - */ |
|
184 | - public function test_import_points_settings() { |
|
185 | - |
|
186 | - update_option( 'cp_comment_points', 10 ); |
|
187 | - update_option( 'cp_post_points', 20 ); |
|
188 | - update_option( 'cp_reg_points', 50 ); |
|
189 | - |
|
190 | - $this->do_points_import( 'settings' ); |
|
191 | - |
|
192 | - $this->assertHookImported( |
|
193 | - array( |
|
194 | - 'event' => 'comment_leave\post', |
|
195 | - 'target' => array( 'comment\post', 'author', 'user' ), |
|
196 | - 'reactor' => 'points_legacy', |
|
197 | - 'points' => 10, |
|
198 | - 'points_type' => 'points', |
|
199 | - 'log_text' => 'Comment on a Post.', |
|
200 | - 'description' => 'Commenting on a Post.', |
|
201 | - 'legacy_log_type' => 'cubepoints-comment', |
|
202 | - 'legacy_meta_key' => 'comment', |
|
203 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
204 | - ) |
|
205 | - ); |
|
206 | - |
|
207 | - $this->assertHookImported( |
|
208 | - array( |
|
209 | - 'event' => 'comment_leave\page', |
|
210 | - 'target' => array( 'comment\page', 'author', 'user' ), |
|
211 | - 'reactor' => 'points_legacy', |
|
212 | - 'points' => 10, |
|
213 | - 'points_type' => 'points', |
|
214 | - 'log_text' => 'Comment on a Page.', |
|
215 | - 'description' => 'Commenting on a Page.', |
|
216 | - 'legacy_log_type' => 'cubepoints-comment', |
|
217 | - 'legacy_meta_key' => 'comment', |
|
218 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
219 | - ) |
|
220 | - ); |
|
221 | - |
|
222 | - $this->assertHookImported( |
|
223 | - array( |
|
224 | - 'event' => 'comment_leave\attachment', |
|
225 | - 'target' => array( 'comment\attachment', 'author', 'user' ), |
|
226 | - 'reactor' => 'points_legacy', |
|
227 | - 'points' => 10, |
|
228 | - 'points_type' => 'points', |
|
229 | - 'log_text' => 'Comment on a Media.', |
|
230 | - 'description' => 'Commenting on a Media.', |
|
231 | - 'legacy_log_type' => 'cubepoints-comment', |
|
232 | - 'legacy_meta_key' => 'comment', |
|
233 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
234 | - ) |
|
235 | - ); |
|
236 | - |
|
237 | - $this->assertHookImported( |
|
238 | - array( |
|
239 | - 'event' => 'post_publish\post', |
|
240 | - 'target' => array( 'post\post', 'author', 'user' ), |
|
241 | - 'reactor' => 'points_legacy', |
|
242 | - 'points' => 20, |
|
243 | - 'points_type' => 'points', |
|
244 | - 'log_text' => 'Published a Post.', |
|
245 | - 'description' => 'Publishing a Post.', |
|
246 | - 'blocker' => array( 'toggle_off' => true ), |
|
247 | - 'legacy_log_type' => 'cubepoints-post', |
|
248 | - 'legacy_meta_key' => 'post', |
|
249 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
250 | - 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
251 | - ) |
|
252 | - ); |
|
253 | - |
|
254 | - $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
255 | - |
|
256 | - $this->assertEmpty( |
|
257 | - $reaction_store->get_reactions_to_event( 'post_publish\page' ) |
|
258 | - ); |
|
259 | - |
|
260 | - $this->assertEmpty( |
|
261 | - $reaction_store->get_reactions_to_event( 'post_publish\attachment' ) |
|
262 | - ); |
|
263 | - |
|
264 | - $this->assertEmpty( |
|
265 | - $reaction_store->get_reactions_to_event( 'media_upload' ) |
|
266 | - ); |
|
267 | - |
|
268 | - $this->assertHookImported( |
|
269 | - array( |
|
270 | - 'event' => 'user_register', |
|
271 | - 'target' => array( 'user' ), |
|
272 | - 'reactor' => 'points_legacy', |
|
273 | - 'points' => 50, |
|
274 | - 'points_type' => 'points', |
|
275 | - 'log_text' => 'Registration.', |
|
276 | - 'description' => 'Registration.', |
|
277 | - 'legacy_log_type' => 'cubepoints-register', |
|
278 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
279 | - ) |
|
280 | - ); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Test that it imports legacy points hooks on install. |
|
285 | - * |
|
286 | - * @since 1.0.0 |
|
287 | - * |
|
288 | - * @coversNothing |
|
289 | - */ |
|
290 | - public function test_imported_post_points_hook_does_not_refire() { |
|
291 | - |
|
292 | - update_option( 'cp_post_points', 20 ); |
|
293 | - |
|
294 | - $user_id = $this->factory->user->create(); |
|
295 | - $post_id = $this->factory->post->create( |
|
296 | - array( |
|
297 | - 'post_author' => $user_id, |
|
298 | - 'post_type' => 'post', |
|
299 | - ) |
|
300 | - ); |
|
301 | - |
|
302 | - $this->assertEquals( 120, cp_getPoints( $user_id ) ); |
|
303 | - |
|
304 | - $this->factory->post->update_object( |
|
305 | - $post_id |
|
306 | - , array( 'post_status' => 'draft' ) |
|
307 | - ); |
|
308 | - |
|
309 | - $this->assertEquals( 120, cp_getPoints( $user_id ) ); |
|
310 | - |
|
311 | - $this->do_points_import( 'settings' ); |
|
312 | - $this->do_points_import( 'user_points' ); |
|
313 | - $this->do_points_import( 'logs' ); |
|
314 | - |
|
315 | - $this->assertEquals( |
|
316 | - 120 |
|
317 | - , wordpoints_get_points( $user_id, 'points' ) |
|
318 | - ); |
|
319 | - |
|
320 | - $this->factory->post->update_object( |
|
321 | - $post_id |
|
322 | - , array( 'post_status' => 'publish' ) |
|
323 | - ); |
|
324 | - |
|
325 | - $this->assertEquals( |
|
326 | - 120 |
|
327 | - , wordpoints_get_points( $user_id, 'points' ) |
|
328 | - ); |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Test importing the settings from the post author points module to points hooks. |
|
333 | - * |
|
334 | - * @since 1.1.0 |
|
335 | - * |
|
336 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
337 | - * @covers WordPoints_CubePoints_Importer::format_settings_for_post_type |
|
338 | - */ |
|
339 | - public function test_import_post_author_points() { |
|
340 | - |
|
341 | - cp_module_activation_set( 'post_author_points', 'active' ); |
|
342 | - |
|
343 | - update_option( 'cp_post_author_points', 15 ); |
|
344 | - |
|
345 | - $this->do_points_import( 'settings' ); |
|
346 | - |
|
347 | - $this->assertHookImported( |
|
348 | - array( |
|
349 | - 'event' => 'comment_leave\post', |
|
350 | - 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
351 | - 'reactor' => 'points_legacy', |
|
352 | - 'points' => 15, |
|
353 | - 'points_type' => 'points', |
|
354 | - 'log_text' => 'Received a comment on a Post.', |
|
355 | - 'description' => 'Receiving a comment on a Post.', |
|
356 | - 'legacy_log_type' => 'cubepoints-post_author', |
|
357 | - 'legacy_meta_key' => 'comment', |
|
358 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
359 | - ) |
|
360 | - ); |
|
361 | - |
|
362 | - $this->assertHookImported( |
|
363 | - array( |
|
364 | - 'event' => 'comment_leave\page', |
|
365 | - 'target' => array( 'comment\page', 'post\page', 'post\page', 'author', 'user' ), |
|
366 | - 'reactor' => 'points_legacy', |
|
367 | - 'points' => 15, |
|
368 | - 'points_type' => 'points', |
|
369 | - 'log_text' => 'Received a comment on a Page.', |
|
370 | - 'description' => 'Receiving a comment on a Page.', |
|
371 | - 'legacy_log_type' => 'cubepoints-post_author', |
|
372 | - 'legacy_meta_key' => 'comment', |
|
373 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
374 | - ) |
|
375 | - ); |
|
376 | - |
|
377 | - $this->assertHookImported( |
|
378 | - array( |
|
379 | - 'event' => 'comment_leave\attachment', |
|
380 | - 'target' => array( 'comment\attachment', 'post\attachment', 'post\attachment', 'author', 'user' ), |
|
381 | - 'reactor' => 'points_legacy', |
|
382 | - 'points' => 15, |
|
383 | - 'points_type' => 'points', |
|
384 | - 'log_text' => 'Received a comment on a Media.', |
|
385 | - 'description' => 'Receiving a comment on a Media.', |
|
386 | - 'legacy_log_type' => 'cubepoints-post_author', |
|
387 | - 'legacy_meta_key' => 'comment', |
|
388 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
389 | - ) |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Test importing the settings from the daily points module to points hooks. |
|
395 | - * |
|
396 | - * @since 1.1.0 |
|
397 | - * |
|
398 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
399 | - * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
400 | - */ |
|
401 | - public function test_import_periodic_points() { |
|
402 | - |
|
403 | - cp_module_activation_set( 'dailypoints', 'active' ); |
|
404 | - |
|
405 | - update_option( 'cp_module_dailypoints_points', 30 ); |
|
406 | - update_option( 'cp_module_dailypoints_time', DAY_IN_SECONDS ); |
|
407 | - |
|
408 | - $this->do_points_import( 'settings' ); |
|
409 | - |
|
410 | - $this->assertHookImported( |
|
411 | - array( |
|
412 | - 'event' => 'user_visit', |
|
413 | - 'target' => array( 'current:user' ), |
|
414 | - 'reactor' => 'points_legacy', |
|
415 | - 'points' => 30, |
|
416 | - 'points_type' => 'points', |
|
417 | - 'log_text' => 'Visiting the site.', |
|
418 | - 'description' => 'Visiting the site.', |
|
419 | - 'points_legacy_periods' => array( |
|
420 | - 'fire' => array( |
|
421 | - array( |
|
422 | - 'length' => DAY_IN_SECONDS, |
|
423 | - 'args' => array( array( 'current:user' ) ), |
|
424 | - 'relative' => true, |
|
425 | - ), |
|
426 | - ), |
|
427 | - ), |
|
428 | - 'points_legacy_reversals' => array(), |
|
429 | - 'legacy_log_type' => 'cubepoints-dailypoints', |
|
430 | - ) |
|
431 | - ); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Test that the imported user visit hook respects CubePoints's started periods. |
|
436 | - * |
|
437 | - * @since 1.2.0 |
|
438 | - * |
|
439 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
440 | - * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
441 | - */ |
|
442 | - public function test_import_periodic_points_respect_old_periods() { |
|
443 | - |
|
444 | - if ( version_compare( $GLOBALS['wp_version'], '4.5', '>=' ) ) { |
|
445 | - $this->setExpectedDeprecated( 'get_currentuserinfo' ); |
|
446 | - } |
|
447 | - |
|
448 | - cp_module_activation_set( 'dailypoints', 'active' ); |
|
449 | - |
|
450 | - update_option( 'cp_module_dailypoints_points', 30 ); |
|
451 | - update_option( 'cp_module_dailypoints_time', DAY_IN_SECONDS ); |
|
452 | - |
|
453 | - $user_id = $this->factory->user->create(); |
|
454 | - |
|
455 | - wp_set_current_user( $user_id ); |
|
456 | - |
|
457 | - $this->assertEquals( 100, cp_getPoints( $user_id ) ); |
|
458 | - |
|
459 | - cp_module_dailypoints_checkTimer(); |
|
460 | - |
|
461 | - $this->assertEquals( 130, cp_getPoints( $user_id ) ); |
|
462 | - |
|
463 | - // Running again shouldn't hit again. |
|
464 | - cp_module_dailypoints_checkTimer(); |
|
465 | - |
|
466 | - $this->assertEquals( 130, cp_getPoints( $user_id ) ); |
|
467 | - |
|
468 | - $this->do_points_import( 'settings' ); |
|
469 | - $this->do_points_import( 'user_points' ); |
|
470 | - $this->do_points_import( 'logs' ); |
|
471 | - |
|
472 | - $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
473 | - |
|
474 | - wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
475 | - |
|
476 | - $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
477 | - |
|
478 | - // Fast-forward and try again. |
|
479 | - global $wpdb; |
|
480 | - |
|
481 | - $id = $wpdb->get_var( |
|
482 | - " |
|
20 | + /** |
|
21 | + * The importer used in the tests. |
|
22 | + * |
|
23 | + * @since 1.0.0 |
|
24 | + * |
|
25 | + * @var WordPoints_CubePoints_Importer |
|
26 | + */ |
|
27 | + protected $importer; |
|
28 | + |
|
29 | + /** |
|
30 | + * @since 1.0.0 |
|
31 | + */ |
|
32 | + public function setUp() { |
|
33 | + |
|
34 | + parent::setUp(); |
|
35 | + |
|
36 | + // These are usually inactive by default. We activate them in the tests |
|
37 | + // bootstrap so that they will be fully loaded, but deactivate them here to |
|
38 | + // restore default behavior. |
|
39 | + cp_module_activation_set( 'post_author_points', false ); |
|
40 | + cp_module_activation_set( 'dailypoints', false ); |
|
41 | + |
|
42 | + $this->importer = new WordPoints_CubePoints_Importer( 'Test CubePoints' ); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @since 1.1.0 |
|
47 | + */ |
|
48 | + public function tearDown() { |
|
49 | + |
|
50 | + WordPoints_Rank_Groups::deregister_group( 'points_type-points' ); |
|
51 | + WordPoints_Rank_Types::deregister_type( 'points-points' ); |
|
52 | + |
|
53 | + parent::tearDown(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Test that it returns true when CubePoints is installed. |
|
58 | + * |
|
59 | + * @since 1.0.0 |
|
60 | + * |
|
61 | + * @covers WordPoints_CubePoints_Importer::is_cubepoints_installed |
|
62 | + */ |
|
63 | + public function test_is_cubepoints_installed() { |
|
64 | + |
|
65 | + $this->assertTrue( $this->importer->is_cubepoints_installed() ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Test that it returns false when CubePoints is not installed. |
|
70 | + * |
|
71 | + * @since 1.0.0 |
|
72 | + * |
|
73 | + * @covers WordPoints_CubePoints_Importer::is_cubepoints_installed |
|
74 | + */ |
|
75 | + public function test_is_cubepoints_not_installed() { |
|
76 | + |
|
77 | + delete_option( 'cp_db_version' ); |
|
78 | + |
|
79 | + $this->assertFalse( $this->importer->is_cubepoints_installed() ); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Test that it returns true when CubePoints is installed. |
|
84 | + * |
|
85 | + * @since 1.0.0 |
|
86 | + * |
|
87 | + * @covers WordPoints_CubePoints_Importer::is_available |
|
88 | + */ |
|
89 | + public function test_is_cubepoints_is_available() { |
|
90 | + |
|
91 | + $this->assertTrue( $this->importer->is_available() ); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Test that it returns a WP_Error when CubePoints is not installed. |
|
96 | + * |
|
97 | + * @since 1.0.0 |
|
98 | + * |
|
99 | + * @covers WordPoints_CubePoints_Importer::is_available |
|
100 | + */ |
|
101 | + public function test_is_cubepoints_not_available() { |
|
102 | + |
|
103 | + delete_option( 'cp_db_version' ); |
|
104 | + |
|
105 | + $this->assertWPError( $this->importer->is_available() ); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Test that it returns true when CubePoints is active. |
|
110 | + * |
|
111 | + * @since 1.0.0 |
|
112 | + * |
|
113 | + * @covers WordPoints_CubePoints_Importer::is_cubepoints_active |
|
114 | + */ |
|
115 | + public function test_is_cubepoints_active() { |
|
116 | + |
|
117 | + $this->assertEquals( |
|
118 | + function_exists( 'cp_ready' ) |
|
119 | + , $this->importer->is_cubepoints_active() |
|
120 | + ); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Test importing the excluded users. |
|
125 | + * |
|
126 | + * @since 1.0.0 |
|
127 | + * |
|
128 | + * @covers WordPoints_CubePoints_Importer::import_excluded_users |
|
129 | + */ |
|
130 | + public function test_import_excluded_users() { |
|
131 | + |
|
132 | + $user_ids = $this->factory->user->create_many( 3 ); |
|
133 | + $user_logins = array(); |
|
134 | + |
|
135 | + foreach ( $user_ids as $user_id ) { |
|
136 | + $user_logins[] = get_userdata( $user_id )->user_login; |
|
137 | + } |
|
138 | + |
|
139 | + update_option( 'cp_topfilter', $user_logins ); |
|
140 | + |
|
141 | + $this->do_points_import( 'excluded_users' ); |
|
142 | + |
|
143 | + $this->assertEquals( |
|
144 | + $user_ids |
|
145 | + , wordpoints_get_excluded_users( 'tests' ) |
|
146 | + ); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Test importing the excluded users gives a warning if there are none. |
|
151 | + * |
|
152 | + * @since 1.0.0 |
|
153 | + * |
|
154 | + * @covers WordPoints_CubePoints_Importer::import_excluded_users |
|
155 | + */ |
|
156 | + public function test_import_excluded_users_none() { |
|
157 | + |
|
158 | + delete_option( 'cp_topfilter' ); |
|
159 | + |
|
160 | + $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
161 | + |
|
162 | + $this->importer->do_import( |
|
163 | + array( |
|
164 | + 'points' => array( |
|
165 | + 'excluded_users' => '1', |
|
166 | + '_data' => array( 'points_type' => 'points' ), |
|
167 | + ), |
|
168 | + ) |
|
169 | + , $feedback |
|
170 | + ); |
|
171 | + |
|
172 | + $this->assertCount( 1, $feedback->messages['warning'] ); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Test importing the points settings to points hooks. |
|
177 | + * |
|
178 | + * @since 1.1.0 |
|
179 | + * |
|
180 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
181 | + * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
182 | + * @covers WordPoints_CubePoints_Importer::format_settings_for_post_type |
|
183 | + */ |
|
184 | + public function test_import_points_settings() { |
|
185 | + |
|
186 | + update_option( 'cp_comment_points', 10 ); |
|
187 | + update_option( 'cp_post_points', 20 ); |
|
188 | + update_option( 'cp_reg_points', 50 ); |
|
189 | + |
|
190 | + $this->do_points_import( 'settings' ); |
|
191 | + |
|
192 | + $this->assertHookImported( |
|
193 | + array( |
|
194 | + 'event' => 'comment_leave\post', |
|
195 | + 'target' => array( 'comment\post', 'author', 'user' ), |
|
196 | + 'reactor' => 'points_legacy', |
|
197 | + 'points' => 10, |
|
198 | + 'points_type' => 'points', |
|
199 | + 'log_text' => 'Comment on a Post.', |
|
200 | + 'description' => 'Commenting on a Post.', |
|
201 | + 'legacy_log_type' => 'cubepoints-comment', |
|
202 | + 'legacy_meta_key' => 'comment', |
|
203 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
204 | + ) |
|
205 | + ); |
|
206 | + |
|
207 | + $this->assertHookImported( |
|
208 | + array( |
|
209 | + 'event' => 'comment_leave\page', |
|
210 | + 'target' => array( 'comment\page', 'author', 'user' ), |
|
211 | + 'reactor' => 'points_legacy', |
|
212 | + 'points' => 10, |
|
213 | + 'points_type' => 'points', |
|
214 | + 'log_text' => 'Comment on a Page.', |
|
215 | + 'description' => 'Commenting on a Page.', |
|
216 | + 'legacy_log_type' => 'cubepoints-comment', |
|
217 | + 'legacy_meta_key' => 'comment', |
|
218 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
219 | + ) |
|
220 | + ); |
|
221 | + |
|
222 | + $this->assertHookImported( |
|
223 | + array( |
|
224 | + 'event' => 'comment_leave\attachment', |
|
225 | + 'target' => array( 'comment\attachment', 'author', 'user' ), |
|
226 | + 'reactor' => 'points_legacy', |
|
227 | + 'points' => 10, |
|
228 | + 'points_type' => 'points', |
|
229 | + 'log_text' => 'Comment on a Media.', |
|
230 | + 'description' => 'Commenting on a Media.', |
|
231 | + 'legacy_log_type' => 'cubepoints-comment', |
|
232 | + 'legacy_meta_key' => 'comment', |
|
233 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
234 | + ) |
|
235 | + ); |
|
236 | + |
|
237 | + $this->assertHookImported( |
|
238 | + array( |
|
239 | + 'event' => 'post_publish\post', |
|
240 | + 'target' => array( 'post\post', 'author', 'user' ), |
|
241 | + 'reactor' => 'points_legacy', |
|
242 | + 'points' => 20, |
|
243 | + 'points_type' => 'points', |
|
244 | + 'log_text' => 'Published a Post.', |
|
245 | + 'description' => 'Publishing a Post.', |
|
246 | + 'blocker' => array( 'toggle_off' => true ), |
|
247 | + 'legacy_log_type' => 'cubepoints-post', |
|
248 | + 'legacy_meta_key' => 'post', |
|
249 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
250 | + 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
251 | + ) |
|
252 | + ); |
|
253 | + |
|
254 | + $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
255 | + |
|
256 | + $this->assertEmpty( |
|
257 | + $reaction_store->get_reactions_to_event( 'post_publish\page' ) |
|
258 | + ); |
|
259 | + |
|
260 | + $this->assertEmpty( |
|
261 | + $reaction_store->get_reactions_to_event( 'post_publish\attachment' ) |
|
262 | + ); |
|
263 | + |
|
264 | + $this->assertEmpty( |
|
265 | + $reaction_store->get_reactions_to_event( 'media_upload' ) |
|
266 | + ); |
|
267 | + |
|
268 | + $this->assertHookImported( |
|
269 | + array( |
|
270 | + 'event' => 'user_register', |
|
271 | + 'target' => array( 'user' ), |
|
272 | + 'reactor' => 'points_legacy', |
|
273 | + 'points' => 50, |
|
274 | + 'points_type' => 'points', |
|
275 | + 'log_text' => 'Registration.', |
|
276 | + 'description' => 'Registration.', |
|
277 | + 'legacy_log_type' => 'cubepoints-register', |
|
278 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
279 | + ) |
|
280 | + ); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Test that it imports legacy points hooks on install. |
|
285 | + * |
|
286 | + * @since 1.0.0 |
|
287 | + * |
|
288 | + * @coversNothing |
|
289 | + */ |
|
290 | + public function test_imported_post_points_hook_does_not_refire() { |
|
291 | + |
|
292 | + update_option( 'cp_post_points', 20 ); |
|
293 | + |
|
294 | + $user_id = $this->factory->user->create(); |
|
295 | + $post_id = $this->factory->post->create( |
|
296 | + array( |
|
297 | + 'post_author' => $user_id, |
|
298 | + 'post_type' => 'post', |
|
299 | + ) |
|
300 | + ); |
|
301 | + |
|
302 | + $this->assertEquals( 120, cp_getPoints( $user_id ) ); |
|
303 | + |
|
304 | + $this->factory->post->update_object( |
|
305 | + $post_id |
|
306 | + , array( 'post_status' => 'draft' ) |
|
307 | + ); |
|
308 | + |
|
309 | + $this->assertEquals( 120, cp_getPoints( $user_id ) ); |
|
310 | + |
|
311 | + $this->do_points_import( 'settings' ); |
|
312 | + $this->do_points_import( 'user_points' ); |
|
313 | + $this->do_points_import( 'logs' ); |
|
314 | + |
|
315 | + $this->assertEquals( |
|
316 | + 120 |
|
317 | + , wordpoints_get_points( $user_id, 'points' ) |
|
318 | + ); |
|
319 | + |
|
320 | + $this->factory->post->update_object( |
|
321 | + $post_id |
|
322 | + , array( 'post_status' => 'publish' ) |
|
323 | + ); |
|
324 | + |
|
325 | + $this->assertEquals( |
|
326 | + 120 |
|
327 | + , wordpoints_get_points( $user_id, 'points' ) |
|
328 | + ); |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Test importing the settings from the post author points module to points hooks. |
|
333 | + * |
|
334 | + * @since 1.1.0 |
|
335 | + * |
|
336 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
337 | + * @covers WordPoints_CubePoints_Importer::format_settings_for_post_type |
|
338 | + */ |
|
339 | + public function test_import_post_author_points() { |
|
340 | + |
|
341 | + cp_module_activation_set( 'post_author_points', 'active' ); |
|
342 | + |
|
343 | + update_option( 'cp_post_author_points', 15 ); |
|
344 | + |
|
345 | + $this->do_points_import( 'settings' ); |
|
346 | + |
|
347 | + $this->assertHookImported( |
|
348 | + array( |
|
349 | + 'event' => 'comment_leave\post', |
|
350 | + 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
351 | + 'reactor' => 'points_legacy', |
|
352 | + 'points' => 15, |
|
353 | + 'points_type' => 'points', |
|
354 | + 'log_text' => 'Received a comment on a Post.', |
|
355 | + 'description' => 'Receiving a comment on a Post.', |
|
356 | + 'legacy_log_type' => 'cubepoints-post_author', |
|
357 | + 'legacy_meta_key' => 'comment', |
|
358 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
359 | + ) |
|
360 | + ); |
|
361 | + |
|
362 | + $this->assertHookImported( |
|
363 | + array( |
|
364 | + 'event' => 'comment_leave\page', |
|
365 | + 'target' => array( 'comment\page', 'post\page', 'post\page', 'author', 'user' ), |
|
366 | + 'reactor' => 'points_legacy', |
|
367 | + 'points' => 15, |
|
368 | + 'points_type' => 'points', |
|
369 | + 'log_text' => 'Received a comment on a Page.', |
|
370 | + 'description' => 'Receiving a comment on a Page.', |
|
371 | + 'legacy_log_type' => 'cubepoints-post_author', |
|
372 | + 'legacy_meta_key' => 'comment', |
|
373 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
374 | + ) |
|
375 | + ); |
|
376 | + |
|
377 | + $this->assertHookImported( |
|
378 | + array( |
|
379 | + 'event' => 'comment_leave\attachment', |
|
380 | + 'target' => array( 'comment\attachment', 'post\attachment', 'post\attachment', 'author', 'user' ), |
|
381 | + 'reactor' => 'points_legacy', |
|
382 | + 'points' => 15, |
|
383 | + 'points_type' => 'points', |
|
384 | + 'log_text' => 'Received a comment on a Media.', |
|
385 | + 'description' => 'Receiving a comment on a Media.', |
|
386 | + 'legacy_log_type' => 'cubepoints-post_author', |
|
387 | + 'legacy_meta_key' => 'comment', |
|
388 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
389 | + ) |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Test importing the settings from the daily points module to points hooks. |
|
395 | + * |
|
396 | + * @since 1.1.0 |
|
397 | + * |
|
398 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
399 | + * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
400 | + */ |
|
401 | + public function test_import_periodic_points() { |
|
402 | + |
|
403 | + cp_module_activation_set( 'dailypoints', 'active' ); |
|
404 | + |
|
405 | + update_option( 'cp_module_dailypoints_points', 30 ); |
|
406 | + update_option( 'cp_module_dailypoints_time', DAY_IN_SECONDS ); |
|
407 | + |
|
408 | + $this->do_points_import( 'settings' ); |
|
409 | + |
|
410 | + $this->assertHookImported( |
|
411 | + array( |
|
412 | + 'event' => 'user_visit', |
|
413 | + 'target' => array( 'current:user' ), |
|
414 | + 'reactor' => 'points_legacy', |
|
415 | + 'points' => 30, |
|
416 | + 'points_type' => 'points', |
|
417 | + 'log_text' => 'Visiting the site.', |
|
418 | + 'description' => 'Visiting the site.', |
|
419 | + 'points_legacy_periods' => array( |
|
420 | + 'fire' => array( |
|
421 | + array( |
|
422 | + 'length' => DAY_IN_SECONDS, |
|
423 | + 'args' => array( array( 'current:user' ) ), |
|
424 | + 'relative' => true, |
|
425 | + ), |
|
426 | + ), |
|
427 | + ), |
|
428 | + 'points_legacy_reversals' => array(), |
|
429 | + 'legacy_log_type' => 'cubepoints-dailypoints', |
|
430 | + ) |
|
431 | + ); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Test that the imported user visit hook respects CubePoints's started periods. |
|
436 | + * |
|
437 | + * @since 1.2.0 |
|
438 | + * |
|
439 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
440 | + * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
441 | + */ |
|
442 | + public function test_import_periodic_points_respect_old_periods() { |
|
443 | + |
|
444 | + if ( version_compare( $GLOBALS['wp_version'], '4.5', '>=' ) ) { |
|
445 | + $this->setExpectedDeprecated( 'get_currentuserinfo' ); |
|
446 | + } |
|
447 | + |
|
448 | + cp_module_activation_set( 'dailypoints', 'active' ); |
|
449 | + |
|
450 | + update_option( 'cp_module_dailypoints_points', 30 ); |
|
451 | + update_option( 'cp_module_dailypoints_time', DAY_IN_SECONDS ); |
|
452 | + |
|
453 | + $user_id = $this->factory->user->create(); |
|
454 | + |
|
455 | + wp_set_current_user( $user_id ); |
|
456 | + |
|
457 | + $this->assertEquals( 100, cp_getPoints( $user_id ) ); |
|
458 | + |
|
459 | + cp_module_dailypoints_checkTimer(); |
|
460 | + |
|
461 | + $this->assertEquals( 130, cp_getPoints( $user_id ) ); |
|
462 | + |
|
463 | + // Running again shouldn't hit again. |
|
464 | + cp_module_dailypoints_checkTimer(); |
|
465 | + |
|
466 | + $this->assertEquals( 130, cp_getPoints( $user_id ) ); |
|
467 | + |
|
468 | + $this->do_points_import( 'settings' ); |
|
469 | + $this->do_points_import( 'user_points' ); |
|
470 | + $this->do_points_import( 'logs' ); |
|
471 | + |
|
472 | + $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
473 | + |
|
474 | + wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
475 | + |
|
476 | + $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
477 | + |
|
478 | + // Fast-forward and try again. |
|
479 | + global $wpdb; |
|
480 | + |
|
481 | + $id = $wpdb->get_var( |
|
482 | + " |
|
483 | 483 | SELECT `id` |
484 | 484 | FROM `{$wpdb->wordpoints_points_logs}` |
485 | 485 | ORDER BY `id` DESC |
486 | 486 | LIMIT 1 |
487 | 487 | " |
488 | - ); |
|
489 | - |
|
490 | - // Don't go all the way yet. |
|
491 | - $updated = $wpdb->update( |
|
492 | - $wpdb->wordpoints_points_logs |
|
493 | - , array( 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) - DAY_IN_SECONDS + HOUR_IN_SECONDS ) ) |
|
494 | - , array( 'id' => $id ) |
|
495 | - , array( '%s' ) |
|
496 | - , array( '%d' ) |
|
497 | - ); |
|
498 | - |
|
499 | - $this->assertEquals( 1, $updated ); |
|
500 | - |
|
501 | - // The periods cache will still hold the old date. |
|
502 | - $this->flush_cache(); |
|
503 | - |
|
504 | - wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
505 | - |
|
506 | - // Points should have been awarded again yet. |
|
507 | - $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
508 | - |
|
509 | - // This time go all the way. |
|
510 | - $updated = $wpdb->update( |
|
511 | - $wpdb->wordpoints_points_logs |
|
512 | - , array( 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) - DAY_IN_SECONDS - 1 ) ) |
|
513 | - , array( 'id' => $id ) |
|
514 | - , array( '%s' ) |
|
515 | - , array( '%d' ) |
|
516 | - ); |
|
517 | - |
|
518 | - $this->assertEquals( 1, $updated ); |
|
519 | - |
|
520 | - // The periods cache will still hold the old date. |
|
521 | - $this->flush_cache(); |
|
522 | - |
|
523 | - wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
524 | - |
|
525 | - // Points should have been awarded again. |
|
526 | - $this->assertEquals( 160, wordpoints_get_points( $user_id, 'points' ) ); |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Test the imported periods when the site has a positive GMT offset. |
|
531 | - * |
|
532 | - * @since 1.2.0 |
|
533 | - * |
|
534 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
535 | - * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
536 | - */ |
|
537 | - public function test_import_periods_positive_gmt_offset() { |
|
538 | - |
|
539 | - update_option( 'gmt_offset', 5 ); |
|
540 | - |
|
541 | - $this->test_import_periodic_points_respect_old_periods(); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Test the imported periods when the site has a negative GMT offset. |
|
546 | - * |
|
547 | - * @since 1.2.0 |
|
548 | - * |
|
549 | - * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
550 | - * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
551 | - */ |
|
552 | - public function test_import_periods_negative_gmt_offset() { |
|
553 | - |
|
554 | - update_option( 'gmt_offset', -5 ); |
|
555 | - |
|
556 | - $this->test_import_periodic_points_respect_old_periods(); |
|
557 | - } |
|
558 | - |
|
559 | - /** |
|
560 | - * Test importing the user's points. |
|
561 | - * |
|
562 | - * @since 1.0.0 |
|
563 | - * |
|
564 | - * @covers WordPoints_CubePoints_Importer::import_user_points |
|
565 | - * @covers WordPoints_CubePoints_Importer::get_next_user_points_batch |
|
566 | - */ |
|
567 | - public function test_import_user_points() { |
|
568 | - |
|
569 | - $user_points = array(); |
|
570 | - |
|
571 | - foreach ( array( 20, 10, 45 ) as $points ) { |
|
572 | - |
|
573 | - $user_id = $this->factory->user->create(); |
|
574 | - cp_updatePoints( $user_id, $points ); |
|
575 | - $user_points[ $user_id ] = $points; |
|
576 | - } |
|
577 | - |
|
578 | - $this->do_points_import( 'user_points' ); |
|
579 | - |
|
580 | - foreach ( $user_points as $user_id => $points ) { |
|
581 | - $this->assertEquals( $points, wordpoints_get_points( $user_id, 'points' ) ); |
|
582 | - } |
|
583 | - } |
|
584 | - |
|
585 | - /** |
|
586 | - * Test importing the points logs. |
|
587 | - * |
|
588 | - * @since 1.0.0 |
|
589 | - * |
|
590 | - * @covers WordPoints_CubePoints_Importer::import_points_logs |
|
591 | - * @covers WordPoints_CubePoints_Importer::import_points_log |
|
592 | - * @covers WordPoints_CubePoints_Importer::get_next_points_logs_batch |
|
593 | - * @covers WordPoints_CubePoints_Importer::render_points_log_text |
|
594 | - */ |
|
595 | - public function test_import_points_logs() { |
|
596 | - |
|
597 | - remove_action( 'publish_post', 'cp_newPost' ); |
|
598 | - |
|
599 | - $user_id = $this->factory->user->create(); |
|
600 | - cp_points( 'misc', $user_id, 10, 'Testing things.' ); |
|
601 | - |
|
602 | - $user_id_2 = $this->factory->user->create(); |
|
603 | - $post_id = $this->factory->post->create(); |
|
604 | - cp_points( 'post', $user_id_2, 25, $post_id ); |
|
605 | - |
|
606 | - $this->do_points_import( 'logs' ); |
|
607 | - |
|
608 | - $query = new WordPoints_Points_Logs_Query( array( 'orderby' => 'id' ) ); |
|
609 | - $logs = $query->get(); |
|
610 | - |
|
611 | - $this->assertCount( 4, $logs ); |
|
612 | - |
|
613 | - $log = $logs[2]; |
|
614 | - |
|
615 | - $this->assertEquals( $user_id, $log->user_id ); |
|
616 | - $this->assertEquals( 10, $log->points ); |
|
617 | - $this->assertEquals( 'Testing things.', $log->text ); |
|
618 | - $this->assertEquals( 'cubepoints-misc', $log->log_type ); |
|
619 | - $this->assertEquals( 'points', $log->points_type ); |
|
620 | - $this->assertEquals( 'misc', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
621 | - $this->assertEquals( 'Testing things.', wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
622 | - |
|
623 | - $log = $logs[0]; |
|
624 | - |
|
625 | - $this->assertEquals( $user_id_2, $log->user_id ); |
|
626 | - $this->assertEquals( 25, $log->points ); |
|
627 | - $this->assertStringMatchesFormat( 'Post on "<a href="%s">Post title %s</a>"', $log->text ); |
|
628 | - $this->assertEquals( 'cubepoints-post', $log->log_type ); |
|
629 | - $this->assertEquals( 'points', $log->points_type ); |
|
630 | - $this->assertEquals( 'post', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
631 | - $this->assertEquals( $post_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
632 | - $this->assertEquals( $post_id, wordpoints_get_points_log_meta( $log->id, 'post', true ) ); |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * Test importing points logs that have been reversed. |
|
637 | - * |
|
638 | - * @since 1.2.0 |
|
639 | - * |
|
640 | - * @covers WordPoints_CubePoints_Importer::import_points_logs |
|
641 | - * @covers WordPoints_CubePoints_Importer::import_points_log |
|
642 | - * @covers WordPoints_CubePoints_Importer::get_next_points_logs_batch |
|
643 | - * @covers WordPoints_CubePoints_Importer::render_points_log_text |
|
644 | - */ |
|
645 | - public function test_import_points_logs_reversals() { |
|
646 | - |
|
647 | - remove_action( 'publish_post', 'cp_newPost' ); |
|
648 | - remove_action( 'cp_comment_add', 'cp_module_post_author_points_comment_add' ); |
|
649 | - remove_action( 'cp_comment_remove', 'cp_module_post_author_points_comment_remove' ); |
|
650 | - |
|
651 | - update_option( 'cp_comment_points', 10 ); |
|
652 | - update_option( 'cp_del_comment_points', 10 ); |
|
653 | - |
|
654 | - $user_id = $this->factory->user->create(); |
|
655 | - $post_id = $this->factory->post->create(); |
|
656 | - |
|
657 | - $comment_id = $this->factory->comment->create( |
|
658 | - array( 'user_id' => $user_id, 'comment_post_ID' => $post_id, 'comment_approved' => 0 ) |
|
659 | - ); |
|
660 | - |
|
661 | - wp_update_comment( |
|
662 | - array( 'comment_ID' => $comment_id, 'comment_approved' => 1 ) |
|
663 | - ); |
|
664 | - |
|
665 | - $user_id_2 = $this->factory->user->create(); |
|
666 | - $comment_id_2 = $this->factory->comment->create( |
|
667 | - array( 'user_id' => $user_id_2, 'comment_post_ID' => $post_id, 'comment_approved' => 0 ) |
|
668 | - ); |
|
669 | - |
|
670 | - wp_update_comment( |
|
671 | - array( 'comment_ID' => $comment_id_2, 'comment_approved' => 1 ) |
|
672 | - ); |
|
673 | - |
|
674 | - // Now reverse the two transactions. |
|
675 | - wp_update_comment( |
|
676 | - array( 'comment_ID' => $comment_id, 'comment_approved' => 0 ) |
|
677 | - ); |
|
678 | - |
|
679 | - wp_update_comment( |
|
680 | - array( 'comment_ID' => $comment_id_2, 'comment_approved' => 0 ) |
|
681 | - ); |
|
682 | - |
|
683 | - $this->do_points_import( 'logs' ); |
|
684 | - |
|
685 | - $query = new WordPoints_Points_Logs_Query( |
|
686 | - array( 'orderby' => 'id', 'order' => 'ASC' ) |
|
687 | - ); |
|
688 | - |
|
689 | - $logs = $query->get(); |
|
690 | - |
|
691 | - $this->assertCount( 6, $logs ); |
|
692 | - |
|
693 | - // The first log will be for when the first user was created, so we skip it. |
|
694 | - $log = $logs[1]; |
|
695 | - |
|
696 | - $this->assertEquals( $user_id, $log->user_id ); |
|
697 | - $this->assertEquals( 10, $log->points ); |
|
698 | - $this->assertEquals( 'cubepoints-comment', $log->log_type ); |
|
699 | - $this->assertEquals( 'points', $log->points_type ); |
|
700 | - $this->assertEquals( 'comment', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
701 | - $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
702 | - $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
703 | - $this->assertEquals( $logs[4]->id, wordpoints_get_points_log_meta( $log->id, 'auto_reversed', true ) ); |
|
704 | - |
|
705 | - // The third log is for when the second user was created, so we skip it, too. |
|
706 | - $log = $logs[3]; |
|
707 | - |
|
708 | - $this->assertEquals( $user_id_2, $log->user_id ); |
|
709 | - $this->assertEquals( 10, $log->points ); |
|
710 | - $this->assertEquals( 'cubepoints-comment', $log->log_type ); |
|
711 | - $this->assertEquals( 'points', $log->points_type ); |
|
712 | - $this->assertEquals( 'comment', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
713 | - $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
714 | - $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
715 | - $this->assertEquals( $logs[5]->id, wordpoints_get_points_log_meta( $log->id, 'auto_reversed', true ) ); |
|
716 | - |
|
717 | - $log = $logs[4]; |
|
718 | - |
|
719 | - $this->assertEquals( $user_id, $log->user_id ); |
|
720 | - $this->assertEquals( -10, $log->points ); |
|
721 | - $this->assertEquals( 'cubepoints-comment_remove', $log->log_type ); |
|
722 | - $this->assertEquals( 'points', $log->points_type ); |
|
723 | - $this->assertEquals( 'comment_remove', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
724 | - $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
725 | - $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
726 | - $this->assertEquals( $logs[1]->id, wordpoints_get_points_log_meta( $log->id, 'original_log_id', true ) ); |
|
727 | - |
|
728 | - $log = $logs[5]; |
|
729 | - |
|
730 | - $this->assertEquals( $user_id_2, $log->user_id ); |
|
731 | - $this->assertEquals( -10, $log->points ); |
|
732 | - $this->assertEquals( 'cubepoints-comment_remove', $log->log_type ); |
|
733 | - $this->assertEquals( 'points', $log->points_type ); |
|
734 | - $this->assertEquals( 'comment_remove', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
735 | - $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
736 | - $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
737 | - $this->assertEquals( $logs[3]->id, wordpoints_get_points_log_meta( $log->id, 'original_log_id', true ) ); |
|
738 | - } |
|
739 | - |
|
740 | - /** |
|
741 | - * Test that ranks are imported. |
|
742 | - * |
|
743 | - * @since 1.1.0 |
|
744 | - * |
|
745 | - * @covers WordPoints_CubePoints_Importer::import_ranks |
|
746 | - */ |
|
747 | - public function test_ranks_import() { |
|
748 | - |
|
749 | - update_option( |
|
750 | - 'cp_module_ranks_data' |
|
751 | - , array( 0 => 'Newbie', 1000 => 'Biggie', 5000 => 'Oldie' ) |
|
752 | - ); |
|
753 | - |
|
754 | - wordpoints_register_points_ranks(); |
|
755 | - |
|
756 | - $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
757 | - |
|
758 | - $this->importer->do_import( |
|
759 | - array( |
|
760 | - 'ranks' => array( |
|
761 | - 'ranks' => '1', |
|
762 | - '_data' => array( 'rank_group' => 'points_type-points' ), |
|
763 | - ), |
|
764 | - ) |
|
765 | - , $feedback |
|
766 | - ); |
|
767 | - |
|
768 | - $this->assertCount( 4, $feedback->messages['info'] ); |
|
769 | - $this->assertCount( 1, $feedback->messages['success'] ); |
|
770 | - |
|
771 | - $group = WordPoints_Rank_Groups::get_group( 'points_type-points' ); |
|
772 | - |
|
773 | - $base_rank = wordpoints_get_rank( $group->get_rank( 0 ) ); |
|
774 | - $this->assertEquals( 'base', $base_rank->type ); |
|
775 | - $this->assertEquals( 'Newbie', $base_rank->name ); |
|
776 | - |
|
777 | - $second_rank = wordpoints_get_rank( $group->get_rank( 1 ) ); |
|
778 | - $this->assertEquals( 1000, $second_rank->points ); |
|
779 | - $this->assertEquals( 'Biggie', $second_rank->name ); |
|
780 | - |
|
781 | - $third_rank = wordpoints_get_rank( $group->get_rank( 2 ) ); |
|
782 | - $this->assertEquals( 5000, $third_rank->points ); |
|
783 | - $this->assertEquals( 'Oldie', $third_rank->name ); |
|
784 | - } |
|
785 | - |
|
786 | - /** |
|
787 | - * Test that there is an error if there are no ranks import. |
|
788 | - * |
|
789 | - * @since 1.1.0 |
|
790 | - * |
|
791 | - * @covers WordPoints_CubePoints_Importer::import_ranks |
|
792 | - */ |
|
793 | - public function test_error_if_no_ranks_to_import() { |
|
794 | - |
|
795 | - wordpoints_register_points_ranks(); |
|
796 | - |
|
797 | - $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
798 | - |
|
799 | - $this->importer->do_import( |
|
800 | - array( |
|
801 | - 'ranks' => array( |
|
802 | - 'ranks' => '1', |
|
803 | - '_data' => array( 'rank_group' => 'points_type-points' ), |
|
804 | - ), |
|
805 | - ) |
|
806 | - , $feedback |
|
807 | - ); |
|
808 | - |
|
809 | - $this->assertCount( 4, $feedback->messages['info'] ); |
|
810 | - $this->assertCount( 1, $feedback->messages['error'] ); |
|
811 | - |
|
812 | - } |
|
813 | - |
|
814 | - // |
|
815 | - // Helpers. |
|
816 | - // |
|
817 | - |
|
818 | - /** |
|
819 | - * Do the import for the points settings. |
|
820 | - * |
|
821 | - * @since 1.1.0 |
|
822 | - * |
|
823 | - * @param string $type The type of points import. |
|
824 | - */ |
|
825 | - protected function do_points_import( $type ) { |
|
826 | - |
|
827 | - $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
828 | - |
|
829 | - $this->importer->do_import( |
|
830 | - array( |
|
831 | - 'points' => array( |
|
832 | - $type => '1', |
|
833 | - '_data' => array( 'points_type' => 'points' ), |
|
834 | - ), |
|
835 | - ) |
|
836 | - , $feedback |
|
837 | - ); |
|
838 | - |
|
839 | - $this->assertCount( 4, $feedback->messages['info'] ); |
|
840 | - $this->assertCount( 1, $feedback->messages['success'] ); |
|
841 | - } |
|
842 | - |
|
843 | - /** |
|
844 | - * Assert that a hook was imported. |
|
845 | - * |
|
846 | - * Actually just checks that the hook exists. |
|
847 | - * |
|
848 | - * @since 1.1.0 |
|
849 | - * @since 1.2.0 Now just accepts a single parameter, $settings. |
|
850 | - * |
|
851 | - * @param array $settings The expected reaction settings. |
|
852 | - */ |
|
853 | - protected function assertHookImported( $settings ) { |
|
854 | - |
|
855 | - $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
856 | - |
|
857 | - $reactions = $reaction_store->get_reactions_to_event( $settings['event'] ); |
|
858 | - |
|
859 | - $this->assertNotEmpty( $reactions ); |
|
860 | - |
|
861 | - foreach ( $reactions as $reaction ) { |
|
862 | - if ( $settings === $reaction->get_all_meta() ) { |
|
863 | - $this->assertEquals( $settings, $reaction->get_all_meta() ); |
|
864 | - return; |
|
865 | - } |
|
866 | - } |
|
867 | - |
|
868 | - $this->assertEquals( $settings, $reaction->get_all_meta() ); |
|
869 | - } |
|
488 | + ); |
|
489 | + |
|
490 | + // Don't go all the way yet. |
|
491 | + $updated = $wpdb->update( |
|
492 | + $wpdb->wordpoints_points_logs |
|
493 | + , array( 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) - DAY_IN_SECONDS + HOUR_IN_SECONDS ) ) |
|
494 | + , array( 'id' => $id ) |
|
495 | + , array( '%s' ) |
|
496 | + , array( '%d' ) |
|
497 | + ); |
|
498 | + |
|
499 | + $this->assertEquals( 1, $updated ); |
|
500 | + |
|
501 | + // The periods cache will still hold the old date. |
|
502 | + $this->flush_cache(); |
|
503 | + |
|
504 | + wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
505 | + |
|
506 | + // Points should have been awarded again yet. |
|
507 | + $this->assertEquals( 130, wordpoints_get_points( $user_id, 'points' ) ); |
|
508 | + |
|
509 | + // This time go all the way. |
|
510 | + $updated = $wpdb->update( |
|
511 | + $wpdb->wordpoints_points_logs |
|
512 | + , array( 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp', true ) - DAY_IN_SECONDS - 1 ) ) |
|
513 | + , array( 'id' => $id ) |
|
514 | + , array( '%s' ) |
|
515 | + , array( '%d' ) |
|
516 | + ); |
|
517 | + |
|
518 | + $this->assertEquals( 1, $updated ); |
|
519 | + |
|
520 | + // The periods cache will still hold the old date. |
|
521 | + $this->flush_cache(); |
|
522 | + |
|
523 | + wordpoints_hooks()->get_sub_app( 'router' )->{'wp,10'}(); |
|
524 | + |
|
525 | + // Points should have been awarded again. |
|
526 | + $this->assertEquals( 160, wordpoints_get_points( $user_id, 'points' ) ); |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Test the imported periods when the site has a positive GMT offset. |
|
531 | + * |
|
532 | + * @since 1.2.0 |
|
533 | + * |
|
534 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
535 | + * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
536 | + */ |
|
537 | + public function test_import_periods_positive_gmt_offset() { |
|
538 | + |
|
539 | + update_option( 'gmt_offset', 5 ); |
|
540 | + |
|
541 | + $this->test_import_periodic_points_respect_old_periods(); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Test the imported periods when the site has a negative GMT offset. |
|
546 | + * |
|
547 | + * @since 1.2.0 |
|
548 | + * |
|
549 | + * @covers WordPoints_CubePoints_Importer::import_points_settings |
|
550 | + * @covers WordPoints_CubePoints_Importer::import_daily_points_hook |
|
551 | + */ |
|
552 | + public function test_import_periods_negative_gmt_offset() { |
|
553 | + |
|
554 | + update_option( 'gmt_offset', -5 ); |
|
555 | + |
|
556 | + $this->test_import_periodic_points_respect_old_periods(); |
|
557 | + } |
|
558 | + |
|
559 | + /** |
|
560 | + * Test importing the user's points. |
|
561 | + * |
|
562 | + * @since 1.0.0 |
|
563 | + * |
|
564 | + * @covers WordPoints_CubePoints_Importer::import_user_points |
|
565 | + * @covers WordPoints_CubePoints_Importer::get_next_user_points_batch |
|
566 | + */ |
|
567 | + public function test_import_user_points() { |
|
568 | + |
|
569 | + $user_points = array(); |
|
570 | + |
|
571 | + foreach ( array( 20, 10, 45 ) as $points ) { |
|
572 | + |
|
573 | + $user_id = $this->factory->user->create(); |
|
574 | + cp_updatePoints( $user_id, $points ); |
|
575 | + $user_points[ $user_id ] = $points; |
|
576 | + } |
|
577 | + |
|
578 | + $this->do_points_import( 'user_points' ); |
|
579 | + |
|
580 | + foreach ( $user_points as $user_id => $points ) { |
|
581 | + $this->assertEquals( $points, wordpoints_get_points( $user_id, 'points' ) ); |
|
582 | + } |
|
583 | + } |
|
584 | + |
|
585 | + /** |
|
586 | + * Test importing the points logs. |
|
587 | + * |
|
588 | + * @since 1.0.0 |
|
589 | + * |
|
590 | + * @covers WordPoints_CubePoints_Importer::import_points_logs |
|
591 | + * @covers WordPoints_CubePoints_Importer::import_points_log |
|
592 | + * @covers WordPoints_CubePoints_Importer::get_next_points_logs_batch |
|
593 | + * @covers WordPoints_CubePoints_Importer::render_points_log_text |
|
594 | + */ |
|
595 | + public function test_import_points_logs() { |
|
596 | + |
|
597 | + remove_action( 'publish_post', 'cp_newPost' ); |
|
598 | + |
|
599 | + $user_id = $this->factory->user->create(); |
|
600 | + cp_points( 'misc', $user_id, 10, 'Testing things.' ); |
|
601 | + |
|
602 | + $user_id_2 = $this->factory->user->create(); |
|
603 | + $post_id = $this->factory->post->create(); |
|
604 | + cp_points( 'post', $user_id_2, 25, $post_id ); |
|
605 | + |
|
606 | + $this->do_points_import( 'logs' ); |
|
607 | + |
|
608 | + $query = new WordPoints_Points_Logs_Query( array( 'orderby' => 'id' ) ); |
|
609 | + $logs = $query->get(); |
|
610 | + |
|
611 | + $this->assertCount( 4, $logs ); |
|
612 | + |
|
613 | + $log = $logs[2]; |
|
614 | + |
|
615 | + $this->assertEquals( $user_id, $log->user_id ); |
|
616 | + $this->assertEquals( 10, $log->points ); |
|
617 | + $this->assertEquals( 'Testing things.', $log->text ); |
|
618 | + $this->assertEquals( 'cubepoints-misc', $log->log_type ); |
|
619 | + $this->assertEquals( 'points', $log->points_type ); |
|
620 | + $this->assertEquals( 'misc', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
621 | + $this->assertEquals( 'Testing things.', wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
622 | + |
|
623 | + $log = $logs[0]; |
|
624 | + |
|
625 | + $this->assertEquals( $user_id_2, $log->user_id ); |
|
626 | + $this->assertEquals( 25, $log->points ); |
|
627 | + $this->assertStringMatchesFormat( 'Post on "<a href="%s">Post title %s</a>"', $log->text ); |
|
628 | + $this->assertEquals( 'cubepoints-post', $log->log_type ); |
|
629 | + $this->assertEquals( 'points', $log->points_type ); |
|
630 | + $this->assertEquals( 'post', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
631 | + $this->assertEquals( $post_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
632 | + $this->assertEquals( $post_id, wordpoints_get_points_log_meta( $log->id, 'post', true ) ); |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * Test importing points logs that have been reversed. |
|
637 | + * |
|
638 | + * @since 1.2.0 |
|
639 | + * |
|
640 | + * @covers WordPoints_CubePoints_Importer::import_points_logs |
|
641 | + * @covers WordPoints_CubePoints_Importer::import_points_log |
|
642 | + * @covers WordPoints_CubePoints_Importer::get_next_points_logs_batch |
|
643 | + * @covers WordPoints_CubePoints_Importer::render_points_log_text |
|
644 | + */ |
|
645 | + public function test_import_points_logs_reversals() { |
|
646 | + |
|
647 | + remove_action( 'publish_post', 'cp_newPost' ); |
|
648 | + remove_action( 'cp_comment_add', 'cp_module_post_author_points_comment_add' ); |
|
649 | + remove_action( 'cp_comment_remove', 'cp_module_post_author_points_comment_remove' ); |
|
650 | + |
|
651 | + update_option( 'cp_comment_points', 10 ); |
|
652 | + update_option( 'cp_del_comment_points', 10 ); |
|
653 | + |
|
654 | + $user_id = $this->factory->user->create(); |
|
655 | + $post_id = $this->factory->post->create(); |
|
656 | + |
|
657 | + $comment_id = $this->factory->comment->create( |
|
658 | + array( 'user_id' => $user_id, 'comment_post_ID' => $post_id, 'comment_approved' => 0 ) |
|
659 | + ); |
|
660 | + |
|
661 | + wp_update_comment( |
|
662 | + array( 'comment_ID' => $comment_id, 'comment_approved' => 1 ) |
|
663 | + ); |
|
664 | + |
|
665 | + $user_id_2 = $this->factory->user->create(); |
|
666 | + $comment_id_2 = $this->factory->comment->create( |
|
667 | + array( 'user_id' => $user_id_2, 'comment_post_ID' => $post_id, 'comment_approved' => 0 ) |
|
668 | + ); |
|
669 | + |
|
670 | + wp_update_comment( |
|
671 | + array( 'comment_ID' => $comment_id_2, 'comment_approved' => 1 ) |
|
672 | + ); |
|
673 | + |
|
674 | + // Now reverse the two transactions. |
|
675 | + wp_update_comment( |
|
676 | + array( 'comment_ID' => $comment_id, 'comment_approved' => 0 ) |
|
677 | + ); |
|
678 | + |
|
679 | + wp_update_comment( |
|
680 | + array( 'comment_ID' => $comment_id_2, 'comment_approved' => 0 ) |
|
681 | + ); |
|
682 | + |
|
683 | + $this->do_points_import( 'logs' ); |
|
684 | + |
|
685 | + $query = new WordPoints_Points_Logs_Query( |
|
686 | + array( 'orderby' => 'id', 'order' => 'ASC' ) |
|
687 | + ); |
|
688 | + |
|
689 | + $logs = $query->get(); |
|
690 | + |
|
691 | + $this->assertCount( 6, $logs ); |
|
692 | + |
|
693 | + // The first log will be for when the first user was created, so we skip it. |
|
694 | + $log = $logs[1]; |
|
695 | + |
|
696 | + $this->assertEquals( $user_id, $log->user_id ); |
|
697 | + $this->assertEquals( 10, $log->points ); |
|
698 | + $this->assertEquals( 'cubepoints-comment', $log->log_type ); |
|
699 | + $this->assertEquals( 'points', $log->points_type ); |
|
700 | + $this->assertEquals( 'comment', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
701 | + $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
702 | + $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
703 | + $this->assertEquals( $logs[4]->id, wordpoints_get_points_log_meta( $log->id, 'auto_reversed', true ) ); |
|
704 | + |
|
705 | + // The third log is for when the second user was created, so we skip it, too. |
|
706 | + $log = $logs[3]; |
|
707 | + |
|
708 | + $this->assertEquals( $user_id_2, $log->user_id ); |
|
709 | + $this->assertEquals( 10, $log->points ); |
|
710 | + $this->assertEquals( 'cubepoints-comment', $log->log_type ); |
|
711 | + $this->assertEquals( 'points', $log->points_type ); |
|
712 | + $this->assertEquals( 'comment', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
713 | + $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
714 | + $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
715 | + $this->assertEquals( $logs[5]->id, wordpoints_get_points_log_meta( $log->id, 'auto_reversed', true ) ); |
|
716 | + |
|
717 | + $log = $logs[4]; |
|
718 | + |
|
719 | + $this->assertEquals( $user_id, $log->user_id ); |
|
720 | + $this->assertEquals( -10, $log->points ); |
|
721 | + $this->assertEquals( 'cubepoints-comment_remove', $log->log_type ); |
|
722 | + $this->assertEquals( 'points', $log->points_type ); |
|
723 | + $this->assertEquals( 'comment_remove', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
724 | + $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
725 | + $this->assertEquals( $comment_id, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
726 | + $this->assertEquals( $logs[1]->id, wordpoints_get_points_log_meta( $log->id, 'original_log_id', true ) ); |
|
727 | + |
|
728 | + $log = $logs[5]; |
|
729 | + |
|
730 | + $this->assertEquals( $user_id_2, $log->user_id ); |
|
731 | + $this->assertEquals( -10, $log->points ); |
|
732 | + $this->assertEquals( 'cubepoints-comment_remove', $log->log_type ); |
|
733 | + $this->assertEquals( 'points', $log->points_type ); |
|
734 | + $this->assertEquals( 'comment_remove', wordpoints_get_points_log_meta( $log->id, 'cubepoints_type', true ) ); |
|
735 | + $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'cubepoints_data', true ) ); |
|
736 | + $this->assertEquals( $comment_id_2, wordpoints_get_points_log_meta( $log->id, 'comment', true ) ); |
|
737 | + $this->assertEquals( $logs[3]->id, wordpoints_get_points_log_meta( $log->id, 'original_log_id', true ) ); |
|
738 | + } |
|
739 | + |
|
740 | + /** |
|
741 | + * Test that ranks are imported. |
|
742 | + * |
|
743 | + * @since 1.1.0 |
|
744 | + * |
|
745 | + * @covers WordPoints_CubePoints_Importer::import_ranks |
|
746 | + */ |
|
747 | + public function test_ranks_import() { |
|
748 | + |
|
749 | + update_option( |
|
750 | + 'cp_module_ranks_data' |
|
751 | + , array( 0 => 'Newbie', 1000 => 'Biggie', 5000 => 'Oldie' ) |
|
752 | + ); |
|
753 | + |
|
754 | + wordpoints_register_points_ranks(); |
|
755 | + |
|
756 | + $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
757 | + |
|
758 | + $this->importer->do_import( |
|
759 | + array( |
|
760 | + 'ranks' => array( |
|
761 | + 'ranks' => '1', |
|
762 | + '_data' => array( 'rank_group' => 'points_type-points' ), |
|
763 | + ), |
|
764 | + ) |
|
765 | + , $feedback |
|
766 | + ); |
|
767 | + |
|
768 | + $this->assertCount( 4, $feedback->messages['info'] ); |
|
769 | + $this->assertCount( 1, $feedback->messages['success'] ); |
|
770 | + |
|
771 | + $group = WordPoints_Rank_Groups::get_group( 'points_type-points' ); |
|
772 | + |
|
773 | + $base_rank = wordpoints_get_rank( $group->get_rank( 0 ) ); |
|
774 | + $this->assertEquals( 'base', $base_rank->type ); |
|
775 | + $this->assertEquals( 'Newbie', $base_rank->name ); |
|
776 | + |
|
777 | + $second_rank = wordpoints_get_rank( $group->get_rank( 1 ) ); |
|
778 | + $this->assertEquals( 1000, $second_rank->points ); |
|
779 | + $this->assertEquals( 'Biggie', $second_rank->name ); |
|
780 | + |
|
781 | + $third_rank = wordpoints_get_rank( $group->get_rank( 2 ) ); |
|
782 | + $this->assertEquals( 5000, $third_rank->points ); |
|
783 | + $this->assertEquals( 'Oldie', $third_rank->name ); |
|
784 | + } |
|
785 | + |
|
786 | + /** |
|
787 | + * Test that there is an error if there are no ranks import. |
|
788 | + * |
|
789 | + * @since 1.1.0 |
|
790 | + * |
|
791 | + * @covers WordPoints_CubePoints_Importer::import_ranks |
|
792 | + */ |
|
793 | + public function test_error_if_no_ranks_to_import() { |
|
794 | + |
|
795 | + wordpoints_register_points_ranks(); |
|
796 | + |
|
797 | + $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
798 | + |
|
799 | + $this->importer->do_import( |
|
800 | + array( |
|
801 | + 'ranks' => array( |
|
802 | + 'ranks' => '1', |
|
803 | + '_data' => array( 'rank_group' => 'points_type-points' ), |
|
804 | + ), |
|
805 | + ) |
|
806 | + , $feedback |
|
807 | + ); |
|
808 | + |
|
809 | + $this->assertCount( 4, $feedback->messages['info'] ); |
|
810 | + $this->assertCount( 1, $feedback->messages['error'] ); |
|
811 | + |
|
812 | + } |
|
813 | + |
|
814 | + // |
|
815 | + // Helpers. |
|
816 | + // |
|
817 | + |
|
818 | + /** |
|
819 | + * Do the import for the points settings. |
|
820 | + * |
|
821 | + * @since 1.1.0 |
|
822 | + * |
|
823 | + * @param string $type The type of points import. |
|
824 | + */ |
|
825 | + protected function do_points_import( $type ) { |
|
826 | + |
|
827 | + $feedback = new WordPoints_Importer_Tests_Feedback(); |
|
828 | + |
|
829 | + $this->importer->do_import( |
|
830 | + array( |
|
831 | + 'points' => array( |
|
832 | + $type => '1', |
|
833 | + '_data' => array( 'points_type' => 'points' ), |
|
834 | + ), |
|
835 | + ) |
|
836 | + , $feedback |
|
837 | + ); |
|
838 | + |
|
839 | + $this->assertCount( 4, $feedback->messages['info'] ); |
|
840 | + $this->assertCount( 1, $feedback->messages['success'] ); |
|
841 | + } |
|
842 | + |
|
843 | + /** |
|
844 | + * Assert that a hook was imported. |
|
845 | + * |
|
846 | + * Actually just checks that the hook exists. |
|
847 | + * |
|
848 | + * @since 1.1.0 |
|
849 | + * @since 1.2.0 Now just accepts a single parameter, $settings. |
|
850 | + * |
|
851 | + * @param array $settings The expected reaction settings. |
|
852 | + */ |
|
853 | + protected function assertHookImported( $settings ) { |
|
854 | + |
|
855 | + $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
856 | + |
|
857 | + $reactions = $reaction_store->get_reactions_to_event( $settings['event'] ); |
|
858 | + |
|
859 | + $this->assertNotEmpty( $reactions ); |
|
860 | + |
|
861 | + foreach ( $reactions as $reaction ) { |
|
862 | + if ( $settings === $reaction->get_all_meta() ) { |
|
863 | + $this->assertEquals( $settings, $reaction->get_all_meta() ); |
|
864 | + return; |
|
865 | + } |
|
866 | + } |
|
867 | + |
|
868 | + $this->assertEquals( $settings, $reaction->get_all_meta() ); |
|
869 | + } |
|
870 | 870 | } |
871 | 871 | |
872 | 872 | // EOF |
@@ -13,7 +13,7 @@ |
||
13 | 13 | * @since 1.0.0 |
14 | 14 | */ |
15 | 15 | class AcceptanceTester extends \WordPoints\Tests\Codeception\AcceptanceTester { |
16 | - // Add any methods here that you want. |
|
16 | + // Add any methods here that you want. |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | // EOF |