Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
63.46% covered (warning)
63.46%
33 / 52
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Top_Posts
67.35% covered (warning)
67.35%
33 / 49
0.00% covered (danger)
0.00%
0 / 4
8.71
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 register_routes
91.67% covered (success)
91.67%
33 / 36
0.00% covered (danger)
0.00%
0 / 1
2.00
 get_post_types
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 get_top_posts
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Get post types and top posts.
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Top Posts & Pages block endpoint.
14 */
15class WPCOM_REST_API_V2_Endpoint_Top_Posts extends WP_REST_Controller {
16    /**
17     * Constructor.
18     */
19    public function __construct() {
20        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
21
22        if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) {
23            require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-top-posts-helper.php';
24        }
25    }
26
27    /**
28     * Register endpoint routes.
29     */
30    public function register_routes() {
31        register_rest_route(
32            'wpcom/v2',
33            '/post-types',
34            array(
35                array(
36                    'methods'             => WP_REST_Server::READABLE,
37                    'callback'            => array( $this, 'get_post_types' ),
38                    'permission_callback' => function () {
39                        return current_user_can( 'edit_posts' );
40                    },
41                ),
42            )
43        );
44
45        // Number of posts and selected post types are not needed in the Editor.
46        // This is to minimise requests when it can already be handled by the block.
47        register_rest_route(
48            'wpcom/v2',
49            '/top-posts',
50            array(
51                array(
52                    'methods'             => WP_REST_Server::READABLE,
53                    'callback'            => array( $this, 'get_top_posts' ),
54                    'permission_callback' => function () {
55                        return current_user_can( 'edit_posts' );
56                    },
57                    'args'                => array(
58                        'period' => array(
59                            'description'       => __( 'Timeframe for stats.', 'jetpack' ),
60                            'type'              => array( 'string', 'integer' ),
61                            'required'          => true,
62                            'validate_callback' => function ( $param ) {
63                                return is_numeric( $param ) || is_string( $param );
64                            },
65                        ),
66                    ),
67                ),
68            )
69        );
70    }
71
72    /**
73     * Get the site's post types.
74     *
75     * @return array Site's post types.
76     */
77    public function get_post_types() {
78        $post_types       = array_values( get_post_types( array( 'public' => true ) ) );
79        $post_types_array = array();
80
81        foreach ( $post_types as $type ) {
82            $post_types_array[] = array(
83                'label' => get_post_type_object( $type )->labels->name,
84                'id'    => $type,
85            );
86        }
87
88        return $post_types_array;
89    }
90
91    /**
92     * Get the site's top content.
93     *
94     * @param \WP_REST_Request $request request object.
95     *
96     * @return array Data on top posts.
97     */
98    public function get_top_posts( $request ) {
99        $period = $request->get_param( 'period' );
100        return Jetpack_Top_Posts_Helper::get_top_posts( $period );
101    }
102}
103
104wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Top_Posts' );