| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub Interaction REST-Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use WP_REST_Response; |
| 11 |
use Activitypub\Http; |
| 12 |
|
| 13 |
/** |
| 14 |
* Interaction class. |
| 15 |
*/ |
| 16 |
class Interaction { |
| 17 |
/** |
| 18 |
* Initialize the class, registering WordPress hooks. |
| 19 |
*/ |
| 20 |
public static function init() { |
| 21 |
self::register_routes(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Register routes |
| 26 |
*/ |
| 27 |
public static function register_routes() { |
| 28 |
\register_rest_route( |
| 29 |
ACTIVITYPUB_REST_NAMESPACE, |
| 30 |
'/interactions', |
| 31 |
array( |
| 32 |
array( |
| 33 |
'methods' => \WP_REST_Server::READABLE, |
| 34 |
'callback' => array( self::class, 'get' ), |
| 35 |
'permission_callback' => '__return_true', |
| 36 |
'args' => array( |
| 37 |
'uri' => array( |
| 38 |
'type' => 'string', |
| 39 |
'required' => true, |
| 40 |
'sanitize_callback' => 'esc_url', |
| 41 |
), |
| 42 |
), |
| 43 |
), |
| 44 |
) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle GET request. |
| 50 |
* |
| 51 |
* @param \WP_REST_Request $request The request object. |
| 52 |
* |
| 53 |
* @return WP_REST_Response Redirect to the editor or die. |
| 54 |
*/ |
| 55 |
public static function get( $request ) { |
| 56 |
$uri = $request->get_param( 'uri' ); |
| 57 |
$redirect_url = null; |
| 58 |
$object = Http::get_remote_object( $uri ); |
| 59 |
|
| 60 |
if ( |
| 61 |
\is_wp_error( $object ) || |
| 62 |
! isset( $object['type'] ) |
| 63 |
) { |
| 64 |
\wp_die( |
| 65 |
\esc_html__( |
| 66 |
'The URL is not supported!', |
| 67 |
'activitypub' |
| 68 |
), |
| 69 |
400 |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! empty( $object['url'] ) ) { |
| 74 |
$uri = \esc_url( $object['url'] ); |
| 75 |
} |
| 76 |
|
| 77 |
switch ( $object['type'] ) { |
| 78 |
case 'Group': |
| 79 |
case 'Person': |
| 80 |
case 'Service': |
| 81 |
case 'Application': |
| 82 |
case 'Organization': |
| 83 |
/** |
| 84 |
* Filters the URL used for following an ActivityPub actor. |
| 85 |
* |
| 86 |
* @param string $redirect_url The URL to redirect to. |
| 87 |
* @param string $uri The URI of the actor to follow. |
| 88 |
* @param array $object The full actor object data. |
| 89 |
*/ |
| 90 |
$redirect_url = \apply_filters( 'activitypub_interactions_follow_url', $redirect_url, $uri, $object ); |
| 91 |
break; |
| 92 |
default: |
| 93 |
$redirect_url = \admin_url( 'post-new.php?in_reply_to=' . $uri ); |
| 94 |
/** |
| 95 |
* Filters the URL used for replying to an ActivityPub object. |
| 96 |
* |
| 97 |
* By default, this redirects to the WordPress post editor with the in_reply_to parameter set. |
| 98 |
* |
| 99 |
* @param string $redirect_url The URL to redirect to. |
| 100 |
* @param string $uri The URI of the object to reply to. |
| 101 |
* @param array $object The full object data being replied to. |
| 102 |
*/ |
| 103 |
$redirect_url = \apply_filters( 'activitypub_interactions_reply_url', $redirect_url, $uri, $object ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Filter the redirect URL. |
| 108 |
* |
| 109 |
* @param string $redirect_url The URL to redirect to. |
| 110 |
* @param string $uri The URI of the object. |
| 111 |
* @param array $object The object. |
| 112 |
*/ |
| 113 |
$redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object ); |
| 114 |
|
| 115 |
// Check if hook is implemented. |
| 116 |
if ( ! $redirect_url ) { |
| 117 |
\wp_die( |
| 118 |
esc_html__( |
| 119 |
'This Interaction type is not supported yet!', |
| 120 |
'activitypub' |
| 121 |
), |
| 122 |
400 |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return new WP_REST_Response( |
| 127 |
null, |
| 128 |
302, |
| 129 |
array( |
| 130 |
'Location' => \esc_url( $redirect_url ), |
| 131 |
) |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|