| 1 |
<?php |
| 2 |
/** |
| 3 |
* NodeInfo controller file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use function Activitypub\get_masked_wp_version; |
| 11 |
use function Activitypub\get_rest_url_by_path; |
| 12 |
|
| 13 |
/** |
| 14 |
* ActivityPub NodeInfo Controller. |
| 15 |
* |
| 16 |
* @author Matthias Pfefferle |
| 17 |
* |
| 18 |
* @see https://nodeinfo.diaspora.software/ |
| 19 |
*/ |
| 20 |
class Nodeinfo_Controller extends \WP_REST_Controller { |
| 21 |
/** |
| 22 |
* The namespace of this controller's route. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $namespace = ACTIVITYPUB_REST_NAMESPACE; |
| 27 |
|
| 28 |
/** |
| 29 |
* The REST base for this controller's route. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $rest_base = 'nodeinfo'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Register routes. |
| 37 |
*/ |
| 38 |
public function register_routes() { |
| 39 |
\register_rest_route( |
| 40 |
$this->namespace, |
| 41 |
'/' . $this->rest_base, |
| 42 |
array( |
| 43 |
array( |
| 44 |
'methods' => \WP_REST_Server::READABLE, |
| 45 |
'callback' => array( $this, 'get_items' ), |
| 46 |
'permission_callback' => '__return_true', |
| 47 |
), |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
\register_rest_route( |
| 52 |
$this->namespace, |
| 53 |
'/' . $this->rest_base . '/(?P<version>\d\.\d)', |
| 54 |
array( |
| 55 |
'args' => array( |
| 56 |
'version' => array( |
| 57 |
'description' => 'The version of the NodeInfo schema.', |
| 58 |
'type' => 'string', |
| 59 |
'required' => true, |
| 60 |
), |
| 61 |
), |
| 62 |
array( |
| 63 |
'methods' => \WP_REST_Server::READABLE, |
| 64 |
'callback' => array( $this, 'get_item' ), |
| 65 |
'permission_callback' => '__return_true', |
| 66 |
), |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Retrieves the NodeInfo discovery profile. |
| 73 |
* |
| 74 |
* @param \WP_REST_Request $request The request object. |
| 75 |
* |
| 76 |
* @return \WP_REST_Response Response object. |
| 77 |
*/ |
| 78 |
public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 79 |
$response = array( |
| 80 |
'links' => array( |
| 81 |
|
| 82 |
/* |
| 83 |
* Needs http protocol for spec compliance. |
| 84 |
* @ticket https://github.com/Automattic/wordpress-activitypub/pull/1275 |
| 85 |
*/ |
| 86 |
array( |
| 87 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', |
| 88 |
'href' => get_rest_url_by_path( '/nodeinfo/2.0' ), |
| 89 |
), |
| 90 |
array( |
| 91 |
'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.0', |
| 92 |
'href' => get_rest_url_by_path( '/nodeinfo/2.0' ), |
| 93 |
), |
| 94 |
array( |
| 95 |
'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.1', |
| 96 |
'href' => get_rest_url_by_path( '/nodeinfo/2.1' ), |
| 97 |
), |
| 98 |
array( |
| 99 |
'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.1', |
| 100 |
'href' => get_rest_url_by_path( '/nodeinfo/2.1' ), |
| 101 |
), |
| 102 |
array( |
| 103 |
'rel' => 'https://www.w3.org/ns/activitystreams#Application', |
| 104 |
'href' => get_rest_url_by_path( 'application' ), |
| 105 |
), |
| 106 |
), |
| 107 |
); |
| 108 |
|
| 109 |
return \rest_ensure_response( $response ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Retrieves the NodeInfo profile. |
| 114 |
* |
| 115 |
* @param \WP_REST_Request $request The request object. |
| 116 |
* @return \WP_REST_Response Response object. |
| 117 |
*/ |
| 118 |
public function get_item( $request ) { |
| 119 |
$version = $request->get_param( 'version' ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Fires before the NodeInfo data is created and sent to the client. |
| 123 |
* |
| 124 |
* @param string $version The NodeInfo version. |
| 125 |
*/ |
| 126 |
\do_action( 'activitypub_rest_nodeinfo_pre', $version ); |
| 127 |
|
| 128 |
switch ( $version ) { |
| 129 |
case '2.0': |
| 130 |
case '2.1': |
| 131 |
$response = $this->get_version_2_X( $version ); |
| 132 |
break; |
| 133 |
|
| 134 |
default: |
| 135 |
$response = new \WP_Error( 'activitypub_rest_nodeinfo_invalid_version', 'Unsupported NodeInfo version.', array( 'status' => 405 ) ); |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
return \rest_ensure_response( $response ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the NodeInfo 2.0 data. |
| 144 |
* |
| 145 |
* @param string $version The NodeInfo version. |
| 146 |
* |
| 147 |
* @return array The NodeInfo data. |
| 148 |
*/ |
| 149 |
public function get_version_2_X( $version ) { |
| 150 |
$posts = \wp_count_posts(); |
| 151 |
$comments = \wp_count_comments(); |
| 152 |
|
| 153 |
$nodeinfo = array( |
| 154 |
'version' => $version, |
| 155 |
'software' => array( |
| 156 |
'name' => 'wordpress', |
| 157 |
'version' => get_masked_wp_version(), |
| 158 |
), |
| 159 |
'openRegistrations' => (bool) get_option( 'users_can_register' ), |
| 160 |
'usage' => array( |
| 161 |
'localPosts' => (int) $posts->publish, |
| 162 |
'localComments' => $comments->approved, |
| 163 |
), |
| 164 |
'metadata' => array( |
| 165 |
'nodeName' => \get_bloginfo( 'name' ), |
| 166 |
'nodeDescription' => \get_bloginfo( 'description' ), |
| 167 |
'nodeIcon' => \get_site_icon_url(), |
| 168 |
), |
| 169 |
); |
| 170 |
|
| 171 |
/** |
| 172 |
* Filter the NodeInfo data. |
| 173 |
* |
| 174 |
* @param array $nodeinfo The NodeInfo data. |
| 175 |
* @param string $version The NodeInfo version. |
| 176 |
*/ |
| 177 |
return \apply_filters( 'nodeinfo_data', $nodeinfo, $version ); |
| 178 |
} |
| 179 |
} |
| 180 |
|