command.php (4220B)
1 <?php 2 namespace Elementor\Modules\WpCli; 3 4 use Elementor\Api; 5 use Elementor\Plugin; 6 use Elementor\TemplateLibrary\Source_Local; 7 use Elementor\Utils; 8 9 if ( ! defined( 'ABSPATH' ) ) { 10 exit; // Exit if accessed directly 11 } 12 13 /** 14 * Elementor Page Builder cli tools. 15 */ 16 class Command extends \WP_CLI_Command { 17 18 /** 19 * Flush the Elementor Page Builder CSS Cache. 20 * 21 * [--network] 22 * Flush CSS Cache for all the sites in the network. 23 * 24 * ## EXAMPLES 25 * 26 * 1. wp elementor flush-css 27 * - This will flush the CSS files for elementor page builder. 28 * 29 * 2. wp elementor flush-css --network 30 * - This will flush the CSS files for elementor page builder for all the sites in the network. 31 * 32 * @since 2.1.0 33 * @access public 34 * @alias flush-css 35 */ 36 public function flush_css( $args, $assoc_args ) { 37 $network = ! empty( $assoc_args['network'] ) && is_multisite(); 38 39 if ( $network ) { 40 /** @var \WP_Site[] $blogs */ 41 $blogs = get_sites(); 42 43 foreach ( $blogs as $keys => $blog ) { 44 // Cast $blog as an array instead of object 45 $blog_id = $blog->blog_id; 46 47 switch_to_blog( $blog_id ); 48 49 Plugin::$instance->files_manager->clear_cache(); 50 51 \WP_CLI::success( 'Flushed the Elementor CSS Cache for site - ' . get_option( 'home' ) ); 52 53 restore_current_blog(); 54 } 55 } else { 56 Plugin::$instance->files_manager->clear_cache(); 57 58 \WP_CLI::success( 'Flushed the Elementor CSS Cache' ); 59 } 60 } 61 62 /** 63 * Print system info powered by Elementor 64 * 65 * ## EXAMPLES 66 * 67 * 1. wp elementor system-info 68 * - This will print the System Info in JSON format 69 * 70 * @since 3.0.11 71 * @access public 72 * @alias system-info 73 */ 74 public function system_info() { 75 echo wp_json_encode( \Elementor\Tracker::get_tracking_data() ); 76 } 77 78 /** 79 * Replace old URLs with new URLs in all Elementor pages. 80 * 81 * [--force] 82 * Suppress error messages. instead, return "0 affected rows.". 83 * 84 * ## EXAMPLES 85 * 86 * 1. wp elementor replace-urls <old> <new> 87 * - This will replace all <old> URLs with the <new> URL. 88 * 89 * 2. wp elementor replace-urls <old> <new> --force 90 * - This will replace all <old> URLs with the <new> URL without throw errors. 91 * 92 * @access public 93 * @alias replace-urls 94 */ 95 public function replace_urls( $args, $assoc_args ) { 96 if ( empty( $args[0] ) ) { 97 \WP_CLI::error( 'Please set the `old` URL' ); 98 } 99 100 if ( empty( $args[1] ) ) { 101 \WP_CLI::error( 'Please set the `new` URL' ); 102 } 103 104 try { 105 $results = Utils::replace_urls( $args[0], $args[1] ); 106 \WP_CLI::success( $results ); 107 } catch ( \Exception $e ) { 108 if ( isset( $assoc_args['force'] ) ) { 109 \WP_CLI::success( '0 rows affected.' ); 110 } else { 111 \WP_CLI::error( $e->getMessage() ); 112 } 113 } 114 } 115 116 /** 117 * Sync Elementor Library. 118 * 119 * ## EXAMPLES 120 * 121 * 1. wp elementor sync-library 122 * - This will sync the library with Elementor cloud library. 123 * 124 * @since 2.1.0 125 * @access public 126 * @alias sync-library 127 */ 128 public function sync_library( $args, $assoc_args ) { 129 // TODO: 130 // \WP_CLI::warning( 'command is deprecated since 2.8.0 Please use: wp elementor library sync' ); 131 132 $data = Api::get_library_data( true ); 133 134 if ( empty( $data ) ) { 135 \WP_CLI::error( 'Cannot sync library.' ); 136 } 137 138 \WP_CLI::success( 'Library has been synced.' ); 139 } 140 141 /** 142 * Import template files to the Library. 143 * 144 * ## EXAMPLES 145 * 146 * 1. wp elementor import-library <file-path> 147 * - This will import a file or a zip of multiple files to the library. 148 * 149 * @since 2.1.0 150 * @access public 151 * @alias import-library 152 */ 153 public function import_library( $args, $assoc_args ) { 154 // TODO: 155 // \WP_CLI::warning( 'command is deprecated since 2.8.0 Please use: wp elementor library import' ); 156 157 if ( empty( $args[0] ) ) { 158 \WP_CLI::error( 'Please set file path.' ); 159 } 160 161 /** @var Source_Local $source */ 162 $source = Plugin::$instance->templates_manager->get_source( 'local' ); 163 164 $imported_items = $source->import_template( basename( $args[0] ), $args[0] ); 165 166 if ( is_wp_error( $imported_items ) ) { 167 \WP_CLI::error( $imported_items->get_error_message() ); 168 } 169 170 \WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' ); 171 } 172 }