2018-09-10 00:48:14 +00:00
|
|
|
<?php
|
|
|
|
namespace deliver;
|
|
|
|
|
2018-09-14 01:52:13 +00:00
|
|
|
require_once plugin_dir_path( __FILE__ ) . 'activities.php';
|
|
|
|
|
2018-09-10 00:48:14 +00:00
|
|
|
function deliver_activity( $activity ) {
|
|
|
|
$recipients = array();
|
2018-09-15 19:18:20 +00:00
|
|
|
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $field ) {
|
2018-09-10 00:48:14 +00:00
|
|
|
$recipients = array_merge(
|
|
|
|
$recipients, retrieve_recipients_for_field( $field, $activity )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$recipients = array_unique( $recipients );
|
|
|
|
if ( array_key_exists( 'actor', $activity ) ) {
|
|
|
|
$recipients = remove_actor_inbox_from_recipients( $activity['actor'], $recipients );
|
|
|
|
}
|
2018-09-14 01:52:13 +00:00
|
|
|
$activity = \activities\strip_private_fields( $activity );
|
2018-09-10 00:48:14 +00:00
|
|
|
post_activity_to_inboxes( $activity, $recipients );
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_actor_inbox_from_recipients( $actor, $recipients ) {
|
|
|
|
if ( array_key_exists( 'inbox', $actor ) ) {
|
|
|
|
$key = array_search( $actor['inbox'], $recipients );
|
|
|
|
if ( $key ) {
|
2018-09-15 19:18:20 +00:00
|
|
|
unset( $recipients[$key] );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve_recipients_for_field( $field, $activity ) {
|
|
|
|
$recipients = array();
|
|
|
|
if ( array_key_exists( $field, $activity ) ) {
|
2018-09-21 19:46:25 +00:00
|
|
|
foreach ( $activity[$field] as $object ) {
|
|
|
|
$recipients = array_merge( $recipients, retrieve_recipients( $object , 0 ) );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
}
|
|
|
|
|
2018-09-21 19:46:25 +00:00
|
|
|
function retrieve_recipients( $object, $depth ) {
|
|
|
|
if ( $depth === 30 ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
if ( !array_key_exists( 'type', $object ) ) {
|
2018-09-10 00:48:14 +00:00
|
|
|
return new \WP_Error(
|
2018-09-21 19:46:25 +00:00
|
|
|
'invalid_object', __( 'Expected an object type', 'pterotype' ), array( 'status' => 400 )
|
2018-09-10 00:48:14 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-21 19:46:25 +00:00
|
|
|
switch ( $object['type'] ) {
|
2018-09-10 00:48:14 +00:00
|
|
|
case 'Collection':
|
|
|
|
case 'OrderedCollection':
|
|
|
|
$items = array();
|
|
|
|
$recipients = array();
|
2018-09-21 19:46:25 +00:00
|
|
|
if ( array_key_exists( 'items', $object ) ) {
|
|
|
|
$items = $object['items'];
|
|
|
|
} else if ( array_key_exists( 'orderedItems', $object ) ) {
|
|
|
|
$items = $object['orderedItems'];
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
if ( count( $items ) > 0 ) {
|
|
|
|
// recursive case: call retrieve_recipients on each $item
|
|
|
|
// merge the results and return them
|
|
|
|
foreach ( $items as $item ) {
|
2018-09-21 19:46:25 +00:00
|
|
|
$recipients[] = retrieve_recipients( $item, $depth + 1 );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
2018-09-21 19:46:25 +00:00
|
|
|
case 'Link':
|
|
|
|
if ( !array_key_exists( 'href', $object ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_link',
|
|
|
|
__( 'Link requires an "href" field', 'pterotype' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$link_target = wp_remote_retrieve_body( wp_remote_get ( $response_body['href'] ) );
|
|
|
|
return retrieve_recipients( $link_target, $depth + 1 );
|
2018-09-10 00:48:14 +00:00
|
|
|
default: // an actor
|
|
|
|
if ( array_key_exists( 'inbox', $response_body ) ) {
|
|
|
|
return array( $response_body['inbox'] );
|
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function post_activity_to_inboxes( $activity, $recipients ) {
|
|
|
|
foreach ( $inbox as $recipients ) {
|
2018-09-10 00:51:59 +00:00
|
|
|
$args = array(
|
|
|
|
'body' => $activity,
|
|
|
|
'headers' => array( 'Content-Type' => 'application/ld+json' )
|
|
|
|
);
|
|
|
|
// TODO do something with the result?
|
|
|
|
wp_remote_post( $inbox, $args );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|