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-app-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.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-goodreads.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
2 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
84 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get blog stats. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Stats\WPCOM_Stats; |
| 9 | |
| 10 | /** |
| 11 | * Blog Stats block endpoint. |
| 12 | */ |
| 13 | class WPCOM_REST_API_V2_Endpoint_Blog_Stats extends WP_REST_Controller { |
| 14 | /** |
| 15 | * Constructor. |
| 16 | */ |
| 17 | public function __construct() { |
| 18 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Register endpoint routes. |
| 23 | */ |
| 24 | public function register_routes() { |
| 25 | register_rest_route( |
| 26 | 'wpcom/v2', |
| 27 | '/blog-stats', |
| 28 | array( |
| 29 | array( |
| 30 | 'methods' => WP_REST_Server::READABLE, |
| 31 | 'callback' => array( $this, 'get_blog_stats' ), |
| 32 | 'permission_callback' => function () { |
| 33 | return current_user_can( 'edit_posts' ); |
| 34 | }, |
| 35 | 'args' => array( |
| 36 | 'post_id' => array( |
| 37 | 'description' => __( 'Post ID to obtain stats for.', 'jetpack' ), |
| 38 | 'type' => array( 'string', 'integer' ), |
| 39 | 'required' => false, |
| 40 | 'validate_callback' => function ( $param ) { |
| 41 | return is_numeric( $param ); |
| 42 | }, |
| 43 | ), |
| 44 | ), |
| 45 | ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the blog stats. |
| 52 | * |
| 53 | * @param \WP_REST_Request $request Request object. |
| 54 | * |
| 55 | * @return array Blog stats. |
| 56 | */ |
| 57 | public function get_blog_stats( $request ) { |
| 58 | $wpcom_stats = new WPCOM_Stats(); |
| 59 | $post_id = $request->get_param( 'post_id' ); |
| 60 | $post_data = $wpcom_stats->convert_stats_array_to_object( |
| 61 | $wpcom_stats->get_post_views( $post_id, array( 'fields' => 'views' ) ) |
| 62 | ); |
| 63 | $blog_data = $wpcom_stats->convert_stats_array_to_object( |
| 64 | $wpcom_stats->get_stats( array( 'fields' => 'stats' ) ) |
| 65 | ); |
| 66 | |
| 67 | if ( ! isset( $blog_data->stats->views ) || ! isset( $blog_data->stats->visitors ) ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if ( ! isset( $post_data->views ) ) { |
| 72 | $post_data->views = 0; |
| 73 | } |
| 74 | |
| 75 | return array( |
| 76 | 'post-views' => $post_data->views, |
| 77 | 'blog-visitors' => $blog_data->stats->visitors, |
| 78 | 'blog-views' => $blog_data->stats->views, |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Blog_Stats' ); |
| 84 |