| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post data trait, that provides methods to handle post data and the itm_source |
| 4 |
* parameter |
| 5 |
* |
| 6 |
* @package Parsely |
| 7 |
* @since 3.17.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace Parsely\REST_API\Stats; |
| 13 |
|
| 14 |
use Parsely\Parsely; |
| 15 |
use Parsely\Utils\Utils; |
| 16 |
use stdClass; |
| 17 |
use WP_REST_Request; |
| 18 |
|
| 19 |
/** |
| 20 |
* Post data trait, that provides methods to handle post data and the itm_source |
| 21 |
* parameter. |
| 22 |
* |
| 23 |
* @since 3.17.0 |
| 24 |
*/ |
| 25 |
trait Post_Data_Trait { |
| 26 |
/** |
| 27 |
* The itm_source of the post's URL. |
| 28 |
* |
| 29 |
* @since 3.17.0 |
| 30 |
* |
| 31 |
* @var string $itm_source |
| 32 |
*/ |
| 33 |
private $itm_source; |
| 34 |
|
| 35 |
/** |
| 36 |
* Sets the itm_source value. |
| 37 |
* |
| 38 |
* @since 3.17.0 |
| 39 |
* |
| 40 |
* @param string $source The source of the item. |
| 41 |
*/ |
| 42 |
private function set_itm_source( string $source ): void { |
| 43 |
$this->itm_source = $source; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Sets the itm_source value from the request, if it exists. |
| 48 |
* |
| 49 |
* @since 3.17.0 |
| 50 |
* |
| 51 |
* @param WP_REST_Request $request The request object. |
| 52 |
*/ |
| 53 |
private function set_itm_source_from_request( WP_REST_Request $request ): void { |
| 54 |
$source = $request->get_param( 'itm_source' ); |
| 55 |
if ( null !== $source ) { |
| 56 |
$this->set_itm_source( $source ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the itm_source parameter arguments, to be used in the REST API |
| 62 |
* route registration. |
| 63 |
* |
| 64 |
* @since 3.17.0 |
| 65 |
* |
| 66 |
* @return array<string, mixed> |
| 67 |
*/ |
| 68 |
private function get_itm_source_param_args(): array { |
| 69 |
return array( |
| 70 |
'itm_source' => array( |
| 71 |
'description' => __( 'The source of the item.', 'wp-parsely' ), |
| 72 |
'type' => 'string', |
| 73 |
'required' => false, |
| 74 |
), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Extracts the post data from the passed object. |
| 80 |
* |
| 81 |
* Should only be used with endpoints that return post data. |
| 82 |
* |
| 83 |
* @since 3.10.0 |
| 84 |
* @since 3.17.0 Moved from the `Base_API_Proxy` class. |
| 85 |
* |
| 86 |
* @param array<mixed> $item The object to extract the data from. |
| 87 |
* @return array<string, mixed> The extracted data. |
| 88 |
*/ |
| 89 |
protected function extract_post_data( array $item ): array { |
| 90 |
$data = array(); |
| 91 |
|
| 92 |
if ( isset( $item['author'] ) ) { |
| 93 |
$data['author'] = $item['author']; |
| 94 |
} |
| 95 |
|
| 96 |
if ( isset( $item['metrics']['views'] ) ) { |
| 97 |
$data['views'] = number_format_i18n( $item['metrics']['views'] ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $item['metrics']['visitors'] ) ) { |
| 101 |
$data['visitors'] = number_format_i18n( $item['metrics']['visitors'] ); |
| 102 |
} |
| 103 |
|
| 104 |
// The avg_engaged metric can be in different locations depending on the |
| 105 |
// endpoint and passed sort/url parameters. |
| 106 |
$avg_engaged = $item['metrics']['avg_engaged'] ?? $item['avg_engaged'] ?? null; |
| 107 |
if ( null !== $avg_engaged ) { |
| 108 |
$data['avgEngaged'] = Utils::get_formatted_duration( (float) $avg_engaged ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $item['pub_date'] ) ) { |
| 112 |
$data['date'] = wp_date( Utils::get_date_format(), strtotime( $item['pub_date'] ) ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( isset( $item['title'] ) ) { |
| 116 |
$data['title'] = $item['title']; |
| 117 |
} |
| 118 |
|
| 119 |
if ( isset( $item['url'] ) ) { |
| 120 |
$site_id = $this->parsely->get_site_id(); |
| 121 |
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.url_to_postid_url_to_postid |
| 122 |
$post_id = url_to_postid( $item['url'] ); // 0 if the post cannot be found. |
| 123 |
|
| 124 |
$post_url = Parsely::get_url_with_itm_source( $item['url'], null ); |
| 125 |
if ( Utils::parsely_is_https_supported() ) { |
| 126 |
$post_url = str_replace( 'http://', 'https://', $post_url ); |
| 127 |
} |
| 128 |
|
| 129 |
$data['rawUrl'] = $post_url; |
| 130 |
$data['dashUrl'] = Parsely::get_dash_url( $site_id, $post_url ); |
| 131 |
$data['id'] = Parsely::get_url_with_itm_source( $post_url, null ); // Unique. |
| 132 |
$data['postId'] = $post_id; // Might not be unique. |
| 133 |
$data['url'] = Parsely::get_url_with_itm_source( $post_url, $this->itm_source ); |
| 134 |
|
| 135 |
// Set thumbnail URL, falling back to the Parse.ly thumbnail if needed. |
| 136 |
$thumbnail_url = get_the_post_thumbnail_url( $post_id, 'thumbnail' ); |
| 137 |
if ( false !== $thumbnail_url ) { |
| 138 |
$data['thumbnailUrl'] = $thumbnail_url; |
| 139 |
} elseif ( isset( $item['thumb_url_medium'] ) ) { |
| 140 |
$data['thumbnailUrl'] = $item['thumb_url_medium']; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $data; |
| 145 |
} |
| 146 |
} |
| 147 |
|