diff --git a/activitypub.php b/activitypub.php index 83692972d..12865515a 100644 --- a/activitypub.php +++ b/activitypub.php @@ -15,6 +15,8 @@ namespace Activitypub; +use WP_CLI; + use function Activitypub\is_blog_public; use function Activitypub\site_supports_blocks; @@ -218,3 +220,14 @@ function get_plugin_version() { return $meta['Version']; } + +// Check for CLI env, to add the CLI commands +if ( defined( 'WP_CLI' ) && WP_CLI ) { + WP_CLI::add_command( + 'activitypub', + '\Activitypub\Cli', + array( + 'shortdesc' => __( 'ActivityPub related commands: Meta-Infos, Delete and soon Self-Destruct.', 'activitypub' ), + ) + ); +} diff --git a/includes/class-cli.php b/includes/class-cli.php new file mode 100644 index 000000000..b30cdb9f6 --- /dev/null +++ b/includes/class-cli.php @@ -0,0 +1,204 @@ + $value ) { + WP_CLI::line( $key . ': ' . $value ); + } + } + + /** + * Remove the entire blog from the Fediverse. + * + * ## EXAMPLES + * + * $ wp activitypub self-destruct + * + * @param array|null $args The arguments. + * @param array|null $assoc_args The associative arguments. + * + * @return void + */ + public function self_destruct( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + WP_CLI::warning( __( 'Self-Destructing is not implemented yet.', 'activitypub' ) ); + } + + /** + * Delete or Update a Post, Page, Custom Post Type or Attachment. + * + * ## OPTIONS + * + * + * : The action to perform. Either `delete` or `update`. + * --- + * options: + * - delete + * - update + * --- + * + * + * : The id of the Post, Page, Custom Post Type or Attachment. + * + * ## EXAMPLES + * + * $ wp activitypub post delete 1 + * + * @synopsis + * + * @param array|null $args The arguments. + * @param array|null $assoc_args The associative arguments. + * + * @return void + */ + public function post( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $post = get_post( $args[1] ); + + if ( ! $post ) { + WP_CLI::error( __( 'Post not found.', 'activitypub' ) ); + } + + switch ( $args[0] ) { + case 'delete': + // translators: %s is the ID of the post. + WP_CLI::confirm( sprintf( __( 'Do you really want to delete the (Custom) Post with the ID: %s', 'activitypub' ), $args[1] ) ); + Scheduler::schedule_post_activity( 'trash', 'publish', $args[1] ); + WP_CLI::success( __( '"Delete"-Activity is queued.', 'activitypub' ) ); + break; + case 'update': + Scheduler::schedule_post_activity( 'publish', 'publish', $args[1] ); + WP_CLI::success( __( '"Update"-Activity is queued.', 'activitypub' ) ); + break; + default: + WP_CLI::error( __( 'Unknown action.', 'activitypub' ) ); + } + } + + /** + * Delete or Update a Comment. + * + * ## OPTIONS + * + * + * : The action to perform. Either `delete` or `update`. + * --- + * options: + * - delete + * - update + * --- + * + * + * : The id of the Comment. + * + * ## EXAMPLES + * + * $ wp activitypub comment delete 1 + * + * @synopsis + * + * @param array|null $args The arguments. + * @param array|null $assoc_args The associative arguments. + * + * @return void + */ + public function comment( $args, $assoc_args ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $comment = get_comment( $args[1] ); + + if ( ! $comment ) { + WP_CLI::error( __( 'Comment not found.', 'activitypub' ) ); + } + + if ( was_comment_received( $comment ) ) { + WP_CLI::error( __( 'This comment was received via ActivityPub and cannot be deleted or updated.', 'activitypub' ) ); + } + + switch ( $args[0] ) { + case 'delete': + // translators: %s is the ID of the comment. + WP_CLI::confirm( sprintf( __( 'Do you really want to delete the Comment with the ID: %s', 'activitypub' ), $args[1] ) ); + Scheduler::schedule_comment_activity( 'trash', 'approved', $args[1] ); + WP_CLI::success( __( '"Delete"-Activity is queued.', 'activitypub' ) ); + break; + case 'update': + Scheduler::schedule_comment_activity( 'approved', 'approved', $args[1] ); + WP_CLI::success( __( '"Update"-Activity is queued.', 'activitypub' ) ); + break; + default: + WP_CLI::error( __( 'Unknown action.', 'activitypub' ) ); + } + } +} diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index 4a1ed533e..631da8d77 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -117,6 +117,10 @@ public static function deregister_schedules() { public static function schedule_post_activity( $new_status, $old_status, $post ) { $post = get_post( $post ); + if ( ! $post ) { + return; + } + if ( 'ap_extrafield' === $post->post_type ) { self::schedule_profile_update( $post->post_author ); return; @@ -182,7 +186,7 @@ public static function schedule_comment_activity( $new_status, $old_status, $com $comment = get_comment( $comment ); // federate only comments that are written by a registered user. - if ( ! $comment->user_id ) { + if ( ! $comment || ! $comment->user_id ) { return; }