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.4.3.php

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

290 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * 1.4.3 Upgrades
4 *
5 * @package GamiPress\Admin\Upgrades\1.4.3
6 * @since 1.4.3
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Return 1.4.3 as last required upgrade
13 *
14 * @return string
15 */
16 function gamipress_143_is_last_required_upgrade() {
17
18 return '1.4.3';
19
20 }
21 add_filter( 'gamipress_get_last_required_upgrade', 'gamipress_143_is_last_required_upgrade', 143 );
22
23 /**
24 * Process 1.4.3 upgrades
25 *
26 * @param string $stored_version
27 *
28 * @return string
29 */
30 function gamipress_143_upgrades( $stored_version ) {
31
32 // Already upgrade
33 if ( version_compare( $stored_version, '1.4.3', '>=' ) ) {
34 return $stored_version;
35 }
36
37 // Prevent run upgrade until database tables are created
38 if( ! gamipress_database_table_exists( 'gamipress_user_earnings' ) ) {
39 return $stored_version;
40 }
41
42 // Check if there is something to migrate
43 $upgrade_check = gamipress_143_maybe_upgrade();
44
45 if( $upgrade_check === 0 ) {
46
47 // There is nothing to update, so upgrade
48 $stored_version = '1.4.3';
49
50 } else if( is_gamipress_upgrade_completed( 'update_user_earnings' ) ) {
51
52 // Migrations are finished, so upgrade
53 $stored_version = '1.4.3';
54
55 }
56
57 return $stored_version;
58
59 }
60 add_filter( 'gamipress_process_upgrades', 'gamipress_143_upgrades', 143 );
61
62 /**
63 * 1.4.3 upgrades notices
64 */
65 function gamipress_143_upgrades_notices() {
66
67 // Bail if GamiPress is active network wide and we are not in main site
68 if( gamipress_is_network_wide_active() && ! is_main_site() ) {
69 return;
70 }
71
72 // Check user permissions
73 if ( ! current_user_can( gamipress_get_manager_capability() ) ) {
74 return;
75 }
76
77 // Already upgraded!
78 if( is_gamipress_upgraded_to( '1.4.3' ) ) {
79 return;
80 }
81
82 $running_upgrade = get_option( 'gamipress_running_upgrade' );
83
84 // Other upgrade already running
85 if( $running_upgrade && $running_upgrade !== '1.4.3' ) {
86 return;
87 }
88
89 if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) :
90 ?>
91 <div id="gamipress-upgrade-notice" class="updated">
92
93 <?php if( $running_upgrade === '1.4.3' ) : ?>
94
95 <p>
96 <?php _e( 'Upgrading GamiPress database...', 'gamipress' ); ?>
97 </p>
98 <div class="gamipress-upgrade-progress" data-running-upgrade="1.4.3">
99 <div class="gamipress-upgrade-progress-bar" style="width: 0%;"></div>
100 </div>
101
102 <?php else : ?>
103
104 <p>
105 <?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' ); ?>
106 </p>
107 <p>
108 <a href="javascript:void(0);" onClick="jQuery(this).parent().next('p').slideToggle();" class="button"><?php _e( 'Learn more about this upgrade', 'gamipress' ); ?></a>
109 <a href="javascript:void(0);" onClick="gamipress_start_upgrade('1.4.3')" class="button button-primary"><?php _e( 'Start the upgrade', 'gamipress' ); ?></a>
110 </p>
111 <p style="display: none;">
112 <?php _e( '<strong>About this upgrade:</strong><br />This is a <strong><em>mandatory</em></strong> update that will update all user earnings with new database table fields. This upgrade provides better performance and scalability.', 'gamipress' ); ?>
113 </p>
114
115 <?php endif; ?>
116
117 </div>
118 <?php
119 endif;
120
121 }
122 add_action( 'admin_notices', 'gamipress_143_upgrades_notices' );
123
124 /**
125 * Return the number of entries to upgrade
126 *
127 * @return int
128 */
129 function gamipress_143_upgrade_size() {
130
131 global $wpdb;
132
133 $upgrade_size = 0;
134
135 // Retrieve the count of users earnings to upgrade
136 if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) {
137
138 $user_earnings = GamiPress()->db->user_earnings;
139
140 $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = ''" );
141
142 $upgrade_size += absint( $earnings_count );
143
144 }
145
146 return $upgrade_size;
147
148 }
149
150 /**
151 * Ajax function to meet the upgrade size
152 */
153 function gamipress_ajax_143_upgrade_info() {
154 // Security check, forces to die if not security passed
155 check_ajax_referer( 'gamipress_admin', 'nonce' );
156
157 // Already upgraded
158 if ( is_gamipress_upgraded_to( '1.4.3' ) ) {
159 wp_send_json_success( array( 'upgraded' => true ) );
160 }
161
162 $upgrade_size = gamipress_143_upgrade_size();
163
164 wp_send_json_success( array( 'total' => $upgrade_size ) );
165 }
166 add_action( 'wp_ajax_gamipress_143_upgrade_info', 'gamipress_ajax_143_upgrade_info' );
167
168 /**
169 * Ajax process of 1.4.3 upgrades
170 */
171 function gamipress_ajax_process_143_upgrade() {
172 // Security check, forces to die if not security passed
173 check_ajax_referer( 'gamipress_admin', 'nonce' );
174
175 global $wpdb;
176
177 // Already upgraded
178 if ( is_gamipress_upgraded_to( '1.4.3' ) ) {
179 wp_send_json_success( array( 'upgraded' => true ) );
180 }
181
182 ignore_user_abort( true );
183 set_time_limit( 0 );
184
185 // Add option to meet that upgrade process has been started
186 update_option( 'gamipress_running_upgrade', '1.4.3' );
187
188 $current = isset( $_REQUEST['current'] ) ? absint( $_REQUEST['current'] ) : 0;
189
190 // ----------------------------
191 // User earnings update
192 // ----------------------------
193 if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) {
194
195 // Update gamipress_user_earnings table title field
196 $ct_table = ct_setup_table( 'gamipress_user_earnings' );
197
198 $limit = 100;
199
200 $user_earnings = GamiPress()->db->user_earnings;
201
202 // Retrieve all user earnings without title
203 $results = $wpdb->get_results( "SELECT * FROM {$user_earnings} AS e WHERE e.title = '' LIMIT {$limit}" );
204
205 foreach( $results as $user_earning ) {
206
207 // Set the new title using WordPress (no title)
208 $title = __( '(no title)' );
209
210 if( gamipress_post_exists( $user_earning->post_id ) ) {
211 $post_title = gamipress_get_post_field( 'post_title', $user_earning->post_id );
212
213 if( ! empty( $post_title ) ) {
214 $title = $post_title;
215 }
216 }
217
218 $ct_table->db->update(
219 array( 'title' => $title ),
220 array( 'user_earning_id' => $user_earning->user_earning_id )
221 );
222
223 $current++;
224 }
225
226 $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = ''" );
227
228 if( absint( $earnings_count ) === 0 ) {
229 gamipress_set_upgrade_complete( 'update_user_earnings' );
230 }
231
232 }
233
234 // Successfully upgraded
235 if( is_gamipress_upgrade_completed( 'update_user_earnings' ) ) {
236
237 // Remove option to meet that upgrade process has been finished
238 delete_option( 'gamipress_running_upgrade' );
239
240 // Updated stored version
241 update_option( 'gamipress_version', '1.4.3' );
242
243 wp_send_json_success( array( 'upgraded' => true ) );
244 }
245
246 wp_send_json_success( array( 'current' => $current ) );
247
248 }
249 add_action( 'wp_ajax_gamipress_process_143_upgrade', 'gamipress_ajax_process_143_upgrade' );
250
251 function gamipress_ajax_stop_process_143_upgrade() {
252 // Security check, forces to die if not security passed
253 check_ajax_referer( 'gamipress_admin', 'nonce' );
254
255 $running_upgrade = get_option( 'gamipress_running_upgrade' );
256
257 // Check if is out upgrade
258 if( $running_upgrade === '1.4.3' ) {
259 delete_option( 'gamipress_running_upgrade' );
260 }
261
262 wp_send_json_success();
263 }
264 add_action( 'wp_ajax_gamipress_stop_process_143_upgrade', 'gamipress_ajax_stop_process_143_upgrade' );
265
266 /**
267 * Check if it is necessary upgrade
268 *
269 * @return int
270 */
271 function gamipress_143_maybe_upgrade() {
272
273 global $wpdb;
274
275 $upgrade_check = 0;
276
277 // Retrieve the count of users earnings to upgrade
278 if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) {
279
280 $user_earnings = GamiPress()->db->user_earnings;
281
282 $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = '' LIMIT 1" );
283
284 $upgrade_check += absint( $earnings_count );
285
286 }
287
288 return $upgrade_check;
289
290 }