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-15 19:18:20 +00:00
|
|
|
foreach ( $activity[$field] as $url ) {
|
2018-09-10 00:48:14 +00:00
|
|
|
$recipients = array_merge( $recipients, retrieve_recipients( $url ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve_recipients( $url ) {
|
|
|
|
// TODO add an arg to keep track of recursion and cut off after 30 recursions
|
|
|
|
$response_body = wp_remote_retrieve_body( wp_remote_get ( $url ) );
|
|
|
|
// possible responses:
|
|
|
|
// - actor json
|
|
|
|
// - collection
|
|
|
|
if ( !array_key_exists( 'type', $response_body ) ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'invalid_object', __( 'Expected an object type', 'pterotype' )
|
2018-09-10 00:48:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
switch ( $response_body['type'] ) {
|
|
|
|
case 'Collection':
|
|
|
|
case 'OrderedCollection':
|
|
|
|
$items = array();
|
|
|
|
$recipients = array();
|
|
|
|
if ( array_key_exists( 'items', $response_body ) ) {
|
|
|
|
$items = $response_body['items'];
|
|
|
|
} else if ( array_key_exists( 'orderedItems', $response_body ) ) {
|
|
|
|
$items = $response_body['orderedItems'];
|
|
|
|
}
|
|
|
|
if ( count( $items ) > 0 ) {
|
|
|
|
// recursive case: call retrieve_recipients on each $item
|
|
|
|
// merge the results and return them
|
|
|
|
foreach ( $items as $item ) {
|
|
|
|
// TODO what will $item look like? Could be an actor JSON, or just a URL?
|
|
|
|
$recipients[] = retrieve_recipients( $item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|