Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add / CoAuthors Plus Delete All Terms: needs testing and fix #471

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Command/General/CoAuthorPlusMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,23 @@ public function register_commands() {
]
);

WP_CLI::add_command(
'newspack-content-migrator co-authors-delete-all-author-terms',
[ $this, 'cmd_delete_all_author_terms' ],
[
'shortdesc' => 'Delete all "author" terms (and taxonomies) on the site.',
'synopsis' => [
[
'type' => 'flag',
'name' => 'dry-run',
'description' => 'Do a dry run without deleting anything.',
'optional' => true,
'repeating' => false,
],
],
]
);

WP_CLI::add_command(
'newspack-content-migrator co-authors-delete-all-co-authors',
[ $this, 'cmd_delete_all_co_authors' ],
Expand Down Expand Up @@ -1186,6 +1203,45 @@ public function cmd_fix_gas_post_counts( $args, $assoc_args ) {
}
}

public function cmd_delete_all_author_terms( array $positional_args, array $assoc_args ): void {

WP_CLI::line( "For now, please use: wp term delete author $(wp term list author --format=ids)" );
WP_CLI::line( "or, fix and test the todo in code below." );

return;

$dry_run = $assoc_args['dry-run'] ?? false;
if ( $dry_run ) {
WP_CLI::log( 'Dry run is enabled, so this command will only show what would be deleted.' );
}

global $wpdb;

$term_ids = $wpdb->get_col( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'author'" );

$total= count( $term_ids );
$counter = 0;
WP_CLI::confirm( sprintf( 'About to delete %d author terms/taxonomies. Is that OK?', $total ) );

foreach ( $term_ids as $term_id ) {

WP_CLI::log( sprintf( '(%d)/(%d) Author Term #%d.', ++ $counter, $total, $term_id ) );
if ( ! $dry_run ) {

// TODO: turn off CoAuthorsPlus term callback: '_update_users_posts_count' );
// it takes too long to run each sql and the term will be deleted anyway
// so counts are not needed.

// wp_delete_term( $term_id, 'author' );

WP_CLI::log( sprintf( 'Deleted #%d.', $term_id ) );
}
}

wp_cache_flush();
WP_CLI::success( 'Done' );
}

public function cmd_delete_all_co_authors( array $positional_args, array $assoc_args ): void {
$dry_run = $assoc_args['dry-run'] ?? false;
if ( $dry_run ) {
Expand Down