From 58c04856c9a9efbc7574249b144142bf67ed7ee9 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 21 Jun 2023 17:10:52 +0200 Subject: [PATCH] check if a user is enabled or not --- includes/class-user-factory.php | 42 ++++++++++++++++++------------ includes/functions.php | 46 +++++++++++++++++++++++++++++++++ includes/model/class-post.php | 2 +- 3 files changed, 72 insertions(+), 18 deletions(-) diff --git a/includes/class-user-factory.php b/includes/class-user-factory.php index ce70ccb..4a0ce5a 100644 --- a/includes/class-user-factory.php +++ b/includes/class-user-factory.php @@ -30,24 +30,32 @@ class User_Factory { * @return \Acitvitypub\Model\User The User. */ public static function get_by_id( $user_id ) { - $user_id = (int) $user_id; - - if ( self::BLOG_USER_ID === $user_id ) { - return new Blog_User( $user_id ); - } elseif ( self::APPLICATION_USER_ID === $user_id ) { - return new Application_User( $user_id ); - } else { - $user = get_user_by( 'ID', $user_id ); - if ( ! $user || ! \user_can( $user, 'publish_posts' ) ) { - return new WP_Error( - 'activitypub_user_not_found', - \__( 'User not found', 'activitypub' ), - array( 'status' => 404 ) - ); - } - - return new User( $user->ID ); + if ( is_string( $user_id ) || is_numeric( $user_id ) ) { + $user_id = (int) $user_id; } + + if ( + self::BLOG_USER_ID === $user_id && + is_user_enabled( $user_id ) + ) { + return new Blog_User( $user_id ); + } elseif ( + self::APPLICATION_USER_ID === $user_id && + is_user_enabled( $user_id ) + ) { + return new Application_User( $user_id ); + } elseif ( + $user_id > 0 && + is_user_enabled( $user_id ) + ) { + return new User( $user_id ); + } + + return new WP_Error( + 'activitypub_user_not_found', + \__( 'User not found', 'activitypub' ), + array( 'status' => 404 ) + ); } /** diff --git a/includes/functions.php b/includes/functions.php index 595e85b..7fc62d8 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -277,3 +277,49 @@ function is_activitypub_request() { function is_single_user_mode() { return ACTIVITYPUB_SINGLE_USER_MODE; } + +/** + * This function checks if a user is enabled for ActivityPub. + * + * @param int $user_id The User-ID. + * + * @return boolean True if the user is enabled, false otherwise. + */ +function is_user_enabled( $user_id ) { + switch ( $user_id ) { + // if the user is the application user, it's always enabled. + case \Activitypub\User_Factory::APPLICATION_USER_ID: + return true; + // if the user is the blog user, it's only enabled in single-user mode. + case \Activitypub\User_Factory::BLOG_USER_ID: + if ( is_single_user_mode() ) { + return true; + } + + return false; + // if the user is any other user, it's enabled if it can publish posts. + default: + if ( + ! is_single_user_mode() && + \user_can( $user_id, 'publish_posts' ) + ) { + return true; + } + + return false; + } +} + +if ( ! function_exists( 'get_self_link' ) ) { + /** + * Get the correct self URL + * + * @return boolean + */ + function get_self_link() { + $host = wp_parse_url( home_url() ); + + return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); + } +} + diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 5ebeb34..5004162 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -197,7 +197,7 @@ class Post { * @return int the User ID. */ public function get_user_id() { - return apply_filters( 'activitypub_post_user_id', $this->post_author, $this->post ); + return apply_filters( 'activitypub_post_user_id', $this->get_post_author(), $this->post ); } /**