| 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 |
// Skip if process is actively running (has lock) or queue already has items. |
| 153 |
// Stuck queues (items exist but no lock) are handled by the shutdown fallback |
| 154 |
// registered in the constructor, so we don't need to re-push callbacks here. |
| 155 |
if ( $updater->is_process_locked() || $updater->is_running() ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
$upgrade_callbacks = $this->get_upgrade_callbacks(); |
| 160 |
|
| 161 |
if ( empty( $upgrade_callbacks ) ) { |
| 162 |
$this->on_runner_complete(); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$this->clear_cache(); |
| 167 |
|
| 168 |
foreach ( $upgrade_callbacks as $callback ) { |
| 169 |
$updater->push_to_queue( [ |
| 170 |
'callback' => $callback, |
| 171 |
] ); |
| 172 |
} |
| 173 |
|
| 174 |
$updater->save()->dispatch(); |
| 175 |
|
| 176 |
Plugin::$instance->logger->get_logger()->info( 'Elementor data updater process has been queued.', [ |
| 177 |
'meta' => [ |
| 178 |
'plugin' => $this->get_plugin_label(), |
| 179 |
'from' => $this->current_version, |
| 180 |
'to' => $this->get_new_version(), |
| 181 |
], |
| 182 |
] ); |
| 183 |
} |
| 184 |
|
| 185 |
protected function update_db_version() { |
| 186 |
update_option( $this->get_version_option_name(), $this->get_new_version() ); |
| 187 |
} |
| 188 |
|
| 189 |
public function get_upgrade_callbacks() { |
| 190 |
$prefix = '_v_'; |
| 191 |
$upgrades_class = $this->get_upgrades_class(); |
| 192 |
$upgrades_reflection = new \ReflectionClass( $upgrades_class ); |
| 193 |
|
| 194 |
$callbacks = []; |
| 195 |
|
| 196 |
foreach ( $upgrades_reflection->getMethods() as $method ) { |
| 197 |
$method_name = $method->getName(); |
| 198 |
|
| 199 |
if ( '_on_each_version' === $method_name ) { |
| 200 |
$callbacks[] = [ $upgrades_class, $method_name ]; |
| 201 |
continue; |
| 202 |
} |
| 203 |
|
| 204 |
if ( false === strpos( $method_name, $prefix ) ) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
if ( ! preg_match_all( "/$prefix(\d+_\d+_\d+)/", $method_name, $matches ) ) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
$method_version = str_replace( '_', '.', $matches[1][0] ); |
| 213 |
|
| 214 |
if ( ! version_compare( $method_version, $this->current_version, '>' ) ) { |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
$callbacks[] = [ $upgrades_class, $method_name ]; |
| 219 |
} |
| 220 |
|
| 221 |
return $callbacks; |
| 222 |
} |
| 223 |
|
| 224 |
public function __construct() { |
| 225 |
// If upgrade is completed - show the notice only for admins. |
| 226 |
// Note: in this case `should_upgrade` returns false, because it's already upgraded. |
| 227 |
if ( is_admin() && current_user_can( 'update_plugins' ) && $this->get_flag( 'completed' ) ) { |
| 228 |
add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_completed' ] ); |
| 229 |
} |
| 230 |
|
| 231 |
// Don't run upgrade logic during background process AJAX requests. |
| 232 |
// The AJAX handler (maybe_handle) will process the queue. |
| 233 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 234 |
$ajax_action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; |
| 235 |
|
| 236 |
if ( wp_doing_ajax() && $ajax_action && false !== strpos( $ajax_action, 'updater' ) ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! $this->should_upgrade() ) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
$updater = $this->get_task_runner(); |
| 245 |
|
| 246 |
$this->start_run(); |
| 247 |
|
| 248 |
// If queue has items but process is not locked, it's stuck - try to process on shutdown. |
| 249 |
if ( $updater->is_running() && ! $updater->is_process_locked() ) { |
| 250 |
if ( is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() ) { |
| 251 |
add_action( 'shutdown', [ $updater, 'maybe_handle_on_shutdown' ], 0 ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ( $updater->is_running() && current_user_can( 'update_plugins' ) ) { |
| 256 |
add_action( 'admin_notices', [ $this, 'admin_notice_upgrade_is_running' ] ); |
| 257 |
} |
| 258 |
|
| 259 |
parent::__construct(); |
| 260 |
} |
| 261 |
} |
| 262 |
|