PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.2
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 / RemoteAPI / class-analytics-posts-api.php

class-analytics-posts-api.php in Parse.ly 3.16.2, at src/RemoteAPI/class-analytics-posts-api.php

108 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for Analytics Posts API (`/analytics/posts`).
4 *
5 * @package Parsely
6 * @since 3.4.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\RemoteAPI;
12
13 use Parsely\Endpoints\Base_Endpoint;
14 use Parsely\Parsely;
15 use WP_Error;
16 use WP_REST_Request;
17
18 /**
19 * Class for Analytics Posts API (`/analytics/posts`).
20 *
21 * @since 3.4.0
22 *
23 * @phpstan-type Analytics_Post_API_Params array{
24 * apikey?: string,
25 * secret?: string,
26 * period_start?: string,
27 * period_end?: string,
28 * pub_date_start?: string,
29 * pub_date_end?: string,
30 * sort?: string,
31 * limit?: int<0, 2000>,
32 * }
33 *
34 * @phpstan-type Analytics_Post array{
35 * title?: string,
36 * url?: string,
37 * link?: string,
38 * author?: string,
39 * authors?: string[],
40 * section?: string,
41 * tags?: string[],
42 * metrics?: Analytics_Post_Metrics,
43 * full_content_word_count?: int,
44 * image_url?: string,
45 * metadata?: string,
46 * pub_date?: string,
47 * thumb_url_medium?: string,
48 * }
49 *
50 * @phpstan-type Analytics_Post_Metrics array{
51 * avg_engaged?: float,
52 * views?: int,
53 * visitors?: int,
54 * }
55 */
56 class Analytics_Posts_API extends Base_Endpoint_Remote {
57 public const MAX_RECORDS_LIMIT = 2000;
58 public const ANALYTICS_API_DAYS_LIMIT = 7;
59
60 protected const API_BASE_URL = Parsely::PUBLIC_API_BASE_URL;
61 protected const ENDPOINT = '/analytics/posts';
62 protected const QUERY_FILTER = 'wp_parsely_analytics_posts_endpoint_args';
63
64 /**
65 * Returns whether the endpoint is available for access by the current
66 * user.
67 *
68 * @since 3.14.0
69 * @since 3.16.0 Added the `$request` parameter.
70 *
71 * @param WP_REST_Request|null $request The request object.
72 * @return bool
73 */
74 public function is_available_to_current_user( $request = null ): bool {
75 return current_user_can(
76 // phpcs:ignore WordPress.WP.Capabilities.Undetermined
77 $this->apply_capability_filters(
78 Base_Endpoint::DEFAULT_ACCESS_CAPABILITY
79 )
80 );
81 }
82
83 /**
84 * Calls Parse.ly Analytics API to get posts info.
85 *
86 * Main purpose of this function is to enforce typing.
87 *
88 * @param Analytics_Post_API_Params $api_params Parameters of the API.
89 * @return Analytics_Post[]|WP_Error|null
90 */
91 public function get_posts_analytics( $api_params ) {
92 return $this->get_items( $api_params, true ); // @phpstan-ignore-line
93 }
94
95 /**
96 * Returns the request's options for the remote API call.
97 *
98 * @since 3.9.0
99 *
100 * @return array<string, mixed> The array of options.
101 */
102 public function get_request_options(): array {
103 return array(
104 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
105 );
106 }
107 }
108