PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.6.3
Kubio AI Page Builder v2.6.3
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / CLI / ImportDemoSiteCommand.php
kubio / lib / src / CLI Last commit date
CLI.php 3 years ago ExportDemoSiteCommand.php 1 year ago ImportDemoSiteCommand.php 1 year ago
ImportDemoSiteCommand.php
196 lines
1 <?php
2
3 namespace Kubio\CLI;
4
5 use IlluminateAgnostic\Arr\Support\Arr;
6 use Kubio\DemoSites\DemoSitesImporter;
7 use Kubio\DemoSites\DemoSitesRepository;
8 use WP_CLI;
9
10 class ImportDemoSiteCommand {
11 /**
12 * Import a Kubio design.
13 *
14 * ## OPTIONS
15 *
16 * <design>
17 * : The slug, file path, or the url of the design to import.
18 *
19 * [--verify-ssl=<bool>]
20 * : Whatever or not to enforce SSL on requests.
21 * Default: true
22 *
23 * [--scope=<string>]
24 * : The scope the starter site falls in.
25 * Default: null
26 *
27 * [--fetch-attachments=<bool>]
28 * : The scope the starter site falls in.
29 * Default: true
30 *
31 * ## EXAMPLES
32 *
33 * wp kubio:import-design accounting-free
34 * wp kubio:import-design https://static-assets.kubiobuilder.com/demo-sites/production/accounting-free/content.kds
35 * wp kubio:import-design /home/user/my-site.kds
36 *
37 * @when after_wp_load
38 * @param $args
39 * @param $assoc_args
40 * @throws \WP_CLI\ExitException
41 */
42 public function __invoke( $args, $assoc_args ) {
43
44 // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
45 @ini_set( 'max_execution_time', 0 );
46 // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged
47 set_time_limit( 0 );
48
49 if ( ! defined( 'KUBIO_IS_STARTER_SITES_IMPORT' ) ) {
50 define( 'KUBIO_IS_STARTER_SITES_IMPORT', true );
51 }
52
53 $importer_args = $this->build_importer_args( $args, $assoc_args );
54 $source = $importer_args['__source'];
55
56 if ( is_null( $importer_args ) || empty( $importer_args ) ) {
57 return;
58 }
59
60 $importer = new DemoSitesImporter( 'cli', $importer_args );
61
62 // run importer initial cleanups
63 foreach ( $importer->getBeforeImportActions() as $key => $step ) {
64 $importer_args['before_import_action'] = $step;
65 $importer_args['first_call'] = $key === 0;
66 $importer->executeBeforeImportAction( $step, false );
67 }
68
69 \WP_CLI::line( "Content to be imported: {$source}" );
70 \WP_CLI::line( 'Start importing content' );
71 $importer->cliImportDemoData( $importer_args );
72
73 \WP_CLI::line( 'Finishing import' );
74
75 WP_CLI::success( "Design {$source} was successfully imported!" );
76
77 // remove scoped transient
78 delete_transient( 'kubio-demo-sites-repository' );
79 }
80
81
82 private function normalize_source( $args ) {
83 if ( empty( $args[0] ) || ! is_string( $args[0] ) ) {
84 \WP_CLI::error( 'This command expects the first argument to be an importable design slug, file path or url.' );
85 return null;
86 }
87
88 $design = $args[0];
89 $type = 'slug';
90
91 if ( filter_var( $design, FILTER_VALIDATE_URL ) ) {
92 $type = 'url';
93 } else {
94 $has_extension = substr( $design, -4 ) === '.kds' || substr( $design, -8 ) === '.kds.wxr';
95
96 if ( $has_extension ) {
97 if ( ! file_exists( $design ) ) {
98 \WP_CLI::error( "The design path '{$design}' does not exists" );
99 return null;
100 }
101
102 $type = 'file';
103 }
104 }
105
106 return array(
107 'source' => $design,
108 'type' => $type,
109 );
110 }
111
112 private function normalize_assoc_args( $assoc_args ) {
113 // Normalize associative arguments.
114 foreach ( $assoc_args as $assoc_arg_key => $assoc_arg_value ) {
115 switch ( $assoc_arg_key ) {
116 case 'verify-ssl':
117 $assoc_args[ $assoc_arg_key ] =
118 boolval( json_decode( $assoc_arg_value ) );
119 break;
120 case 'fetch-attachments':
121 $assoc_args[ $assoc_arg_key ] =
122 boolval( json_decode( $assoc_arg_value ) );
123 break;
124 }
125 }
126
127 $defaults = array(
128 'verify-ssl' => true,
129 'fetch-attachments' => true,
130 );
131
132 $assoc_args = wp_parse_args( $assoc_args, $defaults );
133
134 return $assoc_args;
135 }
136
137 private function build_importer_args( $args, $assoc_args ) {
138 $assoc_args = $this->normalize_assoc_args( $assoc_args );
139 $design_info = $this->normalize_source( $args );
140
141 if ( is_null( $design_info ) ) {
142 return null;
143 }
144
145 $importer_args = array(
146 '__source' => $design_info['source'],
147 );
148
149 switch ( $design_info['type'] ) {
150 case 'file':
151 case 'url':
152 $importer_args = array_merge(
153 $importer_args,
154 array(
155 'is_custom' => true,
156 'kds_url' => $design_info['source'],
157 )
158 );
159 break;
160 case 'slug':
161 $repo = new DemoSitesRepository();
162 $design = $design_info['source'];
163
164 $repo->retrieveDemoSites( false, $assoc_args['verify-ssl'], Arr::get( $assoc_args, 'scope', null ) );
165
166 $data = get_transient( 'kubio-demo-sites-repository' );
167
168 if ( ! Arr::get( (array) $data, "demos.{$design}" ) ) {
169 \WP_CLI::error( '"' . $design . '" is not a valid design slug.' );
170 return null;
171 }
172
173 $importer_args = array_merge(
174 $importer_args,
175 array(
176 'is_custom' => false,
177 'slug' => $design,
178 )
179 );
180
181 if ( ! empty( $assoc_args['non-ssl'] ) ) {
182 $importer_args['allow--non-ssl'] = $assoc_args['non-ssl'];
183 }
184 break;
185 }
186
187 $importer_args['fetch_attachments'] = $assoc_args['fetch-attachments'];
188
189 if ( ! $importer_args['fetch_attachments'] ) {
190 add_filter( 'kubio/importer/disabled-import-remote-file', '__return_true' );
191 }
192
193 return $importer_args;
194 }
195 }
196