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