PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.6
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.6
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / includes / admin / upgrades / 1.5.1.php

1.5.1.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.6, at includes/admin/upgrades/1.5.1.php

436 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * 1.5.1 Upgrades
4 *
5 * @package GamiPress\Admin\Upgrades\1.5.1
6 * @since 1.5.1
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Return 1.5.1 as last required upgrade
13 *
14 * @return string
15 */
16 function gamipress_151_is_last_required_upgrade() {
17
18 return '1.5.1';
19
20 }
21 add_filter( 'gamipress_get_last_required_upgrade', 'gamipress_151_is_last_required_upgrade', 151 );
22
23 /**
24 * Process 1.5.1 upgrades
25 *
26 * @param string $stored_version
27 *
28 * @return string
29 */
30 function gamipress_151_upgrades( $stored_version ) {
31
32 // Already upgrade
33 if ( version_compare( $stored_version, '1.5.1', '>=' ) ) {
34 return $stored_version;
35 }
36
37 // Check if there is something to migrate
38 $upgrade_check = gamipress_151_maybe_upgrade();
39
40 if( $upgrade_check === 0 ) {
41
42 // There is nothing to update, so upgrade
43 $stored_version = '1.5.1';
44
45 } else if( is_gamipress_upgrade_completed( 'update_requirements_relationships' ) && is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) {
46
47 // Migrations are finished, so upgrade
48 $stored_version = '1.5.1';
49
50 }
51
52 return $stored_version;
53
54 }
55 add_filter( 'gamipress_process_upgrades', 'gamipress_151_upgrades', 151 );
56
57 /**
58 * 1.5.1 upgrades notices
59 */
60 function gamipress_151_upgrades_notices() {
61
62 // Bail if GamiPress is active network wide and we are not in main site
63 if( gamipress_is_network_wide_active() && ! is_main_site() ) {
64 return;
65 }
66
67 // Check user permissions
68 if ( ! current_user_can( gamipress_get_manager_capability() ) ) {
69 return;
70 }
71
72 // Already upgraded!
73 if( is_gamipress_upgraded_to( '1.5.1' ) ) {
74 return;
75 }
76
77 $running_upgrade = get_option( 'gamipress_running_upgrade' );
78
79 // Other upgrade already running
80 if( $running_upgrade && $running_upgrade !== '1.5.1' ) {
81 return;
82 }
83
84 if( ! is_gamipress_upgrade_completed( 'update_requirements_relationships' )
85 || ! is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) :
86 ?>
87
88 <div id="gamipress-upgrade-notice" class="updated">
89
90 <?php if( $running_upgrade === '1.5.1' ) : ?>
91
92 <p>
93 <?php _e( 'Upgrading GamiPress database...', 'gamipress' ); ?>
94 </p>
95 <div class="gamipress-upgrade-progress" data-running-upgrade="1.5.1">
96 <div class="gamipress-upgrade-progress-bar" style="width: 0%;"></div>
97 </div>
98
99 <?php else : ?>
100
101 <p>
102 <?php _e( 'GamiPress needs to upgrade the database. <strong>Please backup your database before starting this upgrade.</strong> This upgrade routine will be making changes to the database that are not reversible.', 'gamipress' ); ?>
103 </p>
104 <p>
105 <a href="javascript:void(0);" onClick="jQuery(this).parent().next('p').slideToggle();" class="button"><?php _e( 'Learn more about this upgrade', 'gamipress' ); ?></a>
106 <a href="javascript:void(0);" onClick="gamipress_start_upgrade('1.5.1')" class="button button-primary"><?php _e( 'Start the upgrade', 'gamipress' ); ?></a>
107 </p>
108 <p style="display: none;">
109 <?php _e( '<strong>About this upgrade:</strong><br />This is a <strong><em>mandatory</em></strong> update that will update all requirements relationships (achievements and their steps, points types and their automatic awards and deducts, ranks and their requirements).', 'gamipress' ); ?>
110 <br>
111 <?php _e( 'On GamiPress 1.5.1, an internal library to handle relationships has been removed to make relationships <strong>WordPress based</strong> instead.', 'gamipress' ); ?>
112 <br>
113 <?php _e( 'Removing this library will improve the <strong>compatibility</strong> of GamiPress with other plugins and themes as well as will <strong>speed up</strong> all relationship related queries making GamiPress much faster!', 'gamipress' ); ?>
114 </p>
115
116 <?php endif; ?>
117
118 </div>
119
120 <?php
121 endif;
122
123 }
124 add_action( 'admin_notices', 'gamipress_151_upgrades_notices' );
125
126 /**
127 * Return the number of entries to upgrade
128 *
129 * @return int
130 */
131 function gamipress_151_upgrade_size() {
132
133 global $wpdb;
134
135 $upgrade_size = 0;
136
137 $p2p = ( property_exists( $wpdb, 'p2p' ) ? $wpdb->p2p : $wpdb->prefix . 'p2p' );
138
139 // Multisite support
140 if( gamipress_is_network_wide_active() ) {
141 $p2p = $wpdb->base_prefix . 'p2p';
142 }
143
144 // Extra check for new installs
145 if( ! gamipress_database_table_exists( $p2p ) ) {
146 return 0;
147 }
148
149 // Retrieve the count of post upgrade
150 if( ! is_gamipress_upgrade_completed( 'update_requirements_relationships' ) ) {
151
152 // Setup vars
153 $posts = GamiPress()->db->posts;
154 $requirements_types = gamipress_get_requirement_types_slugs();
155
156 $posts_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$posts} AS p WHERE p.post_type IN ( '" . implode( "', '", $requirements_types ) . "' ) AND p.post_parent = 0" );
157
158 $upgrade_size += absint( $posts_count );
159
160 }
161
162 // Retrieve the count of meta data upgrade
163 if( ! is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) {
164
165 // Setup vars
166 $postmeta = GamiPress()->db->postmeta;
167
168 $meta_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$postmeta} AS pm WHERE pm.post_id NOT IN ( SELECT spm.post_id FROM {$postmeta} AS spm WHERE spm.meta_key = '_gamipress_achievement_post' ) AND pm.meta_key = '_gamipress_trigger_type' AND pm.meta_value = 'specific-achievement'" );
169
170 $upgrade_size += absint( $meta_count );
171
172 }
173
174 return $upgrade_size;
175
176 }
177
178 /**
179 * Ajax function to meet the upgrade size
180 */
181 function gamipress_ajax_151_upgrade_info() {
182 // Security check, forces to die if not security passed
183 check_ajax_referer( 'gamipress_admin', 'nonce' );
184
185 // Already upgraded
186 if ( is_gamipress_upgraded_to( '1.5.1' ) ) {
187 wp_send_json_success( array( 'upgraded' => true ) );
188 }
189
190 $upgrade_size = gamipress_151_upgrade_size();
191
192 wp_send_json_success( array( 'total' => $upgrade_size ) );
193
194 }
195 add_action( 'wp_ajax_gamipress_151_upgrade_info', 'gamipress_ajax_151_upgrade_info' );
196
197 /**
198 * Ajax process of 1.5.1 upgrades
199 */
200 function gamipress_ajax_process_151_upgrade() {
201 // Security check, forces to die if not security passed
202 check_ajax_referer( 'gamipress_admin', 'nonce' );
203
204 global $wpdb;
205
206 // Already upgraded
207 if ( is_gamipress_upgraded_to( '1.5.1' ) ) {
208 wp_send_json_success( array( 'upgraded' => true ) );
209 }
210
211 ignore_user_abort( true );
212 set_time_limit( 0 );
213
214 // Add option to meet that upgrade process has been started
215 update_option( 'gamipress_running_upgrade', '1.5.1' );
216
217 // Setup common vars
218 $posts = GamiPress()->db->posts;
219 $postmeta = GamiPress()->db->postmeta;
220 $current = isset( $_REQUEST['current'] ) ? absint( $_REQUEST['current'] ) : 0;
221 $limit = 200;
222
223 // --------------------------------------------------------
224 // Requirements relationship update
225 // --------------------------------------------------------
226 // We need to migrate all p2p {requirement-type}-to-* connections to the requirement post_parent field
227 // Also, we need to migrate the p2p meta 'order' to the requirement menu_order field
228
229 if( ! is_gamipress_upgrade_completed( 'update_requirements_relationships' ) ) {
230
231 // Migrate from p2p table to posts
232 $continue = true;
233
234 // Setup the P2P tables
235 $p2p = ( property_exists( $wpdb, 'p2p' ) ? $wpdb->p2p : $wpdb->prefix . 'p2p' );
236 $p2pmeta = ( property_exists( $wpdb, 'p2pmeta' ) ? $wpdb->p2pmeta : $wpdb->prefix . 'p2pmeta' );
237
238 // Multisite support
239 if( gamipress_is_network_wide_active() ) {
240 $p2p = $wpdb->base_prefix . 'p2p';
241 $p2pmeta = $wpdb->base_prefix . 'p2pmeta';
242 }
243
244 // Extra check for new installs
245 if( ! gamipress_database_table_exists( $p2p ) ) {
246 gamipress_set_upgrade_complete( 'update_requirements_relationships' );
247 $continue = false;
248 }
249
250 if( $continue ) {
251 // Get our requirement types
252 $requirements_types = gamipress_get_requirement_types_slugs();
253
254 // Retrieve all requirements without parent
255 $results = $wpdb->get_results( "SELECT p.ID, p.post_type FROM {$posts} AS p WHERE p.post_type IN ( '" . implode( "', '", $requirements_types ) . "' ) AND p.post_parent = 0 LIMIT {$limit}" );
256
257 foreach( $results as $post ) {
258
259 // Get the requirement relationship from the P2P table
260 // p2p_from is the requirement ID
261 // p2p_to is the parent ID (achievement, points type or rank)
262 $p2p_entry = $wpdb->get_row( "SELECT p2p.p2p_id, p2p.p2p_to FROM {$p2p} AS p2p WHERE p2p.p2p_from = {$post->ID} AND p2p.p2p_type LIKE '{$post->post_type}-to-%'" );
263
264 if( $p2p_entry ) {
265
266 // Setup the vars to update our post
267 $post_parent = $p2p_entry->p2p_to;
268 $menu_order = absint( $wpdb->get_var( "SELECT p2pmeta.meta_value FROM {$p2pmeta} AS p2pmeta WHERE p2pmeta.p2p_id = {$p2p_entry->p2p_id} AND p2pmeta.meta_key = 'order'" ) );
269
270 // Update the requirement object to meet the new relationships
271 wp_update_post( array(
272 'ID' => $post->ID,
273 'post_parent' => $post_parent,
274 'menu_order' => $menu_order,
275 ) );
276
277 } else {
278
279 // Delete this requirement since is not well connected
280 wp_delete_post( $post->ID );
281
282 }
283
284 $current++;
285 }
286
287 $posts_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$posts} AS p WHERE p.post_type IN ( '" . implode( "', '", $requirements_types ) . "' ) AND p.post_parent = 0" );
288
289 if( absint( $posts_count ) === 0 ) {
290 gamipress_set_upgrade_complete( 'update_requirements_relationships' );
291 }
292 }
293
294 }
295
296 // --------------------------------------------------------
297 // Requirements achievements relationship update
298 // --------------------------------------------------------
299 // For specific-achievement triggers is required to update relationships between {achievement-type}-to-{requirement}
300 // So, we need to move specific-achievement connection to _gamipress_achievement_post meta
301
302 if( is_gamipress_upgrade_completed( 'update_requirements_relationships' ) && ! is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) {
303
304 $continue = true;
305
306 // Extra check for new installs
307 if( ! gamipress_database_table_exists( $p2p ) ) {
308 gamipress_set_upgrade_complete( 'update_achievements_relationships' );
309 $continue = false;
310 }
311
312 if( $continue ) {
313 // Retrieve all requirements with specific-achievement trigger type and without _gamipress_achievement_post meta
314 $results = $wpdb->get_results(
315 "SELECT pm.post_id
316 FROM {$postmeta} AS pm
317 WHERE pm.post_id NOT IN ( SELECT spm.post_id FROM {$postmeta} AS spm WHERE spm.meta_key = '_gamipress_achievement_post' )
318 AND pm.meta_key = '_gamipress_trigger_type' AND pm.meta_value = 'specific-achievement'
319 LIMIT {$limit}"
320 );
321
322 foreach( $results as $result ) {
323
324 $requirement_type = gamipress_get_post_type( $result->post_id );
325 $achievement_type = gamipress_get_post_meta( $result->post_id, '_gamipress_achievement_type' );
326
327 // Get the requirement relationship from the P2P table
328 // p2p_from is the achievement ID
329 // p2p_to is the requirement ID
330 $achievement_id = absint( $wpdb->get_var( "SELECT p2p.p2p_from FROM {$p2p} AS p2p WHERE p2p.p2p_to = {$result->post_id} AND p2p.p2p_type = '{$achievement_type}-to-{$requirement_type}'" ) );
331
332 gamipress_update_post_meta( $result->post_id, '_gamipress_achievement_post', $achievement_id );
333
334 $current++;
335
336 }
337
338 $meta_count = $wpdb->get_var(
339 "SELECT COUNT(*)
340 FROM {$postmeta} AS pm
341 WHERE pm.post_id NOT IN ( SELECT spm.post_id FROM {$postmeta} AS spm WHERE spm.meta_key = '_gamipress_achievement_post' )
342 AND pm.meta_key = '_gamipress_trigger_type' AND pm.meta_value = 'specific-achievement'"
343 );
344
345 if( absint( $meta_count ) === 0 ) {
346 gamipress_set_upgrade_complete( 'update_requirements_relationships' );
347 }
348 }
349
350 }
351
352 // Successfully upgraded
353 if( is_gamipress_upgrade_completed( 'update_requirements_relationships' )
354 && is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) {
355
356 // Remove option to meet that upgrade process has been finished
357 delete_option( 'gamipress_running_upgrade' );
358
359 // Updated stored version
360 update_option( 'gamipress_version', '1.5.1' );
361
362 wp_send_json_success( array( 'upgraded' => true ) );
363
364 }
365
366 wp_send_json_success( array( 'current' => $current ) );
367
368 }
369 add_action( 'wp_ajax_gamipress_process_151_upgrade', 'gamipress_ajax_process_151_upgrade' );
370
371 function gamipress_ajax_stop_process_151_upgrade() {
372 // Security check, forces to die if not security passed
373 check_ajax_referer( 'gamipress_admin', 'nonce' );
374
375 $running_upgrade = get_option( 'gamipress_running_upgrade' );
376
377 // Check if is out upgrade
378 if( $running_upgrade === '1.5.1' )
379 delete_option( 'gamipress_running_upgrade' );
380
381 wp_send_json_success();
382
383 }
384 add_action( 'wp_ajax_gamipress_stop_process_151_upgrade', 'gamipress_ajax_stop_process_151_upgrade' );
385
386 /**
387 * Check if it is necessary upgrade
388 *
389 * @return int
390 */
391 function gamipress_151_maybe_upgrade() {
392
393 global $wpdb;
394
395 $upgrade_check = 0;
396
397 $p2p = ( property_exists( $wpdb, 'p2p' ) ? $wpdb->p2p : $wpdb->prefix . 'p2p' );
398
399 // Multisite support
400 if( gamipress_is_network_wide_active() ) {
401 $p2p = $wpdb->base_prefix . 'p2p';
402 }
403
404 // Extra check for new installs
405 if( ! gamipress_database_table_exists( $p2p ) ) {
406 return 0;
407 }
408
409 // Retrieve the count of post upgrade
410 if( ! is_gamipress_upgrade_completed( 'update_requirements_relationships' ) ) {
411
412 // Setup vars
413 $posts = GamiPress()->db->posts;
414 $requirements_types = gamipress_get_requirement_types_slugs();
415
416 $posts_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$posts} AS p WHERE p.post_type IN ( '" . implode( "', '", $requirements_types ) . "' ) AND p.post_parent = 0 LIMIT 1" );
417
418 $upgrade_check += absint( $posts_count );
419
420 }
421
422 // Retrieve the count of meta data upgrade
423 if( ! is_gamipress_upgrade_completed( 'update_achievements_relationships' ) ) {
424
425 // Setup vars
426 $postmeta = GamiPress()->db->postmeta;
427
428 $meta_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$postmeta} AS pm WHERE pm.post_id NOT IN ( SELECT spm.post_id FROM {$postmeta} AS spm WHERE spm.meta_key = '_gamipress_achievement_post' ) AND pm.meta_key = '_gamipress_trigger_type' AND pm.meta_value = 'specific-achievement' LIMIT 1" );
429
430 $upgrade_check += absint( $meta_count );
431
432 }
433
434 return $upgrade_check;
435
436 }