| 1 |
<?php |
| 2 |
/** |
| 3 |
* 6.9.4 Upgrades |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Upgrades\6.9.4 |
| 6 |
* @since 6.9.4 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Return 6.9.4 as last required upgrade |
| 13 |
* |
| 14 |
* @return string |
| 15 |
*/ |
| 16 |
function gamipress_694_is_last_required_upgrade() { |
| 17 |
|
| 18 |
return '6.9.4'; |
| 19 |
|
| 20 |
} |
| 21 |
add_filter( 'gamipress_get_last_required_upgrade', 'gamipress_694_is_last_required_upgrade', 694 ); |
| 22 |
|
| 23 |
/** |
| 24 |
* Process 6.9.4 upgrades |
| 25 |
* |
| 26 |
* @param string $stored_version |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
function gamipress_694_upgrades( $stored_version ) { |
| 31 |
|
| 32 |
// Already upgrade |
| 33 |
if ( version_compare( $stored_version, '6.9.4', '>=' ) ) { |
| 34 |
return $stored_version; |
| 35 |
} |
| 36 |
|
| 37 |
// Prevent run upgrade until database tables are created |
| 38 |
if( ! gamipress_database_table_has_column( GamiPress()->db->logs, 'points_type' ) ) { |
| 39 |
return $stored_version; |
| 40 |
} |
| 41 |
|
| 42 |
// Check if there is something to migrate |
| 43 |
$upgrade_check = gamipress_upgrade_694_maybe_upgrade(); |
| 44 |
|
| 45 |
if( $upgrade_check === 0 ) { |
| 46 |
|
| 47 |
// There is nothing to update, so upgrade |
| 48 |
$stored_version = '6.9.4'; |
| 49 |
|
| 50 |
} else if( is_gamipress_upgrade_completed( 'update_logs_points_types' ) ) { |
| 51 |
|
| 52 |
// Migrations are finished, so upgrade |
| 53 |
$stored_version = '6.9.4'; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
return $stored_version; |
| 58 |
|
| 59 |
} |
| 60 |
add_filter( 'gamipress_process_upgrades', 'gamipress_694_upgrades', 694 ); |
| 61 |
|
| 62 |
/** |
| 63 |
* 6.9.4 upgrades notices |
| 64 |
*/ |
| 65 |
function gamipress_694_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( '6.9.4' ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$running_upgrade = get_option( 'gamipress_running_upgrade' ); |
| 83 |
|
| 84 |
// Other upgrade already running |
| 85 |
if( $running_upgrade && $running_upgrade !== '6.9.4' ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if( ! is_gamipress_upgrade_completed( 'update_logs_points_types' ) ) : ?> |
| 90 |
|
| 91 |
<div id="gamipress-upgrade-notice" class="updated"> |
| 92 |
|
| 93 |
<?php if( $running_upgrade === '6.9.4' ) : ?> |
| 94 |
|
| 95 |
<p> |
| 96 |
<?php _e( 'Upgrading GamiPress database...', 'gamipress' ); ?> |
| 97 |
</p> |
| 98 |
<div class="gamipress-upgrade-progress" data-running-upgrade="6.9.4"> |
| 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('6.9.4')" 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 the GamiPress logs adding some extra information on them to improve database queries.', 'gamipress' ); ?> |
| 113 |
<br> |
| 114 |
<?php _e( 'Depending on the number of logs 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' ); ?> |
| 115 |
</p> |
| 116 |
|
| 117 |
<?php endif; ?> |
| 118 |
|
| 119 |
</div> |
| 120 |
|
| 121 |
<?php |
| 122 |
endif; |
| 123 |
|
| 124 |
} |
| 125 |
add_action( 'admin_notices', 'gamipress_694_upgrades_notices' ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Return the number of entries to upgrade |
| 129 |
* |
| 130 |
* @return int |
| 131 |
*/ |
| 132 |
function gamipress_694_upgrade_size() { |
| 133 |
|
| 134 |
global $wpdb; |
| 135 |
|
| 136 |
$upgrade_size = 0; |
| 137 |
|
| 138 |
// Retrieve the count of post upgrade |
| 139 |
if( ! is_gamipress_upgrade_completed( 'update_logs_points_types' ) |
| 140 |
&& gamipress_database_table_exists( GamiPress()->db->logs ) |
| 141 |
&& gamipress_database_table_has_column( GamiPress()->db->logs, 'points_type' ) ) { |
| 142 |
|
| 143 |
$upgrade_size = absint( $wpdb->get_var( $wpdb->prepare( |
| 144 |
"SELECT COUNT(*) " . gamipress_upgrade_694_query() |
| 145 |
) ) ); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
return $upgrade_size; |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Ajax function to meet the upgrade size |
| 155 |
*/ |
| 156 |
function gamipress_ajax_694_upgrade_info() { |
| 157 |
// Security check, forces to die if not security passed |
| 158 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 159 |
|
| 160 |
// Already upgraded |
| 161 |
if ( is_gamipress_upgraded_to( '6.9.4' ) ) { |
| 162 |
wp_send_json_success( array( 'upgraded' => true ) ); |
| 163 |
} |
| 164 |
|
| 165 |
$upgrade_size = gamipress_694_upgrade_size(); |
| 166 |
|
| 167 |
wp_send_json_success( array( 'total' => $upgrade_size ) ); |
| 168 |
|
| 169 |
} |
| 170 |
add_action( 'wp_ajax_gamipress_694_upgrade_info', 'gamipress_ajax_694_upgrade_info' ); |
| 171 |
|
| 172 |
/** |
| 173 |
* Ajax process of 6.9.4 upgrades |
| 174 |
*/ |
| 175 |
function gamipress_ajax_process_694_upgrade() { |
| 176 |
// Security check, forces to die if not security passed |
| 177 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 178 |
|
| 179 |
global $wpdb; |
| 180 |
|
| 181 |
// Already upgraded |
| 182 |
if ( is_gamipress_upgraded_to( '6.9.4' ) ) { |
| 183 |
wp_send_json_success( array( 'upgraded' => true ) ); |
| 184 |
} |
| 185 |
|
| 186 |
ignore_user_abort( true ); |
| 187 |
set_time_limit( 0 ); |
| 188 |
|
| 189 |
// Add option to meet that upgrade process has been started |
| 190 |
update_option( 'gamipress_running_upgrade', '6.9.4' ); |
| 191 |
|
| 192 |
// -------------------------------------------------------- |
| 193 |
// Update user earnings parent_post_type meta |
| 194 |
// -------------------------------------------------------- |
| 195 |
// 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 |
| 196 |
|
| 197 |
if( ! is_gamipress_upgrade_completed( 'update_logs_points_types' ) ) { |
| 198 |
|
| 199 |
// Setup vars |
| 200 |
$logs = GamiPress()->db->logs; |
| 201 |
$logs_meta = GamiPress()->db->logs_meta; |
| 202 |
$current = isset( $_REQUEST['current'] ) ? absint( $_REQUEST['current'] ) : 0; |
| 203 |
$limit = 50; |
| 204 |
|
| 205 |
if( $current === 0 ) { |
| 206 |
// Update the type column to varchar for performance |
| 207 |
$wpdb->query("ALTER TABLE `{$logs}` MODIFY COLUMN `type` VARCHAR(50) CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate} NOT NULL;"); |
| 208 |
} |
| 209 |
|
| 210 |
// Retrieve all requirements without parent (ordered by post_id for performance) |
| 211 |
$results = $wpdb->get_results( $wpdb->prepare( "SELECT l.log_id " . gamipress_upgrade_694_query() . " LIMIT {$limit}" ) ); |
| 212 |
|
| 213 |
foreach( $results as $log ) { |
| 214 |
|
| 215 |
$points = absint( gamipress_get_log_meta( $log->log_id, '_gamipress_points', true ) ); |
| 216 |
$points_type = gamipress_get_log_meta( $log->log_id, '_gamipress_points_type', true ); |
| 217 |
|
| 218 |
// Update the log points and points type |
| 219 |
$wpdb->query("UPDATE `{$logs}` SET `points`={$points},`points_type`='{$points_type}' WHERE `log_id`={$log->log_id}"); |
| 220 |
|
| 221 |
$current++; |
| 222 |
} |
| 223 |
|
| 224 |
$count = absint( $wpdb->get_var( $wpdb->prepare( |
| 225 |
"SELECT COUNT(*) " . gamipress_upgrade_694_query() |
| 226 |
) ) ); |
| 227 |
|
| 228 |
if( $count === 0 ) { |
| 229 |
gamipress_set_upgrade_complete( 'update_logs_points_types' ); |
| 230 |
} |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
// Successfully upgraded |
| 235 |
if( is_gamipress_upgrade_completed( 'update_logs_points_types' ) ) { |
| 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', '6.9.4' ); |
| 242 |
|
| 243 |
wp_send_json_success( array( 'upgraded' => true ) ); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
wp_send_json_success( array( 'current' => $current ) ); |
| 248 |
|
| 249 |
} |
| 250 |
add_action( 'wp_ajax_gamipress_process_694_upgrade', 'gamipress_ajax_process_694_upgrade' ); |
| 251 |
|
| 252 |
function gamipress_ajax_stop_process_694_upgrade() { |
| 253 |
// Security check, forces to die if not security passed |
| 254 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 255 |
|
| 256 |
$running_upgrade = get_option( 'gamipress_running_upgrade' ); |
| 257 |
|
| 258 |
// Check if is out upgrade |
| 259 |
if( $running_upgrade === '6.9.4' ) |
| 260 |
delete_option( 'gamipress_running_upgrade' ); |
| 261 |
|
| 262 |
wp_send_json_success(); |
| 263 |
|
| 264 |
} |
| 265 |
add_action( 'wp_ajax_gamipress_stop_process_694_upgrade', 'gamipress_ajax_stop_process_694_upgrade' ); |
| 266 |
|
| 267 |
/** |
| 268 |
* Check if it is necessary upgrade |
| 269 |
* |
| 270 |
* @return int |
| 271 |
*/ |
| 272 |
function gamipress_upgrade_694_maybe_upgrade() { |
| 273 |
|
| 274 |
global $wpdb; |
| 275 |
|
| 276 |
$upgrade_check = 0; |
| 277 |
|
| 278 |
// Retrieve the count of post upgrade |
| 279 |
if( ! is_gamipress_upgrade_completed( 'update_logs_points_types' ) |
| 280 |
&& gamipress_database_table_exists( GamiPress()->db->logs ) |
| 281 |
&& gamipress_database_table_has_column( GamiPress()->db->logs, 'points_type' )) { |
| 282 |
|
| 283 |
$upgrade_check = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) " . gamipress_upgrade_694_query() . " LIMIT 1" ) ) ); |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
return $upgrade_check; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Gets the common query for this upgrade |
| 293 |
* |
| 294 |
* @return string |
| 295 |
*/ |
| 296 |
function gamipress_upgrade_694_query() { |
| 297 |
|
| 298 |
// Setup vars |
| 299 |
$logs = GamiPress()->db->logs; |
| 300 |
$logs_meta = GamiPress()->db->logs_meta; |
| 301 |
|
| 302 |
return "FROM {$logs} AS l |
| 303 |
LEFT JOIN {$logs_meta} lm ON ( lm.log_id = l.log_id AND lm.meta_key = '_gamipress_points_type' ) |
| 304 |
WHERE l.points_type = '' |
| 305 |
AND lm.meta_value != ''"; |
| 306 |
|
| 307 |
} |