| 1 |
<?php |
| 2 |
/** |
| 3 |
* Application Controller file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use Activitypub\Model\Application; |
| 11 |
|
| 12 |
/** |
| 13 |
* ActivityPub Application Controller. |
| 14 |
*/ |
| 15 |
class Application_Controller extends \WP_REST_Controller { |
| 16 |
/** |
| 17 |
* The namespace of this controller's route. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $namespace = ACTIVITYPUB_REST_NAMESPACE; |
| 22 |
|
| 23 |
/** |
| 24 |
* The base of this controller's route. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $rest_base = 'application'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Register routes. |
| 32 |
*/ |
| 33 |
public function register_routes() { |
| 34 |
\register_rest_route( |
| 35 |
$this->namespace, |
| 36 |
'/' . $this->rest_base, |
| 37 |
array( |
| 38 |
array( |
| 39 |
'methods' => \WP_REST_Server::READABLE, |
| 40 |
'callback' => array( $this, 'get_item' ), |
| 41 |
'permission_callback' => '__return_true', |
| 42 |
), |
| 43 |
'schema' => array( $this, 'get_item_schema' ), |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieves the application actor profile. |
| 50 |
* |
| 51 |
* @param \WP_REST_Request $request The request object. |
| 52 |
* @return \WP_REST_Response Response object. |
| 53 |
*/ |
| 54 |
public function get_item( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 55 |
$json = ( new Application() )->to_array(); |
| 56 |
|
| 57 |
$rest_response = new \WP_REST_Response( $json, 200 ); |
| 58 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 59 |
|
| 60 |
return $rest_response; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Retrieves the schema for the application endpoint. |
| 65 |
* |
| 66 |
* @return array Schema data. |
| 67 |
*/ |
| 68 |
public function get_item_schema() { |
| 69 |
if ( $this->schema ) { |
| 70 |
return $this->add_additional_fields_schema( $this->schema ); |
| 71 |
} |
| 72 |
|
| 73 |
$this->schema = array( |
| 74 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 75 |
'title' => 'application', |
| 76 |
'type' => 'object', |
| 77 |
'properties' => array( |
| 78 |
'@context' => array( |
| 79 |
'type' => 'array', |
| 80 |
'items' => array( |
| 81 |
'type' => array( 'string', 'object' ), |
| 82 |
), |
| 83 |
), |
| 84 |
'id' => array( |
| 85 |
'type' => 'string', |
| 86 |
'format' => 'uri', |
| 87 |
), |
| 88 |
'type' => array( |
| 89 |
'type' => 'string', |
| 90 |
'enum' => array( 'Application' ), |
| 91 |
), |
| 92 |
'name' => array( |
| 93 |
'type' => 'string', |
| 94 |
), |
| 95 |
'icon' => array( |
| 96 |
'type' => 'object', |
| 97 |
'properties' => array( |
| 98 |
'type' => array( |
| 99 |
'type' => 'string', |
| 100 |
), |
| 101 |
'url' => array( |
| 102 |
'type' => 'string', |
| 103 |
'format' => 'uri', |
| 104 |
), |
| 105 |
), |
| 106 |
), |
| 107 |
'published' => array( |
| 108 |
'type' => 'string', |
| 109 |
'format' => 'date-time', |
| 110 |
), |
| 111 |
'summary' => array( |
| 112 |
'type' => 'string', |
| 113 |
), |
| 114 |
'url' => array( |
| 115 |
'type' => 'string', |
| 116 |
'format' => 'uri', |
| 117 |
), |
| 118 |
'inbox' => array( |
| 119 |
'type' => 'string', |
| 120 |
'format' => 'uri', |
| 121 |
), |
| 122 |
'outbox' => array( |
| 123 |
'type' => 'string', |
| 124 |
'format' => 'uri', |
| 125 |
), |
| 126 |
'streams' => array( |
| 127 |
'type' => 'array', |
| 128 |
'items' => array( |
| 129 |
'type' => 'string', |
| 130 |
), |
| 131 |
), |
| 132 |
'preferredUsername' => array( |
| 133 |
'type' => 'string', |
| 134 |
), |
| 135 |
'publicKey' => array( |
| 136 |
'type' => 'object', |
| 137 |
'properties' => array( |
| 138 |
'id' => array( |
| 139 |
'type' => 'string', |
| 140 |
'format' => 'uri', |
| 141 |
), |
| 142 |
'owner' => array( |
| 143 |
'type' => 'string', |
| 144 |
'format' => 'uri', |
| 145 |
), |
| 146 |
'publicKeyPem' => array( |
| 147 |
'type' => 'string', |
| 148 |
), |
| 149 |
), |
| 150 |
), |
| 151 |
'manuallyApprovesFollowers' => array( |
| 152 |
'type' => 'boolean', |
| 153 |
), |
| 154 |
'discoverable' => array( |
| 155 |
'type' => 'boolean', |
| 156 |
), |
| 157 |
'indexable' => array( |
| 158 |
'type' => 'boolean', |
| 159 |
), |
| 160 |
'implements' => array( |
| 161 |
'type' => 'array', |
| 162 |
'items' => array( |
| 163 |
'type' => 'object', |
| 164 |
'properties' => array( |
| 165 |
'href' => array( |
| 166 |
'type' => 'string', |
| 167 |
'format' => 'uri', |
| 168 |
), |
| 169 |
'name' => array( |
| 170 |
'type' => 'string', |
| 171 |
), |
| 172 |
), |
| 173 |
), |
| 174 |
), |
| 175 |
'webfinger' => array( |
| 176 |
'type' => 'string', |
| 177 |
), |
| 178 |
), |
| 179 |
); |
| 180 |
|
| 181 |
return $this->add_additional_fields_schema( $this->schema ); |
| 182 |
} |
| 183 |
} |
| 184 |
|