| @@ -1,136 +1,204 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * WP-CLI commands registration. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 2 | +namespace Activitypub; | |
| 7 | 3 | |
| 8 | -namespace Activitypub; | |
| 4 | +use WP_CLI; | |
| 5 | +use WP_CLI_Command; | |
| 6 | +use Activitypub\Scheduler; | |
| 9 | 7 | |
| 8 | +use function Activitypub\was_comment_received; | |
| 9 | + | |
| 10 | 10 | /** |
| 11 | - * ActivityPub CLI command registry. | |
| 11 | + * WP-CLI commands | |
| 12 | 12 | * |
| 13 | - * Registers all ActivityPub CLI subcommands with WP-CLI. | |
| 14 | - * | |
| 15 | 13 | * @package Activitypub |
| 16 | 14 | */ |
| 17 | -class Cli { | |
| 18 | - | |
| 15 | +class Cli extends WP_CLI_Command { | |
| 19 | 16 | /** |
| 20 | - * Register all ActivityPub CLI commands. | |
| 17 | + * Check the Plugins Meta-Informations | |
| 21 | 18 | * |
| 22 | - * This method registers the main 'activitypub' command namespace and all its | |
| 23 | - * subcommands for managing ActivityPub functionality via WP-CLI. | |
| 19 | + * ## OPTIONS | |
| 24 | 20 | * |
| 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] | |
| 21 | + * [--Name] | |
| 22 | + * The Plugin Name | |
| 23 | + * | |
| 24 | + * [--PluginURI] | |
| 25 | + * The Plugin URI | |
| 26 | + * | |
| 27 | + * [--Version] | |
| 28 | + * The Plugin Version | |
| 29 | + * | |
| 30 | + * [--Description] | |
| 31 | + * The Plugin Description | |
| 32 | + * | |
| 33 | + * [--Author] | |
| 34 | + * The Plugin Author | |
| 35 | + * | |
| 36 | + * [--AuthorURI] | |
| 37 | + * The Plugin Author URI | |
| 38 | + * | |
| 39 | + * [--TextDomain] | |
| 40 | + * The Plugin Text Domain | |
| 41 | + * | |
| 42 | + * [--DomainPath] | |
| 43 | + * The Plugin Domain Path | |
| 44 | + * | |
| 45 | + * [--Network] | |
| 46 | + * The Plugin Network | |
| 47 | + * | |
| 48 | + * [--RequiresWP] | |
| 49 | + * The Plugin Requires at least | |
| 50 | + * | |
| 51 | + * [--RequiresPHP] | |
| 52 | + * The Plugin Requires PHP | |
| 53 | + * | |
| 54 | + * [--UpdateURI] | |
| 55 | + * The Plugin Update URI | |
| 56 | + * | |
| 57 | + * See: https://developer.wordpress.org/reference/functions/get_plugin_data/#return | |
| 58 | + * | |
| 59 | + * ## EXAMPLES | |
| 60 | + * | |
| 61 | + * $ wp webmention meta | |
| 62 | + * | |
| 63 | + * $ wp webmention meta --Version | |
| 64 | + * Version: 1.0.0 | |
| 65 | + * | |
| 66 | + * @param array|null $args The arguments. | |
| 67 | + * @param array|null $assoc_args The associative arguments. | |
| 68 | + * | |
| 69 | + * @return void | |
| 37 | 70 | */ |
| 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 | - ); | |
| 71 | + public function meta( $args, $assoc_args ) { | |
| 72 | + $plugin_data = get_plugin_meta(); | |
| 47 | 73 | |
| 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 | - ); | |
| 74 | + if ( $assoc_args ) { | |
| 75 | + $plugin_data = array_intersect_key( $plugin_data, $assoc_args ); | |
| 76 | + } else { | |
| 77 | + WP_CLI::line( __( "ActivityPub Plugin Meta:\n", 'activitypub' ) ); | |
| 78 | + } | |
| 55 | 79 | |
| 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 | - ); | |
| 80 | + foreach ( $plugin_data as $key => $value ) { | |
| 81 | + WP_CLI::line( $key . ': ' . $value ); | |
| 82 | + } | |
| 83 | + } | |
| 63 | 84 | |
| 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 | - ); | |
| 85 | + /** | |
| 86 | + * Remove the entire blog from the Fediverse. | |
| 87 | + * | |
| 88 | + * ## EXAMPLES | |
| 89 | + * | |
| 90 | + * $ wp activitypub self-destruct | |
| 91 | + * | |
| 92 | + * @param array|null $args The arguments. | |
| 93 | + * @param array|null $assoc_args The associative arguments. | |
| 94 | + * | |
| 95 | + * @return void | |
| 96 | + */ | |
| 97 | + public function self_destruct( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 98 | + WP_CLI::warning( __( 'Self-Destructing is not implemented yet.', 'activitypub' ) ); | |
| 99 | + } | |
| 71 | 100 | |
| 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 | - ); | |
| 101 | + /** | |
| 102 | + * Delete or Update a Post, Page, Custom Post Type or Attachment. | |
| 103 | + * | |
| 104 | + * ## OPTIONS | |
| 105 | + * | |
| 106 | + * <action> | |
| 107 | + * : The action to perform. Either `delete` or `update`. | |
| 108 | + * --- | |
| 109 | + * options: | |
| 110 | + * - delete | |
| 111 | + * - update | |
| 112 | + * --- | |
| 113 | + * | |
| 114 | + * <id> | |
| 115 | + * : The id of the Post, Page, Custom Post Type or Attachment. | |
| 116 | + * | |
| 117 | + * ## EXAMPLES | |
| 118 | + * | |
| 119 | + * $ wp activitypub post delete 1 | |
| 120 | + * | |
| 121 | + * @synopsis <action> <id> | |
| 122 | + * | |
| 123 | + * @param array|null $args The arguments. | |
| 124 | + * @param array|null $assoc_args The associative arguments. | |
| 125 | + * | |
| 126 | + * @return void | |
| 127 | + */ | |
| 128 | + public function post( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 129 | + $post = get_post( $args[1] ); | |
| 79 | 130 | |
| 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 | - ); | |
| 131 | + if ( ! $post ) { | |
| 132 | + WP_CLI::error( __( 'Post not found.', 'activitypub' ) ); | |
| 133 | + } | |
| 87 | 134 | |
| 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 | - ); | |
| 135 | + switch ( $args[0] ) { | |
| 136 | + case 'delete': | |
| 137 | + // translators: %s is the ID of the post. | |
| 138 | + WP_CLI::confirm( sprintf( __( 'Do you really want to delete the (Custom) Post with the ID: %s', 'activitypub' ), $args[1] ) ); | |
| 139 | + Scheduler::schedule_post_activity( 'trash', 'publish', $args[1] ); | |
| 140 | + WP_CLI::success( __( '"Delete"-Activity is queued.', 'activitypub' ) ); | |
| 141 | + break; | |
| 142 | + case 'update': | |
| 143 | + Scheduler::schedule_post_activity( 'publish', 'publish', $args[1] ); | |
| 144 | + WP_CLI::success( __( '"Update"-Activity is queued.', 'activitypub' ) ); | |
| 145 | + break; | |
| 146 | + default: | |
| 147 | + WP_CLI::error( __( 'Unknown action.', 'activitypub' ) ); | |
| 148 | + } | |
| 149 | + } | |
| 95 | 150 | |
| 96 | - \WP_CLI::add_command( | |
| 97 | - 'activitypub follow', | |
| 98 | - '\Activitypub\Cli\Follow_Command', | |
| 99 | - array( | |
| 100 | - 'shortdesc' => 'Follow a remote ActivityPub user.', | |
| 101 | - ) | |
| 102 | - ); | |
| 151 | + /** | |
| 152 | + * Delete or Update a Comment. | |
| 153 | + * | |
| 154 | + * ## OPTIONS | |
| 155 | + * | |
| 156 | + * <action> | |
| 157 | + * : The action to perform. Either `delete` or `update`. | |
| 158 | + * --- | |
| 159 | + * options: | |
| 160 | + * - delete | |
| 161 | + * - update | |
| 162 | + * --- | |
| 163 | + * | |
| 164 | + * <id> | |
| 165 | + * : The id of the Comment. | |
| 166 | + * | |
| 167 | + * ## EXAMPLES | |
| 168 | + * | |
| 169 | + * $ wp activitypub comment delete 1 | |
| 170 | + * | |
| 171 | + * @synopsis <action> <id> | |
| 172 | + * | |
| 173 | + * @param array|null $args The arguments. | |
| 174 | + * @param array|null $assoc_args The associative arguments. | |
| 175 | + * | |
| 176 | + * @return void | |
| 177 | + */ | |
| 178 | + public function comment( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 179 | + $comment = get_comment( $args[1] ); | |
| 103 | 180 | |
| 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 | - ); | |
| 181 | + if ( ! $comment ) { | |
| 182 | + WP_CLI::error( __( 'Comment not found.', 'activitypub' ) ); | |
| 183 | + } | |
| 111 | 184 | |
| 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 | - ); | |
| 185 | + if ( was_comment_received( $comment ) ) { | |
| 186 | + WP_CLI::error( __( 'This comment was received via ActivityPub and cannot be deleted or updated.', 'activitypub' ) ); | |
| 187 | + } | |
| 119 | 188 | |
| 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 | - ); | |
| 127 | - | |
| 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 | - ); | |
| 189 | + switch ( $args[0] ) { | |
| 190 | + case 'delete': | |
| 191 | + // translators: %s is the ID of the comment. | |
| 192 | + WP_CLI::confirm( sprintf( __( 'Do you really want to delete the Comment with the ID: %s', 'activitypub' ), $args[1] ) ); | |
| 193 | + Scheduler::schedule_comment_activity( 'trash', 'approved', $args[1] ); | |
| 194 | + WP_CLI::success( __( '"Delete"-Activity is queued.', 'activitypub' ) ); | |
| 195 | + break; | |
| 196 | + case 'update': | |
| 197 | + Scheduler::schedule_comment_activity( 'approved', 'approved', $args[1] ); | |
| 198 | + WP_CLI::success( __( '"Update"-Activity is queued.', 'activitypub' ) ); | |
| 199 | + break; | |
| 200 | + default: | |
| 201 | + WP_CLI::error( __( 'Unknown action.', 'activitypub' ) ); | |
| 202 | + } | |
| 135 | 203 | } |
| 136 | 204 | } |