| 1 |
<?php |
| 2 |
/** |
| 3 |
* Moderators_Controller file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
|
| 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 |
use Collection; |
| 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 = 'collections/moderators'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Register routes. |
| 36 |
*/ |
| 37 |
public function register_routes() { |
| 38 |
\register_rest_route( |
| 39 |
$this->namespace, |
| 40 |
'/' . $this->rest_base, |
| 41 |
array( |
| 42 |
array( |
| 43 |
'methods' => \WP_REST_Server::READABLE, |
| 44 |
'callback' => array( $this, 'get_items' ), |
| 45 |
'permission_callback' => '__return_true', |
| 46 |
'args' => array( |
| 47 |
'page' => array( |
| 48 |
'description' => 'Current page of the collection.', |
| 49 |
'type' => 'integer', |
| 50 |
'minimum' => 1, |
| 51 |
// No default so we can differentiate between Collection and CollectionPage requests. |
| 52 |
), |
| 53 |
'per_page' => array( |
| 54 |
'description' => 'Maximum number of items to be returned in result set.', |
| 55 |
'type' => 'integer', |
| 56 |
'default' => 100, |
| 57 |
'minimum' => 1, |
| 58 |
), |
| 59 |
'order' => array( |
| 60 |
'description' => 'Order sort attribute ascending or descending.', |
| 61 |
'type' => 'string', |
| 62 |
'default' => 'desc', |
| 63 |
'enum' => array( 'asc', 'desc' ), |
| 64 |
), |
| 65 |
), |
| 66 |
), |
| 67 |
'schema' => array( $this, 'get_item_schema' ), |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Retrieves a collection of moderators. |
| 74 |
* |
| 75 |
* @param \WP_REST_Request $request The request object. |
| 76 |
* @return \WP_REST_Response|\WP_Error Response object or WP_Error object. |
| 77 |
*/ |
| 78 |
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 79 |
$actors = array(); |
| 80 |
|
| 81 |
foreach ( Actors::get_collection() as $user ) { |
| 82 |
$actors[] = $user->get_id(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Filter the list of moderators. |
| 87 |
* |
| 88 |
* @param array $actors The list of moderators. |
| 89 |
*/ |
| 90 |
$actors = \apply_filters( 'activitypub_rest_moderators', $actors ); |
| 91 |
|
| 92 |
$response = array( |
| 93 |
'id' => get_rest_url_by_path( 'collections/moderators' ), |
| 94 |
'type' => 'OrderedCollection', |
| 95 |
'totalItems' => \count( $actors ), |
| 96 |
'orderedItems' => $actors, |
| 97 |
); |
| 98 |
|
| 99 |
// Set the JSON-LD context if not already set. |
| 100 |
if ( empty( $response['@context'] ) ) { |
| 101 |
// Ensure the context is the first element in the response. |
| 102 |
$response = array( '@context' => $this->json_ld_context ) + $response; |
| 103 |
} |
| 104 |
|
| 105 |
if ( \is_wp_error( $response ) ) { |
| 106 |
return $response; |
| 107 |
} |
| 108 |
|
| 109 |
$response = \rest_ensure_response( $response ); |
| 110 |
$response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 111 |
|
| 112 |
return $response; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Retrieves the schema for the Moderators endpoint. |
| 117 |
* |
| 118 |
* @return array Schema data. |
| 119 |
*/ |
| 120 |
public function get_item_schema() { |
| 121 |
if ( $this->schema ) { |
| 122 |
return $this->add_additional_fields_schema( $this->schema ); |
| 123 |
} |
| 124 |
|
| 125 |
$schema = array( |
| 126 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 127 |
'title' => 'moderators', |
| 128 |
'type' => 'object', |
| 129 |
'properties' => array( |
| 130 |
'@context' => array( |
| 131 |
'type' => 'array', |
| 132 |
'items' => array( |
| 133 |
'type' => array( 'string', 'object' ), |
| 134 |
), |
| 135 |
'required' => true, |
| 136 |
), |
| 137 |
'id' => array( |
| 138 |
'type' => 'string', |
| 139 |
'format' => 'uri', |
| 140 |
'required' => true, |
| 141 |
), |
| 142 |
'type' => array( |
| 143 |
'type' => 'string', |
| 144 |
'enum' => array( 'OrderedCollection' ), |
| 145 |
'required' => true, |
| 146 |
), |
| 147 |
'orderedItems' => array( |
| 148 |
'type' => 'array', |
| 149 |
'items' => array( |
| 150 |
'type' => 'string', |
| 151 |
'format' => 'uri', |
| 152 |
), |
| 153 |
'required' => true, |
| 154 |
), |
| 155 |
'totalItems' => array( |
| 156 |
'type' => 'integer', |
| 157 |
'minimum' => 0, |
| 158 |
'required' => true, |
| 159 |
), |
| 160 |
), |
| 161 |
); |
| 162 |
|
| 163 |
$this->schema = $schema; |
| 164 |
|
| 165 |
return $this->add_additional_fields_schema( $this->schema ); |
| 166 |
} |
| 167 |
} |
| 168 |
|