PluginProbe
ActivityPub / 9.0.2
ActivityPub v9.0.2
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 9.0.2, at includes/rest/admin/class-statistics-controller.php

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