Respond correctly to requests with Accept: application/ld+json

This commit is contained in:
Jeremy Dormitzer 2018-10-21 09:41:17 -04:00
parent 44f3117eaa
commit d8c4a5cd8b
3 changed files with 41 additions and 0 deletions

View File

@ -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' );
?>

View File

@ -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;
}
}
}
?>

View File

@ -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 );