PluginProbe
Elementor Website Builder – more than just a page builder / 3.6.4
Elementor Website Builder – more than just a page builder v3.6.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / wp-cli / library.php

library.php in Elementor Website Builder – more than just a page builder 3.6.4, at modules/wp-cli/library.php

260 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /** @var \WP_Site[] $sites */
45 $sites = get_sites();
46
47 foreach ( $sites as $keys => $blog ) {
48 // Cast $blog as an array instead of object
49 $blog_id = $blog->blog_id;
50
51 switch_to_blog( $blog_id );
52
53 \WP_CLI::line( 'Site #' . $blog_id . ' - ' . get_option( 'blogname' ) );
54
55 $this->do_sync( isset( $assoc_args['force'] ) );
56
57 \WP_CLI::success( 'Done! - ' . get_option( 'home' ) );
58
59 restore_current_blog();
60 }
61 } else {
62 $this->do_sync( isset( $assoc_args['force'] ) );
63 \WP_CLI::success( 'Done!' );
64 }
65 }
66
67 /**
68 * Import template files to the Library.
69 *
70 * ## EXAMPLES
71 *
72 * 1. wp elementor library import <file-path>
73 * - This will import a file or a zip of multiple files to the library.
74 *
75 * @param $args
76 * @param $assoc_args
77 *
78 * @since 2.8.0
79 * @access public
80 */
81 public function import( $args ) {
82 if ( empty( $args[0] ) ) {
83 \WP_CLI::error( 'Please set file path.' );
84 }
85
86 $file = $args[0];
87
88 /** @var Source_Local $source */
89 $source = Plugin::$instance->templates_manager->get_source( 'local' );
90
91 $imported_items = $source->import_template( basename( $file ), $file );
92
93 if ( is_wp_error( $imported_items ) ) {
94 \WP_CLI::error( $imported_items->get_error_message() );
95 }
96
97 \WP_CLI::success( count( $imported_items ) . ' item(s) has been imported.' );
98 }
99
100
101 /**
102 * Import all template files from a directory.
103 *
104 * ## EXAMPLES
105 *
106 * 1. wp elementor library import-dir <file-path>
107 * - This will import all JSON files from <file-path>
108 *
109 * @param $args
110 *
111 * @since 3.4.7
112 * @access public
113 * @alias import-dir
114 */
115 public function import_dir( $args ) {
116 if ( empty( $args[0] ) ) {
117 \WP_CLI::error( 'Please set dir path.' );
118 }
119
120 $dir = $args[0];
121
122 if ( ! file_exists( $dir ) ) {
123 \WP_CLI::error( "Dir `{$dir}` not found." );
124 }
125
126 $files = glob( $dir . '/*.json' );
127
128 if ( empty( $files ) ) {
129 \WP_CLI::error( 'Files not found.' );
130 }
131
132 /** @var Source_Local $source */
133 $source = Plugin::$instance->templates_manager->get_source( 'local' );
134
135 $succeed = [];
136 $errors = [];
137
138 foreach ( $files as $file ) {
139 $basename = basename( $file );
140
141 if ( ! file_exists( $file ) ) {
142 $errors[ $basename ] = $file . ' file not found.';
143 continue;
144 }
145
146 $imported_items = $source->import_template( $basename, $file );
147
148 if ( is_wp_error( $imported_items ) ) {
149 $errors[ $basename ] = $imported_items->get_error_message();
150 } else {
151 $succeed[ $basename ] = true;
152 }
153 }
154
155 $succeed_message = count( $succeed ) . ' item(s) has been imported.';
156
157 if ( ! empty( $errors ) ) {
158 $error_message = var_export( $errors, 1 );
159 if ( ! empty( $succeed ) ) {
160 $error_message = $succeed_message . ' ' . count( $errors ) . ' has errors: ' . $error_message;
161 }
162 \WP_CLI::error( $error_message );
163 }
164
165 \WP_CLI::success( $succeed_message );
166 }
167
168 /**
169 * Connect site to Elementor Library.
170 * (Network is not supported)
171 *
172 * --user
173 * The user to connect <id|login|email>
174 *
175 * --token
176 * A connect token from Elementor Account Dashboard.
177 *
178 * ## EXAMPLES
179 *
180 * 1. wp elementor library connect --user=admin --token=<connect-cli-token>
181 * - This will connect the admin to Elementor library.
182 *
183 * @param $args
184 * @param $assoc_args
185 *
186 * @since 2.8.0
187 * @access public
188 */
189 public function connect( $args, $assoc_args ) {
190 if ( ! get_current_user_id() ) {
191 \WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' );
192 }
193
194 if ( empty( $assoc_args['token'] ) ) {
195 \WP_CLI::error( 'Please set connect token.' );
196 }
197
198 $_REQUEST['mode'] = 'cli';
199 $_REQUEST['token'] = $assoc_args['token'];
200
201 $app = $this->get_library_app();
202
203 $app->set_auth_mode( 'cli' );
204
205 $app->action_authorize();
206
207 $app->action_get_token();
208 }
209
210 /**
211 * Disconnect site from Elementor Library.
212 *
213 * --user
214 * The user to disconnect <id|login|email>
215 *
216 * ## EXAMPLES
217 *
218 * 1. wp elementor library disconnect --user=admin
219 * - This will disconnect the admin from Elementor library.
220 *
221 * @param $args
222 * @param $assoc_args
223 *
224 * @since 2.8.0
225 * @access public
226 */
227 public function disconnect() {
228 if ( ! get_current_user_id() ) {
229 \WP_CLI::error( 'Please set user to connect (--user=<id|login|email>).' );
230 }
231
232 $_REQUEST['mode'] = 'cli';
233
234 $this->get_library_app()->action_disconnect();
235 }
236
237 private function do_sync() {
238 $data = Api::get_library_data( true );
239
240 if ( empty( $data ) ) {
241 \WP_CLI::error( 'Cannot sync library.' );
242 }
243 }
244
245 /**
246 * @return \Elementor\Core\Common\Modules\Connect\Apps\Library
247 */
248 private function get_library_app() {
249 $connect = Plugin::$instance->common->get_component( 'connect' );
250 $app = $connect->get_app( 'library' );
251 // Before init.
252 if ( ! $app ) {
253 $connect->init();
254 $app = $connect->get_app( 'library' );
255 }
256
257 return $app;
258 }
259 }
260