| 1 |
<?php |
| 2 |
namespace Elementor\Modules\WpCli; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Elementor Page Builder cli tools. |
| 12 |
*/ |
| 13 |
class Update extends \WP_CLI_Command { |
| 14 |
|
| 15 |
/** |
| 16 |
* Update the DB after plugin upgrade. |
| 17 |
* |
| 18 |
* [--network] |
| 19 |
* Update DB in all the sites in the network. |
| 20 |
* |
| 21 |
* [--force] |
| 22 |
* Force update even if it's looks like that update is in progress. |
| 23 |
* |
| 24 |
* |
| 25 |
* ## EXAMPLES |
| 26 |
* |
| 27 |
* 1. wp elementor update db |
| 28 |
* - This will Upgrade the DB if needed. |
| 29 |
* |
| 30 |
* 2. wp elementor update db --force |
| 31 |
* - This will Upgrade the DB even if another process is running. |
| 32 |
* |
| 33 |
* 3. wp elementor update db --network |
| 34 |
* - This will Upgrade the DB for each site in the network if needed. |
| 35 |
* |
| 36 |
* @since 2.4.0 |
| 37 |
* @access public |
| 38 |
* |
| 39 |
* @param $args |
| 40 |
* @param $assoc_args |
| 41 |
*/ |
| 42 |
public function db( $args, $assoc_args ) { |
| 43 |
$network = ! empty( $assoc_args['network'] ) && is_multisite(); |
| 44 |
|
| 45 |
if ( $network ) { |
| 46 |
$blog_ids = get_sites( [ |
| 47 |
'fields' => 'ids', |
| 48 |
'number' => 0, |
| 49 |
] ); |
| 50 |
|
| 51 |
foreach ( $blog_ids as $blog_id ) { |
| 52 |
switch_to_blog( $blog_id ); |
| 53 |
|
| 54 |
\WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) ); |
| 55 |
|
| 56 |
$this->do_db_upgrade( $assoc_args ); |
| 57 |
|
| 58 |
\WP_CLI::success( 'Done! - ' . get_option( 'home' ) ); |
| 59 |
|
| 60 |
restore_current_blog(); |
| 61 |
} |
| 62 |
} else { |
| 63 |
$this->do_db_upgrade( $assoc_args ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
protected function get_update_db_manager_class() { |
| 68 |
return '\Elementor\Core\Upgrade\Manager'; |
| 69 |
} |
| 70 |
|
| 71 |
protected function do_db_upgrade( $assoc_args ) { |
| 72 |
$manager_class = $this->get_update_db_manager_class(); |
| 73 |
|
| 74 |
/** @var \Elementor\Core\Upgrade\Manager $manager */ |
| 75 |
$manager = new $manager_class(); |
| 76 |
|
| 77 |
$updater = $manager->get_task_runner(); |
| 78 |
|
| 79 |
if ( $updater->is_process_locked() && empty( $assoc_args['force'] ) ) { |
| 80 |
\WP_CLI::warning( 'Oops! Process is already running. Use --force to force run.' ); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! $manager->should_upgrade() ) { |
| 85 |
\WP_CLI::success( 'The DB is already updated!' ); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$callbacks = $manager->get_upgrade_callbacks(); |
| 90 |
$did_tasks = false; |
| 91 |
|
| 92 |
if ( ! empty( $callbacks ) ) { |
| 93 |
Plugin::$instance->logger->get_logger()->info( 'Update DB has been started', [ |
| 94 |
'meta' => [ |
| 95 |
'plugin' => $manager->get_plugin_label(), |
| 96 |
'from' => $manager->get_current_version(), |
| 97 |
'to' => $manager->get_new_version(), |
| 98 |
], |
| 99 |
] ); |
| 100 |
|
| 101 |
$updater->handle_immediately( $callbacks ); |
| 102 |
$did_tasks = true; |
| 103 |
} |
| 104 |
|
| 105 |
$manager->on_runner_complete( $did_tasks ); |
| 106 |
|
| 107 |
\WP_CLI::success( count( $callbacks ) . ' updates(s) has been applied.' ); |
| 108 |
} |
| 109 |
} |
| 110 |
|