| 1 |
<?php |
| 2 |
/** |
| 3 |
* Related Posts Trait, providing the related posts functionality |
| 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\Parsely; |
| 14 |
use Parsely\Services\Content_API\Content_API_Service; |
| 15 |
use stdClass; |
| 16 |
use WP_Error; |
| 17 |
use WP_REST_Request; |
| 18 |
|
| 19 |
/** |
| 20 |
* Related Posts Trait, providing the related posts functionality. |
| 21 |
* |
| 22 |
* @since 3.17.0 |
| 23 |
*/ |
| 24 |
trait Related_Posts_Trait { |
| 25 |
/** |
| 26 |
* The itm_source of for the post URL. |
| 27 |
* |
| 28 |
* @since 3.17.0 |
| 29 |
* |
| 30 |
* @var string $itm_source The source of the item. |
| 31 |
*/ |
| 32 |
abstract protected function get_itm_source_param_args(): array; |
| 33 |
|
| 34 |
/** |
| 35 |
* Sets the itm_source value from the WP_REST_Request object. |
| 36 |
* |
| 37 |
* @since 3.17.0 |
| 38 |
* |
| 39 |
* @param WP_REST_Request $request The request object. |
| 40 |
*/ |
| 41 |
abstract protected function set_itm_source_from_request( WP_REST_Request $request ); |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the API arguments for the related posts endpoint. |
| 45 |
* |
| 46 |
* @since 3.17.0 |
| 47 |
* @since 3.24.2 Accepted the `score` sort, and bounded `limit` and `page`. |
| 48 |
* |
| 49 |
* @return array<string, mixed> |
| 50 |
*/ |
| 51 |
private function get_related_posts_param_args(): array { |
| 52 |
return array_merge( |
| 53 |
array( |
| 54 |
'sort' => array( |
| 55 |
'description' => __( 'The sort order.', 'wp-parsely' ), |
| 56 |
'type' => 'string', |
| 57 |
// `score` is what the Recommendations Block and Widget send. |
| 58 |
'enum' => array( 'score', '_score', 'pub_date' ), |
| 59 |
'required' => false, |
| 60 |
'default' => 'score', |
| 61 |
), |
| 62 |
'limit' => array( |
| 63 |
'description' => __( 'The number of related posts to return.', 'wp-parsely' ), |
| 64 |
'type' => 'integer', |
| 65 |
'required' => false, |
| 66 |
'default' => 10, |
| 67 |
'minimum' => 1, |
| 68 |
'maximum' => 100, |
| 69 |
), |
| 70 |
'pub_date_start' => array( |
| 71 |
'description' => __( 'The start of the publication date.', 'wp-parsely' ), |
| 72 |
'type' => 'string', |
| 73 |
'required' => false, |
| 74 |
), |
| 75 |
'pub_date_end' => array( |
| 76 |
'description' => __( 'The end of the publication date.', 'wp-parsely' ), |
| 77 |
'type' => 'string', |
| 78 |
'required' => false, |
| 79 |
), |
| 80 |
'page' => array( |
| 81 |
'description' => __( 'The page number.', 'wp-parsely' ), |
| 82 |
'type' => 'integer', |
| 83 |
'required' => false, |
| 84 |
'default' => 1, |
| 85 |
'minimum' => 1, |
| 86 |
), |
| 87 |
'section' => array( |
| 88 |
'description' => __( 'The section of the post.', 'wp-parsely' ), |
| 89 |
'type' => 'string', |
| 90 |
'required' => false, |
| 91 |
), |
| 92 |
'tag' => array( |
| 93 |
'description' => __( 'The tag of the post.', 'wp-parsely' ), |
| 94 |
'type' => 'string', |
| 95 |
'required' => false, |
| 96 |
), |
| 97 |
'author' => array( |
| 98 |
'description' => __( 'The author of the post.', 'wp-parsely' ), |
| 99 |
'type' => 'string', |
| 100 |
'required' => false, |
| 101 |
), |
| 102 |
), |
| 103 |
$this->get_itm_source_param_args() |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get related posts for a given URL. |
| 109 |
* |
| 110 |
* @since 3.17.0 |
| 111 |
* @since 3.24.2 Skips upstream items without a URL, and replaces non-string fields with empty strings. |
| 112 |
* |
| 113 |
* @param WP_REST_Request $request The request object. |
| 114 |
* @param string $url The URL to get related posts for. |
| 115 |
* @return array<mixed>|WP_Error |
| 116 |
*/ |
| 117 |
public function get_related_posts_of_url( WP_REST_Request $request, string $url ) { |
| 118 |
// Set the itm_source parameter. |
| 119 |
$this->set_itm_source_from_request( $request ); |
| 120 |
|
| 121 |
/** |
| 122 |
* The raw related posts data, received by the API. |
| 123 |
* |
| 124 |
* @var array<mixed>|WP_Error $related_posts_request |
| 125 |
*/ |
| 126 |
$related_posts_request = $this->content_api->get_related_posts_with_url( |
| 127 |
$url, |
| 128 |
array( |
| 129 |
'url' => $url, |
| 130 |
'sort' => $request->get_param( 'sort' ), |
| 131 |
'limit' => $request->get_param( 'limit' ), |
| 132 |
'pub_date_start' => $request->get_param( 'pub_date_start' ), |
| 133 |
'pub_date_end' => $request->get_param( 'pub_date_end' ), |
| 134 |
'page' => $request->get_param( 'page' ), |
| 135 |
'section' => $request->get_param( 'section' ), |
| 136 |
'tag' => $request->get_param( 'tag' ), |
| 137 |
'author' => $request->get_param( 'author' ), |
| 138 |
) |
| 139 |
); |
| 140 |
|
| 141 |
if ( is_wp_error( $related_posts_request ) ) { |
| 142 |
return $related_posts_request; |
| 143 |
} |
| 144 |
|
| 145 |
$related_posts = array(); |
| 146 |
|
| 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 |
} |
| 160 |
|
| 161 |
return $related_posts; |
| 162 |
} |
| 163 |
} |
| 164 |
|