| 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_context; |
| 13 |
use function Activitypub\get_rest_url_by_path; |
| 14 |
|
| 15 |
/** |
| 16 |
* ActivityPub Moderators_Controller class. |
| 17 |
*/ |
| 18 |
class Moderators_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 = '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 |
), |
| 47 |
'schema' => array( $this, 'get_item_schema' ), |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Retrieves a collection of moderators. |
| 54 |
* |
| 55 |
* @param \WP_REST_Request $request The request object. |
| 56 |
* @return \WP_REST_Response|\WP_Error Response object or WP_Error object. |
| 57 |
*/ |
| 58 |
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 59 |
$actors = array(); |
| 60 |
|
| 61 |
foreach ( Actors::get_collection() as $user ) { |
| 62 |
$actors[] = $user->get_id(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Filter the list of moderators. |
| 67 |
* |
| 68 |
* @param array $actors The list of moderators. |
| 69 |
*/ |
| 70 |
$actors = apply_filters( 'activitypub_rest_moderators', $actors ); |
| 71 |
|
| 72 |
$response = array( |
| 73 |
'@context' => get_context(), |
| 74 |
'id' => get_rest_url_by_path( 'collections/moderators' ), |
| 75 |
'type' => 'OrderedCollection', |
| 76 |
'orderedItems' => $actors, |
| 77 |
); |
| 78 |
|
| 79 |
$response = \rest_ensure_response( $response ); |
| 80 |
$response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 81 |
|
| 82 |
return $response; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Retrieves the schema for the Moderators endpoint. |
| 87 |
* |
| 88 |
* @return array Schema data. |
| 89 |
*/ |
| 90 |
public function get_item_schema() { |
| 91 |
if ( $this->schema ) { |
| 92 |
return $this->add_additional_fields_schema( $this->schema ); |
| 93 |
} |
| 94 |
|
| 95 |
$schema = array( |
| 96 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 97 |
'title' => 'moderators', |
| 98 |
'type' => 'object', |
| 99 |
'properties' => array( |
| 100 |
'@context' => array( |
| 101 |
'type' => 'array', |
| 102 |
'items' => array( |
| 103 |
'type' => array( 'string', 'object' ), |
| 104 |
), |
| 105 |
'required' => true, |
| 106 |
), |
| 107 |
'id' => array( |
| 108 |
'type' => 'string', |
| 109 |
'format' => 'uri', |
| 110 |
'required' => true, |
| 111 |
), |
| 112 |
'type' => array( |
| 113 |
'type' => 'string', |
| 114 |
'enum' => array( 'OrderedCollection' ), |
| 115 |
'required' => true, |
| 116 |
), |
| 117 |
'orderedItems' => array( |
| 118 |
'type' => 'array', |
| 119 |
'items' => array( |
| 120 |
'type' => 'string', |
| 121 |
'format' => 'uri', |
| 122 |
), |
| 123 |
'required' => true, |
| 124 |
), |
| 125 |
), |
| 126 |
); |
| 127 |
|
| 128 |
$this->schema = $schema; |
| 129 |
|
| 130 |
return $this->add_additional_fields_schema( $this->schema ); |
| 131 |
} |
| 132 |
} |
| 133 |
|