] * : Comma-separated paths relative to wp-content. * * ## EXAMPLES * * wp marchnote export --exclude-spam --exclude-revisions */ public function export( $args, $assoc ) { $input = array( 'exclude_paths' => str_replace( ',', "\n", (string) ( $assoc['exclude'] ?? '' ) ) ); foreach ( Marchnote_Export::OPTIONS as $k ) { $input[ $k ] = ! empty( $assoc[ 'exclude-' . str_replace( '_', '-', substr( $k, 3 ) ) ] ); } $res = $this->run( Marchnote_Export::start( $input ) ); WP_CLI::success( $res['message'] . ' ' . Marchnote_Storage::backups_dir() . '/' . $res['data']['file'] ); } /** * Import an archive, replacing this site's database and files. * * ## OPTIONS * * * : Archive path, or the name of a file in the backups folder. * * [--skip-media] * : Do not restore media. * * [--skip-plugins] * : Do not restore plugins. * * [--skip-themes] * : Do not restore themes. * * [--skip-database] * : Do not restore database. * * [--replace=] * : Extra replacements as "from|to", separated by commas. * * [--yes] * : Do not ask for confirmation. * * ## EXAMPLES * * wp marchnote import example-com-20261013.mwp --yes */ public function import( $args, $assoc ) { $path = is_file( $args[0] ) ? realpath( $args[0] ) : Marchnote_Storage::backup_path( $args[0] ); if ( ! $path ) { WP_CLI::error( 'File not found: ' . $args[0] ); } $input = array( 'replace' => array() ); foreach ( array( 'skip_media', 'skip_plugins', 'skip_themes', 'skip_database' ) as $k ) { $input[ $k ] = ! empty( $assoc[ str_replace( '_', '-', $k ) ] ); } foreach ( array_filter( explode( ',', (string) ( $assoc['replace'] ?? '' ) ) ) as $pair ) { $parts = explode( '|', $pair, 2 ); if ( 2 === count( $parts ) ) { $input['replace'][] = array( 'from' => $parts[0], 'to' => $parts[1], ); } } $yes = ! empty( $assoc['yes'] ); $res = $this->run( Marchnote_Import::start( $path, $input ), function ( $info ) use ( $yes ) { WP_CLI::log( sprintf( 'Archive from %s (%s), %d files, %s.', $info['source'], $info['created'], $info['files'], $info['bytes'] ) ); if ( ! $yes ) { WP_CLI::confirm( 'This overwrites the database and files of this site. Continue?' ); } } ); WP_CLI::success( $res['message'] ); } /** * List archives in the backups folder. * * @subcommand list */ public function list_( $args, $assoc ) { $rows = array_map( function ( $b ) { return array( 'name' => $b['name'], 'size' => size_format( $b['size'], 1 ), 'created' => gmdate( 'Y-m-d H:i', $b['mtime'] ), ); }, Marchnote_Storage::list_backups() ); WP_CLI\Utils\format_items( 'table', $rows, array( 'name', 'size', 'created' ) ); } private function run( Marchnote_Job $job, $on_confirm = null ) { $confirm = false; $last = ''; while ( true ) { $r = Marchnote_Runner::step( $job, array( 'confirm' => $confirm ) ); if ( $r['message'] !== $last ) { WP_CLI::log( sprintf( '[%3d%%] %s', $r['progress'], $r['message'] ) ); $last = $r['message']; } if ( 'error' === $r['status'] ) { WP_CLI::error( $r['message'] ); } if ( 'done' === $r['status'] ) { return $r; } if ( 'confirm' === $r['status'] ) { $on_confirm && $on_confirm( $r['data'] ); $confirm = true; } $job = Marchnote_Job::load( $job->id ); } } }