| 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 |
* [--regenerate] |
| 25 |
* Re-create the CSS files. Otherwise they will be created by a page visit. |
| 26 |
* |
| 27 |
* ## EXAMPLES |
| 28 |
* |
| 29 |
* 1. wp elementor flush-css |
| 30 |
* - This will flush the CSS files for elementor page builder. |
| 31 |
* |
| 32 |
* 2. wp elementor flush-css --network |
| 33 |
* - This will flush the CSS files for elementor page builder for all the sites in the network. |
| 34 |
* |
| 35 |
* 3. wp elementor flush-css --regenerate |
| 36 |
* - This will flush the CSS files for elementor page builder and re-create the new CSS files. |
| 37 |
* |
| 38 |
* @since 2.1.0 |
| 39 |
* @access public |
| 40 |
* @alias flush-css |
| 41 |
*/ |
| 42 |
public function flush_css( $args, $assoc_args ) { |
| 43 |
$network = ! empty( $assoc_args['network'] ) && is_multisite(); |
| 44 |
$should_regenerate = ! empty( $assoc_args['regenerate'] ); |
| 45 |
|
| 46 |
if ( $network ) { |
| 47 |
$blog_ids = get_sites( [ |
| 48 |
'fields' => 'ids', |
| 49 |
'number' => 0, |
| 50 |
] ); |
| 51 |
|
| 52 |
foreach ( $blog_ids as $blog_id ) { |
| 53 |
switch_to_blog( $blog_id ); |
| 54 |
|
| 55 |
$this->handle_flush( $should_regenerate, 'Flushed the Elementor CSS Cache for site - ' . get_option( 'home' ) ); |
| 56 |
|
| 57 |
restore_current_blog(); |
| 58 |
} |
| 59 |
} else { |
| 60 |
$this->handle_flush( $should_regenerate, 'Flushed the Elementor CSS Cache' ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private function handle_flush( bool $should_regenerate, string $success_message ): void { |
| 65 |
Plugin::$instance->files_manager->clear_cache(); |
| 66 |
|
| 67 |
if ( $should_regenerate ) { |
| 68 |
Plugin::$instance->files_manager->generate_css(); |
| 69 |
} |
| 70 |
|
| 71 |
\WP_CLI::success( $success_message ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Print system info powered by Elementor |
| 76 |
* |
| 77 |
* ## EXAMPLES |
| 78 |
* |
| 79 |
* 1. wp elementor system-info |
| 80 |
* - This will print the System Info in JSON format |
| 81 |
* |
| 82 |
* @since 3.0.11 |
| 83 |
* @access public |
| 84 |
* @alias system-info |
| 85 |
*/ |
| 86 |
public function system_info() { |
| 87 |
echo wp_json_encode( \Elementor\Tracker::get_tracking_data() ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Replace old URLs with new URLs in all Elementor pages. |
| 92 |
* |
| 93 |
* [--force] |
| 94 |
* Suppress error messages. instead, return "0 database rows affected.". |
| 95 |
* |
| 96 |
* ## EXAMPLES |
| 97 |
* |
| 98 |
* 1. wp elementor replace-urls <old> <new> |
| 99 |
* - This will replace all <old> URLs with the <new> URL. |
| 100 |
* |
| 101 |
* 2. wp elementor replace-urls <old> <new> --force |
| 102 |
* - This will replace all <old> URLs with the <new> URL without throw errors. |
| 103 |
* |
| 104 |
* @access public |
| 105 |
* @alias replace-urls |
| 106 |
*/ |
| 107 |
public function replace_urls( $args, $assoc_args ) { |
| 108 |
if ( empty( $args[0] ) ) { |
| 109 |
\WP_CLI::error( 'Please set the `old` URL' ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( empty( $args[1] ) ) { |
| 113 |
\WP_CLI::error( 'Please set the `new` URL' ); |
| 114 |
} |
| 115 |
|
| 116 |
try { |
| 117 |
$results = Utils::replace_urls( $args[0], $args[1] ); |
| 118 |
\WP_CLI::success( $results ); |
| 119 |
} catch ( \Exception $e ) { |
| 120 |
if ( isset( $assoc_args['force'] ) ) { |
| 121 |
\WP_CLI::success( '0 database rows affected.' ); |
| 122 |
} else { |
| 123 |
\WP_CLI::error( $e->getMessage() ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Sync Elementor Library. |
| 130 |
* |
| 131 |
* ## EXAMPLES |
| 132 |
* |
| 133 |
* 1. wp elementor sync-library |
| 134 |
* - This will sync the library with Elementor cloud library. |
| 135 |
* |
| 136 |
* @since 2.1.0 |
| 137 |
* @access public |
| 138 |
* @alias sync-library |
| 139 |
*/ |
| 140 |
public function sync_library( $args, $assoc_args ) { |
| 141 |
// TODO: |
| 142 |
// \WP_CLI::warning( 'command is deprecated since 2.8.0 Please use: wp elementor library sync' ); |
| 143 |
|
| 144 |
$data = Api::get_library_data( true ); |
| 145 |
|
| 146 |
if ( empty( $data ) ) { |
| 147 |
\WP_CLI::error( 'Cannot sync library.' ); |
| 148 |
} |
| 149 |
|
| 150 |
\WP_CLI::success( 'Library has been synced.' ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Import template files to the Library. |
| 155 |
* |
| 156 |
* ## EXAMPLES |
| 157 |
* |
| 158 |
* 1. wp elementor import-library <file-path> |
| 159 |
* - This will import a file or a zip of multiple files to the library. |
| 160 |
* |
| 161 |
* @since 2.1.0 |
| 162 |
* @access public |
| 163 |
* @alias import-library |
| 164 |
*/ |
| 165 |
public function import_library( $args, $assoc_args ) { |
| 166 |
// TODO: |
| 167 |
// \WP_CLI::warning( 'command is deprecated since 2.8.0 Please use: wp elementor library import' ); |
| 168 |
|
| 169 |
if ( empty( $args[0] ) ) { |
| 170 |
\WP_CLI::error( 'Please set file path.' ); |
| 171 |
} |
| 172 |
|
| 173 |
/** @var Source_Local $source */ |
| 174 |
$source = Plugin::$instance->templates_manager->get_source( 'local' ); |
| 175 |
|
| 176 |
$imported_items = $source->import_template( basename( $args[0] ), $args[0] ); |
| 177 |
|
| 178 |
if ( is_wp_error( $imported_items ) ) { |
| 179 |
\WP_CLI::error( $imported_items->get_error_message() ); |
| 180 |
} |
| 181 |
|
| 182 |
\WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' ); |
| 183 |
} |
| 184 |
} |
| 185 |
|