ImportDemoSiteCommand.php
194 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 | ini_set( 'max_execution_time', 0 ); |
| 45 | set_time_limit( 0 ); |
| 46 | |
| 47 | if ( ! defined( 'KUBIO_IS_STARTER_SITES_IMPORT' ) ) { |
| 48 | define( 'KUBIO_IS_STARTER_SITES_IMPORT', true ); |
| 49 | } |
| 50 | |
| 51 | $importer_args = $this->build_importer_args( $args, $assoc_args ); |
| 52 | $source = $importer_args['__source']; |
| 53 | |
| 54 | if ( is_null( $importer_args ) || empty( $importer_args ) ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $importer = new DemoSitesImporter( 'cli', $importer_args ); |
| 59 | |
| 60 | // run importer initial cleanups |
| 61 | foreach ( $importer->getBeforeImportActions() as $key => $step ) { |
| 62 | $importer_args['before_import_action'] = $step; |
| 63 | $importer_args['first_call'] = $key === 0; |
| 64 | $importer->executeBeforeImportAction( $step, false ); |
| 65 | } |
| 66 | |
| 67 | \WP_CLI::line( "Content to be imported: {$source}" ); |
| 68 | \WP_CLI::line( 'Start importing content' ); |
| 69 | $importer->cliImportDemoData( $importer_args ); |
| 70 | |
| 71 | \WP_CLI::line( 'Finishing import' ); |
| 72 | |
| 73 | WP_CLI::success( "Design {$source} was successfully imported!" ); |
| 74 | |
| 75 | // remove scoped transient |
| 76 | delete_transient( 'kubio-demo-sites-repository' ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | private function normalize_source( $args ) { |
| 81 | if ( empty( $args[0] ) || ! is_string( $args[0] ) ) { |
| 82 | \WP_CLI::error( 'This command expects the first argument to be an importable design slug, file path or url.' ); |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | $design = $args[0]; |
| 87 | $type = 'slug'; |
| 88 | |
| 89 | if ( filter_var( $design, FILTER_VALIDATE_URL ) ) { |
| 90 | $type = 'url'; |
| 91 | } else { |
| 92 | $has_extension = substr( $design, -4 ) === '.kds' || substr( $design, -8 ) === '.kds.wxr'; |
| 93 | |
| 94 | if ( $has_extension ) { |
| 95 | if ( ! file_exists( $design ) ) { |
| 96 | \WP_CLI::error( "The design path '{$design}' does not exists" ); |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | $type = 'file'; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return array( |
| 105 | 'source' => $design, |
| 106 | 'type' => $type, |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | private function normalize_assoc_args( $assoc_args ) { |
| 111 | // Normalize associative arguments. |
| 112 | foreach ( $assoc_args as $assoc_arg_key => $assoc_arg_value ) { |
| 113 | switch ( $assoc_arg_key ) { |
| 114 | case 'verify-ssl': |
| 115 | $assoc_args[ $assoc_arg_key ] = |
| 116 | boolval( json_decode( $assoc_arg_value ) ); |
| 117 | break; |
| 118 | case 'fetch-attachments': |
| 119 | $assoc_args[ $assoc_arg_key ] = |
| 120 | boolval( json_decode( $assoc_arg_value ) ); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $defaults = array( |
| 126 | 'verify-ssl' => true, |
| 127 | 'fetch-attachments' => true, |
| 128 | ); |
| 129 | |
| 130 | $assoc_args = wp_parse_args( $assoc_args, $defaults ); |
| 131 | |
| 132 | return $assoc_args; |
| 133 | } |
| 134 | |
| 135 | private function build_importer_args( $args, $assoc_args ) { |
| 136 | $assoc_args = $this->normalize_assoc_args( $assoc_args ); |
| 137 | $design_info = $this->normalize_source( $args ); |
| 138 | |
| 139 | if ( is_null( $design_info ) ) { |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | $importer_args = array( |
| 144 | '__source' => $design_info['source'], |
| 145 | ); |
| 146 | |
| 147 | switch ( $design_info['type'] ) { |
| 148 | case 'file': |
| 149 | case 'url': |
| 150 | $importer_args = array_merge( |
| 151 | $importer_args, |
| 152 | array( |
| 153 | 'is_custom' => true, |
| 154 | 'kds_url' => $design_info['source'], |
| 155 | ) |
| 156 | ); |
| 157 | break; |
| 158 | case 'slug': |
| 159 | $repo = new DemoSitesRepository(); |
| 160 | $design = $design_info['source']; |
| 161 | |
| 162 | $repo->retrieveDemoSites( false, $assoc_args['verify-ssl'], Arr::get( $assoc_args, 'scope', null ) ); |
| 163 | |
| 164 | $data = get_transient( 'kubio-demo-sites-repository' ); |
| 165 | |
| 166 | if ( ! Arr::get( (array) $data, "demos.{$design}" ) ) { |
| 167 | \WP_CLI::error( '"' . $design . '" is not a valid design slug.' ); |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | $importer_args = array_merge( |
| 172 | $importer_args, |
| 173 | array( |
| 174 | 'is_custom' => false, |
| 175 | 'slug' => $design, |
| 176 | ) |
| 177 | ); |
| 178 | |
| 179 | if ( ! empty( $assoc_args['non-ssl'] ) ) { |
| 180 | $importer_args['allow--non-ssl'] = $assoc_args['non-ssl']; |
| 181 | } |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | $importer_args['fetch_attachments'] = $assoc_args['fetch-attachments']; |
| 186 | |
| 187 | if ( ! $importer_args['fetch_attachments'] ) { |
| 188 | add_filter( 'kubio/importer/disabled-import-remote-file', '__return_true' ); |
| 189 | } |
| 190 | |
| 191 | return $importer_args; |
| 192 | } |
| 193 | } |
| 194 |