pgraded() { if ( ! $this->is_core_plugin() ) { return; } // Store the previous version from which the core plugin was upgraded. $upgraded_from = get_option( static::UPGRADED_FROM_OPTION_NAME, '' ); // Store the previous core version in the option. update_option( static::PREVIOUS_CORE_VERSION_OPTION_NAME, $upgraded_from ); /** * Fires after the core plugin has been upgraded. * Please note: some of the migrations that run via Active Scheduler can be not completed yet. * * @since 1.8.8 * * @param string $upgraded_from The version from which the core plugin was upgraded. * @param Base $migration_obj The migration class instance. */ do_action( 'wpforms_migrations_base_core_upgraded', $upgraded_from, $this ); } /** * If upgrade has occurred, update versions option in the database. * * @since 1.7.5 */ public function update_versions() { // Retrieve the last migrated versions. $last_migrated = get_option( static::MIGRATED_OPTION_NAME, [] ); $migrated = array_merge( $last_migrated, $this->migrated ); /** * Store current version upgrade timestamp even if there were no migrations to it. * We need it in wpforms_get_upgraded_timestamp() for further usage in Event Driven Plugin Notifications. */ $migrated[ static::CURRENT_VERSION ] = $migrated[ static::CURRENT_VERSION ] ?? time(); uksort( $last_migrated, 'version_compare' ); uksort( $migrated, 'version_compare' ); if ( $migrated === $last_migrated ) { return; } update_option( static::MIGRATED_OPTION_NAME, $migrated ); $fully_completed = array_reduce( $migrated, static function ( $carry, $status ) { return $carry && ( $status >= 0 ); }, true ); if ( ! $fully_completed ) { return; } $this->log( sprintf( 'Migration of %1$s to %2$s is fully completed.', static::PLUGIN_NAME, static::CURRENT_VERSION ) ); // We need to run further only for core plugin (Lite and Pro). if ( ! $this->is_core_plugin() ) { return; } $last_completed = array_filter( $last_migrated, static function ( $status ) { return $status >= 0; } ); if ( ! $last_completed ) { return; } // Store the current core version in the option. update_option( static::UPGRADED_FROM_OPTION_NAME, $this->get_max_version( $last_completed ) ); $this->core_upgraded(); } /** * Get upgrade classes. * * @since 1.7.5 * * @return string[] */ protected function get_upgrade_classes(): array { $classes = static::UPGRADE_CLASSES; sort( $classes ); return $classes; } /** * Get an upgrade version from the class name. * * @since 1.7.5 * * @param string $class_name Class name. * * @return string */ public function get_upgrade_version( string $class_name ): string { // Find only the digits and underscores to get version number. if ( ! preg_match( '/(\d_?)+/', $class_name, $matches ) ) { return ''; } $raw_version = $matches[0]; if ( strpos( $raw_version, '_' ) ) { // Modern notation: 1_10_0_3 means 1.10.0.3 version. return str_replace( '_', '.', $raw_version ); } // Legacy notation, with 1-digit subversion numbers: 1751 means 1.7.5.1 version. return implode( '.', str_split( $raw_version ) ); } /** * Get plugin/addon name. * * @since 1.7.5 * * @param string $class_name Upgrade class name. * * @return string * @noinspection PhpUnusedParameterInspection */ protected function get_plugin_name( string $class_name ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found return static::PLUGIN_NAME; } /** * Force log message to WPForms logger. * * @since 1.7.5 * * @param string $message The error message that should be logged. */ protected function log( string $message ) { wpforms_log( 'Migration', $message, [ 'type' => 'log', 'force' => true, ] ); } /** * Determine if migration is allowed. * * @since 1.7.5 */ private function is_allowed(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['service-worker'] ) ) { return false; } return wp_doing_cron() || is_admin() || wpforms_doing_wp_cli(); } /** * Maybe create custom plugin tables. * * @since 1.7.6 */ public function maybe_create_tables() { if ( $this->tables_check_done ) { /** * We should do tables check only once - when the first migration has been started. * The DB::get_existing_custom_tables() without caching causes performance issue * on huge multisite with thousands of tables. */ return; } DB::create_custom_tables( true ); $this->tables_check_done = true; } /** * Maybe convert the migration option format. * * @since 1.7.5 */ private function maybe_convert_migration_option() { /** * Retrieve the migration option and check its format. * Old format: a string 'x.y.z' containing the last migrated version. * New format: [ 'x.y.z' => {status}, 'x1.y1.z1' => {status}... ], * where {status} is a migration status. * Negative means some status (-1 for 'started' etc.), * zero means completed earlier at unknown time, * positive means completion timestamp. */ $this->migrated = get_option( static::MIGRATED_OPTION_NAME ); // If the option is an array, it means that it is already converted to the new format. if ( is_array( $this->migrated ) ) { return; } /** * Convert the option to the new format. * * Old option names contained 'version', * like 'wpforms_version', 'wpforms_version_lite', 'wpforms_stripe_version' etc. * We preserve old options for downgrade cases. * New option names should contain 'versions' and be like 'wpforms_versions' etc. */ $this->migrated = get_option( str_replace( 'versions', 'version', static::MIGRATED_OPTION_NAME ) ); $version = $this->migrated === false ? self::INITIAL_FAKE_VERSION : (string) $this->migrated; $timestamp = $version === static::CURRENT_VERSION ? time() : 0; $this->migrated = [ $version => $timestamp ]; $max_version = $this->get_max_version( $this->migrated ); foreach ( $this->get_upgrade_classes() as $upgrade_class ) { $upgrade_version = $this->get_upgrade_version( $upgrade_class ); if ( ! isset( $this->migrated[ $upgrade_version ] ) && version_compare( $upgrade_version, $max_version, '<' ) ) { $this->migrated[ $upgrade_version ] = 0; } } unset( $this->migrated[ self::INITIAL_FAKE_VERSION ] ); ksort( $this->migrated ); update_option( static::MIGRATED_OPTION_NAME, $this->migrated ); } /** * Get the max version. * * @since 1.7.5 * * @param array $versions Versions. * * @return string */ private function get_max_version( array $versions ): string { // phpcs:ignore WPForms.Formatting.EmptyLineBeforeReturn.RemoveEmptyLineBeforeReturnStatement return array_reduce( array_keys( $versions ), static function ( $carry, $version ) { return version_compare( $version, $carry, '>' ) ? $version : $carry; }, self::INITIAL_FAKE_VERSION ); } /** * Determine if it is the core plugin (Lite or Pro). * * @since 1.7.5 * * @return bool True if it is the core plugin. */ protected function is_core_plugin(): bool { // phpcs:ignore WPForms.Formatting.EmptyLineBeforeReturn.RemoveEmptyLineBeforeReturnStatement return strpos( static::MIGRATED_OPTION_NAME, 'wpforms_versions' ) === 0; } /** * Log migration message. * * @since 1.8.2.3 * * @param bool $migrated Migration status. * @param string $plugin_name Plugin name. * @param string $upgrade_version Upgrade version. * * @return void */ private function log_migration_message( bool $migrated, string $plugin_name, string $upgrade_version ) { $message = $migrated ? sprintf( 'Migration of %1$s to %2$s completed.', $plugin_name, $upgrade_version ) : sprintf( 'Migration of %1$s to %2$s failed.', $plugin_name, $upgrade_version ); $this->log( $message ); } }