From 0f24239bef8e4354aeec1949c1cf0edb78736d53 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 4 Mar 2025 15:24:16 -0600 Subject: [PATCH 1/2] Move: Add cli command to update domains --- includes/class-cli.php | 98 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/includes/class-cli.php b/includes/class-cli.php index 4d42693af..e7b418eb6 100644 --- a/includes/class-cli.php +++ b/includes/class-cli.php @@ -7,12 +7,17 @@ namespace Activitypub; +use Activitypub\Collection\Actors; use Activitypub\Collection\Outbox; +use Activitypub\Model\Blog; use Activitypub\Scheduler\Comment; use Activitypub\Scheduler\Post; use WP_CLI; use WP_CLI_Command; +use function Activitypub\is_user_disabled; +use function Activitypub\is_user_type_disabled; + /** * WP-CLI commands. * @@ -231,4 +236,97 @@ public function move( $args ) { WP_CLI::success( 'Moved Scheduled.' ); } } + + /** + * Move all ActivityPub enabled users from one domain to another. + * + * ## OPTIONS + * + * + * The new domain to move to. + * + * ## EXAMPLES + * + * $ wp activitypub move_domain newsite.com + * + * @synopsis + * + * @param array $args The arguments. + */ + public function move_domain( $args ) { + $domain = $args[0]; + + // Make sure the domain has a scheme. + if ( ! preg_match( '#^https?://#', $domain ) ) { + $domain = 'https://' . $domain; + } + + $domain = \esc_url_raw( $domain ); + $domain = \trailingslashit( $domain ); + + // Get the current site URL. + $site_url = site_url( '/' ); + + $moved_count = 0; + $errors = array(); + + // Check if blog user is enabled. + if ( ! is_user_disabled( \Activitypub\Collection\Actors::BLOG_USER_ID ) ) { + WP_CLI::line( 'Moving blog user...' ); + $from = ( new Blog() )->get_id(); + $result = Move::account( $from, str_replace( $site_url, $domain, $from ) ); + + if ( is_wp_error( $result ) ) { + $errors[] = 'Blog user: ' . $result->get_error_message(); + WP_CLI::warning( 'Failed to move blog user: ' . $result->get_error_message() ); + } else { + ++$moved_count; + WP_CLI::line( 'Blog user moved successfully.' ); + } + } + + // Check if regular users are enabled. + if ( ! is_user_type_disabled( 'user' ) ) { + // Get all users with ActivityPub capability. + $users = get_users( array( 'capability__in' => array( 'activitypub' ) ) ); + + WP_CLI::line( sprintf( 'Found %d ActivityPub enabled users.', count( $users ) ) ); + + foreach ( $users as $user ) { + // Skip disabled users. + if ( is_user_disabled( $user->ID ) ) { + WP_CLI::line( sprintf( 'Skipping disabled user %s.', $user->user_login ) ); + continue; + } + + $from = Actors::get_by_id( $user->ID )->get_id(); + $to = str_replace( $site_url, $domain, $from ); + + WP_CLI::line( sprintf( 'Moving user %s from %s to %s...', $user->user_login, $from, $to ) ); + + $result = Move::account( $from, $to ); + + if ( is_wp_error( $result ) ) { + $errors[] = 'User ' . $user->user_login . ': ' . $result->get_error_message(); + WP_CLI::warning( sprintf( 'Failed to move user %s: %s', $user->user_login, $result->get_error_message() ) ); + } else { + ++$moved_count; + WP_CLI::line( sprintf( 'User %s moved successfully.', $user->user_login ) ); + } + } + } + + if ( $moved_count > 0 ) { + WP_CLI::success( sprintf( 'Successfully moved %d users to %s.', $moved_count, $domain ) ); + } else { + WP_CLI::error( 'No users were moved.' ); + } + + if ( ! empty( $errors ) ) { + WP_CLI::warning( 'The following errors occurred:' ); + foreach ( $errors as $error ) { + WP_CLI::line( ' - ' . $error ); + } + } + } } From 90946cfded2d2325f9d63cc9b20281669f95d35f Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 4 Mar 2025 15:27:47 -0600 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 4 ++++ readme.txt | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5254fef11..03d3b07fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +* CLI command to create Move activities with a domain change. + ### Fixed * Updates to certain user meta fields did not trigger an Update activity. diff --git a/readme.txt b/readme.txt index 6143c391c..1e4b66f4c 100644 --- a/readme.txt +++ b/readme.txt @@ -131,6 +131,7 @@ For reasons of data protection, it is not possible to see the followers of other = Unreleased = +* Added: CLI command to create Move activities with a domain change. * Fixed: Updates to certain user meta fields did not trigger an Update activity. = 5.4.1 =