Skip to content

Commit 1555b3d

Browse files
kiranpotphodeCopilotswissspidy
authored
Add --skip_authors and --skip_terms as additional flags (#115)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent e8d715d commit 1555b3d

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

features/export.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,34 @@ Feature: Export content.
12531253
Error: Term is missing a parent
12541254
"""
12551255

1256+
Scenario: Export a site and skip the authors
1257+
Given a WP install
1258+
1259+
When I run `wp export --skip_authors`
1260+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
1261+
And the {EXPORT_FILE} file should not contain:
1262+
"""
1263+
<wp:author>
1264+
"""
1265+
1266+
Scenario: Export a site and skip the terms
1267+
Given a WP install
1268+
1269+
When I run `wp export --skip_terms`
1270+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
1271+
And the {EXPORT_FILE} file should not contain:
1272+
"""
1273+
<wp:term>
1274+
"""
1275+
And the {EXPORT_FILE} file should not contain:
1276+
"""
1277+
<wp:category>
1278+
"""
1279+
And the {EXPORT_FILE} file should not contain:
1280+
"""
1281+
<wp:tag>
1282+
"""
1283+
12561284
@require-wp-5.2 @require-mysql
12571285
Scenario: Export posts with future status
12581286
Given a WP install

src/Export_Command.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Export_Command extends WP_CLI_Command {
3636
private $max_file_size;
3737
private $include_once;
3838
private $wxr_path;
39+
private $exclude = [];
3940

4041
/**
4142
* Exports WordPress content to a WXR file.
@@ -56,6 +57,12 @@ class Export_Command extends WP_CLI_Command {
5657
* [--skip_comments]
5758
* : Don't include comments in the WXR export file.
5859
*
60+
* [--skip_authors]
61+
* : Don't include authors in the WXR export file.
62+
*
63+
* [--skip_terms]
64+
* : Don't include terms (categories, tags, custom taxonomy terms and nav menu terms) in the WXR export file.
65+
*
5966
* [--max_file_size=<MB>]
6067
* : A single export file should have this many megabytes. -1 for unlimited.
6168
* ---
@@ -150,6 +157,8 @@ public function __invoke( $_, $assoc_args ) {
150157
'with_attachments' => true, // or FALSE if user requested some post__in
151158
'start_id' => null,
152159
'skip_comments' => null,
160+
'skip_authors' => null,
161+
'skip_terms' => null,
153162
'max_file_size' => 15,
154163
'filename_format' => '{site}.wordpress.{date}.{n}.xml',
155164
'include_once' => null,
@@ -174,6 +183,27 @@ public function __invoke( $_, $assoc_args ) {
174183
$defaults['with_attachments']
175184
);
176185

186+
$this->export_args['skip_authors'] = Utils\get_flag_value(
187+
$assoc_args,
188+
'skip_authors',
189+
$defaults['skip_authors']
190+
);
191+
192+
$this->export_args['skip_terms'] = Utils\get_flag_value(
193+
$assoc_args,
194+
'skip_terms',
195+
$defaults['skip_terms']
196+
);
197+
198+
// Re-calculate exclusions after validation to ensure consistency.
199+
$this->exclude = [];
200+
if ( $this->export_args['skip_authors'] ) {
201+
$this->exclude[] = 'authors';
202+
}
203+
if ( $this->export_args['skip_terms'] ) {
204+
$this->exclude = array_merge( $this->exclude, array( 'categories', 'tags', 'nav_menu_terms', 'custom_taxonomies_terms' ) );
205+
}
206+
177207
if ( ! function_exists( 'wp_export' ) ) {
178208
self::load_export_api();
179209
}
@@ -209,6 +239,7 @@ static function ( $file_path ) {
209239
'destination_directory' => $this->wxr_path,
210240
'filename_template' => self::get_filename_template( $assoc_args['filename_format'] ),
211241
'include_once' => $this->include_once,
242+
'exclude' => $this->exclude,
212243
],
213244
]
214245
);
@@ -534,6 +565,42 @@ private function check_skip_comments( $skip ) {
534565
return true;
535566
}
536567

568+
/**
569+
* @param string|null $skip
570+
*
571+
* @phpstan-ignore method.unused
572+
*/
573+
private function check_skip_authors( $skip ) {
574+
if ( null === $skip ) {
575+
return true;
576+
}
577+
578+
if ( 0 !== (int) $skip && 1 !== (int) $skip ) {
579+
WP_CLI::warning( 'skip_authors needs to be 0 (no) or 1 (yes).' );
580+
return false;
581+
}
582+
$this->export_args['skip_authors'] = $skip;
583+
return true;
584+
}
585+
586+
/**
587+
* @param string|null $skip
588+
*
589+
* @phpstan-ignore method.unused
590+
*/
591+
private function check_skip_terms( $skip ) {
592+
if ( null === $skip ) {
593+
return true;
594+
}
595+
596+
if ( 0 !== (int) $skip && 1 !== (int) $skip ) {
597+
WP_CLI::warning( 'skip_terms needs to be 0 (no) or 1 (yes).' );
598+
return false;
599+
}
600+
$this->export_args['skip_terms'] = $skip;
601+
return true;
602+
}
603+
537604
/**
538605
* @param string $size
539606
*

src/WP_Export_Split_Files_Writer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ public function __construct( $formatter, $writer_args = [] ) {
4545
$this->subsequent_sections = array_diff( $this->available_sections, $writer_args['include_once'] );
4646
}
4747

48+
if ( ! empty( $writer_args['exclude'] ) ) {
49+
if ( ! empty( $writer_args['exclude'] ) ) {
50+
$this->available_sections = array_diff( $this->available_sections, $writer_args['exclude'] );
51+
}
52+
}
53+
4854
$this->destination_directory = $writer_args['destination_directory'];
4955
$this->filename_template = $writer_args['filename_template'];
50-
$this->before_posts_xml = $this->formatter->before_posts();
56+
$this->before_posts_xml = $this->formatter->before_posts( $this->available_sections );
5157
$this->after_posts_xml = $this->formatter->after_posts();
5258
}
5359

0 commit comments

Comments
 (0)