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

feat: add command to link all users to GAs #331

Open
wants to merge 2 commits 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
73 changes: 73 additions & 0 deletions src/Command/General/CoAuthorPlusMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use \NewspackCustomContentMigrator\Logic\CoAuthorPlus;
use \NewspackCustomContentMigrator\Logic\Posts;
use \NewspackCustomContentMigrator\PluginSetup;
use \NewspackCustomContentMigrator\Utils\Logger;
use \NewspackCustomContentMigrator\Utils\PHP;
use \WP_CLI;
use \WP_Query;
Expand All @@ -17,6 +18,11 @@
*/
class CoAuthorPlusMigrator implements InterfaceCommand {

/**
* @var Logger.
*/
private $logger;

/**
* Prefix of tags which get converted to Guest Authors.
*
Expand Down Expand Up @@ -51,6 +57,7 @@ class CoAuthorPlusMigrator implements InterfaceCommand {
private function __construct() {
$this->coauthorsplus_logic = new CoAuthorPlus();
$this->posts_logic = new Posts();
$this->logger = new Logger();
}

/**
Expand Down Expand Up @@ -192,6 +199,21 @@ public function register_commands() {
],
]
);
\WP_CLI::add_command(
'newspack-content-migrator co-authors-link-all-users-to-existing-guest-authors',
[ $this, 'cmd_link_all_users_to_guest_authors' ],
[
'shortdesc' => 'Link all WP users to their existing Guest Authors counterparts based on email.',
'synopsis' => [
[
'type' => 'flag',
'name' => 'dry-run',
'optional' => true,
'description' => 'Whether to do a dry-run without making updates.',
],
]
]
);
WP_CLI::add_command(
'newspack-content-migrator co-authors-create-guest-author-and-add-to-post',
array( $this, 'cmd_cap_create_guest_author_and_add_to_post' ),
Expand Down Expand Up @@ -1331,4 +1353,55 @@ private function get_posts( $post_type ) {

}

/**
* Callable for the 'co-authors-link-all-users-to-existing-guest-authors' command.
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function cmd_link_all_users_to_guest_authors( $args, $assoc_args ) {

$dry_run = WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run', false );
$log_file = 'cap-link-all-users-to-guest-authors.log';

if ( $dry_run ) {
$this->logger->log( $log_file, 'Performing a dry-run. No changes will be made.' );
} else {
WP_CLI::line( 'This command will modify the database.');
WP_CLI::line( 'Consider running it with --dry-run first to see what it will do.');
WP_CLI::confirm( "Are you sure you want to continue?", $assoc_args );
}

$guest_authors = new \CoAuthors_Guest_Authors();

// Get the list of users.
$users = get_users();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could worry that on a large site with many users this would be a heavy query. If we only need the ID – we could maybe use a raw query for them instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually only need the user_email, but link_guest_author_to_wp_user takes the WP_User object as a parameter

We could just add the post_meta ourselves... or we could just get the user if needed to pass to that function.

However, if I refactored that now I would not be able to test it in a good way... I think I rather stick with the tested code for now and address this if it becomes an issue in a future migration


foreach( $users as $user ) {
$ga = $guest_authors->get_guest_author_by( 'user_email', $user->user_email );
if ( ! $ga ) {
continue;
}

$this->logger->log( $log_file, sprintf( 'Found guest author %s for user %s', $ga->user_nicename, $user->user_email ) );

$existing_link = get_post_meta( $ga->ID, 'cap-linked_account', true );

if ( ! empty($existing_link ) ) {
$this->logger->log( $log_file, 'Guest author already linked to a user. Skipping.', 'warning' );
continue;
}

if ( $dry_run ) {
continue;
}

// Link the user to the guest author.
$this->coauthorsplus_logic->link_guest_author_to_wp_user( $ga->ID, $user );
$this->logger->log( $log_file, 'Guest author linked', 'success' );

}

}

}