| 1 |
<?php |
| 2 |
/** |
| 3 |
* Stats API Endpoint: Related Posts |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.17.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\REST_API\Stats; |
| 12 |
|
| 13 |
use Parsely\REST_API\Base_Endpoint; |
| 14 |
use Parsely\Services\Content_API\Content_API_Service; |
| 15 |
use WP_Error; |
| 16 |
use WP_REST_Request; |
| 17 |
use WP_REST_Response; |
| 18 |
|
| 19 |
/** |
| 20 |
* The Stats API Related Posts endpoint. |
| 21 |
* |
| 22 |
* Provides an endpoint for retrieving related posts to a given URL. |
| 23 |
* |
| 24 |
* @since 3.17.0 |
| 25 |
*/ |
| 26 |
class Endpoint_Related extends Base_Endpoint { |
| 27 |
use Post_Data_Trait; |
| 28 |
use Related_Posts_Trait; |
| 29 |
|
| 30 |
/** |
| 31 |
* The Parse.ly Content API service. |
| 32 |
* |
| 33 |
* @since 3.17.0 |
| 34 |
* |
| 35 |
* @var Content_API_Service $content_api |
| 36 |
*/ |
| 37 |
public $content_api; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
* |
| 42 |
* @since 3.17.0 |
| 43 |
* |
| 44 |
* @param Stats_Controller $controller The controller. |
| 45 |
*/ |
| 46 |
public function __construct( Stats_Controller $controller ) { |
| 47 |
parent::__construct( $controller ); |
| 48 |
$this->content_api = $this->parsely->get_content_api(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the endpoint's name. |
| 53 |
* |
| 54 |
* @since 3.17.0 |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public static function get_endpoint_name(): string { |
| 59 |
return 'related'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers the routes for the endpoint. |
| 64 |
* |
| 65 |
* @since 3.17.0 |
| 66 |
* @since 3.24.2 Registered the related posts arguments, and required an HTTP(S) URL with a host. |
| 67 |
*/ |
| 68 |
public function register_routes(): void { |
| 69 |
/** |
| 70 |
* GET /related |
| 71 |
* Gets related posts. |
| 72 |
*/ |
| 73 |
$this->register_rest_route( |
| 74 |
'/', |
| 75 |
array( 'GET' ), |
| 76 |
array( $this, 'get_related_posts' ), |
| 77 |
array_merge( |
| 78 |
array( |
| 79 |
'url' => array( |
| 80 |
'description' => __( 'The URL of the post.', 'wp-parsely' ), |
| 81 |
'type' => 'string', |
| 82 |
'pattern' => '^https?://\S+$', |
| 83 |
'required' => true, |
| 84 |
'validate_callback' => array( $this, 'validate_url' ), |
| 85 |
), |
| 86 |
), |
| 87 |
$this->get_related_posts_param_args() |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* API Endpoint: GET /stats/related |
| 94 |
* |
| 95 |
* Gets related posts for a given URL. |
| 96 |
* |
| 97 |
* @since 3.17.0 |
| 98 |
* |
| 99 |
* @param WP_REST_Request $request The request object. |
| 100 |
* @return WP_REST_Response|WP_Error |
| 101 |
*/ |
| 102 |
public function get_related_posts( WP_REST_Request $request ) { |
| 103 |
$url = $request->get_param( 'url' ); |
| 104 |
|
| 105 |
$related_posts = $this->get_related_posts_of_url( $request, $url ); |
| 106 |
|
| 107 |
if ( is_wp_error( $related_posts ) ) { |
| 108 |
return $related_posts; |
| 109 |
} |
| 110 |
|
| 111 |
return new WP_REST_Response( array( 'data' => $related_posts ), 200 ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Validates that the URL has a host. The `pattern` argument checks the scheme. |
| 116 |
* |
| 117 |
* @since 3.24.2 |
| 118 |
* |
| 119 |
* @param mixed $url The raw parameter value, as validation runs before type checks. |
| 120 |
* @return true|WP_Error |
| 121 |
*/ |
| 122 |
public function validate_url( $url ) { |
| 123 |
if ( ! is_string( $url ) || '' === (string) wp_parse_url( $url, PHP_URL_HOST ) ) { |
| 124 |
return new WP_Error( 'invalid_param', __( 'The parameter must be a URL with a host.', 'wp-parsely' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns whether the endpoint is available for access by the current |
| 132 |
* user. |
| 133 |
* |
| 134 |
* @since 3.17.0 |
| 135 |
* |
| 136 |
* @param WP_REST_Request|null $request The request object. |
| 137 |
* @return bool|WP_Error |
| 138 |
*/ |
| 139 |
public function is_available_to_current_user( ?WP_REST_Request $request = null ) { |
| 140 |
return $this->validate_site_id_and_secret( false ); |
| 141 |
} |
| 142 |
} |
| 143 |
|