| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoints: Parse.ly `/analytics/post/detail` API proxy endpoint class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.6.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 `/stats/post/detail` REST API endpoint. |
| 18 |
*/ |
| 19 |
final class Analytics_Post_Detail_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( '/stats/post/detail' ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Cached "proxy" to the Parse.ly `/analytics/post/detail` API endpoint. |
| 30 |
* |
| 31 |
* @param WP_REST_Request $request The request object. |
| 32 |
* @return stdClass|WPError stdClass containing the data or a WP_Error |
| 33 |
* object on failure. |
| 34 |
*/ |
| 35 |
public function get_items( WP_REST_Request $request ) { |
| 36 |
return $this->get_data( $request ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Generates the final data from the passed response. |
| 41 |
* |
| 42 |
* @param array<string, mixed> $response The response received by the proxy. |
| 43 |
* @return array<stdClass> The generated data. |
| 44 |
*/ |
| 45 |
protected function generate_data( array $response ): array { |
| 46 |
$stats_base_url = trailingslashit( 'https://dash.parsely.com/' . $this->parsely->get_api_key() ) . 'find'; |
| 47 |
|
| 48 |
$result = array_map( |
| 49 |
static function( stdClass $item ) use ( $stats_base_url ) { |
| 50 |
return (object) array( |
| 51 |
'avgEngaged' => self::get_duration( (float) $item->avg_engaged ), |
| 52 |
'statsUrl' => $stats_base_url . '?url=' . rawurlencode( $item->url ), |
| 53 |
'url' => $item->url, |
| 54 |
'views' => number_format_i18n( $item->metrics->views ), |
| 55 |
'visitors' => number_format_i18n( $item->metrics->visitors ), |
| 56 |
); |
| 57 |
}, |
| 58 |
$response |
| 59 |
); |
| 60 |
|
| 61 |
return $result; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the passed float as a time duration in m:ss format. |
| 66 |
* |
| 67 |
* Examples: |
| 68 |
* - $time of 1.005 yields '1:00'. |
| 69 |
* - $time of 1.5 yields '1:30'. |
| 70 |
* - $time of 1.999 yields '2:00'. |
| 71 |
* |
| 72 |
* @since 3.6.0 |
| 73 |
* |
| 74 |
* @param float $time The time as a float number. |
| 75 |
* @return string The resulting formatted time duration. |
| 76 |
*/ |
| 77 |
private function get_duration( float $time ): string { |
| 78 |
$minutes = absint( $time ); |
| 79 |
$seconds = absint( round( fmod( $time, 1 ) * 60 ) ); |
| 80 |
|
| 81 |
if ( 60 === $seconds ) { |
| 82 |
$minutes++; |
| 83 |
$seconds = 0; |
| 84 |
} |
| 85 |
|
| 86 |
return sprintf( '%2d:%02d', $minutes, $seconds ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Determines if there are enough permissions to call the endpoint. |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
public function permission_callback(): bool { |
| 95 |
// Unauthenticated. |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
|