| @@ -82,13 +82,13 @@ | ||
| 82 | 82 | $this->content_api = $this->parsely->get_content_api(); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
| 86 | - * Returns the endpoint name. | |
| 86 | + * Returns the endpoint's name. | |
| 87 | 87 | * |
| 88 | 88 | * @since 3.17.0 |
| 89 | 89 | * |
| 90 | - * @return string The endpoint name. | |
| 90 | + * @return string The endpoint's name. | |
| 91 | 91 | */ |
| 92 | 92 | public static function get_endpoint_name(): string { |
| 93 | 93 | return 'post'; |
| 94 | 94 | } |
| @@ -174,18 +174,26 @@ | ||
| 174 | 174 | * @param WP_REST_Request $request The request object. |
| 175 | 175 | * @return WP_REST_Response|WP_Error The response object. |
| 176 | 176 | */ |
| 177 | 177 | public function get_post_details( WP_REST_Request $request ) { |
| 178 | - /** | |
| 179 | - * The post object. | |
| 180 | - * | |
| 181 | - * @var WP_Post $post | |
| 182 | - */ | |
| 183 | - $post = $request->get_param( 'post' ); | |
| 178 | + $post = $this->get_request_post( $request ); | |
| 179 | + | |
| 180 | + if ( ! $post instanceof WP_Post ) { | |
| 181 | + return new WP_Error( | |
| 182 | + 'invalid_post', | |
| 183 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 184 | + array( 'status' => 404 ) | |
| 185 | + ); | |
| 186 | + } | |
| 187 | + | |
| 184 | 188 | $permalink = get_permalink( $post->ID ); |
| 185 | 189 | |
| 186 | 190 | if ( ! is_string( $permalink ) ) { |
| 187 | - return new WP_Error( 'invalid_post', __( 'Invalid post.', 'wp-parsely' ), array( 'status' => 404 ) ); | |
| 191 | + return new WP_Error( | |
| 192 | + 'invalid_post', | |
| 193 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 194 | + array( 'status' => 404 ) | |
| 195 | + ); | |
| 188 | 196 | } |
| 189 | 197 | |
| 190 | 198 | // Set the itm_source parameter. |
| 191 | 199 | $this->set_itm_source_from_request( $request ); |
| @@ -211,12 +219,9 @@ | ||
| 211 | 219 | foreach ( $analytics_request as $data ) { |
| 212 | 220 | $post_data[] = $this->extract_post_data( $data ); |
| 213 | 221 | } |
| 214 | 222 | |
| 215 | - $response_data = array( | |
| 216 | - 'params' => $request->get_params(), | |
| 217 | - 'data' => $post_data, | |
| 218 | - ); | |
| 223 | + $response_data = array( 'data' => $post_data ); | |
| 219 | 224 | |
| 220 | 225 | return new WP_REST_Response( $response_data, 200 ); |
| 221 | 226 | } |
| 222 | 227 | |
| @@ -230,18 +235,26 @@ | ||
| 230 | 235 | * @param WP_REST_Request $request The request object. |
| 231 | 236 | * @return WP_REST_Response|WP_Error The response object. |
| 232 | 237 | */ |
| 233 | 238 | public function get_post_referrers( WP_REST_Request $request ) { |
| 234 | - /** | |
| 235 | - * The post object. | |
| 236 | - * | |
| 237 | - * @var WP_Post $post | |
| 238 | - */ | |
| 239 | - $post = $request->get_param( 'post' ); | |
| 239 | + $post = $this->get_request_post( $request ); | |
| 240 | + | |
| 241 | + if ( ! $post instanceof WP_Post ) { | |
| 242 | + return new WP_Error( | |
| 243 | + 'invalid_post', | |
| 244 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 245 | + array( 'status' => 404 ) | |
| 246 | + ); | |
| 247 | + } | |
| 248 | + | |
| 240 | 249 | $permalink = get_permalink( $post->ID ); |
| 241 | 250 | |
| 242 | 251 | if ( ! is_string( $permalink ) ) { |
| 243 | - return new WP_Error( 'invalid_post', __( 'Invalid post.', 'wp-parsely' ), array( 'status' => 404 ) ); | |
| 252 | + return new WP_Error( | |
| 253 | + 'invalid_post', | |
| 254 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 255 | + array( 'status' => 404 ) | |
| 256 | + ); | |
| 244 | 257 | } |
| 245 | 258 | |
| 246 | 259 | // Set the itm_source parameter. |
| 247 | 260 | $this->set_itm_source_from_request( $request ); |
| @@ -277,10 +290,9 @@ | ||
| 277 | 290 | ); |
| 278 | 291 | $referrers_top = $this->generate_referrers_data( 5, $analytics_request, $direct_views ); |
| 279 | 292 | |
| 280 | 293 | $response_data = array( |
| 281 | - 'params' => $request->get_params(), | |
| 282 | - 'data' => array( | |
| 294 | + 'data' => array( | |
| 283 | 295 | 'top' => $referrers_top, |
| 284 | 296 | 'types' => $referrers_types, |
| 285 | 297 | ), |
| 286 | 298 | ); |
| @@ -298,30 +310,35 @@ | ||
| 298 | 310 | * @param WP_REST_Request $request The request object. |
| 299 | 311 | * @return WP_REST_Response|WP_Error The response data. |
| 300 | 312 | */ |
| 301 | 313 | public function get_related_posts( WP_REST_Request $request ) { |
| 302 | - /** | |
| 303 | - * The post object. | |
| 304 | - * | |
| 305 | - * @var WP_Post $post | |
| 306 | - */ | |
| 307 | - $post = $request->get_param( 'post' ); | |
| 314 | + $post = $this->get_request_post( $request ); | |
| 308 | 315 | |
| 309 | - /** | |
| 310 | - * The post permalink. | |
| 311 | - * | |
| 312 | - * @var string $permalink | |
| 313 | - */ | |
| 316 | + if ( ! $post instanceof WP_Post ) { | |
| 317 | + return new WP_Error( | |
| 318 | + 'invalid_post', | |
| 319 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 320 | + array( 'status' => 404 ) | |
| 321 | + ); | |
| 322 | + } | |
| 323 | + | |
| 314 | 324 | $permalink = get_permalink( $post->ID ); |
| 315 | 325 | |
| 326 | + if ( ! is_string( $permalink ) ) { | |
| 327 | + return new WP_Error( | |
| 328 | + 'invalid_post', | |
| 329 | + __( 'Invalid post.', 'wp-parsely' ), | |
| 330 | + array( 'status' => 404 ) | |
| 331 | + ); | |
| 332 | + } | |
| 333 | + | |
| 316 | 334 | $related_posts = $this->get_related_posts_of_url( $request, $permalink ); |
| 317 | 335 | |
| 318 | - $response_data = array( | |
| 319 | - 'params' => $request->get_params(), | |
| 320 | - 'data' => $related_posts, | |
| 321 | - ); | |
| 336 | + if ( is_wp_error( $related_posts ) ) { | |
| 337 | + return $related_posts; | |
| 338 | + } | |
| 322 | 339 | |
| 323 | - return new WP_REST_Response( $response_data, 200 ); | |
| 340 | + return new WP_REST_Response( array( 'data' => $related_posts ), 200 ); | |
| 324 | 341 | } |
| 325 | 342 | |
| 326 | 343 | /** |
| 327 | 344 | * Generates the referrer types data. |