-
Notifications
You must be signed in to change notification settings - Fork 5
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
leogermani
wants to merge
2
commits into
trunk
Choose a base branch
from
add/co-authors-link-all-users-command
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -17,6 +18,11 @@ | |
*/ | ||
class CoAuthorPlusMigrator implements InterfaceCommand { | ||
|
||
/** | ||
* @var Logger. | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* Prefix of tags which get converted to Guest Authors. | ||
* | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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' ), | ||
|
@@ -1331,4 +1353,66 @@ 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(); | ||
|
||
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' ); | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Simple file logging. | ||
* | ||
* @param string $file File name or path. | ||
* @param string $message Log message. | ||
*/ | ||
public function log( $file, $message ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the Logger instance is used instead of this one. Can it be deleted? |
||
$message .= "\n"; | ||
file_put_contents( $file, $message, FILE_APPEND ); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 parameterWe 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