Add --skip_authors and --skip_terms as additional flags#115
Add --skip_authors and --skip_terms as additional flags#115kiranpotphode wants to merge 10 commits intowp-cli:mainfrom
Conversation
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
There was a problem hiding this comment.
Pull Request Overview
This PR adds functionality to skip authors and terms (categories, tags, custom taxonomy terms, and nav menu terms) from WordPress export files. The implementation follows the existing pattern used by the --skip_comments flag.
Key Changes:
- Added
--skip_authorsand--skip_termscommand-line flags - Implemented exclusion mechanism in
WP_Export_Split_Files_Writerto filter out specified sections - Modified
before_posts()method signature to accept available sections parameter
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Export_Command.php | Added CLI parameters and validation methods for --skip_authors and --skip_terms flags, populating the $exclude array based on user input |
| src/WP_Export_Split_Files_Writer.php | Implemented section exclusion logic and updated before_posts() call to pass filtered sections |
| features/export.feature | Added test scenarios for the new skip flags |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 ] ); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The exclusion logic removes sections from $this->available_sections after it has already been used to calculate $this->subsequent_sections (line 45). This means excluded sections will still be present in subsequent files when using --include_once. The exclusion logic should be moved before line 44 to ensure excluded sections are properly removed from both $this->available_sections and $this->subsequent_sections.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces --skip_authors and --skip_terms flags to the wp export command, allowing for more granular control over exported content. The implementation is solid and includes corresponding tests. I've provided a few suggestions to improve code maintainability by reducing duplication and using more idiomatic PHP, which should make the code cleaner and easier to manage in the future.
| // Re-calculate exclusions after validation to ensure consistency. | ||
| $this->exclude = []; | ||
| if ( $this->export_args['skip_authors'] ) { | ||
| $this->exclude[] = 'authors'; | ||
| } | ||
| if ( $this->export_args['skip_terms'] ) { | ||
| $this->exclude = array_merge( $this->exclude, array( 'categories', 'tags', 'nav_menu_terms', 'custom_taxonomies_terms' ) ); | ||
| } |
There was a problem hiding this comment.
The comment on line 193 is a bit misleading since the exclusion list is being calculated for the first time here, not re-calculated. Additionally, the array on line 199 uses legacy syntax and makes the line quite long. For better readability and adherence to modern PHP standards, I suggest updating the comment and using short array syntax, breaking the line for clarity.
// Calculate exclusions based on flags.
$this->exclude = [];
if ( $this->export_args['skip_authors'] ) {
$this->exclude[] = 'authors';
}
if ( $this->export_args['skip_terms'] ) {
$this->exclude = array_merge(
$this->exclude,
[
'categories',
'tags',
'nav_menu_terms',
'custom_taxonomies_terms',
]
);
}| /** | ||
| * @param string|null $skip | ||
| * | ||
| * @phpstan-ignore method.unused | ||
| */ | ||
| 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; | ||
| } | ||
|
|
||
| /** | ||
| * @param string|null $skip | ||
| * | ||
| * @phpstan-ignore method.unused | ||
| */ | ||
| 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; | ||
| } |
There was a problem hiding this comment.
The functions check_skip_authors and check_skip_terms contain nearly identical logic, which is also duplicated from the existing check_skip_comments function. To improve maintainability and reduce code duplication (following the DRY principle), I recommend extracting this logic into a single private helper method. This will make the code cleaner and easier to update in the future if the validation logic for these flags changes.
/**
* @param string $flag_name
* @param string|null $skip
*
* @return bool
*/
private function check_skip_flag( $flag_name, $skip ) {
if ( null === $skip ) {
return true;
}
if ( 0 !== (int) $skip && 1 !== (int) $skip ) {
WP_CLI::warning( "{$flag_name} needs to be 0 (no) or 1 (yes)." );
return false;
}
$this->export_args[ $flag_name ] = $skip;
return true;
}
/**
* @param string|null $skip
*
* @phpstan-ignore method.unused
*/
private function check_skip_authors( $skip ) {
return $this->check_skip_flag( 'skip_authors', $skip );
}
/**
* @param string|null $skip
*
* @phpstan-ignore method.unused
*/
private function check_skip_terms( $skip ) {
return $this->check_skip_flag( 'skip_terms', $skip );
}| 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 ] ); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The logic to remove excluded sections from $this->available_sections can be significantly simplified by using the built-in array_diff() function. This approach is more concise, readable, and idiomatic than iterating and unsetting elements manually.
if ( ! empty( $writer_args['exclude'] ) ) {
$this->available_sections = array_diff( $this->available_sections, $writer_args['exclude'] );
}
This PR introduces
--skip_authorsand--skip_termsflags forwp exportcommand.Fixes: #72