| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler; |
| 9 |
|
| 10 |
use Activitypub\Collection\Followers; |
| 11 |
use Activitypub\Collection\Interactions; |
| 12 |
|
| 13 |
use function Activitypub\get_remote_metadata_by_actor; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handle Update requests. |
| 17 |
*/ |
| 18 |
class Update { |
| 19 |
/** |
| 20 |
* Initialize the class, registering WordPress hooks. |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
\add_action( |
| 24 |
'activitypub_inbox_update', |
| 25 |
array( self::class, 'handle_update' ) |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Handle "Update" requests. |
| 31 |
* |
| 32 |
* @param array $activity The Activity object. |
| 33 |
*/ |
| 34 |
public static function handle_update( $activity ) { |
| 35 |
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : ''; |
| 36 |
|
| 37 |
switch ( $object_type ) { |
| 38 |
/* |
| 39 |
* Actor Types. |
| 40 |
* |
| 41 |
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types |
| 42 |
*/ |
| 43 |
case 'Person': |
| 44 |
case 'Group': |
| 45 |
case 'Organization': |
| 46 |
case 'Service': |
| 47 |
case 'Application': |
| 48 |
self::update_actor( $activity ); |
| 49 |
break; |
| 50 |
|
| 51 |
/* |
| 52 |
* Object and Link Types. |
| 53 |
* |
| 54 |
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types |
| 55 |
*/ |
| 56 |
case 'Note': |
| 57 |
case 'Article': |
| 58 |
case 'Image': |
| 59 |
case 'Audio': |
| 60 |
case 'Video': |
| 61 |
case 'Event': |
| 62 |
case 'Document': |
| 63 |
self::update_interaction( $activity ); |
| 64 |
break; |
| 65 |
|
| 66 |
/* |
| 67 |
* Minimal Activity. |
| 68 |
* |
| 69 |
* @see https://www.w3.org/TR/activitystreams-core/#example-1 |
| 70 |
*/ |
| 71 |
default: |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Update an Interaction. |
| 78 |
* |
| 79 |
* @param array $activity The Activity object. |
| 80 |
*/ |
| 81 |
public static function update_interaction( $activity ) { |
| 82 |
$commentdata = Interactions::update_comment( $activity ); |
| 83 |
$reaction = null; |
| 84 |
|
| 85 |
if ( ! empty( $commentdata['comment_ID'] ) ) { |
| 86 |
$state = 1; |
| 87 |
$reaction = \get_comment( $commentdata['comment_ID'] ); |
| 88 |
} else { |
| 89 |
$state = $commentdata; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Fires after an Update activity has been handled. |
| 94 |
* |
| 95 |
* @param array $activity The complete Update activity data. |
| 96 |
* @param null $user Always null for Update activities. |
| 97 |
* @param int|array $state 1 if comment was updated successfully, error data otherwise. |
| 98 |
* @param \WP_Comment|null $reaction The updated comment object if successful, null otherwise. |
| 99 |
*/ |
| 100 |
\do_action( 'activitypub_handled_update', $activity, null, $state, $reaction ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Update an Actor. |
| 105 |
* |
| 106 |
* @param array $activity The Activity object. |
| 107 |
*/ |
| 108 |
public static function update_actor( $activity ) { |
| 109 |
// Update cache. |
| 110 |
$actor = get_remote_metadata_by_actor( $activity['actor'], false ); |
| 111 |
|
| 112 |
if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$follower = Followers::get_follower_by_actor( $actor['id'] ); |
| 117 |
|
| 118 |
if ( ! $follower ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$follower->from_array( $actor ); |
| 123 |
$follower->upsert(); |
| 124 |
} |
| 125 |
} |
| 126 |
|