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-interaction-controller.php

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

163 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Interaction Controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Http;
11
12 /**
13 * Interaction Controller.
14 */
15 class Interaction_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 = 'interactions';
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 'args' => array(
43 'uri' => array(
44 'description' => 'The URI or webfinger ID of the object to interact with.',
45 'type' => 'string',
46 'required' => true,
47 'sanitize_callback' => array( $this, 'sanitize_uri' ),
48 ),
49 ),
50 ),
51 )
52 );
53 }
54
55 /**
56 * Sanitize the URI parameter.
57 *
58 * @param string $uri The URI or webfinger ID of the object to interact with.
59 *
60 * @return string Sanitized URI.
61 */
62 public function sanitize_uri( $uri ) {
63 // Remove "acct:" prefix if present.
64 if ( str_starts_with( $uri, 'acct:' ) ) {
65 $uri = \substr( $uri, 5 );
66 }
67
68 // Remove "@" prefix if present.
69 $uri = \ltrim( $uri, '@' );
70
71 if ( is_email( $uri ) ) {
72 return \sanitize_text_field( $uri );
73 }
74
75 return \sanitize_url( $uri );
76 }
77
78 /**
79 * Retrieves the interaction URL for a given URI.
80 *
81 * @param \WP_REST_Request $request The request object.
82 *
83 * @return \WP_REST_Response Response object on success, dies on failure.
84 */
85 public function get_item( $request ) {
86 $uri = $request->get_param( 'uri' );
87 $redirect_url = '';
88 $object = Http::get_remote_object( $uri );
89
90 if ( \is_wp_error( $object ) || ! isset( $object['type'] ) ) {
91 // Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
92 \wp_die(
93 esc_html__( 'The URL is not supported!', 'activitypub' ),
94 '',
95 array(
96 'response' => 400,
97 'back_link' => true,
98 )
99 );
100 }
101
102 if ( ! empty( $object['url'] ) ) {
103 $uri = \esc_url( $object['url'] );
104 }
105
106 switch ( $object['type'] ) {
107 case 'Group':
108 case 'Person':
109 case 'Service':
110 case 'Application':
111 case 'Organization':
112 /**
113 * Filters the URL used for following an ActivityPub actor.
114 *
115 * @param string $redirect_url The URL to redirect to.
116 * @param string $uri The URI of the actor to follow.
117 * @param array $object The full actor object data.
118 */
119 $redirect_url = \apply_filters( 'activitypub_interactions_follow_url', $redirect_url, $uri, $object );
120 break;
121 default:
122 $redirect_url = \admin_url( 'post-new.php?in_reply_to=' . $uri );
123 /**
124 * Filters the URL used for replying to an ActivityPub object.
125 *
126 * By default, this redirects to the WordPress post editor with the in_reply_to parameter set.
127 *
128 * @param string $redirect_url The URL to redirect to.
129 * @param string $uri The URI of the object to reply to.
130 * @param array $object The full object data being replied to.
131 */
132 $redirect_url = \apply_filters( 'activitypub_interactions_reply_url', $redirect_url, $uri, $object );
133 }
134
135 /**
136 * Filters the redirect URL.
137 *
138 * This filter runs after the type-specific filters and allows for final modifications
139 * to the interaction URL regardless of the object type.
140 *
141 * @param string $redirect_url The URL to redirect to.
142 * @param string $uri The URI of the object.
143 * @param array $object The object being interacted with.
144 */
145 $redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object );
146
147 // Check if hook is implemented.
148 if ( ! $redirect_url ) {
149 // Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
150 \wp_die(
151 esc_html__( 'This Interaction type is not supported yet!', 'activitypub' ),
152 '',
153 array(
154 'response' => 400,
155 'back_link' => true,
156 )
157 );
158 }
159
160 return new \WP_REST_Response( null, 302, array( 'Location' => $redirect_url ) );
161 }
162 }
163