|
| 1 | +<?php |
| 2 | +namespace Activitypub\Rest; |
| 3 | + |
| 4 | +use WP_REST_Response; |
| 5 | +use Activitypub\Http; |
| 6 | + |
| 7 | +class Interaction { |
| 8 | + /** |
| 9 | + * Initialize the class, registering WordPress hooks |
| 10 | + */ |
| 11 | + public static function init() { |
| 12 | + self::register_routes(); |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Register routes |
| 17 | + */ |
| 18 | + public static function register_routes() { |
| 19 | + \register_rest_route( |
| 20 | + ACTIVITYPUB_REST_NAMESPACE, |
| 21 | + '/interactions', |
| 22 | + array( |
| 23 | + array( |
| 24 | + 'methods' => \WP_REST_Server::READABLE, |
| 25 | + 'callback' => array( self::class, 'get' ), |
| 26 | + 'permission_callback' => '__return_true', |
| 27 | + 'args' => array( |
| 28 | + 'uri' => array( |
| 29 | + 'type' => 'string', |
| 30 | + 'required' => true, |
| 31 | + 'sanitize_callback' => 'esc_url', |
| 32 | + ), |
| 33 | + ), |
| 34 | + ), |
| 35 | + ) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Handle GET request |
| 41 | + * |
| 42 | + * @param WP_REST_Request $request The request object. |
| 43 | + * |
| 44 | + * @return wp_die|WP_REST_Response Redirect to the editor or die |
| 45 | + */ |
| 46 | + public static function get( $request ) { |
| 47 | + $uri = $request->get_param( 'uri' ); |
| 48 | + $redirect_url = null; |
| 49 | + $object = Http::get_remote_object( $uri ); |
| 50 | + |
| 51 | + if ( |
| 52 | + \is_wp_error( $object ) || |
| 53 | + ! isset( $object['type'] ) |
| 54 | + ) { |
| 55 | + \wp_die( |
| 56 | + \esc_html__( |
| 57 | + 'The URL is not supported!', |
| 58 | + 'activitypub' |
| 59 | + ), |
| 60 | + 400 |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if ( ! empty( $object['url'] ) ) { |
| 65 | + $uri = \esc_url( $object['url'] ); |
| 66 | + } |
| 67 | + |
| 68 | + switch ( $object['type'] ) { |
| 69 | + case 'Group': |
| 70 | + case 'Person': |
| 71 | + case 'Service': |
| 72 | + case 'Application': |
| 73 | + case 'Organization': |
| 74 | + $redirect_url = \apply_filters( 'activitypub_interactions_follow_url', $redirect_url, $uri, $object ); |
| 75 | + break; |
| 76 | + default: |
| 77 | + $redirect_url = \admin_url( 'post-new.php?in_reply_to=' . $uri ); |
| 78 | + $redirect_url = \apply_filters( 'activitypub_interactions_reply_url', $redirect_url, $uri, $object ); |
| 79 | + } |
| 80 | + |
| 81 | + // generic Interaction hook |
| 82 | + $redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object ); |
| 83 | + |
| 84 | + // check if hook is implemented |
| 85 | + if ( ! $redirect_url ) { |
| 86 | + \wp_die( |
| 87 | + esc_html__( |
| 88 | + 'This Interaction type is not supported yet!', |
| 89 | + 'activitypub' |
| 90 | + ), |
| 91 | + 400 |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return new WP_REST_Response( |
| 96 | + null, |
| 97 | + 302, |
| 98 | + array( |
| 99 | + 'Location' => \esc_url( $redirect_url ), |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments