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