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