PluginProbe
ActivityPub / 7.0.0
ActivityPub v7.0.0
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-url-validator-controller.php

class-url-validator-controller.php in ActivityPub 7.0.0, at includes/rest/class-url-validator-controller.php

134 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub URL Validator Controller.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Http;
11 use Activitypub\Embed;
12
13 /**
14 * URL Validator Controller Class.
15 */
16 class URL_Validator_Controller extends \WP_REST_Controller {
17 /**
18 * The namespace of this controller's route.
19 *
20 * @var string
21 */
22 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
23
24 /**
25 * The base of this controller's route.
26 *
27 * @var string
28 */
29 protected $rest_base = 'url/validate';
30
31 /**
32 * Register routes.
33 */
34 public function register_routes() {
35 register_rest_route(
36 $this->namespace,
37 '/' . $this->rest_base,
38 array(
39 array(
40 'methods' => \WP_REST_Server::READABLE,
41 'callback' => array( $this, 'get_items' ),
42 'permission_callback' => array( $this, 'get_items_permissions_check' ),
43 'args' => array(
44 'url' => array(
45 'type' => 'string',
46 'format' => 'uri',
47 'required' => true,
48 ),
49 ),
50 ),
51 'schema' => array( $this, 'get_item_schema' ),
52 )
53 );
54 }
55
56 /**
57 * Check if a given request has access to validate URLs.
58 *
59 * @param \WP_REST_Request $request The request.
60 *
61 * @return bool True if the request has access to validate URLs, false otherwise.
62 */
63 public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
64 return current_user_can( 'edit_posts' );
65 }
66
67 /**
68 * Check if URL is a valid ActivityPub endpoint.
69 *
70 * @param \WP_REST_Request $request The request.
71 *
72 * @return \WP_REST_Response|\WP_Error
73 */
74 public function get_items( $request ) {
75 $url = $request->get_param( 'url' );
76 $object = Http::get_remote_object( $url );
77
78 if ( is_wp_error( $object ) ) {
79 return new \WP_Error(
80 'activitypub_invalid_url',
81 __( 'Invalid URL.', 'activitypub' ),
82 array( 'status' => 400 )
83 );
84 }
85
86 $response = array(
87 'is_activitypub' => ! empty( $object['type'] ),
88 'is_real_oembed' => Embed::has_real_oembed( $url ),
89 'html' => false,
90 );
91
92 if ( $response['is_activitypub'] ) {
93 $response['html'] = wp_oembed_get( $url );
94 }
95
96 return rest_ensure_response( $response );
97 }
98
99 /**
100 * Get the URL validation schema.
101 *
102 * @return array
103 */
104 public function get_item_schema() {
105 if ( $this->schema ) {
106 return $this->add_additional_fields_schema( $this->schema );
107 }
108
109 $schema = array(
110 '$schema' => 'http://json-schema.org/draft-04/schema#',
111 'title' => 'validated-url',
112 'type' => 'object',
113 'properties' => array(
114 'is_activitypub' => array(
115 'type' => 'boolean',
116 'default' => false,
117 ),
118 'is_real_oembed' => array(
119 'type' => 'boolean',
120 'default' => false,
121 ),
122 'html' => array(
123 'type' => 'string',
124 'default' => false,
125 ),
126 ),
127 );
128
129 $this->schema = $schema;
130
131 return $this->add_additional_fields_schema( $this->schema );
132 }
133 }
134