PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.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 / class-moderators-controller.php

class-moderators-controller.php in ActivityPub 5.3.1, at includes/rest/class-moderators-controller.php

132 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Moderators_Controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Activity\Actor;
11 use Activitypub\Collection\Actors;
12 use function Activitypub\get_rest_url_by_path;
13
14 /**
15 * ActivityPub Moderators_Controller class.
16 */
17 class Moderators_Controller extends \WP_REST_Controller {
18
19 /**
20 * The namespace of this controller's route.
21 *
22 * @var string
23 */
24 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
25
26 /**
27 * The base of this controller's route.
28 *
29 * @var string
30 */
31 protected $rest_base = 'collections/moderators';
32
33 /**
34 * Register routes.
35 */
36 public function register_routes() {
37 \register_rest_route(
38 $this->namespace,
39 '/' . $this->rest_base,
40 array(
41 array(
42 'methods' => \WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get_items' ),
44 'permission_callback' => '__return_true',
45 ),
46 'schema' => array( $this, 'get_item_schema' ),
47 )
48 );
49 }
50
51 /**
52 * Retrieves a collection of moderators.
53 *
54 * @param \WP_REST_Request $request The request object.
55 * @return \WP_REST_Response|\WP_Error Response object or WP_Error object.
56 */
57 public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
58 $actors = array();
59
60 foreach ( Actors::get_collection() as $user ) {
61 $actors[] = $user->get_id();
62 }
63
64 /**
65 * Filter the list of moderators.
66 *
67 * @param array $actors The list of moderators.
68 */
69 $actors = apply_filters( 'activitypub_rest_moderators', $actors );
70
71 $response = array(
72 '@context' => Actor::JSON_LD_CONTEXT,
73 'id' => get_rest_url_by_path( 'collections/moderators' ),
74 'type' => 'OrderedCollection',
75 'orderedItems' => $actors,
76 );
77
78 $response = \rest_ensure_response( $response );
79 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
80
81 return $response;
82 }
83
84 /**
85 * Retrieves the schema for the Moderators endpoint.
86 *
87 * @return array Schema data.
88 */
89 public function get_item_schema() {
90 if ( $this->schema ) {
91 return $this->add_additional_fields_schema( $this->schema );
92 }
93
94 $schema = array(
95 '$schema' => 'http://json-schema.org/draft-04/schema#',
96 'title' => 'moderators',
97 'type' => 'object',
98 'properties' => array(
99 '@context' => array(
100 'type' => 'array',
101 'items' => array(
102 'type' => array( 'string', 'object' ),
103 ),
104 'required' => true,
105 ),
106 'id' => array(
107 'type' => 'string',
108 'format' => 'uri',
109 'required' => true,
110 ),
111 'type' => array(
112 'type' => 'string',
113 'enum' => array( 'OrderedCollection' ),
114 'required' => true,
115 ),
116 'orderedItems' => array(
117 'type' => 'array',
118 'items' => array(
119 'type' => 'string',
120 'format' => 'uri',
121 ),
122 'required' => true,
123 ),
124 ),
125 );
126
127 $this->schema = $schema;
128
129 return $this->add_additional_fields_schema( $this->schema );
130 }
131 }
132