Skip to content

Commit

Permalink
Top Posts & Pages Endpoint: Remove Unnecessary Parameters (#34614)
Browse files Browse the repository at this point in the history
* Tidy up Top Posts endpoint

* Fix fatal in Block Editor

* Add changelog

* Address review
  • Loading branch information
Aurorum authored Feb 4, 2024
1 parent 271bd23 commit a9797ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Jetpack_Top_Posts_Helper {
* Returns user's top posts.
*
* @param int $period Period of days to draw stats from.
* @param int $items_count Number of items to display.
* @param string $types Content types to include.
* @param int $items_count Optional. Number of items to display.
* @param string $types Optional. Content types to include.
* @return array
*/
public static function get_top_posts( $period, $items_count, $types ) {
public static function get_top_posts( $period, $items_count = null, $types = null ) {
$all_time_days = floor( ( time() - strtotime( get_option( 'site_created_date' ) ) ) / ( 60 * 60 * 24 * 365 ) );

// While we only display ten posts, users can filter out content types.
Expand All @@ -28,7 +28,7 @@ public static function get_top_posts( $period, $items_count, $types ) {

// We should not override cache when displaying the block on the frontend.
// But we should allow instant preview of changes when editing the block.
$is_rendering_block = isset( $types );
$is_rendering_block = ! empty( $types );
$override_cache = ! $is_rendering_block;

$query_args = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function register_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_top_posts' ),
'permission_callback' => '__return_true',
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => array(
'period' => array(
'description' => __( 'Timeframe for stats.', 'jetpack' ),
Expand All @@ -57,22 +59,6 @@ public function register_routes() {
return is_numeric( $param ) || is_string( $param );
},
),
'number' => array(
'description' => __( 'Number of posts to display.', 'jetpack' ),
'type' => 'integer',
'required' => false,
'validate_callback' => function ( $param ) {
return is_numeric( $param );
},
),
'types' => array(
'description' => __( 'Types of content to include.', 'jetpack' ),
'type' => 'string',
'required' => false,
'validate_callback' => function ( $param ) {
return is_string( $param );
},
),
),
),
)
Expand Down Expand Up @@ -107,9 +93,7 @@ public function get_post_types() {
*/
public function get_top_posts( $request ) {
$period = $request->get_param( 'period' );
$number = $request->get_param( 'number' );
$types = $request->get_param( 'types' );
return Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types );
return Jetpack_Top_Posts_Helper::get_top_posts( $period );
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Top Posts & Pages block: Remove unnecessary parameters from endpoint.

0 comments on commit a9797ca

Please sign in to comment.