| @@ -8,13 +8,16 @@ | ||
| 8 | 8 | namespace Activitypub\Rest; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Activity\Base_Object; |
| 11 | 11 | use Activitypub\Collection\Actors; |
| 12 | +use Activitypub\Model\Application; | |
| 13 | +use Activitypub\Model\Blog; | |
| 14 | +use Activitypub\Model\User; | |
| 12 | 15 | use Activitypub\Transformer\Factory; |
| 13 | 16 | |
| 14 | 17 | use function Activitypub\esc_hashtag; |
| 18 | +use function Activitypub\is_single_user; | |
| 15 | 19 | use function Activitypub\get_rest_url_by_path; |
| 16 | -use function Activitypub\is_single_user; | |
| 17 | 20 | |
| 18 | 21 | /** |
| 19 | 22 | * Collections_Controller class. |
| 20 | 23 | * |
| @@ -36,12 +39,11 @@ | ||
| 36 | 39 | '/' . $this->rest_base . '/collections/(?P<type>[\w\-\.]+)', |
| 37 | 40 | array( |
| 38 | 41 | 'args' => array( |
| 39 | 42 | 'user_id' => array( |
| 40 | - 'description' => 'The user ID or username.', | |
| 41 | - 'type' => 'integer', | |
| 42 | - 'required' => true, | |
| 43 | - 'validate_callback' => array( $this, 'validate_user_id' ), | |
| 43 | + 'description' => 'The user ID or username.', | |
| 44 | + 'type' => 'string', | |
| 45 | + 'required' => true, | |
| 44 | 46 | ), |
| 45 | 47 | 'type' => array( |
| 46 | 48 | 'description' => 'The type of collection to query.', |
| 47 | 49 | 'type' => 'string', |
| @@ -81,16 +83,21 @@ | ||
| 81 | 83 | * @return \WP_REST_Response|\WP_Error Response object or WP_Error object. |
| 82 | 84 | */ |
| 83 | 85 | public function get_items( $request ) { |
| 84 | 86 | $user_id = $request->get_param( 'user_id' ); |
| 87 | + $user = Actors::get_by_various( $user_id ); | |
| 85 | 88 | |
| 89 | + if ( \is_wp_error( $user ) ) { | |
| 90 | + return $user; | |
| 91 | + } | |
| 92 | + | |
| 86 | 93 | switch ( $request->get_param( 'type' ) ) { |
| 87 | 94 | case 'tags': |
| 88 | - $response = $this->get_tags( $request, $user_id ); | |
| 95 | + $response = $this->get_tags( $request, $user ); | |
| 89 | 96 | break; |
| 90 | 97 | |
| 91 | 98 | case 'featured': |
| 92 | - $response = $this->get_featured( $request, $user_id ); | |
| 99 | + $response = $this->get_featured( $request, $user ); | |
| 93 | 100 | break; |
| 94 | 101 | |
| 95 | 102 | default: |
| 96 | 103 | $response = new \WP_Error( 'rest_unknown_collection_type', 'Unknown collection type.', array( 'status' => 404 ) ); |
| @@ -108,14 +115,14 @@ | ||
| 108 | 115 | |
| 109 | 116 | /** |
| 110 | 117 | * Retrieves a collection of featured tags. |
| 111 | 118 | * |
| 112 | - * @param \WP_REST_Request $request The request object. | |
| 113 | - * @param int $user_id Actor ID. | |
| 119 | + * @param \WP_REST_Request $request The request object. | |
| 120 | + * @param User|Blog|Application $user Actor. | |
| 114 | 121 | * |
| 115 | 122 | * @return array Collection of featured tags. |
| 116 | 123 | */ |
| 117 | - public function get_tags( $request, $user_id ) { | |
| 124 | + public function get_tags( $request, $user ) { | |
| 118 | 125 | $tags = \get_terms( |
| 119 | 126 | array( |
| 120 | 127 | 'taxonomy' => 'post_tag', |
| 121 | 128 | 'orderby' => 'count', |
| @@ -129,9 +136,9 @@ | ||
| 129 | 136 | } |
| 130 | 137 | |
| 131 | 138 | $response = array( |
| 132 | 139 | '@context' => Base_Object::JSON_LD_CONTEXT, |
| 133 | - 'id' => get_rest_url_by_path( \sprintf( 'actors/%d/collections/tags', $user_id ) ), | |
| 140 | + 'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/tags', $user->get__id() ) ), | |
| 134 | 141 | 'type' => 'Collection', |
| 135 | 142 | 'totalItems' => \is_countable( $tags ) ? \count( $tags ) : 0, |
| 136 | 143 | 'items' => array(), |
| 137 | 144 | ); |
| @@ -138,9 +145,9 @@ | ||
| 138 | 145 | |
| 139 | 146 | foreach ( $tags as $tag ) { |
| 140 | 147 | $response['items'][] = array( |
| 141 | 148 | 'type' => 'Hashtag', |
| 142 | - 'href' => \esc_url_raw( \get_tag_link( $tag ) ), | |
| 149 | + 'href' => \esc_url( \get_tag_link( $tag ) ), | |
| 143 | 150 | 'name' => esc_hashtag( $tag->name ), |
| 144 | 151 | ); |
| 145 | 152 | } |
| 146 | 153 | |
| @@ -149,20 +156,20 @@ | ||
| 149 | 156 | |
| 150 | 157 | /** |
| 151 | 158 | * Retrieves a collection of featured posts. |
| 152 | 159 | * |
| 153 | - * @param \WP_REST_Request $request The request object. | |
| 154 | - * @param int $user_id Actor ID. | |
| 160 | + * @param \WP_REST_Request $request The request object. | |
| 161 | + * @param User|Blog|Application $user Actor. | |
| 155 | 162 | * |
| 156 | 163 | * @return array Collection of featured posts. |
| 157 | 164 | */ |
| 158 | - public function get_featured( $request, $user_id ) { | |
| 165 | + public function get_featured( $request, $user ) { | |
| 159 | 166 | $posts = array(); |
| 160 | 167 | |
| 161 | - if ( is_single_user() || Actors::BLOG_USER_ID !== $user_id ) { | |
| 168 | + if ( is_single_user() || Actors::BLOG_USER_ID !== $user->get__id() ) { | |
| 162 | 169 | $sticky_posts = \get_option( 'sticky_posts' ); |
| 163 | 170 | |
| 164 | - if ( $sticky_posts && \is_array( $sticky_posts ) ) { | |
| 171 | + if ( $sticky_posts && is_array( $sticky_posts ) ) { | |
| 165 | 172 | // Only show public posts. |
| 166 | 173 | $args = array( |
| 167 | 174 | 'post__in' => $sticky_posts, |
| 168 | 175 | 'ignore_sticky_posts' => 1, |
| @@ -176,10 +183,10 @@ | ||
| 176 | 183 | ), |
| 177 | 184 | ), |
| 178 | 185 | ); |
| 179 | 186 | |
| 180 | - if ( $user_id > 0 ) { | |
| 181 | - $args['author'] = $user_id; | |
| 187 | + if ( $user->get__id() > 0 ) { | |
| 188 | + $args['author'] = $user->get__id(); | |
| 182 | 189 | } |
| 183 | 190 | |
| 184 | 191 | $posts = \get_posts( $args ); |
| 185 | 192 | } |
| @@ -186,9 +193,9 @@ | ||
| 186 | 193 | } |
| 187 | 194 | |
| 188 | 195 | $response = array( |
| 189 | 196 | '@context' => Base_Object::JSON_LD_CONTEXT, |
| 190 | - 'id' => get_rest_url_by_path( \sprintf( 'actors/%d/collections/featured', $user_id ) ), | |
| 197 | + 'id' => get_rest_url_by_path( sprintf( 'actors/%d/collections/featured', $request->get_param( 'user_id' ) ) ), | |
| 191 | 198 | 'type' => 'OrderedCollection', |
| 192 | 199 | 'totalItems' => \is_countable( $posts ) ? \count( $posts ) : 0, |
| 193 | 200 | 'orderedItems' => array(), |
| 194 | 201 | ); |