PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.0-dev2
Elementor Website Builder – more than just a page builder v3.24.0-dev2
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.24.0-dev2, at core/base/db-upgrades-manager.php

243 lines 6.0 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 if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) {
57 define( 'IS_ELEMENTOR_UPGRADE', true );
58 }
59 }
60
61 public function on_runner_complete( $did_tasks = false ) {
62 $logger = Plugin::$instance->logger->get_logger();
63
64 $logger->info( 'Elementor data updater process has been completed.', [
65 'meta' => [
66 'plugin' => $this->get_plugin_label(),
67 'from' => $this->current_version,
68 'to' => $this->get_new_version(),
69 ],
70 ] );
71
72 $this->clear_cache();
73
74 $this->update_db_version();
75
76 if ( $did_tasks ) {
77 $this->add_flag( 'completed' );
78 }
79 }
80
81 protected function clear_cache() {
82 Plugin::$instance->files_manager->clear_cache();
83 }
84
85 public function admin_notice_start_upgrade() {
86 /**
87 * @var Admin_Notices $admin_notices
88 */
89 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
90
91 $options = [
92 'title' => $this->get_updater_label(),
93 'description' => esc_html__( 'Your site database needs to be updated to the latest version.', 'elementor' ),
94 'type' => 'error',
95 'icon' => false,
96 'button' => [
97 'text' => esc_html__( 'Update Now', 'elementor' ),
98 'url' => $this->get_start_action_url(),
99 'class' => 'e-button e-button--cta',
100 ],
101 ];
102
103 $admin_notices->print_admin_notice( $options );
104 }
105
106 public function admin_notice_upgrade_is_running() {
107 /**
108 * @var Admin_Notices $admin_notices
109 */
110 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
111
112 $options = [
113 'title' => $this->get_updater_label(),
114 'description' => esc_html__( 'Database update process is running in the background. Taking a while?', 'elementor' ),
115 'type' => 'warning',
116 'icon' => false,
117 'button' => [
118 'text' => esc_html__( 'Click here to run it now', 'elementor' ),
119 'url' => $this->get_continue_action_url(),
120 'class' => 'e-button e-button--primary',
121 ],
122 ];
123
124 $admin_notices->print_admin_notice( $options );
125 }
126
127 public function admin_notice_upgrade_is_completed() {
128 $this->delete_flag( 'completed' );
129
130 $message = esc_html__( 'The database update process is now complete. Thank you for updating to the latest version!', 'elementor' );
131
132 /**
133 * @var Admin_Notices $admin_notices
134 */
135 $admin_notices = Plugin::$instance->admin->get_component( 'admin-notices' );
136
137 $options = [
138 'description' => '<b>' . $this->get_updater_label() . '</b> - ' . $message,
139 'type' => 'success',
140 'icon' => false,
141 ];
142
143 $admin_notices->print_admin_notice( $options );
144 }
145
146 /**
147 * @access protected
148 */
149 protected function start_run() {
150 $updater = $this->get_task_runner();
151
152 if ( $updater->is_running() ) {
153 return;
154 }
155
156 $upgrade_callbacks = $this->get_upgrade_callbacks();
157
158 if ( empty( $upgrade_callbacks ) ) {
159 $this->on_runner_complete();
160 return;
161 }
162
163 $this->clear_cache();
164
165 foreach ( $upgrade_callbacks as $callback ) {
166 $updater->push_to_queue( [
167 'callback' => $callback,
168 ] );
169 }
170
171 $updater->save()->dispatch();
172
173 Plugin::$instance->logger->get_logger()->info( 'Elementor data updater process has been queued.', [
174 'meta' => [
175 'plugin' => $this->get_plugin_label(),
176 'from' => $this->current_version,
177 'to' => $this->get_new_version(),
178 ],
179 ] );
180 }
181
182 protected function update_db_version() {
183 update_option( $this->get_version_option_name(), $this->get_new_version() );
184 }
185
186 public function get_upgrade_callbacks() {
187 $prefix = '_v_';
188 $upgrades_class = $this->get_upgrades_class();
189 $upgrades_reflection = new \ReflectionClass( $upgrades_class );
190
191 $callbacks = [];
192
193 foreach ( $upgrades_reflection->getMethods() as $method ) {
194 $method_name = $method->getName();
195
196 if ( '_on_each_version' === $method_name ) {
197 $callbacks[] = [ $upgrades_class, $method_name ];
198 continue;
199 }
200
201 if ( false === strpos( $method_name, $prefix ) ) {
202 continue;
203 }
204
205 if ( ! preg_match_all( "/$prefix(\d+_\d+_\d+)/", $method_name, $matches ) ) {
206 continue;
207 }
208
209 $method_version = str_replace( '_', '.', $matches[1][0] );
210
211 if ( ! version_compare( $method_version, $this->current_version, '>' ) ) {
212 continue;
213 }
214
215 $callbacks[] = [ $upgrades_class, $method_name ];
216 }
217
218 return $callbacks;
219 }
220
221 public function __construct() {
222 // If upgrade is completed - show the notice only for admins.
223 // Note: in this case `should_upgrade` returns false, because it's already upgraded.
224 if ( is_admin() && current_user_can( 'update_plugins' ) && $this->get_flag( 'completed' ) ) {
225 add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_completed' ] );
226 }
227
228 if ( ! $this->should_upgrade() ) {
229 return;
230 }
231
232 $updater = $this->get_task_runner();
233
234 $this->start_run();
235
236 if ( $updater->is_running() && current_user_can( 'update_plugins' ) ) {
237 add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_running' ] );
238 }
239
240 parent::__construct();
241 }
242 }
243