update.php (2668B)
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 /** @var \WP_Site[] $sites */ 47 $sites = get_sites(); 48 49 foreach ( $sites as $keys => $blog ) { 50 // Cast $blog as an array instead of object 51 $blog_id = $blog->blog_id; 52 53 switch_to_blog( $blog_id ); 54 55 \WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) ); 56 57 $this->do_db_upgrade( $assoc_args ); 58 59 \WP_CLI::success( 'Done! - ' . get_option( 'home' ) ); 60 61 restore_current_blog(); 62 } 63 } else { 64 $this->do_db_upgrade( $assoc_args ); 65 } 66 } 67 68 protected function get_update_db_manager_class() { 69 return '\Elementor\Core\Upgrade\Manager'; 70 } 71 72 protected function do_db_upgrade( $assoc_args ) { 73 $manager_class = $this->get_update_db_manager_class(); 74 75 /** @var \Elementor\Core\Upgrade\Manager $manager */ 76 $manager = new $manager_class(); 77 78 $updater = $manager->get_task_runner(); 79 80 if ( $updater->is_process_locked() && empty( $assoc_args['force'] ) ) { 81 \WP_CLI::warning( 'Oops! Process is already running. Use --force to force run.' ); 82 return; 83 } 84 85 if ( ! $manager->should_upgrade() ) { 86 \WP_CLI::success( 'The DB is already updated!' ); 87 return; 88 } 89 90 $callbacks = $manager->get_upgrade_callbacks(); 91 $did_tasks = false; 92 93 if ( ! empty( $callbacks ) ) { 94 Plugin::$instance->logger->get_logger()->info( 'Update DB has been started', [ 95 'meta' => [ 96 'plugin' => $manager->get_plugin_label(), 97 'from' => $manager->get_current_version(), 98 'to' => $manager->get_new_version(), 99 ], 100 ] ); 101 102 $updater->handle_immediately( $callbacks ); 103 $did_tasks = true; 104 } 105 106 $manager->on_runner_complete( $did_tasks ); 107 108 \WP_CLI::success( count( $callbacks ) . ' updates(s) has been applied.' ); 109 } 110 }