business-hours.php
7 months ago
class-wpcom-rest-api-v2-endpoint-admin-bar.php
2 months ago
class-wpcom-rest-api-v2-endpoint-admin-color.php
7 months ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
1 month ago
class-wpcom-rest-api-v2-endpoint-agent-guidelines-ai.php
1 week ago
class-wpcom-rest-api-v2-endpoint-ai.php
1 week ago
class-wpcom-rest-api-v2-endpoint-app-media.php
7 months ago
class-wpcom-rest-api-v2-endpoint-application-password-extras.php
3 months ago
class-wpcom-rest-api-v2-endpoint-block-editor-assets.php
2 weeks ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
7 months ago
class-wpcom-rest-api-v2-endpoint-email-preview.php
2 months ago
class-wpcom-rest-api-v2-endpoint-external-media.php
7 months ago
class-wpcom-rest-api-v2-endpoint-following.php
7 months ago
class-wpcom-rest-api-v2-endpoint-goodreads.php
7 months ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
7 months ago
class-wpcom-rest-api-v2-endpoint-guidelines-banner-dismissed.php
1 week ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
2 months ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
7 months ago
class-wpcom-rest-api-v2-endpoint-mcp-settings.php
5 days ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
7 months ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
7 months ago
class-wpcom-rest-api-v2-endpoint-newsletter-email-sent-status.php
3 months ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 months ago
class-wpcom-rest-api-v2-endpoint-profile.php
7 months ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
7 months ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
7 months ago
class-wpcom-rest-api-v2-endpoint-search.php
1 week ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
1 month ago
class-wpcom-rest-api-v2-endpoint-subscribers-list.php
2 weeks ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
7 months ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 months ago
class-wpcom-rest-api-v2-endpoint-transient.php
7 months ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
7 months ago
class-wpcom-rest-api-v3-endpoint-jitm.php
7 months ago
gutenberg-available-extensions.php
7 months ago
hello.php
7 months ago
memberships.php
2 weeks ago
publicize-connection-test-results.php
7 months ago
service-api-keys.php
1 month ago
sites-posts-featured-media-url.php
7 months ago
subscribers.php
7 months ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
105 lines
| 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 |