ExportDemoSiteCommand.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\CLI; |
| 4 | |
| 5 | use Kubio\DemoSites\DemoSites; |
| 6 | use WP_CLI; |
| 7 | |
| 8 | class ExportDemoSiteCommand { |
| 9 | /** |
| 10 | * Export website as a Kubio design. |
| 11 | * |
| 12 | * ## OPTIONS |
| 13 | * |
| 14 | * <file> |
| 15 | * : The design output file. It should have the .kds extension. |
| 16 | * |
| 17 | * |
| 18 | * ## EXAMPLES |
| 19 | * |
| 20 | * wp kubio:export-design /home/user/my-site.kds |
| 21 | * |
| 22 | * @when after_wp_load |
| 23 | * @param $args |
| 24 | * @param $assoc_args |
| 25 | * @throws \WP_CLI\ExitException |
| 26 | */ |
| 27 | public function __invoke( $args, $assoc_args ) { |
| 28 | |
| 29 | $file = empty( $args[0] ) ? null : $args[0]; |
| 30 | |
| 31 | if ( is_null( $file ) ) { |
| 32 | WP_CLI::error( 'Output file name cannot be empty' ); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | if ( substr( $file, -4 ) !== '.kds' ) { |
| 37 | WP_CLI::line( "File: '{$file}' does not have the kds extension! It will be automatically added" ); |
| 38 | $file = "{$file}.kds"; |
| 39 | } |
| 40 | |
| 41 | if ( file_exists( $file ) ) { |
| 42 | WP_CLI::error( "File: '{$file}' already exists. Please use another name!" ); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
| 47 | if ( ! is_writable( dirname( $file ) ) ) { |
| 48 | WP_CLI::error( "File: '{$file}' is not writable. Please use another location!" ); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $content = serialize( DemoSites::exportDemoSiteContent() ); |
| 53 | |
| 54 | file_put_contents( $file, $content ); |
| 55 | |
| 56 | WP_CLI::success( "Demo site successfully exported to '{$file}'." ); |
| 57 | } |
| 58 | } |
| 59 |