diff --git a/includes/init.php b/includes/init.php index 746060c..cb71a9d 100644 --- a/includes/init.php +++ b/includes/init.php @@ -36,4 +36,5 @@ add_action( 'parse_request', '\pterotype\webfinger\parse_request', 111 ); add_filter( 'query_vars', '\pterotype\webfinger\query_vars' ); add_action( 'well_known_webfinger', '\pterotype\webfinger\handle' ); add_action( 'transition_post_status', '\pterotype\posts\handle_post_status_change', 10, 3 ); +add_action( 'template_redirect', '\pterotype\api\handle_non_api_requests' ); ?> diff --git a/includes/server/api.php b/includes/server/api.php index 940f736..cc7b8aa 100644 --- a/includes/server/api.php +++ b/includes/server/api.php @@ -108,4 +108,21 @@ function register_routes() { 'callback' => __NAMESPACE__ . '\get_shares', ) ); } + +function handle_non_api_requests() { + global $wp; + $accept = $_SERVER['HTTP_ACCEPT']; + if ( strpos( $accept, 'application/ld+json' ) !== false ) { + $current_url = \trailingslashit( + home_url( add_query_arg( $_GET, $wp->request ) ) + ); + $objects = \pterotype\objects\get_objects_by( 'url', $current_url ); + if ( count( $objects ) > 0 ) { + $object = $objects[0]; + header( 'Content-Type: application/activity+json', true ); + echo wp_json_encode( $object ); + exit; + } + } +} ?> diff --git a/includes/server/objects.php b/includes/server/objects.php index 39a4b79..39f5b57 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -205,6 +205,29 @@ function get_object_id( $activitypub_id ) { ) ); } +function get_objects_by( $field, $value ) { + global $wpdb; + $objects = $wpdb->get_results( + $wpdb->prepare( + " + SELECT object FROM {$wpdb->prefix}pterotype_objects + WHERE object->\"$.$field\" = %s + ", + $value + ), + ARRAY_A + ); + if ( ! $objects ) { + $objects = array(); + } + return array_map( + function( $result ) { + return json_decode( $result['object'], true ); + }, + $objects + ); +} + function delete_object( $object ) { global $wpdb; $object = \pterotype\util\dereference_object( $object );