| @@ -43,8 +43,9 @@ | ||
| 43 | 43 | /** |
| 44 | 44 | * Returns the API arguments for the related posts endpoint. |
| 45 | 45 | * |
| 46 | 46 | * @since 3.17.0 |
| 47 | + * @since 3.24.2 Accepted the `score` sort, and bounded `limit` and `page`. | |
| 47 | 48 | * |
| 48 | 49 | * @return array<string, mixed> |
| 49 | 50 | */ |
| 50 | 51 | private function get_related_posts_param_args(): array { |
| @@ -52,11 +53,12 @@ | ||
| 52 | 53 | array( |
| 53 | 54 | 'sort' => array( |
| 54 | 55 | 'description' => __( 'The sort order.', 'wp-parsely' ), |
| 55 | 56 | 'type' => 'string', |
| 56 | - 'enum' => array( '_score', 'pub_date' ), | |
| 57 | + // `score` is what the Recommendations Block and Widget send. | |
| 58 | + 'enum' => array( 'score', '_score', 'pub_date' ), | |
| 57 | 59 | 'required' => false, |
| 58 | - 'default' => '_score', | |
| 60 | + 'default' => 'score', | |
| 59 | 61 | ), |
| 60 | 62 | 'limit' => array( |
| 61 | 63 | 'description' => __( 'The number of related posts to return.', 'wp-parsely' ), |
| 62 | 64 | 'type' => 'integer', |
| @@ -61,8 +63,10 @@ | ||
| 61 | 63 | 'description' => __( 'The number of related posts to return.', 'wp-parsely' ), |
| 62 | 64 | 'type' => 'integer', |
| 63 | 65 | 'required' => false, |
| 64 | 66 | 'default' => 10, |
| 67 | + 'minimum' => 1, | |
| 68 | + 'maximum' => 100, | |
| 65 | 69 | ), |
| 66 | 70 | 'pub_date_start' => array( |
| 67 | 71 | 'description' => __( 'The start of the publication date.', 'wp-parsely' ), |
| 68 | 72 | 'type' => 'string', |
| @@ -77,8 +81,9 @@ | ||
| 77 | 81 | 'description' => __( 'The page number.', 'wp-parsely' ), |
| 78 | 82 | 'type' => 'integer', |
| 79 | 83 | 'required' => false, |
| 80 | 84 | 'default' => 1, |
| 85 | + 'minimum' => 1, | |
| 81 | 86 | ), |
| 82 | 87 | 'section' => array( |
| 83 | 88 | 'description' => __( 'The section of the post.', 'wp-parsely' ), |
| 84 | 89 | 'type' => 'string', |
| @@ -102,8 +107,9 @@ | ||
| 102 | 107 | /** |
| 103 | 108 | * Get related posts for a given URL. |
| 104 | 109 | * |
| 105 | 110 | * @since 3.17.0 |
| 111 | + * @since 3.24.2 Skips upstream items without a URL, and replaces non-string fields with empty strings. | |
| 106 | 112 | * |
| 107 | 113 | * @param WP_REST_Request $request The request object. |
| 108 | 114 | * @param string $url The URL to get related posts for. |
| 109 | 115 | * @return array<mixed>|WP_Error |
| @@ -114,9 +120,9 @@ | ||
| 114 | 120 | |
| 115 | 121 | /** |
| 116 | 122 | * The raw related posts data, received by the API. |
| 117 | 123 | * |
| 118 | - * @var array<array<string, string>>|WP_Error $related_posts_request | |
| 124 | + * @var array<mixed>|WP_Error $related_posts_request | |
| 119 | 125 | */ |
| 120 | 126 | $related_posts_request = $this->content_api->get_related_posts_with_url( |
| 121 | 127 | $url, |
| 122 | 128 | array( |
| @@ -135,21 +141,23 @@ | ||
| 135 | 141 | if ( is_wp_error( $related_posts_request ) ) { |
| 136 | 142 | return $related_posts_request; |
| 137 | 143 | } |
| 138 | 144 | |
| 139 | - $itm_source = $this->itm_source; | |
| 145 | + $related_posts = array(); | |
| 140 | 146 | |
| 141 | - $related_posts = array_map( | |
| 142 | - static function ( array $item ) use ( $itm_source ) { | |
| 143 | - return array( | |
| 144 | - 'image_url' => $item['image_url'], | |
| 145 | - 'thumb_url_medium' => $item['thumb_url_medium'], | |
| 146 | - 'title' => $item['title'], | |
| 147 | - 'url' => Parsely::get_url_with_itm_source( $item['url'], $itm_source ), | |
| 148 | - ); | |
| 149 | - }, | |
| 150 | - $related_posts_request | |
| 151 | - ); | |
| 147 | + foreach ( $related_posts_request as $item ) { | |
| 148 | + // Partial upstream replies can omit any key. | |
| 149 | + if ( ! is_array( $item ) || ! isset( $item['url'] ) || ! is_string( $item['url'] ) || '' === $item['url'] ) { | |
| 150 | + continue; | |
| 151 | + } | |
| 152 | + | |
| 153 | + $related_posts[] = array( | |
| 154 | + 'image_url' => is_string( $item['image_url'] ?? null ) ? $item['image_url'] : '', | |
| 155 | + 'thumb_url_medium' => is_string( $item['thumb_url_medium'] ?? null ) ? $item['thumb_url_medium'] : '', | |
| 156 | + 'title' => is_string( $item['title'] ?? null ) ? $item['title'] : '', | |
| 157 | + 'url' => Parsely::get_url_with_itm_source( $item['url'], $this->itm_source ), | |
| 158 | + ); | |
| 159 | + } | |
| 152 | 160 | |
| 153 | 161 | return $related_posts; |
| 154 | 162 | } |
| 155 | 163 | } |