| @@ -1,19 +1,26 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Create handler file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Handler; |
| 3 | 9 | |
| 4 | -use WP_Error; | |
| 5 | 10 | use Activitypub\Collection\Interactions; |
| 6 | 11 | |
| 12 | +use function Activitypub\is_self_ping; | |
| 13 | +use function Activitypub\is_activity_reply; | |
| 7 | 14 | use function Activitypub\is_activity_public; |
| 8 | 15 | use function Activitypub\object_id_to_comment; |
| 9 | 16 | |
| 10 | 17 | /** |
| 11 | - * Handle Create requests | |
| 18 | + * Handle Create requests. | |
| 12 | 19 | */ |
| 13 | 20 | class Create { |
| 14 | 21 | /** |
| 15 | - * Initialize the class, registering WordPress hooks | |
| 22 | + * Initialize the class, registering WordPress hooks. | |
| 16 | 23 | */ |
| 17 | 24 | public static function init() { |
| 18 | 25 | \add_action( |
| 19 | 26 | 'activitypub_inbox_create', |
| @@ -20,47 +27,108 @@ | ||
| 20 | 27 | array( self::class, 'handle_create' ), |
| 21 | 28 | 10, |
| 22 | 29 | 3 |
| 23 | 30 | ); |
| 31 | + | |
| 32 | + \add_filter( | |
| 33 | + 'activitypub_validate_object', | |
| 34 | + array( self::class, 'validate_object' ), | |
| 35 | + 10, | |
| 36 | + 3 | |
| 37 | + ); | |
| 24 | 38 | } |
| 25 | 39 | |
| 26 | 40 | /** |
| 27 | - * Handles "Create" requests | |
| 41 | + * Handles "Create" requests. | |
| 28 | 42 | * |
| 29 | - * @param array $array The activity-object | |
| 30 | - * @param int $user_id The id of the local blog-user | |
| 31 | - * @param Activitypub\Activity $object The activity object | |
| 32 | - * | |
| 33 | - * @return void | |
| 43 | + * @param array $activity The activity-object. | |
| 44 | + * @param int $user_id The id of the local blog-user. | |
| 45 | + * @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null. | |
| 34 | 46 | */ |
| 35 | - public static function handle_create( $array, $user_id, $object = null ) { | |
| 47 | + public static function handle_create( $activity, $user_id, $activity_object = null ) { | |
| 48 | + // Check if Activity is public or not. | |
| 36 | 49 | if ( |
| 37 | - ! isset( $array['object'] ) || | |
| 38 | - ! isset( $array['object']['id'] ) | |
| 50 | + ! is_activity_public( $activity ) || | |
| 51 | + ! is_activity_reply( $activity ) | |
| 39 | 52 | ) { |
| 40 | 53 | return; |
| 41 | 54 | } |
| 42 | 55 | |
| 43 | - // check if Activity is public or not | |
| 44 | - if ( ! is_activity_public( $array ) ) { | |
| 45 | - // @todo maybe send email | |
| 56 | + $check_dupe = object_id_to_comment( $activity['object']['id'] ); | |
| 57 | + | |
| 58 | + // If comment exists, call update action. | |
| 59 | + if ( $check_dupe ) { | |
| 60 | + /** | |
| 61 | + * Fires when a Create activity is received for an existing comment. | |
| 62 | + * | |
| 63 | + * @param array $activity The activity-object. | |
| 64 | + * @param int $user_id The id of the local blog-user. | |
| 65 | + * @param \WP_Comment|\WP_Error $check_dupe The comment object or WP_Error. | |
| 66 | + * @param \Activitypub\Activity\Activity $activity_object The activity object. | |
| 67 | + */ | |
| 68 | + \do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object ); | |
| 46 | 69 | return; |
| 47 | 70 | } |
| 48 | 71 | |
| 49 | - $check_dupe = object_id_to_comment( $array['object']['id'] ); | |
| 50 | - | |
| 51 | - // if comment exists, call update action | |
| 52 | - if ( $check_dupe ) { | |
| 53 | - \do_action( 'activitypub_inbox_update', $array, $user_id, $object ); | |
| 72 | + if ( is_self_ping( $activity['object']['id'] ) ) { | |
| 54 | 73 | return; |
| 55 | 74 | } |
| 56 | 75 | |
| 57 | - $state = Interactions::add_comment( $array ); | |
| 76 | + $state = Interactions::add_comment( $activity ); | |
| 58 | 77 | $reaction = null; |
| 59 | 78 | |
| 60 | - if ( $state && ! \is_wp_error( $reaction ) ) { | |
| 79 | + if ( $state && ! \is_wp_error( $state ) ) { | |
| 61 | 80 | $reaction = \get_comment( $state ); |
| 62 | 81 | } |
| 63 | 82 | |
| 64 | - \do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction ); | |
| 83 | + /** | |
| 84 | + * Fires after a Create activity has been handled. | |
| 85 | + * | |
| 86 | + * @param array $activity The activity-object. | |
| 87 | + * @param int $user_id The id of the local blog-user. | |
| 88 | + * @param \WP_Comment|\WP_Error $state The comment object or WP_Error. | |
| 89 | + * @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error. | |
| 90 | + */ | |
| 91 | + \do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Validate the object. | |
| 96 | + * | |
| 97 | + * @param bool $valid The validation state. | |
| 98 | + * @param string $param The object parameter. | |
| 99 | + * @param \WP_REST_Request $request The request object. | |
| 100 | + * | |
| 101 | + * @return bool The validation state: true if valid, false if not. | |
| 102 | + */ | |
| 103 | + public static function validate_object( $valid, $param, $request ) { | |
| 104 | + $json_params = $request->get_json_params(); | |
| 105 | + | |
| 106 | + if ( empty( $json_params['type'] ) ) { | |
| 107 | + return false; | |
| 108 | + } | |
| 109 | + | |
| 110 | + if ( | |
| 111 | + 'Create' !== $json_params['type'] || | |
| 112 | + is_wp_error( $request ) | |
| 113 | + ) { | |
| 114 | + return $valid; | |
| 115 | + } | |
| 116 | + | |
| 117 | + $object = $json_params['object']; | |
| 118 | + | |
| 119 | + if ( ! is_array( $object ) ) { | |
| 120 | + return false; | |
| 121 | + } | |
| 122 | + | |
| 123 | + $required = array( | |
| 124 | + 'id', | |
| 125 | + 'content', | |
| 126 | + ); | |
| 127 | + | |
| 128 | + if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { | |
| 129 | + return false; | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $valid; | |
| 65 | 133 | } |
| 66 | 134 | } |