| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoints: Parse.ly `/analytics/posts` API proxy endpoint class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.4.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Endpoints; |
| 12 |
|
| 13 |
use stdClass; |
| 14 |
use WP_REST_Request; |
| 15 |
|
| 16 |
/** |
| 17 |
* Configures the `/analytics/posts` REST API endpoint. |
| 18 |
*/ |
| 19 |
final class Analytics_Posts_API_Proxy extends Base_API_Proxy { |
| 20 |
|
| 21 |
/** |
| 22 |
* Registers the endpoint's WP REST route. |
| 23 |
*/ |
| 24 |
public function run(): void { |
| 25 |
$this->register_endpoint( '/analytics/posts' ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Cached "proxy" to the Parse.ly `/analytics/posts` API endpoint. |
| 30 |
* |
| 31 |
* @param WP_REST_Request $request The request object. |
| 32 |
*/ |
| 33 |
public function get_items( WP_REST_Request $request ): stdClass { |
| 34 |
return $this->get_data( $request ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Generates the final data from the passed response. |
| 39 |
* |
| 40 |
* @param array<string, mixed> $response The response received by the proxy. |
| 41 |
* @return array<stdClass> The generated data. |
| 42 |
*/ |
| 43 |
protected function generate_data( array $response ): array { |
| 44 |
$date_format = get_option( 'date_format' ); |
| 45 |
$result = array_map( |
| 46 |
static function( stdClass $item ) use ( $date_format ) { |
| 47 |
return (object) array( |
| 48 |
'author' => $item->author, |
| 49 |
'date' => wp_date( $date_format, strtotime( $item->pub_date ) ), |
| 50 |
'id' => $item->url, |
| 51 |
'title' => $item->title, |
| 52 |
'url' => $item->url, |
| 53 |
'views' => $item->metrics->views, |
| 54 |
); |
| 55 |
}, |
| 56 |
$response |
| 57 |
); |
| 58 |
|
| 59 |
return $result; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Determines if there are enough permissions to call the endpoint. |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function permission_callback(): bool { |
| 68 |
// Unauthenticated. |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
|