PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
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 5.3.1, at includes/rest/class-interaction-controller.php

140 lines 3.9 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 of the object to interact with.',
45 'type' => 'string',
46 'format' => 'uri',
47 'required' => true,
48 ),
49 ),
50 ),
51 )
52 );
53 }
54
55 /**
56 * Retrieves the interaction URL for a given URI.
57 *
58 * @param \WP_REST_Request $request The request object.
59 *
60 * @return \WP_REST_Response Response object on success, dies on failure.
61 */
62 public function get_item( $request ) {
63 $uri = $request->get_param( 'uri' );
64 $redirect_url = null;
65 $object = Http::get_remote_object( $uri );
66
67 if ( \is_wp_error( $object ) || ! isset( $object['type'] ) ) {
68 // Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
69 \wp_die(
70 esc_html__( 'The URL is not supported!', 'activitypub' ),
71 '',
72 array(
73 'response' => 400,
74 'back_link' => true,
75 )
76 );
77 }
78
79 if ( ! empty( $object['url'] ) ) {
80 $uri = \esc_url( $object['url'] );
81 }
82
83 switch ( $object['type'] ) {
84 case 'Group':
85 case 'Person':
86 case 'Service':
87 case 'Application':
88 case 'Organization':
89 /**
90 * Filters the URL used for following an ActivityPub actor.
91 *
92 * @param string $redirect_url The URL to redirect to.
93 * @param string $uri The URI of the actor to follow.
94 * @param array $object The full actor object data.
95 */
96 $redirect_url = \apply_filters( 'activitypub_interactions_follow_url', $redirect_url, $uri, $object );
97 break;
98 default:
99 $redirect_url = \admin_url( 'post-new.php?in_reply_to=' . $uri );
100 /**
101 * Filters the URL used for replying to an ActivityPub object.
102 *
103 * By default, this redirects to the WordPress post editor with the in_reply_to parameter set.
104 *
105 * @param string $redirect_url The URL to redirect to.
106 * @param string $uri The URI of the object to reply to.
107 * @param array $object The full object data being replied to.
108 */
109 $redirect_url = \apply_filters( 'activitypub_interactions_reply_url', $redirect_url, $uri, $object );
110 }
111
112 /**
113 * Filters the redirect URL.
114 *
115 * This filter runs after the type-specific filters and allows for final modifications
116 * to the interaction URL regardless of the object type.
117 *
118 * @param string $redirect_url The URL to redirect to.
119 * @param string $uri The URI of the object.
120 * @param array $object The object being interacted with.
121 */
122 $redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object );
123
124 // Check if hook is implemented.
125 if ( ! $redirect_url ) {
126 // Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
127 \wp_die(
128 esc_html__( 'This Interaction type is not supported yet!', 'activitypub' ),
129 '',
130 array(
131 'response' => 400,
132 'back_link' => true,
133 )
134 );
135 }
136
137 return new \WP_REST_Response( null, 302, array( 'Location' => \esc_url( $redirect_url ) ) );
138 }
139 }
140