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
library.php
289 lines
| 1 | <?php |
| 2 | namespace Elementor\Modules\WpCli; |
| 3 | |
| 4 | use Elementor\Api; |
| 5 | use Elementor\Plugin; |
| 6 | use Elementor\TemplateLibrary\Source_Local; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Elementor Page Builder cli tools. |
| 14 | */ |
| 15 | class Library extends \WP_CLI_Command { |
| 16 | |
| 17 | /** |
| 18 | * Sync Elementor Library. |
| 19 | * |
| 20 | * [--network] |
| 21 | * Sync library in all the sites in the network. |
| 22 | * |
| 23 | * [--force] |
| 24 | * Force sync even if it's looks like that the library is already up to date. |
| 25 | * |
| 26 | * ## EXAMPLES |
| 27 | * |
| 28 | * 1. wp elementor library sync |
| 29 | * - This will sync the library with Elementor cloud library. |
| 30 | * |
| 31 | * 2. wp elementor library sync --force |
| 32 | * - This will sync the library with Elementor cloud even if it's looks like that the library is already up to date. |
| 33 | * |
| 34 | * 3. wp elementor library sync --network |
| 35 | * - This will sync the library with Elementor cloud library for each site in the network if needed. |
| 36 | * |
| 37 | * @since 2.8.0 |
| 38 | * @access public |
| 39 | */ |
| 40 | public function sync( $args, $assoc_args ) { |
| 41 | $network = isset( $assoc_args['network'] ) && is_multisite(); |
| 42 | |
| 43 | if ( $network ) { |
| 44 | $blog_ids = get_sites( [ |
| 45 | 'fields' => 'ids', |
| 46 | 'number' => 0, |
| 47 | ] ); |
| 48 | |
| 49 | foreach ( $blog_ids as $blog_id ) { |
| 50 | switch_to_blog( $blog_id ); |
| 51 | |
| 52 | \WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) ); |
| 53 | |
| 54 | $this->do_sync( isset( $assoc_args['force'] ) ); |
| 55 | |
| 56 | \WP_CLI::success( 'Done! - ' . get_option( 'home' ) ); |
| 57 | |
| 58 | restore_current_blog(); |
| 59 | } |
| 60 | } else { |
| 61 | $this->do_sync( isset( $assoc_args['force'] ) ); |
| 62 | \WP_CLI::success( 'Done!' ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Import template files to the Library. |
| 68 | * |
| 69 | * [--returnType] |
| 70 | * Forms of output. Possible values are 'ids', 'info'. |
| 71 | * if this parameter won't be specified, the import info will be output. |
| 72 | * |
| 73 | * ## EXAMPLES |
| 74 | * |
| 75 | * 1. wp elementor library import <file-path> |
| 76 | * - This will import a file or a zip of multiple files to the library. |
| 77 | * - file-path can be a path or url. |
| 78 | * |
| 79 | * 2. wp elementor library import <file-path> --returnType=info,ids |
| 80 | * |
| 81 | * @param $args |
| 82 | * @param $assoc_args |
| 83 | * |
| 84 | * @since 2.8.0 |
| 85 | * @access public |
| 86 | */ |
| 87 | public function import( $args, $assoc_args ) { |
| 88 | if ( empty( $args[0] ) ) { |
| 89 | \WP_CLI::error( 'Please set file path.' ); |
| 90 | } |
| 91 | |
| 92 | $file = $args[0]; |
| 93 | $imported_items_ids = []; |
| 94 | $return_type = \WP_CLI\Utils\get_flag_value( $assoc_args, 'returnType', 'info' ); |
| 95 | |
| 96 | /** @var Source_Local $source */ |
| 97 | $source = Plugin::$instance->templates_manager->get_source( 'local' ); |
| 98 | |
| 99 | if ( filter_var( $file, FILTER_VALIDATE_URL ) ) { |
| 100 | $tmp_path = download_url( $file ); |
| 101 | if ( is_wp_error( $tmp_path ) ) { |
| 102 | \WP_CLI::error( $tmp_path->get_error_message() ); |
| 103 | } |
| 104 | $file = $tmp_path; |
| 105 | } |
| 106 | |
| 107 | $imported_items = $source->import_template( basename( $file ), $file ); |
| 108 | |
| 109 | if ( is_wp_error( $imported_items ) ) { |
| 110 | \WP_CLI::error( $imported_items->get_error_message() ); |
| 111 | } |
| 112 | |
| 113 | foreach ( $imported_items as $item ) { |
| 114 | $imported_items_ids[] = $item['template_id']; |
| 115 | } |
| 116 | $imported_items_ids = implode( ',', $imported_items_ids ); |
| 117 | |
| 118 | if ( 'ids' === $return_type ) { |
| 119 | \WP_CLI::line( $imported_items_ids ); |
| 120 | } else { |
| 121 | \WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' ); |
| 122 | } |
| 123 | |
| 124 | if ( isset( $tmp_path ) ) { |
| 125 | // Remove the temporary file, now that we're done with it. |
| 126 | Plugin::$instance->uploads_manager->remove_file_or_dir( $file ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Import all template files from a directory. |
| 132 | * |
| 133 | * ## EXAMPLES |
| 134 | * |
| 135 | * 1. wp elementor library import-dir <file-path> |
| 136 | * - This will import all JSON files from <file-path> |
| 137 | * |
| 138 | * @param $args |
| 139 | * |
| 140 | * @since 3.4.7 |
| 141 | * @access public |
| 142 | * @alias import-dir |
| 143 | */ |
| 144 | public function import_dir( $args ) { |
| 145 | if ( empty( $args[0] ) ) { |
| 146 | \WP_CLI::error( 'Please set dir path.' ); |
| 147 | } |
| 148 | |
| 149 | $dir = $args[0]; |
| 150 | |
| 151 | if ( ! file_exists( $dir ) ) { |
| 152 | \WP_CLI::error( "Dir `{$dir}` not found." ); |
| 153 | } |
| 154 | |
| 155 | $files = glob( $dir . '/*.json' ); |
| 156 | |
| 157 | if ( empty( $files ) ) { |
| 158 | \WP_CLI::error( 'Files not found.' ); |
| 159 | } |
| 160 | |
| 161 | /** @var Source_Local $source */ |
| 162 | $source = Plugin::$instance->templates_manager->get_source( 'local' ); |
| 163 | |
| 164 | $succeed = []; |
| 165 | $errors = []; |
| 166 | |
| 167 | foreach ( $files as $file ) { |
| 168 | $basename = basename( $file ); |
| 169 | |
| 170 | if ( ! file_exists( $file ) ) { |
| 171 | $errors[ $basename ] = $file . ' file not found.'; |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | $imported_items = $source->import_template( $basename, $file ); |
| 176 | |
| 177 | if ( is_wp_error( $imported_items ) ) { |
| 178 | $errors[ $basename ] = $imported_items->get_error_message(); |
| 179 | } else { |
| 180 | $succeed[ $basename ] = true; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | $succeed_message = count( $succeed ) . ' item(s) has been imported.'; |
| 185 | |
| 186 | if ( ! empty( $errors ) ) { |
| 187 | $error_message = var_export( $errors, 1 ); |
| 188 | if ( ! empty( $succeed ) ) { |
| 189 | $error_message = $succeed_message . ' ' . count( $errors ) . ' has errors: ' . $error_message; |
| 190 | } |
| 191 | \WP_CLI::error( $error_message ); |
| 192 | } |
| 193 | |
| 194 | \WP_CLI::success( $succeed_message ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Connect site to Elementor Library. |
| 199 | * (Network is not supported) |
| 200 | * |
| 201 | * --user |
| 202 | * The user to connect <id|login|email> |
| 203 | * |
| 204 | * --token |
| 205 | * A connect token from Elementor Account Dashboard. |
| 206 | * |
| 207 | * ## EXAMPLES |
| 208 | * |
| 209 | * 1. wp elementor library connect --user=admin --token=<connect-cli-token> |
| 210 | * - This will connect the admin to Elementor library. |
| 211 | * |
| 212 | * @param $args |
| 213 | * @param $assoc_args |
| 214 | * |
| 215 | * @since 2.8.0 |
| 216 | * @access public |
| 217 | */ |
| 218 | public function connect( $args, $assoc_args ) { |
| 219 | if ( ! get_current_user_id() ) { |
| 220 | \WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' ); |
| 221 | } |
| 222 | |
| 223 | if ( empty( $assoc_args['token'] ) ) { |
| 224 | \WP_CLI::error( 'Please set connect token.' ); |
| 225 | } |
| 226 | |
| 227 | $_REQUEST['mode'] = 'cli'; |
| 228 | $_REQUEST['token'] = $assoc_args['token']; |
| 229 | |
| 230 | $app = $this->get_library_app(); |
| 231 | |
| 232 | $app->set_auth_mode( 'cli' ); |
| 233 | |
| 234 | $app->action_authorize(); |
| 235 | |
| 236 | $app->action_get_token(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Disconnect site from Elementor Library. |
| 241 | * |
| 242 | * --user |
| 243 | * The user to disconnect <id|login|email> |
| 244 | * |
| 245 | * ## EXAMPLES |
| 246 | * |
| 247 | * 1. wp elementor library disconnect --user=admin |
| 248 | * - This will disconnect the admin from Elementor library. |
| 249 | * |
| 250 | * @param $args |
| 251 | * @param $assoc_args |
| 252 | * |
| 253 | * @since 2.8.0 |
| 254 | * @access public |
| 255 | */ |
| 256 | public function disconnect() { |
| 257 | if ( ! get_current_user_id() ) { |
| 258 | \WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' ); |
| 259 | } |
| 260 | |
| 261 | $_REQUEST['mode'] = 'cli'; |
| 262 | |
| 263 | $this->get_library_app()->action_disconnect(); |
| 264 | } |
| 265 | |
| 266 | private function do_sync() { |
| 267 | $data = Api::get_library_data( true ); |
| 268 | |
| 269 | if ( empty( $data ) ) { |
| 270 | \WP_CLI::error( 'Cannot sync library.' ); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @return \Elementor\Core\Common\Modules\Connect\Apps\Library |
| 276 | */ |
| 277 | private function get_library_app() { |
| 278 | $connect = Plugin::$instance->common->get_component( 'connect' ); |
| 279 | $app = $connect->get_app( 'library' ); |
| 280 | // Before init. |
| 281 | if ( ! $app ) { |
| 282 | $connect->init(); |
| 283 | $app = $connect->get_app( 'library' ); |
| 284 | } |
| 285 | |
| 286 | return $app; |
| 287 | } |
| 288 | } |
| 289 |