| @@ -20,10 +20,17 @@ | ||
| 20 | 20 | * |
| 21 | 21 | * @since 0.0.12 |
| 22 | 22 | */ |
| 23 | 23 | class Updater { |
| 24 | + use Get_Instance; | |
| 24 | 25 | |
| 25 | - use Get_Instance; | |
| 26 | + /** | |
| 27 | + * Current DB saved version of SureForms. | |
| 28 | + * | |
| 29 | + * @var string | |
| 30 | + * @since 1.0.0 | |
| 31 | + */ | |
| 32 | + private $old_version; | |
| 26 | 33 | |
| 27 | 34 | /** |
| 28 | 35 | * Constructor. |
| 29 | 36 | * |
| @@ -30,31 +37,83 @@ | ||
| 30 | 37 | * @since 0.0.12 |
| 31 | 38 | * @return void |
| 32 | 39 | */ |
| 33 | 40 | public function __construct() { |
| 34 | - add_action( 'admin_init', [ $this, 'init' ] ); | |
| 41 | + // Get auto saved version number. | |
| 42 | + $this->old_version = Helper::get_string_value( get_option( 'srfm-version', '' ) ); | |
| 43 | + /** | |
| 44 | + * Sets the `init` action with a priority of 10. | |
| 45 | + * Ensures that this initialization runs before the Pro plugin's `init` action. | |
| 46 | + * Allows the necessary setup to complete prior to the Pro plugin's execution. | |
| 47 | + */ | |
| 48 | + add_action( 'init', [ $this, 'init' ], 10 ); | |
| 35 | 49 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] ); |
| 36 | 50 | add_action( 'in_plugin_update_message-' . SRFM_BASENAME, [ $this, 'plugin_update_notification' ], 10 ); |
| 37 | 51 | } |
| 38 | 52 | |
| 39 | 53 | /** |
| 54 | + * Whether or not to call the DB update methods. | |
| 55 | + * | |
| 56 | + * @since 1.0.0 | |
| 57 | + * @return bool | |
| 58 | + */ | |
| 59 | + public function needs_db_update() { | |
| 60 | + if ( ! $this->old_version ) { | |
| 61 | + if ( ! empty( get_posts( [ 'post_type' => SRFM_FORMS_POST_TYPE ] ) ) ) { | |
| 62 | + // Run db update if users have forms created. Edge case for the users directly updating from v0.0.10 or below. | |
| 63 | + return true; | |
| 64 | + } | |
| 65 | + | |
| 66 | + // If old version is empty and no forms are created, it means it is fresh setup. Hence, DB upgrade is not needed. | |
| 67 | + return false; | |
| 68 | + } | |
| 69 | + | |
| 70 | + $updater_callbacks = $this->get_updater_callbacks(); | |
| 71 | + | |
| 72 | + if ( empty( $updater_callbacks ) ) { | |
| 73 | + return false; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $versions = array_keys( $updater_callbacks ); | |
| 77 | + $latest = $versions[ count( $versions ) - 1 ]; | |
| 78 | + | |
| 79 | + return version_compare( $this->old_version, $latest, '<' ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 40 | 83 | * This function will help us to determine the plugin version and update it. |
| 41 | 84 | * Any major change in the option can be handed here on the basis of last plugin version found in the database. |
| 42 | 85 | * |
| 43 | 86 | * @since 0.0.12 |
| 87 | + * @since 1.0.0 -- Added db updater callbacks support. | |
| 44 | 88 | * @return void |
| 45 | 89 | */ |
| 46 | 90 | public function init() { |
| 47 | - // Get auto saved version number. | |
| 48 | - $saved_version = get_option( 'srfm-version', false ); | |
| 91 | + if ( version_compare( SRFM_VER, $this->old_version, '=' ) ) { | |
| 92 | + // Bail early because saved version is already updated and no change detected. | |
| 93 | + return; | |
| 94 | + } | |
| 49 | 95 | |
| 50 | - // Update auto saved version number. | |
| 51 | - if ( ! $saved_version || ! is_string( $saved_version ) ) { | |
| 96 | + if ( $this->needs_db_update() ) { | |
| 97 | + foreach ( $this->get_updater_callbacks() as $updater_version => $updater_callback_functions ) { | |
| 98 | + if ( ! is_array( $updater_callback_functions ) ) { | |
| 99 | + continue; | |
| 100 | + } | |
| 52 | 101 | |
| 53 | - // Update current version. | |
| 54 | - update_option( 'srfm-version', SRFM_VER ); | |
| 55 | - return; | |
| 102 | + if ( $this->old_version && ! version_compare( $this->old_version, $updater_version, '<' ) ) { | |
| 103 | + // Skip as SRFM saved version is not less than updaters version so db upgrade is not needed here. | |
| 104 | + continue; | |
| 105 | + } | |
| 106 | + | |
| 107 | + foreach ( $updater_callback_functions as $updater_callback_function ) { | |
| 108 | + call_user_func( $updater_callback_function, $this->old_version ); | |
| 109 | + } | |
| 110 | + } | |
| 56 | 111 | } |
| 112 | + | |
| 113 | + // Finally update cache and DB with current version. | |
| 114 | + $this->old_version = SRFM_VER; | |
| 115 | + update_option( 'srfm-version', $this->old_version ); | |
| 57 | 116 | } |
| 58 | 117 | |
| 59 | 118 | /** |
| 60 | 119 | * Plugin update notification. |
| @@ -109,5 +168,28 @@ | ||
| 109 | 168 | } |
| 110 | 169 | wp_enqueue_style( 'srfm-update-notice', SRFM_URL . 'admin/assets/css/update-notice.css', [], SRFM_VER ); |
| 111 | 170 | } |
| 112 | 171 | |
| 172 | + /** | |
| 173 | + * Returns an array of DB updater callback functions. | |
| 174 | + * | |
| 175 | + * @since 1.0.0 | |
| 176 | + * @return array<string,array<callable>>> Array of DB updater callback functions | |
| 177 | + */ | |
| 178 | + public function get_updater_callbacks() { | |
| 179 | + return [ | |
| 180 | + '1.0.2' => [ | |
| 181 | + 'SRFM\Inc\Updater_Callbacks::manage_default_dynamic_options', | |
| 182 | + ], | |
| 183 | + '1.0.4' => [ | |
| 184 | + 'SRFM\Inc\Updater_Callbacks::manage_empty_default_dynamic_options', | |
| 185 | + ], | |
| 186 | + '1.2.1' => [ | |
| 187 | + 'SRFM\Inc\Updater_Callbacks::manage_honeypot_option', | |
| 188 | + 'SRFM\Inc\Updater_Callbacks::manage_empty_global_dynamic_options', | |
| 189 | + ], | |
| 190 | + '2.12.3' => [ | |
| 191 | + 'SRFM\Inc\Updater_Callbacks::clear_elementor_page_assets_cache', | |
| 192 | + ], | |
| 193 | + ]; | |
| 194 | + } | |
| 113 | 195 | } |