| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * WP-CLI commands registration. | |
| 3 | + * WP-CLI file. | |
| 4 | 4 | * |
| 5 | 5 | * @package Activitypub |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -6,131 +6,225 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Activitypub; |
| 9 | 9 | |
| 10 | +use Activitypub\Collection\Outbox; | |
| 11 | + | |
| 10 | 12 | /** |
| 11 | - * ActivityPub CLI command registry. | |
| 13 | + * WP-CLI commands. | |
| 12 | 14 | * |
| 13 | - * Registers all ActivityPub CLI subcommands with WP-CLI. | |
| 14 | - * | |
| 15 | 15 | * @package Activitypub |
| 16 | 16 | */ |
| 17 | -class Cli { | |
| 17 | +class Cli extends \WP_CLI_Command { | |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Register all ActivityPub CLI commands. | |
| 20 | + * Remove the entire blog from the Fediverse. | |
| 21 | 21 | * |
| 22 | - * This method registers the main 'activitypub' command namespace and all its | |
| 23 | - * subcommands for managing ActivityPub functionality via WP-CLI. | |
| 22 | + * ## EXAMPLES | |
| 24 | 23 | * |
| 25 | - * Available commands: | |
| 26 | - * - wp activitypub post <delete|update> <id> | |
| 27 | - * - wp activitypub comment <delete|update> <id> | |
| 28 | - * - wp activitypub actor <delete|update> <id> | |
| 29 | - * - wp activitypub outbox <undo|reschedule> <id> | |
| 30 | - * - wp activitypub cache <clear|status> [--type=<type>] | |
| 31 | - * - wp activitypub self-destruct [--status] [--yes] | |
| 32 | - * - wp activitypub move <from> <to> | |
| 33 | - * - wp activitypub follow <remote_user> | |
| 34 | - * - wp activitypub stats <collect|compile|send> | |
| 35 | - * - wp activitypub fetch <url> | |
| 36 | - * - wp activitypub blurhash backfill [--dry-run] [--limit=<n>] [--force] | |
| 24 | + * $ wp activitypub self-destruct | |
| 25 | + * | |
| 26 | + * @param array|null $args The arguments. | |
| 27 | + * @param array|null $assoc_args The associative arguments. | |
| 28 | + * | |
| 29 | + * @return void | |
| 37 | 30 | */ |
| 38 | - public static function register() { | |
| 39 | - // Register parent command with version subcommand. | |
| 40 | - \WP_CLI::add_command( | |
| 41 | - 'activitypub', | |
| 42 | - '\Activitypub\Cli\Command', | |
| 43 | - array( | |
| 44 | - 'shortdesc' => 'Manage ActivityPub plugin functionality and federation.', | |
| 45 | - ) | |
| 46 | - ); | |
| 31 | + public function self_destruct( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 32 | + \WP_CLI::warning( 'Self-Destructing is not implemented yet.' ); | |
| 33 | + } | |
| 47 | 34 | |
| 48 | - \WP_CLI::add_command( | |
| 49 | - 'activitypub post', | |
| 50 | - '\Activitypub\Cli\Post_Command', | |
| 51 | - array( | |
| 52 | - 'shortdesc' => 'Manage ActivityPub posts (delete or update).', | |
| 53 | - ) | |
| 54 | - ); | |
| 35 | + /** | |
| 36 | + * Delete or Update a Post, Page, Custom Post Type or Attachment. | |
| 37 | + * | |
| 38 | + * ## OPTIONS | |
| 39 | + * | |
| 40 | + * <action> | |
| 41 | + * : The action to perform. Either `delete` or `update`. | |
| 42 | + * --- | |
| 43 | + * options: | |
| 44 | + * - delete | |
| 45 | + * - update | |
| 46 | + * --- | |
| 47 | + * | |
| 48 | + * <id> | |
| 49 | + * : The id of the Post, Page, Custom Post Type or Attachment. | |
| 50 | + * | |
| 51 | + * ## EXAMPLES | |
| 52 | + * | |
| 53 | + * $ wp activitypub post delete 1 | |
| 54 | + * | |
| 55 | + * @synopsis <action> <id> | |
| 56 | + * | |
| 57 | + * @param array $args The arguments. | |
| 58 | + */ | |
| 59 | + public function post( $args ) { | |
| 60 | + $post = get_post( $args[1] ); | |
| 55 | 61 | |
| 56 | - \WP_CLI::add_command( | |
| 57 | - 'activitypub comment', | |
| 58 | - '\Activitypub\Cli\Comment_Command', | |
| 59 | - array( | |
| 60 | - 'shortdesc' => 'Manage ActivityPub comments (delete or update).', | |
| 61 | - ) | |
| 62 | - ); | |
| 62 | + if ( ! $post ) { | |
| 63 | + \WP_CLI::error( 'Post not found.' ); | |
| 64 | + } | |
| 63 | 65 | |
| 64 | - \WP_CLI::add_command( | |
| 65 | - 'activitypub actor', | |
| 66 | - '\Activitypub\Cli\Actor_Command', | |
| 67 | - array( | |
| 68 | - 'shortdesc' => 'Manage ActivityPub actors (delete or update).', | |
| 69 | - ) | |
| 70 | - ); | |
| 66 | + switch ( $args[0] ) { | |
| 67 | + case 'delete': | |
| 68 | + \WP_CLI::confirm( 'Do you really want to delete the (Custom) Post with the ID: ' . $args[1] ); | |
| 69 | + add_to_outbox( $post, 'Delete', $post->post_author ); | |
| 70 | + \WP_CLI::success( '"Delete" activity is queued.' ); | |
| 71 | + break; | |
| 72 | + case 'update': | |
| 73 | + add_to_outbox( $post, 'Update', $post->post_author ); | |
| 74 | + \WP_CLI::success( '"Update" activity is queued.' ); | |
| 75 | + break; | |
| 76 | + default: | |
| 77 | + \WP_CLI::error( 'Unknown action.' ); | |
| 78 | + } | |
| 79 | + } | |
| 71 | 80 | |
| 72 | - \WP_CLI::add_command( | |
| 73 | - 'activitypub outbox', | |
| 74 | - '\Activitypub\Cli\Outbox_Command', | |
| 75 | - array( | |
| 76 | - 'shortdesc' => 'Manage ActivityPub outbox items (undo or reschedule).', | |
| 77 | - ) | |
| 78 | - ); | |
| 81 | + /** | |
| 82 | + * Delete or Update a Comment. | |
| 83 | + * | |
| 84 | + * ## OPTIONS | |
| 85 | + * | |
| 86 | + * <action> | |
| 87 | + * : The action to perform. Either `delete` or `update`. | |
| 88 | + * --- | |
| 89 | + * options: | |
| 90 | + * - delete | |
| 91 | + * - update | |
| 92 | + * --- | |
| 93 | + * | |
| 94 | + * <id> | |
| 95 | + * : The id of the Comment. | |
| 96 | + * | |
| 97 | + * ## EXAMPLES | |
| 98 | + * | |
| 99 | + * $ wp activitypub comment delete 1 | |
| 100 | + * | |
| 101 | + * @synopsis <action> <id> | |
| 102 | + * | |
| 103 | + * @param array $args The arguments. | |
| 104 | + */ | |
| 105 | + public function comment( $args ) { | |
| 106 | + $comment = get_comment( $args[1] ); | |
| 79 | 107 | |
| 80 | - \WP_CLI::add_command( | |
| 81 | - 'activitypub self-destruct', | |
| 82 | - '\Activitypub\Cli\Self_Destruct_Command', | |
| 83 | - array( | |
| 84 | - 'shortdesc' => 'Remove the entire blog from the Fediverse.', | |
| 85 | - ) | |
| 86 | - ); | |
| 108 | + if ( ! $comment ) { | |
| 109 | + \WP_CLI::error( 'Comment not found.' ); | |
| 110 | + } | |
| 87 | 111 | |
| 88 | - \WP_CLI::add_command( | |
| 89 | - 'activitypub move', | |
| 90 | - '\Activitypub\Cli\Move_Command', | |
| 91 | - array( | |
| 92 | - 'shortdesc' => 'Move the blog to a new URL.', | |
| 93 | - ) | |
| 94 | - ); | |
| 112 | + if ( was_comment_received( $comment ) ) { | |
| 113 | + \WP_CLI::error( 'This comment was received via ActivityPub and cannot be deleted or updated.' ); | |
| 114 | + } | |
| 95 | 115 | |
| 96 | - \WP_CLI::add_command( | |
| 97 | - 'activitypub follow', | |
| 98 | - '\Activitypub\Cli\Follow_Command', | |
| 99 | - array( | |
| 100 | - 'shortdesc' => 'Follow a remote ActivityPub user.', | |
| 101 | - ) | |
| 102 | - ); | |
| 116 | + switch ( $args[0] ) { | |
| 117 | + case 'delete': | |
| 118 | + \WP_CLI::confirm( 'Do you really want to delete the Comment with the ID: ' . $args[1] ); | |
| 119 | + add_to_outbox( $comment, 'Delete', $comment->user_id ); | |
| 120 | + \WP_CLI::success( '"Delete" activity is queued.' ); | |
| 121 | + break; | |
| 122 | + case 'update': | |
| 123 | + add_to_outbox( $comment, 'Update', $comment->user_id ); | |
| 124 | + \WP_CLI::success( '"Update" activity is queued.' ); | |
| 125 | + break; | |
| 126 | + default: | |
| 127 | + \WP_CLI::error( 'Unknown action.' ); | |
| 128 | + } | |
| 129 | + } | |
| 103 | 130 | |
| 104 | - \WP_CLI::add_command( | |
| 105 | - 'activitypub cache', | |
| 106 | - '\Activitypub\Cli\Cache_Command', | |
| 107 | - array( | |
| 108 | - 'shortdesc' => 'Manage remote media cache (clear or show status).', | |
| 109 | - ) | |
| 110 | - ); | |
| 131 | + /** | |
| 132 | + * Undo an activity that was sent to the Fediverse. | |
| 133 | + * | |
| 134 | + * ## OPTIONS | |
| 135 | + * | |
| 136 | + * <outbox_item_id> | |
| 137 | + * The ID or URL of the outbox item to undo. | |
| 138 | + * | |
| 139 | + * ## EXAMPLES | |
| 140 | + * | |
| 141 | + * $ wp activitypub undo 123 | |
| 142 | + * $ wp activitypub undo "https://example.com/?post_type=ap_outbox&p=123" | |
| 143 | + * | |
| 144 | + * @synopsis <outbox_item_id> | |
| 145 | + * | |
| 146 | + * @param array $args The arguments. | |
| 147 | + */ | |
| 148 | + public function undo( $args ) { | |
| 149 | + $outbox_item_id = $args[0]; | |
| 150 | + if ( ! is_numeric( $outbox_item_id ) ) { | |
| 151 | + $outbox_item_id = url_to_postid( $outbox_item_id ); | |
| 152 | + } | |
| 111 | 153 | |
| 112 | - \WP_CLI::add_command( | |
| 113 | - 'activitypub fetch', | |
| 114 | - '\Activitypub\Cli\Fetch_Command', | |
| 115 | - array( | |
| 116 | - 'shortdesc' => 'Fetch a remote URL with a signed ActivityPub request.', | |
| 117 | - ) | |
| 118 | - ); | |
| 154 | + $outbox_item_id = get_post( $outbox_item_id ); | |
| 155 | + if ( ! $outbox_item_id ) { | |
| 156 | + \WP_CLI::error( 'Activity not found.' ); | |
| 157 | + } | |
| 119 | 158 | |
| 120 | - \WP_CLI::add_command( | |
| 121 | - 'activitypub stats', | |
| 122 | - '\Activitypub\Cli\Stats_Command', | |
| 123 | - array( | |
| 124 | - 'shortdesc' => 'Manage ActivityPub statistics (collect, compile or send).', | |
| 125 | - ) | |
| 126 | - ); | |
| 159 | + $undo_id = Outbox::undo( $outbox_item_id ); | |
| 160 | + if ( ! $undo_id ) { | |
| 161 | + \WP_CLI::error( 'Failed to undo activity.' ); | |
| 162 | + } | |
| 163 | + \WP_CLI::success( 'Undo activity scheduled.' ); | |
| 164 | + } | |
| 127 | 165 | |
| 128 | - \WP_CLI::add_command( | |
| 129 | - 'activitypub blurhash', | |
| 130 | - '\Activitypub\Cli\Blurhash_Command', | |
| 131 | - array( | |
| 132 | - 'shortdesc' => 'Backfill Blurhash placeholders for image attachments.', | |
| 133 | - ) | |
| 134 | - ); | |
| 166 | + /** | |
| 167 | + * Re-Schedule an activity that was sent to the Fediverse before. | |
| 168 | + * | |
| 169 | + * ## OPTIONS | |
| 170 | + * | |
| 171 | + * <outbox_item_id> | |
| 172 | + * The ID or URL of the outbox item to reschedule. | |
| 173 | + * | |
| 174 | + * ## EXAMPLES | |
| 175 | + * | |
| 176 | + * $ wp activitypub reschedule 123 | |
| 177 | + * $ wp activitypub reschedule "https://example.com/?post_type=ap_outbox&p=123" | |
| 178 | + * | |
| 179 | + * @synopsis <outbox_item_id> | |
| 180 | + * | |
| 181 | + * @param array $args The arguments. | |
| 182 | + */ | |
| 183 | + public function reschedule( $args ) { | |
| 184 | + $outbox_item_id = $args[0]; | |
| 185 | + if ( ! is_numeric( $outbox_item_id ) ) { | |
| 186 | + $outbox_item_id = url_to_postid( $outbox_item_id ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + $outbox_item_id = get_post( $outbox_item_id ); | |
| 190 | + if ( ! $outbox_item_id ) { | |
| 191 | + \WP_CLI::error( 'Activity not found.' ); | |
| 192 | + } | |
| 193 | + | |
| 194 | + Outbox::reschedule( $outbox_item_id ); | |
| 195 | + | |
| 196 | + \WP_CLI::success( 'Rescheduled activity.' ); | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Move the blog to a new URL. | |
| 201 | + * | |
| 202 | + * ## OPTIONS | |
| 203 | + * | |
| 204 | + * <from> | |
| 205 | + * The current URL of the blog. | |
| 206 | + * | |
| 207 | + * <to> | |
| 208 | + * The new URL of the blog. | |
| 209 | + * | |
| 210 | + * ## EXAMPLES | |
| 211 | + * | |
| 212 | + * $ wp activitypub move https://example.com/ https://newsite.com/ | |
| 213 | + * | |
| 214 | + * @synopsis <from> <to> | |
| 215 | + * | |
| 216 | + * @param array $args The arguments. | |
| 217 | + */ | |
| 218 | + public function move( $args ) { | |
| 219 | + $from = $args[0]; | |
| 220 | + $to = $args[1]; | |
| 221 | + | |
| 222 | + $outbox_item_id = Move::account( $from, $to ); | |
| 223 | + | |
| 224 | + if ( is_wp_error( $outbox_item_id ) ) { | |
| 225 | + \WP_CLI::error( $outbox_item_id->get_error_message() ); | |
| 226 | + } else { | |
| 227 | + \WP_CLI::success( 'Move Scheduled.' ); | |
| 228 | + } | |
| 135 | 229 | } |
| 136 | 230 | } |