PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.3
Elementor Website Builder – more than just a page builder v3.2.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / base / db-upgrades-manager.php

db-upgrades-manager.php in Elementor Website Builder – more than just a page builder 3.2.3, at core/base/db-upgrades-manager.php

229 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Core\Base;
4
5 use Elementor\Core\Admin\Admin_Notices;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly
10 }
11
12 abstract class DB_Upgrades_Manager extends Background_Task_Manager {
13 protected $current_version = null;
14 protected $query_limit = 100;
15
16 abstract public function get_new_version();
17 abstract public function get_version_option_name();
18 abstract public function get_upgrades_class();
19 abstract public function get_updater_label();
20
21 public function get_task_runner_class() {
22 return 'Elementor\Core\Upgrade\Updater';
23 }
24
25 public function get_query_limit() {
26 return $this->query_limit;
27 }
28
29 public function set_query_limit( $limit ) {
30 $this->query_limit = $limit;
31 }
32
33 public function get_current_version() {
34 if ( null === $this->current_version ) {
35 $this->current_version = get_option( $this->get_version_option_name() );
36 }
37
38 return $this->current_version;
39 }
40
41 public function should_upgrade() {
42 $current_version = $this->get_current_version();
43
44 // It's a new install.
45 if ( ! $current_version ) {
46 $this->update_db_version();
47 return false;
48 }
49
50 return version_compare( $this->get_new_version(), $current_version, '>' );
51 }
52
53 public function on_runner_start() {
54 parent::on_runner_start();
55
56 define( 'IS_ELEMENTOR_UPGRADE', true );
57 }
58
59 public function on_runner_complete( $did_tasks = false ) {
60 $logger = Plugin::$instance->logger->get_logger();
61
62 $logger->info( 'Elementor data updater process has been completed.', [
63 'meta' => [
64 'plugin' => $this->get_plugin_label(),
65 'from' => $this->current_version,
66 'to' => $this->get_new_version(),
67 ],
68 ] );
69
70 Plugin::$instance->files_manager->clear_cache();
71
72 $this->update_db_version();
73
74 if ( $did_tasks ) {
75 $this->add_flag( 'completed' );
76 }
77 }
78
79 public function admin_notice_start_upgrade() {
80 /**
81 * @var Admin_Notices $admin_notices
82 */
83 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
84
85 $options = [
86 'title' => $this->get_updater_label(),
87 'description' => __( 'Your site database needs to be updated to the latest version.', 'elementor' ),
88 'type' => 'error',
89 'icon' => false,
90 'button' => [
91 'text' => __( 'Update Now', 'elementor' ),
92 'url' => $this->get_start_action_url(),
93 'class' => 'e-button e-button--cta',
94 ],
95 ];
96
97 $admin_notices->print_admin_notice( $options );
98 }
99
100 public function admin_notice_upgrade_is_running() {
101 /**
102 * @var Admin_Notices $admin_notices
103 */
104 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
105
106 $options = [
107 'title' => $this->get_updater_label(),
108 'description' => __( 'Database update process is running in the background. Taking a while?', 'elementor' ),
109 'type' => 'warning',
110 'icon' => false,
111 'button' => [
112 'text' => __( 'Click here to run it now', 'elementor' ),
113 'url' => $this->get_continue_action_url(),
114 'class' => 'e-button e-button--primary',
115 ],
116 ];
117
118 $admin_notices->print_admin_notice( $options );
119 }
120
121 public function admin_notice_upgrade_is_completed() {
122 $this->delete_flag( 'completed' );
123
124 $message = __( 'The database update process is now complete. Thank you for updating to the latest version!', 'elementor' );
125
126 /**
127 * @var Admin_Notices $admin_notices
128 */
129 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
130
131 $options = [
132 'description' => '<b>' . $this->get_updater_label() . '</b> - ' . $message,
133 'type' => 'success',
134 'icon' => false,
135 ];
136
137 $admin_notices->print_admin_notice( $options );
138 }
139
140 /**
141 * @access protected
142 */
143 protected function start_run() {
144 $updater = $this->get_task_runner();
145
146 if ( $updater->is_running() ) {
147 return;
148 }
149
150 $upgrade_callbacks = $this->get_upgrade_callbacks();
151
152 if ( empty( $upgrade_callbacks ) ) {
153 $this->on_runner_complete();
154 return;
155 }
156
157 foreach ( $upgrade_callbacks as $callback ) {
158 $updater->push_to_queue( [
159 'callback' => $callback,
160 ] );
161 }
162
163 $updater->save()->dispatch();
164
165 Plugin::$instance->logger->get_logger()->info( 'Elementor data updater process has been queued.', [
166 'meta' => [
167 'plugin' => $this->get_plugin_label(),
168 'from' => $this->current_version,
169 'to' => $this->get_new_version(),
170 ],
171 ] );
172 }
173
174 protected function update_db_version() {
175 update_option( $this->get_version_option_name(), $this->get_new_version() );
176 }
177
178 public function get_upgrade_callbacks() {
179 $prefix = '_v_';
180 $upgrades_class = $this->get_upgrades_class();
181 $upgrades_reflection = new \ReflectionClass( $upgrades_class );
182
183 $callbacks = [];
184
185 foreach ( $upgrades_reflection->getMethods() as $method ) {
186 $method_name = $method->getName();
187 if ( false === strpos( $method_name, $prefix ) ) {
188 continue;
189 }
190
191 if ( ! preg_match_all( "/$prefix(\d+_\d+_\d+)/", $method_name, $matches ) ) {
192 continue;
193 }
194
195 $method_version = str_replace( '_', '.', $matches[1][0] );
196
197 if ( ! version_compare( $method_version, $this->current_version, '>' ) ) {
198 continue;
199 }
200
201 $callbacks[] = [ $upgrades_class, $method_name ];
202 }
203
204 return $callbacks;
205 }
206
207 public function __construct() {
208 // If upgrade is completed - show the notice only for admins.
209 // Note: in this case `should_upgrade` returns false, because it's already upgraded.
210 if ( is_admin() && current_user_can( 'update_plugins' ) && $this->get_flag( 'completed' ) ) {
211 add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_completed' ] );
212 }
213
214 if ( ! $this->should_upgrade() ) {
215 return;
216 }
217
218 $updater = $this->get_task_runner();
219
220 $this->start_run();
221
222 if ( $updater->is_running() && current_user_can( 'update_plugins' ) ) {
223 add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_running' ] );
224 }
225
226 parent::__construct();
227 }
228 }
229