From 69ca1678f873adad3dbdfa128f611f9edee12a4a Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Tue, 18 Jun 2024 19:14:48 +0530 Subject: [PATCH 1/4] feature: add capability to skip authors & terms --- src/Export_Command.php | 47 ++++++++++++++++++++++++++++ src/WP_Export_Split_Files_Writer.php | 9 +++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Export_Command.php b/src/Export_Command.php index e0b063dd..e8d7c213 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -31,6 +31,7 @@ class Export_Command extends WP_CLI_Command { private $max_file_size; private $include_once; private $wxr_path; + private $exclude = []; /** * Exports WordPress content to a WXR file. @@ -51,6 +52,12 @@ class Export_Command extends WP_CLI_Command { * [--skip_comments] * : Don't include comments in the WXR export file. * + * [--skip_authors] + * : Don't include comments in the WXR export file. + * + * [--skip_terms] + * : Don't include comments in the WXR export file. + * * [--max_file_size=] * : A single export file should have this many megabytes. -1 for unlimited. * --- @@ -145,6 +152,8 @@ public function __invoke( $_, $assoc_args ) { 'with_attachments' => true, // or FALSE if user requested some post__in 'start_id' => null, 'skip_comments' => null, + 'skip_authors' => null, + 'skip_terms' => null, 'max_file_size' => 15, 'filename_format' => '{site}.wordpress.{date}.{n}.xml', 'include_once' => null, @@ -169,6 +178,17 @@ public function __invoke( $_, $assoc_args ) { $defaults['with_attachments'] ); + if ( $this->export_args['skip_authors'] ) { + $this->exclude[] = 'authors'; + } + + if ( $this->export_args['skip_terms'] ) { + $this->exclude[] = 'categories'; + $this->exclude[] = 'tags'; + $this->exclude[] = 'custom_taxonomies_terms'; + $this->exclude[] = 'nav_menu_terms'; + } + if ( ! function_exists( 'wp_export' ) ) { self::load_export_api(); } @@ -204,6 +224,7 @@ static function ( $file_path ) { 'destination_directory' => $this->wxr_path, 'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ), 'include_once' => $this->include_once, + 'exclude' => $this->exclude, ], ] ); @@ -468,6 +489,32 @@ private function check_skip_comments( $skip ) { return true; } + private function check_skip_authors( $skip ) { + if ( null === $skip ) { + return true; + } + + if ( 0 !== (int) $skip && 1 !== (int) $skip ) { + WP_CLI::warning( 'skip_authors needs to be 0 (no) or 1 (yes).' ); + return false; + } + $this->export_args['skip_authors'] = $skip; + return true; + } + + private function check_skip_terms( $skip ) { + if ( null === $skip ) { + return true; + } + + if ( 0 !== (int) $skip && 1 !== (int) $skip ) { + WP_CLI::warning( 'skip_terms needs to be 0 (no) or 1 (yes).' ); + return false; + } + $this->export_args['skip_terms'] = $skip; + return true; + } + private function check_max_file_size( $size ) { if ( ! is_numeric( $size ) ) { WP_CLI::warning( 'max_file_size should be numeric.' ); diff --git a/src/WP_Export_Split_Files_Writer.php b/src/WP_Export_Split_Files_Writer.php index 82c3f5e6..addc0500 100644 --- a/src/WP_Export_Split_Files_Writer.php +++ b/src/WP_Export_Split_Files_Writer.php @@ -45,9 +45,16 @@ public function __construct( $formatter, $writer_args = [] ) { $this->subsequent_sections = array_diff( $this->available_sections, $writer_args['include_once'] ); } + foreach ( $writer_args['exclude'] as $exclude ) { + $key = array_search( $exclude, $this->available_sections ); + if ( false !== $key ) { + unset( $this->available_sections[ $key ] ); + } + } + $this->destination_directory = $writer_args['destination_directory']; $this->filename_template = $writer_args['filename_template']; - $this->before_posts_xml = $this->formatter->before_posts(); + $this->before_posts_xml = $this->formatter->before_posts( $this->available_sections ); $this->after_posts_xml = $this->formatter->after_posts(); } From d8b81f61c6605ea6b7f3803d11dedfcbe82298bd Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Thu, 18 Jul 2024 18:23:48 +0530 Subject: [PATCH 2/4] feat: add tests for skip_terms and skip_authors args --- features/export.feature | 30 +++++++++++++++++++++++++++++- src/Export_Command.php | 16 ++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/features/export.feature b/features/export.feature index 87a169b7..53918069 100644 --- a/features/export.feature +++ b/features/export.feature @@ -1235,4 +1235,32 @@ Feature: Export content. Then STDERR should contain: """ Error: Term is missing a parent - """ \ No newline at end of file + """ + + Scenario: Export a site and skip the authors + Given a WP install + + When I run `wp export --skip_authors` + Then save STDOUT 'Writing to file %s' as {EXPORT_FILE} + And the {EXPORT_FILE} file should not contain: + """ + + """ + + Scenario: Export a site and skip the terms + Given a WP install + + When I run `wp export --skip_terms` + Then save STDOUT 'Writing to file %s' as {EXPORT_FILE} + And the {EXPORT_FILE} file should not contain: + """ + + """ + And the {EXPORT_FILE} file should not contain: + """ + + """ + And the {EXPORT_FILE} file should not contain: + """ + + """ diff --git a/src/Export_Command.php b/src/Export_Command.php index e8d7c213..ca725aef 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -53,10 +53,10 @@ class Export_Command extends WP_CLI_Command { * : Don't include comments in the WXR export file. * * [--skip_authors] - * : Don't include comments in the WXR export file. + * : Don't include authors in the WXR export file. * * [--skip_terms] - * : Don't include comments in the WXR export file. + * : Don't include terms (categories, tags, custom taxonomy terms and nav menu terms) in the WXR export file. * * [--max_file_size=] * : A single export file should have this many megabytes. -1 for unlimited. @@ -178,6 +178,18 @@ public function __invoke( $_, $assoc_args ) { $defaults['with_attachments'] ); + $this->export_args['skip_authors'] = Utils\get_flag_value( + $assoc_args, + 'skip_authors', + $defaults['skip_authors'] + ); + + $this->export_args['skip_terms'] = Utils\get_flag_value( + $assoc_args, + 'skip_terms', + $defaults['skip_terms'] + ); + if ( $this->export_args['skip_authors'] ) { $this->exclude[] = 'authors'; } From a08d2e830517406eab4946d6f99c0f540faf9c8a Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Thu, 18 Jul 2024 18:53:15 +0530 Subject: [PATCH 3/4] chore: optimize code --- src/Export_Command.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Export_Command.php b/src/Export_Command.php index ca725aef..749a8fc5 100644 --- a/src/Export_Command.php +++ b/src/Export_Command.php @@ -195,10 +195,7 @@ public function __invoke( $_, $assoc_args ) { } if ( $this->export_args['skip_terms'] ) { - $this->exclude[] = 'categories'; - $this->exclude[] = 'tags'; - $this->exclude[] = 'custom_taxonomies_terms'; - $this->exclude[] = 'nav_menu_terms'; + $this->exclude = array_merge( $this->exclude, array( 'categories', 'tags', 'nav_menu_terms', 'custom_taxonomies_terms' ) ); } if ( ! function_exists( 'wp_export' ) ) { From e0fd98ca31c778de2b0f48cf3efeb455f255c9b9 Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Thu, 18 Jul 2024 19:03:47 +0530 Subject: [PATCH 4/4] chore: add empty array check --- src/WP_Export_Split_Files_Writer.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/WP_Export_Split_Files_Writer.php b/src/WP_Export_Split_Files_Writer.php index addc0500..4bb00563 100644 --- a/src/WP_Export_Split_Files_Writer.php +++ b/src/WP_Export_Split_Files_Writer.php @@ -45,10 +45,12 @@ public function __construct( $formatter, $writer_args = [] ) { $this->subsequent_sections = array_diff( $this->available_sections, $writer_args['include_once'] ); } - foreach ( $writer_args['exclude'] as $exclude ) { - $key = array_search( $exclude, $this->available_sections ); - if ( false !== $key ) { - unset( $this->available_sections[ $key ] ); + if ( ! empty( $writer_args['exclude'] ) ) { + foreach ( $writer_args['exclude'] as $exclude ) { + $key = array_search( $exclude, $this->available_sections, true ); + if ( false !== $key ) { + unset( $this->available_sections[ $key ] ); + } } }