PluginProbe
Parse.ly / 3.6.0
Parse.ly v3.6.0
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Endpoints / class-analytics-posts-api-proxy.php

class-analytics-posts-api-proxy.php in Parse.ly 3.6.0, at src/Endpoints/class-analytics-posts-api-proxy.php

78 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use Parsely\Parsely;
16
17 /**
18 * Configures the `/stats/posts` REST API endpoint.
19 */
20 final class Analytics_Posts_API_Proxy extends Base_API_Proxy {
21
22 /**
23 * Registers the endpoint's WP REST route.
24 */
25 public function run(): void {
26 $this->register_endpoint( '/stats/posts' );
27 }
28
29 /**
30 * Cached "proxy" to the Parse.ly `/analytics/posts` API endpoint.
31 *
32 * @param WP_REST_Request $request The request object.
33 * @return stdClass|WPError stdClass containing the data or a WP_Error
34 * object on failure.
35 */
36 public function get_items( WP_REST_Request $request ) {
37 return $this->get_data( $request );
38 }
39
40 /**
41 * Generates the final data from the passed response.
42 *
43 * @param array<string, mixed> $response The response received by the proxy.
44 * @return array<stdClass> The generated data.
45 */
46 protected function generate_data( array $response ): array {
47 $date_format = get_option( 'date_format' );
48 $stats_base_url = trailingslashit( Parsely::DASHBOARD_BASE_URL . '/' . $this->parsely->get_api_key() ) . 'find';
49
50 $result = array_map(
51 static function( stdClass $item ) use ( $date_format, $stats_base_url ) {
52 return (object) array(
53 'author' => $item->author,
54 'date' => wp_date( $date_format, strtotime( $item->pub_date ) ),
55 'id' => $item->url,
56 'statsUrl' => $stats_base_url . '?url=' . rawurlencode( $item->url ),
57 'title' => $item->title,
58 'url' => $item->url,
59 'views' => $item->metrics->views,
60 );
61 },
62 $response
63 );
64
65 return $result;
66 }
67
68 /**
69 * Determines if there are enough permissions to call the endpoint.
70 *
71 * @return bool
72 */
73 public function permission_callback(): bool {
74 // Unauthenticated.
75 return true;
76 }
77 }
78