PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.9
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.9
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 / 2.3.7.php

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

345 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * 2.3.7 Upgrades
4 *
5 * @package GamiPress\Admin\Upgrades\2.3.7
6 * @since 2.3.7
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Return 2.3.7 as last required upgrade
13 *
14 * @return string
15 */
16 function gamipress_237_is_last_required_upgrade() {
17
18 return '2.3.7';
19
20 }
21 add_filter( 'gamipress_get_last_required_upgrade', 'gamipress_237_is_last_required_upgrade', 237 );
22
23 /**
24 * Process 2.3.7 upgrades
25 *
26 * @param string $stored_version
27 *
28 * @return string
29 */
30 function gamipress_237_upgrades( $stored_version ) {
31
32 // Already upgrade
33 if ( version_compare( $stored_version, '2.3.7', '>=' ) ) {
34 return $stored_version;
35 }
36
37 // Check if there is something to migrate
38 $upgrade_check = gamipress_upgrade_237_maybe_upgrade();
39
40 if( $upgrade_check === 0 ) {
41
42 // There is nothing to update, so upgrade
43 $stored_version = '2.3.7';
44
45 } else if( is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) ) {
46
47 // Migrations are finished, so upgrade
48 $stored_version = '2.3.7';
49
50 }
51
52 return $stored_version;
53
54 }
55 add_filter( 'gamipress_process_upgrades', 'gamipress_237_upgrades', 237 );
56
57 /**
58 * 2.3.7 upgrades notices
59 */
60 function gamipress_237_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( '2.3.7' ) ) {
74 return;
75 }
76
77 $running_upgrade = get_option( 'gamipress_running_upgrade' );
78
79 // Other upgrade already running
80 if( $running_upgrade && $running_upgrade !== '2.3.7' ) {
81 return;
82 }
83
84 if( ! is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) ) : ?>
85
86 <div id="gamipress-upgrade-notice" class="updated">
87
88 <?php if( $running_upgrade === '2.3.7' ) : ?>
89
90 <p>
91 <?php _e( 'Upgrading GamiPress database...', 'gamipress' ); ?>
92 </p>
93 <div class="gamipress-upgrade-progress" data-running-upgrade="2.3.7">
94 <div class="gamipress-upgrade-progress-bar" style="width: 0%;"></div>
95 </div>
96
97 <?php else : ?>
98
99 <p>
100 <?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' ); ?>
101 </p>
102 <p>
103 <a href="javascript:void(0);" onClick="jQuery(this).parent().next('p').slideToggle();" class="button"><?php _e( 'Learn more about this upgrade', 'gamipress' ); ?></a>
104 <a href="javascript:void(0);" onClick="gamipress_start_upgrade('2.3.7')" class="button button-primary"><?php _e( 'Start the upgrade', 'gamipress' ); ?></a>
105 </p>
106 <p style="display: none;">
107 <?php _e( '<strong>About this upgrade:</strong><br />This is a <strong><em>mandatory</em></strong> update that will update the GamiPress user earnings adding some extra information on earnings related to requirements.', 'gamipress' ); ?>
108 <br>
109 <?php _e( 'Depending on the number of user earnings found, this process could take a while, but there is <strong><em>no danger</em></strong> about lose any data because this process will only <strong><em>append</em></strong> information to old entries, so is <strong><em>100% safe to upgrade</em></strong>.', 'gamipress' ); ?>
110 </p>
111
112 <?php endif; ?>
113
114 </div>
115
116 <?php
117 endif;
118
119 }
120 add_action( 'admin_notices', 'gamipress_237_upgrades_notices' );
121
122 /**
123 * Return the number of entries to upgrade
124 *
125 * @return int
126 */
127 function gamipress_237_upgrade_size() {
128
129 global $wpdb;
130
131 $upgrade_size = 0;
132
133 // Retrieve the count of post upgrade
134 if( ! is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) && gamipress_database_table_exists( GamiPress()->db->user_earnings ) ) {
135
136 // Setup vars
137 $user_earnings = GamiPress()->db->user_earnings;
138 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
139
140 $upgrade_size = absint( $wpdb->get_var( $wpdb->prepare(
141 "SELECT COUNT(*)
142 FROM {$user_earnings} AS ue
143 LEFT JOIN {$user_earnings_meta} uem ON ( uem.user_earning_id = ue.user_earning_id AND uem.meta_key = '_gamipress_parent_post_type' )
144 WHERE ue.post_type IN ( 'step', 'rank-requirement' )
145 AND uem.meta_value IS NULL"
146 ) ) );
147
148 }
149
150 return $upgrade_size;
151
152 }
153
154 /**
155 * Ajax function to meet the upgrade size
156 */
157 function gamipress_ajax_237_upgrade_info() {
158 // Security check, forces to die if not security passed
159 check_ajax_referer( 'gamipress_admin', 'nonce' );
160
161 // Already upgraded
162 if ( is_gamipress_upgraded_to( '2.3.7' ) ) {
163 wp_send_json_success( array( 'upgraded' => true ) );
164 }
165
166 $upgrade_size = gamipress_237_upgrade_size();
167
168 wp_send_json_success( array( 'total' => $upgrade_size ) );
169
170 }
171 add_action( 'wp_ajax_gamipress_237_upgrade_info', 'gamipress_ajax_237_upgrade_info' );
172
173 /**
174 * Ajax process of 2.3.7 upgrades
175 */
176 function gamipress_ajax_process_237_upgrade() {
177 // Security check, forces to die if not security passed
178 check_ajax_referer( 'gamipress_admin', 'nonce' );
179
180 global $wpdb;
181
182 // Already upgraded
183 if ( is_gamipress_upgraded_to( '2.3.7' ) ) {
184 wp_send_json_success( array( 'upgraded' => true ) );
185 }
186
187 ignore_user_abort( true );
188 set_time_limit( 0 );
189
190 // Add option to meet that upgrade process has been started
191 update_option( 'gamipress_running_upgrade', '2.3.7' );
192
193 // --------------------------------------------------------
194 // Update user earnings parent_post_type meta
195 // --------------------------------------------------------
196 // We need to update user earnings "parent_post_type" meta to be able to filter correctly requirements by its parent type in user earnings block
197
198 if( ! is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) ) {
199
200 // Setup vars
201 $user_earnings = GamiPress()->db->user_earnings;
202 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
203 $current = isset( $_REQUEST['current'] ) ? absint( $_REQUEST['current'] ) : 0;
204 $limit = 50;
205
206 // Retrieve all requirements without parent (ordered by post_id for performance)
207 $results = $wpdb->get_results( $wpdb->prepare(
208 "SELECT ue.user_earning_id, ue.post_id, ue.post_type
209 FROM {$user_earnings} AS ue
210 LEFT JOIN {$user_earnings_meta} uem ON ( uem.user_earning_id = ue.user_earning_id AND uem.meta_key = '_gamipress_parent_post_type' )
211 WHERE ue.post_type IN ( 'step', 'rank-requirement' )
212 AND uem.meta_value IS NULL
213 ORDER BY ue.post_id ASC
214 LIMIT {$limit}"
215 ) );
216
217 $metas = array();
218 $meta_key = '_gamipress_parent_post_type';
219
220 // Cache for the parent post types
221 $parent_post_types = array();
222
223 foreach( $results as $user_earning ) {
224
225 $meta_value = '';
226
227 if( isset( $parent_post_types[$user_earning->post_id] ) ) {
228 // Get the parent post type for this post ID
229 $meta_value = $parent_post_types[$user_earning->post_id];
230 } else {
231 $parent_id = absint( gamipress_get_post_field( 'post_parent', absint( $user_earning->post_id ) ) );
232
233 if( isset( $parent_post_types[$parent_id] ) ) {
234 // Get the parent post type for this parent ID
235 $meta_value = $parent_post_types[$parent_id];
236 } else {
237 if( $parent_id !== 0 ) {
238 $meta_value = gamipress_get_post_field( 'post_type', $parent_id );
239
240 if( $meta_value === 'points-type' ) {
241 $meta_value = gamipress_get_post_field( 'post_name', $parent_id );
242 }
243 }
244
245 // Cache the parent post type for that post and parent ID
246 $parent_post_types[$user_earning->post_id] = $meta_value;
247 $parent_post_types[$parent_id] = $meta_value;
248 }
249
250 }
251
252 if( empty( $meta_value ) ) {
253 $meta_value = $user_earning->post_type;
254 }
255
256 // Setup the insert value
257 $metas[] = $wpdb->prepare( '%d, %s, %s', array( $user_earning->user_earning_id, $meta_key, $meta_value ) );
258
259 $current++;
260 }
261
262 $metas = implode( '), (', $metas );
263
264 // Is faster to run a single query to insert all metas instead of insert them one-by-one
265 $wpdb->query( "INSERT INTO {$user_earnings_meta} (user_earning_id, meta_key, meta_value) VALUES ({$metas})" );
266
267 $count = absint( $wpdb->get_var( $wpdb->prepare(
268 "SELECT COUNT(*)
269 FROM {$user_earnings} AS ue
270 LEFT JOIN {$user_earnings_meta} uem ON ( uem.user_earning_id = ue.user_earning_id AND uem.meta_key = '_gamipress_parent_post_type' )
271 WHERE ue.post_type IN ( 'step', 'rank-requirement' )
272 AND uem.meta_value IS NULL"
273 ) ) );
274
275 if( $count === 0 ) {
276 gamipress_set_upgrade_complete( 'update_earnings_parent_post_type_meta' );
277 }
278
279 }
280
281 // Successfully upgraded
282 if( is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) ) {
283
284 // Remove option to meet that upgrade process has been finished
285 delete_option( 'gamipress_running_upgrade' );
286
287 // Updated stored version
288 update_option( 'gamipress_version', '2.3.7' );
289
290 wp_send_json_success( array( 'upgraded' => true ) );
291
292 }
293
294 wp_send_json_success( array( 'current' => $current ) );
295
296 }
297 add_action( 'wp_ajax_gamipress_process_237_upgrade', 'gamipress_ajax_process_237_upgrade' );
298
299 function gamipress_ajax_stop_process_237_upgrade() {
300 // Security check, forces to die if not security passed
301 check_ajax_referer( 'gamipress_admin', 'nonce' );
302
303 $running_upgrade = get_option( 'gamipress_running_upgrade' );
304
305 // Check if is out upgrade
306 if( $running_upgrade === '2.3.7' )
307 delete_option( 'gamipress_running_upgrade' );
308
309 wp_send_json_success();
310
311 }
312 add_action( 'wp_ajax_gamipress_stop_process_237_upgrade', 'gamipress_ajax_stop_process_237_upgrade' );
313
314 /**
315 * Check if it is necessary upgrade
316 *
317 * @return int
318 */
319 function gamipress_upgrade_237_maybe_upgrade() {
320
321 global $wpdb;
322
323 $upgrade_check = 0;
324
325 // Retrieve the count of post upgrade
326 if( ! is_gamipress_upgrade_completed( 'update_earnings_parent_post_type_meta' ) && gamipress_database_table_exists( GamiPress()->db->user_earnings ) ) {
327
328 // Setup vars
329 $user_earnings = GamiPress()->db->user_earnings;
330 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
331
332 $upgrade_check = absint( $wpdb->get_var( $wpdb->prepare(
333 "SELECT COUNT(*)
334 FROM {$user_earnings} AS ue
335 LEFT JOIN {$user_earnings_meta} uem ON ( uem.user_earning_id = ue.user_earning_id AND uem.meta_key = '_gamipress_parent_post_type' )
336 WHERE ue.post_type IN ( 'step', 'rank-requirement' )
337 AND uem.meta_value IS NULL
338 LIMIT 1"
339 ) ) );
340
341 }
342
343 return $upgrade_check;
344
345 }