Handle requests from the local server to the local server
This commit is contained in:
parent
93d2268b38
commit
cc2d40b802
@ -73,15 +73,11 @@ function get_recipient_urls( $object, $depth, $acc ) {
|
||||
return $recipients;
|
||||
case Link:
|
||||
if ( array_key_exists( 'href', $object ) ) {
|
||||
$response = wp_remote_get( $object['href'] );
|
||||
$response = \util\get_object_from_url( $object['href'] );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return array();
|
||||
}
|
||||
$link_target = wp_remote_retrieve_body( $response );
|
||||
if ( empty( $link_target ) ) {
|
||||
return array();
|
||||
}
|
||||
return get_recipient_urls( json_decode( $link_target, true ), $depth + 1, $acc );
|
||||
return get_recipient_urls( $link_target, $depth + 1, $acc );
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
@ -106,15 +102,11 @@ function get_recipient_urls( $object, $depth, $acc ) {
|
||||
return $recipients;
|
||||
} else {
|
||||
if ( filter_var( $object, FILTER_VALIDATE_URL ) ) {
|
||||
$response = wp_remote_get( $object );
|
||||
$response = \util\get_object_from_url( $object );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return array();
|
||||
}
|
||||
$response_body = wp_remote_retrieve_body( $response );
|
||||
if ( empty( $response_body ) ) {
|
||||
return $array();
|
||||
}
|
||||
return get_recipient_urls( json_decode( $response_body, true ), $depth + 1, $acc );
|
||||
return get_recipient_urls( $response_body, $depth + 1, $acc );
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
@ -123,13 +115,21 @@ function get_recipient_urls( $object, $depth, $acc ) {
|
||||
}
|
||||
|
||||
function post_activity_to_inboxes( $activity, $recipients ) {
|
||||
foreach ( $inbox as $recipients ) {
|
||||
$args = array(
|
||||
'body' => $activity,
|
||||
'headers' => array( 'Content-Type' => 'application/ld+json' )
|
||||
);
|
||||
// TODO do something with the result?
|
||||
wp_remote_post( $inbox, $args );
|
||||
foreach ( $recipients as $inbox ) {
|
||||
if ( \util\is_local_url( $inbox ) ) {
|
||||
$request = \WP_REST_Request::from_url( $inbox );
|
||||
$request->set_method('POST');
|
||||
$request->set_body( $activity );
|
||||
$request->add_header( 'Content-Type', 'application/ld+json' );
|
||||
$server = rest_get_server();
|
||||
$server->dispatch( $request );
|
||||
} else {
|
||||
$args = array(
|
||||
'body' => $activity,
|
||||
'headers' => array( 'Content-Type' => 'application/ld+json' )
|
||||
);
|
||||
wp_remote_post( $inbox, $args );
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -110,35 +110,11 @@ function references_local_object( $object, $depth ) {
|
||||
if ( $result ) {
|
||||
return $result;
|
||||
}
|
||||
// $field_value is either a url, a Link, or an object
|
||||
if ( is_array( $field_value ) ) {
|
||||
if ( array_key_exists( 'id', $field_value ) ) {
|
||||
return \objects\is_local_object( $field_value );
|
||||
} else if ( array_key_exists( 'href', $field_value ) ) {
|
||||
$response = wp_remote_get( $field_value['href'] );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
if ( empty( $body ) ) {
|
||||
return false;
|
||||
}
|
||||
$body_array = json_decode( $body, true );
|
||||
return $body_array && references_local_object( $body_array, $depth + 1 );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
$dereferenced = \util\dereference_object( $field_value );
|
||||
if ( is_wp_error( $dereferenced ) ) {
|
||||
return false;
|
||||
} else {
|
||||
$response = wp_remote_get( $field_value );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
continue;
|
||||
}
|
||||
$body = wp_remote_retrieve_body( $response );
|
||||
if ( empty( $body ) ) {
|
||||
continue;
|
||||
}
|
||||
$body_array = json_decode( $body, true );
|
||||
$result = $body_array && references_local_object( $body_array, $depth + 1 );
|
||||
return \objects\is_local_object( $dereferenced );
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -262,11 +262,6 @@ function is_local_object( $object ) {
|
||||
if ( !$url ) {
|
||||
return false;
|
||||
}
|
||||
$parsed = parse_url( $url );
|
||||
if ( $parsed ) {
|
||||
$site_host = parse_url( get_site_url() )['host'];
|
||||
return $parsed['host'] === $site_host;
|
||||
}
|
||||
return false;
|
||||
return \util\is_local_url( $url );
|
||||
}
|
||||
?>
|
||||
|
@ -17,11 +17,11 @@ function dereference_object_helper( $object, $depth ) {
|
||||
$object['type'] === 'Link' &&
|
||||
array_key_exists( 'href', $object ) &&
|
||||
filter_var( $object['href'], FILTER_VALIDATE_URL ) ) {
|
||||
return get_object_from_url( $object['href'], $depth );
|
||||
return get_object_from_url_helper( $object['href'], $depth );
|
||||
}
|
||||
return $object;
|
||||
} else if ( filter_var( $object, FILTER_VALIDATE_URL ) ) {
|
||||
return get_object_from_url( $object, $depth );
|
||||
return get_object_from_url_helper( $object, $depth );
|
||||
} else {
|
||||
return new \WP_Error(
|
||||
'invalid_object',
|
||||
@ -31,7 +31,14 @@ function dereference_object_helper( $object, $depth ) {
|
||||
}
|
||||
}
|
||||
|
||||
function get_object_from_url( $url, $depth ) {
|
||||
function get_object_from_url( $url ) {
|
||||
return get_object_from_url_helper( $url, 0 );
|
||||
}
|
||||
|
||||
function get_object_from_url_helper( $url, $depth ) {
|
||||
if ( is_local_url( $url ) ) {
|
||||
return retrieve_local_url( $url );
|
||||
}
|
||||
$response = wp_remote_get( $url );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
@ -48,6 +55,26 @@ function get_object_from_url( $url, $depth ) {
|
||||
return dereference_object_helper( $body_array, $depth + 1 );
|
||||
}
|
||||
|
||||
function retrieve_local_object( $url ) {
|
||||
$server = rest_get_server();
|
||||
$request = new \WP_REST_Request( 'GET', $url );
|
||||
$response = $server->dispatch( $request );
|
||||
is ( $response->is_error() ) {
|
||||
return $response->as_error();
|
||||
} else {
|
||||
return $response->get_data();
|
||||
}
|
||||
}
|
||||
|
||||
function is_local_url( $url ) {
|
||||
$parsed = parse_url( $url );
|
||||
if ( $parsed ) {
|
||||
$site_host = parse_url( get_site_url() )['host'];
|
||||
return $parsed['host'] === $site_host;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function is_same_object( $object1, $object2 ) {
|
||||
return get_id( $object1 ) === get_id( $object2 );
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user