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