PluginProbe
ActivityPub / 9.1.0
ActivityPub v9.1.0
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 / class-stats-image-controller.php

class-stats-image-controller.php in ActivityPub 9.1.0, at includes/rest/class-stats-image-controller.php

157 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Stats_Image_Controller file.
4 *
5 * @package Activitypub
6 * @since 8.1.0
7 */
8
9 namespace Activitypub\Rest;
10
11 use Activitypub\Cache\Stats_Image;
12
13 /**
14 * REST controller that serves stats share images.
15 *
16 * Provides two endpoints:
17 * - /stats/image/{user_id}/{year} — serves the image binary
18 * - /stats/image-url/{user_id}/{year} — returns the image URL as JSON
19 */
20 class Stats_Image_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 = 'stats/image';
35
36 /**
37 * Common route args for user_id and year.
38 *
39 * @return array The route args.
40 */
41 private function get_common_args() {
42 return array(
43 'user_id' => array(
44 'description' => \__( 'The user ID to generate the stats image for.', 'activitypub' ),
45 'type' => 'integer',
46 'required' => true,
47 'sanitize_callback' => 'absint',
48 'validate_callback' => array( $this, 'validate_user_id' ),
49 ),
50 'year' => array(
51 'description' => \__( 'The year to display stats for.', 'activitypub' ),
52 'type' => 'integer',
53 'required' => true,
54 'sanitize_callback' => 'absint',
55 'minimum' => 2000,
56 'maximum' => (int) \gmdate( 'Y' ),
57 ),
58 );
59 }
60
61 /**
62 * Validate the user_id parameter.
63 *
64 * @param mixed $value The parameter value.
65 *
66 * @return true|\WP_Error True if valid, error otherwise.
67 */
68 public function validate_user_id( $value ) {
69 $user_id = (int) $value;
70
71 // Blog user ID (0) is always valid.
72 if ( 0 === $user_id ) {
73 return true;
74 }
75
76 // Check that the user exists.
77 if ( ! \get_user_by( 'id', $user_id ) ) {
78 return new \WP_Error( 'invalid_user', \__( 'Invalid user ID.', 'activitypub' ), array( 'status' => 404 ) );
79 }
80
81 return true;
82 }
83
84 /**
85 * Register routes.
86 */
87 public function register_routes() {
88 $route_pattern = '/(?P<user_id>[\d]+)/(?P<year>[\d]{4})';
89
90 // Serve the image binary.
91 \register_rest_route(
92 $this->namespace,
93 '/' . $this->rest_base . $route_pattern,
94 array(
95 array(
96 'methods' => \WP_REST_Server::READABLE,
97 'callback' => array( $this, 'get_item' ),
98 'permission_callback' => '__return_true',
99 'args' => $this->get_common_args(),
100 ),
101 )
102 );
103
104 // Return the image URL as JSON.
105 \register_rest_route(
106 $this->namespace,
107 '/' . $this->rest_base . '-url' . $route_pattern,
108 array(
109 array(
110 'methods' => \WP_REST_Server::READABLE,
111 'callback' => array( $this, 'get_url' ),
112 'permission_callback' => '__return_true',
113 'args' => $this->get_common_args(),
114 ),
115 )
116 );
117 }
118
119 /**
120 * Serve the stats image binary.
121 *
122 * @param \WP_REST_Request $request The request object.
123 *
124 * @return void|\WP_Error Streams image and exits, or returns error.
125 */
126 public function get_item( $request ) {
127 return Stats_Image::serve(
128 (int) $request->get_param( 'user_id' ),
129 (int) $request->get_param( 'year' )
130 );
131 }
132
133 /**
134 * Return the resolved image URL as JSON.
135 *
136 * Returns the cached file URL if available, otherwise the REST
137 * endpoint URL. Filtered via `activitypub_stats_image_url` so
138 * it can be routed through a CDN or image proxy like Photon.
139 *
140 * @param \WP_REST_Request $request The request object.
141 *
142 * @return \WP_REST_Response|\WP_Error JSON response with the URL.
143 */
144 public function get_url( $request ) {
145 $url = Stats_Image::get_url(
146 (int) $request->get_param( 'user_id' ),
147 (int) $request->get_param( 'year' )
148 );
149
150 if ( \is_wp_error( $url ) ) {
151 return $url;
152 }
153
154 return \rest_ensure_response( array( 'url' => $url ) );
155 }
156 }
157