| @@ -48,9 +48,9 @@ | ||
| 48 | 48 | $this->content_api = $this->parsely->get_content_api(); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | - * Returns the endpoint name. | |
| 52 | + * Returns the endpoint's name. | |
| 53 | 53 | * |
| 54 | 54 | * @since 3.17.0 |
| 55 | 55 | * |
| 56 | 56 | * @return string |
| @@ -62,8 +62,9 @@ | ||
| 62 | 62 | /** |
| 63 | 63 | * Registers the routes for the endpoint. |
| 64 | 64 | * |
| 65 | 65 | * @since 3.17.0 |
| 66 | + * @since 3.24.2 Registered the related posts arguments, and required an HTTP(S) URL with a host. | |
| 66 | 67 | */ |
| 67 | 68 | public function register_routes(): void { |
| 68 | 69 | /** |
| 69 | 70 | * GET /related |
| @@ -72,15 +73,19 @@ | ||
| 72 | 73 | $this->register_rest_route( |
| 73 | 74 | '/', |
| 74 | 75 | array( 'GET' ), |
| 75 | 76 | array( $this, 'get_related_posts' ), |
| 76 | - array( | |
| 77 | - 'url' => array( | |
| 78 | - 'description' => __( 'The URL of the post.', 'wp-parsely' ), | |
| 79 | - 'type' => 'string', | |
| 80 | - 'required' => true, | |
| 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 | + ), | |
| 81 | 86 | ), |
| 82 | - $this->get_related_posts_param_args(), | |
| 87 | + $this->get_related_posts_param_args() | |
| 83 | 88 | ) |
| 84 | 89 | ); |
| 85 | 90 | } |
| 86 | 91 | |
| @@ -103,8 +108,24 @@ | ||
| 103 | 108 | return $related_posts; |
| 104 | 109 | } |
| 105 | 110 | |
| 106 | 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; | |
| 107 | 128 | } |
| 108 | 129 | |
| 109 | 130 | /** |
| 110 | 131 | * Returns whether the endpoint is available for access by the current |