PluginProbe
ActivityPub / 8.1.1
ActivityPub v8.1.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / admin / class-statistics-controller.php

class-statistics-controller.php in ActivityPub 8.1.1, at includes/rest/admin/class-statistics-controller.php

132 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Statistics_Controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest\Admin;
9
10 use Activitypub\Collection\Actors;
11 use Activitypub\Statistics;
12
13 /**
14 * ActivityPub Statistics_Controller class.
15 *
16 * Provides REST endpoints for ActivityPub statistics.
17 */
18 class Statistics_Controller extends \WP_REST_Controller {
19
20 /**
21 * The namespace of this controller's route.
22 *
23 * @var string
24 */
25 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
26
27 /**
28 * The base of this controller's route.
29 *
30 * @var string
31 */
32 protected $rest_base = 'admin/stats';
33
34 /**
35 * Register routes.
36 */
37 public function register_routes() {
38 \register_rest_route(
39 $this->namespace,
40 '/' . $this->rest_base . '/(?P<user_id>[\d]+)',
41 array(
42 array(
43 'methods' => \WP_REST_Server::READABLE,
44 'callback' => array( $this, 'get_item' ),
45 'permission_callback' => array( $this, 'get_item_permissions_check' ),
46 'args' => array(
47 'user_id' => array(
48 'description' => 'The user ID to get stats for.',
49 'type' => 'integer',
50 'required' => true,
51 ),
52 ),
53 ),
54 )
55 );
56 }
57
58 /**
59 * Checks if a given request has access to get stats.
60 *
61 * @param \WP_REST_Request $request The request object.
62 *
63 * @return true|\WP_Error True if the request has access, WP_Error otherwise.
64 */
65 public function get_item_permissions_check( $request ) {
66 $user_id = (int) $request->get_param( 'user_id' );
67
68 // Check if user can access stats for this actor.
69 if ( Actors::BLOG_USER_ID === $user_id ) {
70 if ( ! \current_user_can( 'manage_options' ) ) {
71 return new \WP_Error(
72 'rest_forbidden',
73 \__( 'You do not have permission to view blog stats.', 'activitypub' ),
74 array( 'status' => 403 )
75 );
76 }
77 } elseif ( \get_current_user_id() !== $user_id ) {
78 return new \WP_Error(
79 'rest_forbidden',
80 \__( 'You do not have permission to view this user\'s stats.', 'activitypub' ),
81 array( 'status' => 403 )
82 );
83 }
84
85 return true;
86 }
87
88 /**
89 * Retrieves statistics for a user.
90 *
91 * @param \WP_REST_Request $request The request object.
92 *
93 * @return \WP_REST_Response|\WP_Error Response object or WP_Error object.
94 */
95 public function get_item( $request ) {
96 $user_id = (int) $request->get_param( 'user_id' );
97 $transient_key = 'activitypub_stats_' . $user_id;
98
99 $response = \get_transient( $transient_key );
100
101 if ( false === $response ) {
102 $stats = Statistics::get_current_stats( $user_id, 'month' );
103 $comparison = Statistics::get_period_comparison( $user_id, $stats );
104 $monthly_data = Statistics::get_rolling_monthly_breakdown( $user_id );
105 $comment_types = Statistics::get_comment_types_for_stats();
106
107 $stats_response = array(
108 'posts_count' => $stats['posts_count'],
109 'followers_total' => $stats['followers_total'],
110 'top_posts' => $stats['top_posts'],
111 'top_multiplicator' => $stats['top_multiplicator'],
112 );
113
114 // Include per-type engagement counts from current period stats.
115 foreach ( \array_keys( $comment_types ) as $type ) {
116 $stats_response[ $type . '_count' ] = $stats[ $type . '_count' ] ?? 0;
117 }
118
119 $response = array(
120 'stats' => $stats_response,
121 'comparison' => $comparison,
122 'monthly' => \array_values( $monthly_data ),
123 'comment_types' => $comment_types,
124 );
125
126 \set_transient( $transient_key, $response, 15 * MINUTE_IN_SECONDS );
127 }
128
129 return \rest_ensure_response( $response );
130 }
131 }
132