PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / _inc / lib / core-api / wpcom-endpoints / class-wpcom-rest-api-v2-endpoint-top-posts.php

class-wpcom-rest-api-v2-endpoint-top-posts.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at _inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-top-posts.php

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