← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-top-posts.php
+104
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,104 @@ | ||
| 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' ); | |