PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.0.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.0.2
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / updater.php
updater.php
184 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
25 use Get_Instance;
26
27 /**
28 * Current DB saved version of SureForms.
29 *
30 * @var string
31 * @since 1.0.0
32 */
33 private $old_version;
34
35 /**
36 * Constructor.
37 *
38 * @since 0.0.12
39 * @return void
40 */
41 public function __construct() {
42 // Get auto saved version number.
43 $this->old_version = Helper::get_string_value( get_option( 'srfm-version', '' ) );
44
45 add_action( 'init', [ $this, 'init' ], 10 );
46 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
47 add_action( 'in_plugin_update_message-' . SRFM_BASENAME, [ $this, 'plugin_update_notification' ], 10 );
48 }
49
50 /**
51 * Whether or not to call the DB update methods.
52 *
53 * @since 1.0.0
54 * @return bool
55 */
56 public function needs_db_update() {
57 if ( ! $this->old_version ) {
58 if ( ! empty( get_posts( [ 'post_type' => SRFM_FORMS_POST_TYPE ] ) ) ) {
59 // Run db update if users have forms created. Edge case for the users directly updating from v0.0.10 or below.
60 return true;
61 }
62
63 // If old version is empty and no forms are created, it means it is fresh setup. Hence, DB upgrade is not needed.
64 return false;
65 }
66
67 $updater_callbacks = $this->get_updater_callbacks();
68
69 if ( empty( $updater_callbacks ) ) {
70 return false;
71 }
72
73 $versions = array_keys( $updater_callbacks );
74 $latest = $versions[ count( $versions ) - 1 ];
75
76 return version_compare( $this->old_version, $latest, '<' );
77 }
78
79 /**
80 * This function will help us to determine the plugin version and update it.
81 * Any major change in the option can be handed here on the basis of last plugin version found in the database.
82 *
83 * @since 0.0.12
84 * @since 1.0.0 -- Added db updater callbacks support.
85 * @return void
86 */
87 public function init() {
88 if ( version_compare( SRFM_VER, $this->old_version, '=' ) ) {
89 // Bail early because saved version is already updated and no change detected.
90 return;
91 }
92
93 if ( $this->needs_db_update() ) {
94 foreach ( $this->get_updater_callbacks() as $updater_version => $updater_callback_functions ) {
95 if ( ! is_array( $updater_callback_functions ) ) {
96 continue;
97 }
98
99 if ( $this->old_version && ! version_compare( $this->old_version, $updater_version, '<' ) ) {
100 // Skip as SRFM saved version is not less than updaters version so db upgrade is not needed here.
101 continue;
102 }
103
104 foreach ( $updater_callback_functions as $updater_callback_function ) {
105 call_user_func( $updater_callback_function, $this->old_version );
106 }
107 }
108 }
109
110 // Finally update cache and DB with current version.
111 $this->old_version = SRFM_VER;
112 update_option( 'srfm-version', $this->old_version );
113 }
114
115 /**
116 * Plugin update notification.
117 * This function will help us to display the update notice to the user.
118 * before updating the plugin.
119 * this info is fetched from latest available readme from repository.
120 *
121 * @param array<mixed> $data Plugin data.
122 * @since 0.0.12
123 * @return void
124 */
125 public function plugin_update_notification( $data ) {
126 if ( ! empty( $data['upgrade_notice'] ) ) { ?>
127 <hr class="srfm-plugin-update-notification__separator" />
128 <div class="srfm-plugin-update-notification">
129 <div class="srfm-plugin-update-notification__icon">
130 <span class="dashicons dashicons-info"></span>
131 </div>
132 <div>
133 <div class="srfm-plugin-update-notification__title">
134 <?php echo esc_html__( 'Heads up!', 'sureforms' ); ?>
135 </div>
136 <div class="srfm-plugin-update-notification__message">
137 <?php
138 $upgrade_notice = is_string( $data['upgrade_notice'] ) ? $data['upgrade_notice'] : '';
139 printf(
140 wp_kses(
141 $upgrade_notice,
142 [ 'a' => [ 'href' => [] ] ]
143 )
144 );
145 ?>
146 </div>
147 </div>
148 </div>
149 <?php
150 } //end if
151 }
152
153 /**
154 * Enqueue styles.
155 * This function will help us to enqueue the styles for the update notice.
156 *
157 * @since 0.0.12
158 * @return void
159 */
160 public function enqueue_styles() {
161
162 $screen = get_current_screen();
163 if ( empty( $screen->id ) || 'plugins' !== $screen->id ) {
164 return;
165 }
166 wp_enqueue_style( 'srfm-update-notice', SRFM_URL . 'admin/assets/css/update-notice.css', [], SRFM_VER );
167 }
168
169 /**
170 * Returns an array of DB updater callback functions.
171 *
172 * @since 1.0.0
173 * @return array<string,array<callable>>> Array of DB updater callback functions
174 */
175 public function get_updater_callbacks() {
176 return [
177 '1.0.2' => [
178 'SRFM\Inc\Updater_Callbacks::manage_default_dynamic_options',
179 ],
180 ];
181 }
182
183 }
184