| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | use Elementor\Plugin; |
| 6 | 6 | use Elementor\TemplateLibrary\Source_Local; |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | - exit; // Exit if accessed directly. | |
| 9 | + exit; // Exit if accessed directly | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Elementor Page Builder cli tools. |
| @@ -40,14 +40,15 @@ | ||
| 40 | 40 | public function sync( $args, $assoc_args ) { |
| 41 | 41 | $network = isset( $assoc_args['network'] ) && is_multisite(); |
| 42 | 42 | |
| 43 | 43 | if ( $network ) { |
| 44 | - $blog_ids = get_sites( [ | |
| 45 | - 'fields' => 'ids', | |
| 46 | - 'number' => 0, | |
| 47 | - ] ); | |
| 44 | + /** @var \WP_Site[] $sites */ | |
| 45 | + $sites = get_sites(); | |
| 48 | 46 | |
| 49 | - foreach ( $blog_ids as $blog_id ) { | |
| 47 | + foreach ( $sites as $keys => $blog ) { | |
| 48 | + // Cast $blog as an array instead of object | |
| 49 | + $blog_id = $blog->blog_id; | |
| 50 | + | |
| 50 | 51 | switch_to_blog( $blog_id ); |
| 51 | 52 | |
| 52 | 53 | \WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) ); |
| 53 | 54 | |
| @@ -65,20 +66,13 @@ | ||
| 65 | 66 | |
| 66 | 67 | /** |
| 67 | 68 | * Import template files to the Library. |
| 68 | 69 | * |
| 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 | 70 | * ## EXAMPLES |
| 74 | 71 | * |
| 75 | 72 | * 1. wp elementor library import <file-path> |
| 76 | 73 | * - This will import a file or a zip of multiple files to the library. |
| 77 | - * - file-path can be a path or url. | |
| 78 | 74 | * |
| 79 | - * 2. wp elementor library import <file-path> --returnType=info,ids | |
| 80 | - * | |
| 81 | 75 | * @param $args |
| 82 | 76 | * @param $assoc_args |
| 83 | 77 | * |
| 84 | 78 | * @since 2.8.0 |
| @@ -83,28 +77,18 @@ | ||
| 83 | 77 | * |
| 84 | 78 | * @since 2.8.0 |
| 85 | 79 | * @access public |
| 86 | 80 | */ |
| 87 | - public function import( $args, $assoc_args ) { | |
| 81 | + public function import( $args ) { | |
| 88 | 82 | if ( empty( $args[0] ) ) { |
| 89 | 83 | \WP_CLI::error( 'Please set file path.' ); |
| 90 | 84 | } |
| 91 | 85 | |
| 92 | 86 | $file = $args[0]; |
| 93 | - $imported_items_ids = []; | |
| 94 | - $return_type = \WP_CLI\Utils\get_flag_value( $assoc_args, 'returnType', 'info' ); | |
| 95 | 87 | |
| 96 | 88 | /** @var Source_Local $source */ |
| 97 | 89 | $source = Plugin::$instance->templates_manager->get_source( 'local' ); |
| 98 | 90 | |
| 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 | 91 | $imported_items = $source->import_template( basename( $file ), $file ); |
| 108 | 92 | |
| 109 | 93 | if ( is_wp_error( $imported_items ) ) { |
| 110 | 94 | \WP_CLI::error( $imported_items->get_error_message() ); |
| @@ -109,93 +93,12 @@ | ||
| 109 | 93 | if ( is_wp_error( $imported_items ) ) { |
| 110 | 94 | \WP_CLI::error( $imported_items->get_error_message() ); |
| 111 | 95 | } |
| 112 | 96 | |
| 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 | - } | |
| 97 | + \WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' ); | |
| 128 | 98 | } |
| 129 | 99 | |
| 130 | 100 | /** |
| 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 | 101 | * Connect site to Elementor Library. |
| 199 | 102 | * (Network is not supported) |
| 200 | 103 | * |
| 201 | 104 | * --user |
| @@ -227,10 +130,8 @@ | ||
| 227 | 130 | $_REQUEST['mode'] = 'cli'; |
| 228 | 131 | $_REQUEST['token'] = $assoc_args['token']; |
| 229 | 132 | |
| 230 | 133 | $app = $this->get_library_app(); |
| 231 | - | |
| 232 | - $app->set_auth_mode( 'cli' ); | |
| 233 | 134 | |
| 234 | 135 | $app->action_authorize(); |
| 235 | 136 | |
| 236 | 137 | $app->action_get_token(); |