requires_files(); try { MainWP_Helper::instance()->check_classes_exists( array( '\WC_Install' ) ); MainWP_Helper::instance()->check_methods( '\WC_Install', array( 'needs_db_update' ) ); if ( \WC_Install::needs_db_update() ) { $next_scheduled_date = \WC()->queue()->get_next( 'woocommerce_run_update_callback', null, 'woocommerce-db-updates' ); if ( ! $next_scheduled_date ) { $current_db_version = get_option( 'woocommerce_db_version', null ); // need. $db_upgrades['woocommerce/woocommerce.php'] = array( 'update' => $this->get_needs_db_update(), 'Name' => 'WooCommerce', 'db_version' => $current_db_version ? $current_db_version : '', ); } } } catch ( MainWP_Exception $e ) { // not exit here! error_log( $e->getMessage() ); //phpcs:ignore -- for debug. } return $db_upgrades; } /** * Is a DB update needed? * * @since 3.2.0 * @return boolean */ public static function get_needs_db_update() { $db_versions = array( 'new_db_version' => '', 'slug' => 'woocommerce/woocommerce.php', ); $updates = \WC_Install::get_db_update_callbacks(); $update_versions = array_keys( $updates ); usort( $update_versions, 'version_compare' ); if ( ! empty( $update_versions ) ) { $db_versions['new_db_version'] = end( $update_versions ); } return $db_versions; } /** * Method update_db() * * Update WC DB. * * @return array Action result. */ public function update_db() { if ( ! static::$is_plugin_woocom_installed ) { return false; } $success = false; try { MainWP_Helper::instance()->check_classes_exists( array( '\WC_Install' ) ); MainWP_Helper::instance()->check_methods( '\WC_Install', array( 'needs_db_update' ) ); $this->update_wc_db(); $success = true; } catch ( MainWP_Exception $e ) { error_log( $e->getMessage() ); //phpcs:ignore -- for debug. } try { MainWP_Helper::instance()->check_classes_exists( array( '\WC_Admin_Notices' ) ); MainWP_Helper::instance()->check_methods( '\WC_Admin_Notices', array( 'remove_notice' ) ); static::hide_notice( 'update' ); } catch ( MainWP_Exception $e ) { error_log( $e->getMessage() ); //phpcs:ignore -- for debug. } return $success; } /** * Push all needed DB updates to the queue for processing. */ private static function update_wc_db() { MainWP_Helper::instance()->check_methods( '\WC_Install', array( 'get_db_update_callbacks' ) ); $current_db_version = get_option( 'woocommerce_db_version' ); $loop = 0; foreach ( \WC_Install::get_db_update_callbacks() as $version => $update_callbacks ) { if ( version_compare( $current_db_version, $version, '<' ) ) { foreach ( $update_callbacks as $update_callback ) { \WC()->queue()->schedule_single( time() + $loop, 'woocommerce_run_update_callback', array( 'update_callback' => $update_callback, ), 'woocommerce-db-updates' ); ++$loop; } } } // After the callbacks finish, update the db version to the current WC version. $current_wc_version = WC()->version; if ( version_compare( $current_db_version, $current_wc_version, '<' ) && ! \WC()->queue()->get_next( 'woocommerce_update_db_to_current_version' ) ) { \WC()->queue()->schedule_single( time() + $loop, 'woocommerce_update_db_to_current_version', array( 'version' => $current_wc_version, ), 'woocommerce-db-updates' ); } } /** * Hide a single notice. * * @param string $name Notice name. */ private static function hide_notice( $name ) { \WC_Admin_Notices::remove_notice( $name ); update_user_meta( get_current_user_id(), 'dismissed_' . $name . '_notice', true ); do_action( 'woocommerce_hide_' . $name . '_notice' ); } }