Skip to content

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vaurdan committed Oct 18, 2024
1 parent e103011 commit ccaa940
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/content-helper/post-list-stats/class-post-list-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ public function get_parsely_stats_response(): ?array {

$response = $this->content_api->get_posts(
array(
'period_start' => 'max_days',
'period_start' => Endpoint_Analytics_Posts::MAX_PERIOD,
'pub_date_start' => $date_params['pub_date_start'] ?? '',
'pub_date_end' => $date_params['pub_date_end'] ?? '',
'limit' => 'max',
'limit' => Endpoint_Analytics_Posts::MAX_LIMIT,
'sort' => 'avg_engaged', // Note: API sends different stats on different sort options.
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public function generate_excerpt( WP_REST_Request $request ) {
return $response;
}

// TODO: For now, only return the first suggestion. When the UI is ready to handle multiple suggestions, we can
// TODO: return the entire array.
// TODO: For now, only return the first suggestion. When the UI is ready to handle multiple suggestions, we can return the entire array.
$response = $response[0] ?? '';
return new WP_REST_Response( array( 'data' => $response ), 200 );
}
Expand Down
4 changes: 3 additions & 1 deletion src/rest-api/stats/class-endpoint-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use WP_REST_Request;
use WP_REST_Response;
use stdClass;
use const _PHPStan_4f7beffdf\__;

/**
* The Stats API Posts endpoint.
Expand Down Expand Up @@ -210,7 +211,7 @@ public function validate_max_length_is_5( $string_or_array ) {
}

if ( count( $string_or_array ) > 5 ) {
return new WP_Error( 'invalid_param', 'The parameter must have at most 5 items.' );
return new WP_Error( 'invalid_param', __( 'The parameter must have at most 5 items.', 'wp-parsely' ) );
}

return true;
Expand Down Expand Up @@ -260,6 +261,7 @@ public function get_posts( WP_REST_Request $request ) {

// Process the data.
$posts = array();

/**
* The analytics data object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,27 @@
* }
*/
class Endpoint_Analytics_Posts extends Content_API_Base_Endpoint {
public const MAX_RECORDS_LIMIT = 2000;
public const ANALYTICS_API_DAYS_LIMIT = 7;
private const MAX_RECORDS_LIMIT = 2000;
private const ANALYTICS_API_DAYS_LIMIT = 7;

/**
* Maximum limit for the number of records to return, to be
* used in the `limit` parameter.
*
* @since 3.17.0
*
* @var string
*/
public const MAX_LIMIT = 'max';

/**
* Maximum period for the API request, to be used in the `period_start` parameter.
*
* @since 3.17.0
*
* @var string
*/
public const MAX_PERIOD = 'max_days';

/**
* Returns the endpoint for the API request.
Expand Down Expand Up @@ -117,13 +136,13 @@ public function call( array $args = array() ) {
$query_args = array_filter( $args );

// If the period_start is set to 'max_days', set it to the maximum days limit.
if ( 'max_days' === $query_args['period_start'] ) {
if ( self::MAX_PERIOD === $query_args['period_start'] ) {
$query_args['period_start'] = self::ANALYTICS_API_DAYS_LIMIT . 'd';
}

// If the limit is set to 'max' or greater than the maximum records limit,
// set it to the maximum records limit.
if ( 'max' === $query_args['limit'] || $query_args['limit'] > self::MAX_RECORDS_LIMIT ) {
if ( self::MAX_LIMIT === $query_args['limit'] || $query_args['limit'] > self::MAX_RECORDS_LIMIT ) {
$query_args['limit'] = self::MAX_RECORDS_LIMIT;
}

Expand Down

0 comments on commit ccaa940

Please sign in to comment.