DemoSites.php
4 years ago
DemoSitesHelpers.php
4 years ago
DemoSitesImportBlockMap.php
4 years ago
DemoSitesImporter.php
4 years ago
DemoSitesLogger.php
4 years ago
DemoSitesRepository.php
4 years ago
WXRExporter.php
4 years ago
WXRImporter.php
4 years ago
WpCliCommand.php
4 years ago
WpCliCommand.php
109 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-name> |
| 18 | * : The slug 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-demo-site |
| 31 | * |
| 32 | * @when after_wp_load |
| 33 | * @param $args |
| 34 | * @param $assoc_args |
| 35 | * @throws \WP_CLI\ExitException |
| 36 | */ |
| 37 | public function __invoke( $args, $assoc_args ) { |
| 38 | |
| 39 | ini_set( 'max_execution_time', 0 ); |
| 40 | set_time_limit( 0 ); |
| 41 | |
| 42 | if ( empty( $args[0] ) || ! is_string( $args[0] ) ) { |
| 43 | \WP_CLI::error( 'This command expects the first argument to be an importable design slug.' ); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // Interpret some associative arguments as booleans. |
| 48 | foreach ( $assoc_args as $assoc_arg_key => $assoc_arg_value ) { |
| 49 | |
| 50 | switch ( $assoc_arg_key ) { |
| 51 | case 'verify-ssl': |
| 52 | $assoc_args[ $assoc_arg_key ] = |
| 53 | boolval( json_decode( $assoc_arg_value ) ); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | $defaults = array( |
| 59 | 'verify-ssl' => true, |
| 60 | ); |
| 61 | |
| 62 | $assoc_args = wp_parse_args( $assoc_args, $defaults ); |
| 63 | $designName = $args[0]; |
| 64 | $repo = new DemoSitesRepository(); |
| 65 | |
| 66 | $repo->retrieveDemoSites( false, $assoc_args['verify-ssl'], Arr::get( $assoc_args, 'scope', null ) ); |
| 67 | $data = get_transient( 'kubio-demo-sites-repository' ); |
| 68 | |
| 69 | if ( ! Arr::get( (array) $data, "demos.{$designName}" ) ) { |
| 70 | \WP_CLI::error( '"' . $designName . '" is not a valid design slug.' ); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | WP_CLI::line( 'Let\'s import "' . $designName . '" ...' ); |
| 75 | |
| 76 | $importer_args = array( |
| 77 | 'slug' => $designName, |
| 78 | 'before_import_action' => 'init', |
| 79 | ); |
| 80 | |
| 81 | if ( ! empty( $assoc_args['non-ssl'] ) ) { |
| 82 | $importer_args['allow--non-ssl'] = $assoc_args['non-ssl']; |
| 83 | } |
| 84 | |
| 85 | $importer = new DemoSitesImporter( 'cli', $importer_args ); |
| 86 | |
| 87 | foreach ( $importer->getBeforeImportActions() as $key => $step ) { |
| 88 | $importer_args['before_import_action'] = $step; |
| 89 | $importer_args['first_call'] = $key === 0; |
| 90 | $importer->executeBeforeImportAction( $step, false ); |
| 91 | } |
| 92 | |
| 93 | \WP_CLI::line( 'Start importing content' ); |
| 94 | |
| 95 | $importer->cliImportDemoData(); |
| 96 | |
| 97 | \WP_CLI::line( 'Finishing import' ); |
| 98 | |
| 99 | WP_CLI::success( '"' . $designName . '" was successfully imported!' ); |
| 100 | |
| 101 | // remove scoped transient |
| 102 | delete_transient( 'kubio-demo-sites-repository' ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 107 | WP_CLI::add_command( 'kubio:import-design', 'DemoSites_Import_Command' ); |
| 108 | } |
| 109 |