business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
2 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
4 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
117 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get post types and top posts. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Top Posts & Pages block endpoint. |
| 10 | */ |
| 11 | class WPCOM_REST_API_V2_Endpoint_Top_Posts extends WP_REST_Controller { |
| 12 | /** |
| 13 | * Constructor. |
| 14 | */ |
| 15 | public function __construct() { |
| 16 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 17 | |
| 18 | if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) { |
| 19 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-top-posts-helper.php'; |
| 20 | } |
| 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' => '__return_true', |
| 51 | 'args' => array( |
| 52 | 'period' => array( |
| 53 | 'description' => __( 'Timeframe for stats.', 'jetpack' ), |
| 54 | 'type' => array( 'string', 'integer' ), |
| 55 | 'required' => true, |
| 56 | 'validate_callback' => function ( $param ) { |
| 57 | return is_numeric( $param ) || is_string( $param ); |
| 58 | }, |
| 59 | ), |
| 60 | 'number' => array( |
| 61 | 'description' => __( 'Number of posts to display.', 'jetpack' ), |
| 62 | 'type' => 'integer', |
| 63 | 'required' => false, |
| 64 | 'validate_callback' => function ( $param ) { |
| 65 | return is_numeric( $param ); |
| 66 | }, |
| 67 | ), |
| 68 | 'types' => array( |
| 69 | 'description' => __( 'Types of content to include.', 'jetpack' ), |
| 70 | 'type' => 'string', |
| 71 | 'required' => false, |
| 72 | 'validate_callback' => function ( $param ) { |
| 73 | return is_string( $param ); |
| 74 | }, |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the site's post types. |
| 84 | * |
| 85 | * @return array Site's post types. |
| 86 | */ |
| 87 | public function get_post_types() { |
| 88 | $post_types = array_values( get_post_types( array( 'public' => true ) ) ); |
| 89 | $post_types_array = array(); |
| 90 | |
| 91 | foreach ( $post_types as $type ) { |
| 92 | $post_types_array[] = array( |
| 93 | 'label' => get_post_type_object( $type )->labels->name, |
| 94 | 'id' => $type, |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | return $post_types_array; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the site's top content. |
| 103 | * |
| 104 | * @param \WP_REST_Request $request request object. |
| 105 | * |
| 106 | * @return array Data on top posts. |
| 107 | */ |
| 108 | public function get_top_posts( $request ) { |
| 109 | $period = $request->get_param( 'period' ); |
| 110 | $number = $request->get_param( 'number' ); |
| 111 | $types = $request->get_param( 'types' ); |
| 112 | return Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Top_Posts' ); |
| 117 |