From 69ba1c87e127b4c654aaa9f9edb3c4f816672712 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 11 Aug 2023 11:16:06 +0200 Subject: [PATCH] fix sticky posts endpoint --- includes/rest/class-collection.php | 43 +++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/includes/rest/class-collection.php b/includes/rest/class-collection.php index 3936d78..b29a090 100644 --- a/includes/rest/class-collection.php +++ b/includes/rest/class-collection.php @@ -5,8 +5,10 @@ use WP_REST_Server; use WP_REST_Response; use Activitypub\Transformer\Post; use Activitypub\Activity\Activity; +use Activitypub\Collection\Users as User_Collection; use function Activitypub\esc_hashtag; +use function Activitypub\is_single_user; use function Activitypub\get_rest_url_by_path; /** @@ -65,7 +67,13 @@ class Collection { */ public static function tags_get( $request ) { $user_id = $request->get_param( 'user_id' ); - $number = 4; + $user = User_Collection::get_by_various( $user_id ); + + if ( is_wp_error( $user ) ) { + return $user; + } + + $number = 4; $tags = \get_terms( array( @@ -82,7 +90,7 @@ class Collection { $response = array( '@context' => Activity::CONTEXT, - 'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user_id ) ), + 'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user->get__id() ) ), 'type' => 'Collection', 'totalItems' => count( $tags ), 'items' => array(), @@ -108,17 +116,32 @@ class Collection { */ public static function featured_get( $request ) { $user_id = $request->get_param( 'user_id' ); + $user = User_Collection::get_by_various( $user_id ); - $args = array( - 'post__in' => \get_option( 'sticky_posts' ), - 'ignore_sticky_posts' => 1, - ); - - if ( $user_id > 0 ) { - $args['author'] = $user_id; + if ( is_wp_error( $user ) ) { + return $user; } - $posts = \get_posts( $args ); + $sticky_posts = \get_option( 'sticky_posts' ); + + if ( ! is_single_user() && User_Collection::BLOG_USER_ID === $user->get__id() ) { + $posts = array(); + } elseif ( $sticky_posts ) { + $args = array( + 'post__in' => $sticky_posts, + 'ignore_sticky_posts' => 1, + 'orderby' => 'date', + 'order' => 'DESC', + ); + + if ( $user->get__id() > 0 ) { + $args['author'] = $user->get__id(); + } + + $posts = \get_posts( $args ); + } else { + $posts = array(); + } $response = array( '@context' => Activity::CONTEXT,