DemoPartsRepository.php
3 years ago
DemoSites.php
3 years ago
DemoSitesHelpers.php
4 years ago
DemoSitesImportBlockMap.php
4 years ago
DemoSitesImporter.php
3 years ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
3 years ago
WXRExporter.php
4 years ago
WXRImporter.php
3 years ago
WpCliCommand.php
3 years ago
WpCliCommand.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Str\Support\Arr; |
| 4 | use Kubio\DemoSites\DemoSitesImporter; |
| 5 | use Kubio\DemoSites\DemoSitesRepository; |
| 6 | |
| 7 | /** |
| 8 | * Implements example command. |
| 9 | */ |
| 10 | class DemoSites_Import_Command { |
| 11 | |
| 12 | /** |
| 13 | * Import a Kubio design. |
| 14 | * |
| 15 | * ## OPTIONS |
| 16 | * |
| 17 | * <design> |
| 18 | * : The slug or the url of the design to import. |
| 19 | * |
| 20 | * [--verify-ssl=<bool>] |
| 21 | * : Whatever or not to enforce SSL on requests. |
| 22 | * Default: true |
| 23 | * |
| 24 | * [--scope=<string>] |
| 25 | * : Whatever or not to enforce SSL on requests. |
| 26 | * Default: null |
| 27 | * |
| 28 | * ## EXAMPLES |
| 29 | * |
| 30 | * wp kubio:import-design accounting-free |
| 31 | * wp kubio:import-design https://static-assets.kubiobuilder.com/demo-sites/production/accounting-free/content.kds |
| 32 | * |
| 33 | * @when after_wp_load |
| 34 | * @param $args |
| 35 | * @param $assoc_args |
| 36 | * @throws \WP_CLI\ExitException |
| 37 | */ |
| 38 | public function __invoke( $args, $assoc_args ) { |
| 39 | |
| 40 | if ( ! defined( 'KUBIO_IS_STARTER_SITES_IMPORT' ) ) { |
| 41 | define( 'KUBIO_IS_STARTER_SITES_IMPORT', true ); |
| 42 | } |
| 43 | |
| 44 | ini_set( 'max_execution_time', 0 ); |
| 45 | set_time_limit( 0 ); |
| 46 | |
| 47 | if ( empty( $args[0] ) || ! is_string( $args[0] ) ) { |
| 48 | \WP_CLI::error( 'This command expects the first argument to be an importable design slug.' ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // Interpret some associative arguments as booleans. |
| 53 | foreach ( $assoc_args as $assoc_arg_key => $assoc_arg_value ) { |
| 54 | |
| 55 | switch ( $assoc_arg_key ) { |
| 56 | case 'verify-ssl': |
| 57 | $assoc_args[ $assoc_arg_key ] = |
| 58 | boolval( json_decode( $assoc_arg_value ) ); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $defaults = array( |
| 64 | 'verify-ssl' => true, |
| 65 | ); |
| 66 | |
| 67 | $assoc_args = wp_parse_args( $assoc_args, $defaults ); |
| 68 | $design = $args[0]; |
| 69 | |
| 70 | $importer_args = array(); |
| 71 | |
| 72 | $is_kds_url = filter_var( $design, FILTER_VALIDATE_URL ) |
| 73 | && ( substr_compare( $design, '.kds', -4 ) === 0 || substr_compare( $design, '.kds.wxr', -8 ) === 0 ); |
| 74 | |
| 75 | if ( $is_kds_url ) { |
| 76 | $importer_args = array( |
| 77 | 'is_custom' => true, |
| 78 | 'kds_url' => $design, |
| 79 | ); |
| 80 | |
| 81 | WP_CLI::line( 'Let\'s import "' . $design . '"' ); |
| 82 | } else { |
| 83 | $repo = new DemoSitesRepository(); |
| 84 | |
| 85 | $repo->retrieveDemoSites( false, $assoc_args['verify-ssl'], Arr::get( $assoc_args, 'scope', null ) ); |
| 86 | $data = get_transient( 'kubio-demo-sites-repository' ); |
| 87 | |
| 88 | if ( ! Arr::get( (array) $data, "demos.{$design}" ) ) { |
| 89 | \WP_CLI::error( '"' . $design . '" is not a valid design slug.' ); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | WP_CLI::line( 'Let\'s import "' . $design . '" ...' ); |
| 94 | |
| 95 | $importer_args = array( |
| 96 | 'is_custom' => false, |
| 97 | 'slug' => $design, |
| 98 | 'before_import_action' => 'init', |
| 99 | ); |
| 100 | |
| 101 | if ( ! empty( $assoc_args['non-ssl'] ) ) { |
| 102 | $importer_args['allow--non-ssl'] = $assoc_args['non-ssl']; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | $importer = new DemoSitesImporter( 'cli', $importer_args ); |
| 107 | |
| 108 | foreach ( $importer->getBeforeImportActions() as $key => $step ) { |
| 109 | $importer_args['before_import_action'] = $step; |
| 110 | $importer_args['first_call'] = $key === 0; |
| 111 | $importer->executeBeforeImportAction( $step, false ); |
| 112 | } |
| 113 | |
| 114 | \WP_CLI::line( 'Start importing content' ); |
| 115 | |
| 116 | $importer->cliImportDemoData( $importer_args ); |
| 117 | |
| 118 | \WP_CLI::line( 'Finishing import' ); |
| 119 | |
| 120 | WP_CLI::success( '"' . $design . '" was successfully imported!' ); |
| 121 | |
| 122 | // remove scoped transient |
| 123 | delete_transient( 'kubio-demo-sites-repository' ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 128 | WP_CLI::add_command( 'kubio:import-design', 'DemoSites_Import_Command' ); |
| 129 | } |
| 130 |