=' ) ) { return $stored_version; } // Prevent run upgrade until database tables are created if( ! gamipress_database_table_exists( 'gamipress_user_earnings' ) ) { return $stored_version; } // Check if there is something to migrate $upgrade_check = gamipress_143_maybe_upgrade(); if( $upgrade_check === 0 ) { // There is nothing to update, so upgrade $stored_version = '1.4.3'; } else if( is_gamipress_upgrade_completed( 'update_user_earnings' ) ) { // Migrations are finished, so upgrade $stored_version = '1.4.3'; } return $stored_version; } add_filter( 'gamipress_process_upgrades', 'gamipress_143_upgrades', 143 ); /** * 1.4.3 upgrades notices */ function gamipress_143_upgrades_notices() { // Bail if GamiPress is active network wide and we are not in main site if( gamipress_is_network_wide_active() && ! is_main_site() ) { return; } // Check user permissions if ( ! current_user_can( gamipress_get_manager_capability() ) ) { return; } // Already upgraded! if( is_gamipress_upgraded_to( '1.4.3' ) ) { return; } $running_upgrade = get_option( 'gamipress_running_upgrade' ); // Other upgrade already running if( $running_upgrade && $running_upgrade !== '1.4.3' ) { return; } if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) : ?>

Please backup your database before starting this upgrade. This upgrade routine will be making changes to the database that are not reversible.', 'gamipress' ); ?>

About this upgrade:
This is a mandatory update that will update all user earnings with new database table fields. This upgrade provides better performance and scalability.', 'gamipress' ); ?>

db->user_earnings; $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = ''" ); $upgrade_size += absint( $earnings_count ); } return $upgrade_size; } /** * Ajax function to meet the upgrade size */ function gamipress_ajax_143_upgrade_info() { // Security check, forces to die if not security passed check_ajax_referer( 'gamipress_admin', 'nonce' ); // Already upgraded if ( is_gamipress_upgraded_to( '1.4.3' ) ) { wp_send_json_success( array( 'upgraded' => true ) ); } $upgrade_size = gamipress_143_upgrade_size(); wp_send_json_success( array( 'total' => $upgrade_size ) ); } add_action( 'wp_ajax_gamipress_143_upgrade_info', 'gamipress_ajax_143_upgrade_info' ); /** * Ajax process of 1.4.3 upgrades */ function gamipress_ajax_process_143_upgrade() { // Security check, forces to die if not security passed check_ajax_referer( 'gamipress_admin', 'nonce' ); global $wpdb; // Already upgraded if ( is_gamipress_upgraded_to( '1.4.3' ) ) { wp_send_json_success( array( 'upgraded' => true ) ); } ignore_user_abort( true ); set_time_limit( 0 ); // Add option to meet that upgrade process has been started update_option( 'gamipress_running_upgrade', '1.4.3' ); $current = isset( $_REQUEST['current'] ) ? absint( $_REQUEST['current'] ) : 0; // ---------------------------- // User earnings update // ---------------------------- if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) { // Update gamipress_user_earnings table title field $ct_table = ct_setup_table( 'gamipress_user_earnings' ); $limit = 100; $user_earnings = GamiPress()->db->user_earnings; // Retrieve all user earnings without title $results = $wpdb->get_results( "SELECT * FROM {$user_earnings} AS e WHERE e.title = '' LIMIT {$limit}" ); foreach( $results as $user_earning ) { // Set the new title using WordPress (no title) $title = __( '(no title)' ); if( gamipress_post_exists( $user_earning->post_id ) ) { $post_title = gamipress_get_post_field( 'post_title', $user_earning->post_id ); if( ! empty( $post_title ) ) { $title = $post_title; } } $ct_table->db->update( array( 'title' => $title ), array( 'user_earning_id' => $user_earning->user_earning_id ) ); $current++; } $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = ''" ); if( absint( $earnings_count ) === 0 ) { gamipress_set_upgrade_complete( 'update_user_earnings' ); } } // Successfully upgraded if( is_gamipress_upgrade_completed( 'update_user_earnings' ) ) { // Remove option to meet that upgrade process has been finished delete_option( 'gamipress_running_upgrade' ); // Updated stored version update_option( 'gamipress_version', '1.4.3' ); wp_send_json_success( array( 'upgraded' => true ) ); } wp_send_json_success( array( 'current' => $current ) ); } add_action( 'wp_ajax_gamipress_process_143_upgrade', 'gamipress_ajax_process_143_upgrade' ); function gamipress_ajax_stop_process_143_upgrade() { // Security check, forces to die if not security passed check_ajax_referer( 'gamipress_admin', 'nonce' ); $running_upgrade = get_option( 'gamipress_running_upgrade' ); // Check if is out upgrade if( $running_upgrade === '1.4.3' ) { delete_option( 'gamipress_running_upgrade' ); } wp_send_json_success(); } add_action( 'wp_ajax_gamipress_stop_process_143_upgrade', 'gamipress_ajax_stop_process_143_upgrade' ); /** * Check if it is necessary upgrade * * @return int */ function gamipress_143_maybe_upgrade() { global $wpdb; $upgrade_check = 0; // Retrieve the count of users earnings to upgrade if( ! is_gamipress_upgrade_completed( 'update_user_earnings' ) ) { $user_earnings = GamiPress()->db->user_earnings; $earnings_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS e WHERE e.title = '' LIMIT 1" ); $upgrade_check += absint( $earnings_count ); } return $upgrade_check; }