PluginProbe
Elementor Website Builder – more than just a page builder / 3.9.1
Elementor Website Builder – more than just a page builder v3.9.1
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.9.1, at modules/wp-cli/library.php

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