abilities
2 weeks ago
admin
1 month ago
ai-form-builder
2 weeks ago
blocks
1 month ago
compatibility
3 weeks ago
database
3 weeks ago
email
3 months ago
fields
3 weeks ago
global-settings
2 weeks ago
lib
2 weeks ago
migrator
3 weeks ago
page-builders
3 weeks ago
payments
2 weeks ago
single-form-settings
1 month ago
traits
1 month ago
activator.php
1 year ago
admin-ajax.php
3 weeks ago
background-process.php
8 months ago
create-new-form.php
2 months ago
duplicate-form.php
1 month ago
entries.php
3 weeks ago
events-scheduler.php
2 years ago
export.php
1 month ago
field-validation.php
2 weeks ago
form-restriction.php
1 month ago
form-styling.php
3 months ago
form-submit.php
3 weeks ago
forms-data.php
3 months ago
frontend-assets.php
2 weeks ago
generate-form-markup.php
3 weeks ago
gutenberg-hooks.php
1 month ago
helper.php
3 weeks ago
learn.php
3 months ago
onboarding.php
1 month ago
post-types.php
1 month ago
rest-api.php
2 weeks ago
smart-tags.php
2 months ago
submit-token.php
3 months ago
translatable.php
2 weeks ago
updater-callbacks.php
1 year ago
updater.php
1 year ago
updater.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SureForms Updater. |
| 4 | * Manages important update related to the plugin. |
| 5 | * |
| 6 | * @package sureforms. |
| 7 | * @since 0.0.12 |
| 8 | */ |
| 9 | |
| 10 | namespace SRFM\Inc; |
| 11 | |
| 12 | use SRFM\Inc\Traits\Get_Instance; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Updater class. |
| 20 | * |
| 21 | * @since 0.0.12 |
| 22 | */ |
| 23 | class Updater { |
| 24 | use Get_Instance; |
| 25 | |
| 26 | /** |
| 27 | * Current DB saved version of SureForms. |
| 28 | * |
| 29 | * @var string |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | private $old_version; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | * |
| 37 | * @since 0.0.12 |
| 38 | * @return void |
| 39 | */ |
| 40 | public function __construct() { |
| 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 ); |
| 49 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] ); |
| 50 | add_action( 'in_plugin_update_message-' . SRFM_BASENAME, [ $this, 'plugin_update_notification' ], 10 ); |
| 51 | } |
| 52 | |
| 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 | /** |
| 83 | * This function will help us to determine the plugin version and update it. |
| 84 | * Any major change in the option can be handed here on the basis of last plugin version found in the database. |
| 85 | * |
| 86 | * @since 0.0.12 |
| 87 | * @since 1.0.0 -- Added db updater callbacks support. |
| 88 | * @return void |
| 89 | */ |
| 90 | public function init() { |
| 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 | } |
| 95 | |
| 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 | } |
| 101 | |
| 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 | } |
| 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 ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Plugin update notification. |
| 120 | * This function will help us to display the update notice to the user. |
| 121 | * before updating the plugin. |
| 122 | * this info is fetched from latest available readme from repository. |
| 123 | * |
| 124 | * @param array<mixed> $data Plugin data. |
| 125 | * @since 0.0.12 |
| 126 | * @return void |
| 127 | */ |
| 128 | public function plugin_update_notification( $data ) { |
| 129 | if ( ! empty( $data['upgrade_notice'] ) ) { ?> |
| 130 | <hr class="srfm-plugin-update-notification__separator" /> |
| 131 | <div class="srfm-plugin-update-notification"> |
| 132 | <div class="srfm-plugin-update-notification__icon"> |
| 133 | <span class="dashicons dashicons-info"></span> |
| 134 | </div> |
| 135 | <div> |
| 136 | <div class="srfm-plugin-update-notification__title"> |
| 137 | <?php echo esc_html__( 'Heads up!', 'sureforms' ); ?> |
| 138 | </div> |
| 139 | <div class="srfm-plugin-update-notification__message"> |
| 140 | <?php |
| 141 | $upgrade_notice = is_string( $data['upgrade_notice'] ) ? $data['upgrade_notice'] : ''; |
| 142 | printf( |
| 143 | wp_kses( |
| 144 | $upgrade_notice, |
| 145 | [ 'a' => [ 'href' => [] ] ] |
| 146 | ) |
| 147 | ); |
| 148 | ?> |
| 149 | </div> |
| 150 | </div> |
| 151 | </div> |
| 152 | <?php |
| 153 | } //end if |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Enqueue styles. |
| 158 | * This function will help us to enqueue the styles for the update notice. |
| 159 | * |
| 160 | * @since 0.0.12 |
| 161 | * @return void |
| 162 | */ |
| 163 | public function enqueue_styles() { |
| 164 | |
| 165 | $screen = get_current_screen(); |
| 166 | if ( empty( $screen->id ) || 'plugins' !== $screen->id ) { |
| 167 | return; |
| 168 | } |
| 169 | wp_enqueue_style( 'srfm-update-notice', SRFM_URL . 'admin/assets/css/update-notice.css', [], SRFM_VER ); |
| 170 | } |
| 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 | ]; |
| 191 | } |
| 192 | } |
| 193 |