| @@ -14,9 +14,21 @@ | ||
| 14 | 14 | /** |
| 15 | 15 | * Initialize the class, registering WordPress hooks |
| 16 | 16 | */ |
| 17 | 17 | public static function init() { |
| 18 | - \add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 3 ); | |
| 18 | + \add_action( | |
| 19 | + 'activitypub_inbox_create', | |
| 20 | + array( self::class, 'handle_create' ), | |
| 21 | + 10, | |
| 22 | + 3 | |
| 23 | + ); | |
| 24 | + | |
| 25 | + \add_filter( | |
| 26 | + 'activitypub_validate_object', | |
| 27 | + array( self::class, 'validate_object' ), | |
| 28 | + 10, | |
| 29 | + 3 | |
| 30 | + ); | |
| 19 | 31 | } |
| 20 | 32 | |
| 21 | 33 | /** |
| 22 | 34 | * Handles "Create" requests |
| @@ -27,15 +39,8 @@ | ||
| 27 | 39 | * |
| 28 | 40 | * @return void |
| 29 | 41 | */ |
| 30 | 42 | public static function handle_create( $array, $user_id, $object = null ) { |
| 31 | - if ( | |
| 32 | - ! isset( $array['object'] ) || | |
| 33 | - ! isset( $array['object']['id'] ) | |
| 34 | - ) { | |
| 35 | - return; | |
| 36 | - } | |
| 37 | - | |
| 38 | 43 | // check if Activity is public or not |
| 39 | 44 | if ( ! is_activity_public( $array ) ) { |
| 40 | 45 | // @todo maybe send email |
| 41 | 46 | return; |
| @@ -51,11 +56,45 @@ | ||
| 51 | 56 | |
| 52 | 57 | $state = Interactions::add_comment( $array ); |
| 53 | 58 | $reaction = null; |
| 54 | 59 | |
| 55 | - if ( $state && ! \is_wp_error( $reaction ) ) { | |
| 60 | + if ( $state && ! \is_wp_error( $state ) ) { | |
| 56 | 61 | $reaction = \get_comment( $state ); |
| 57 | 62 | } |
| 58 | 63 | |
| 59 | 64 | \do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction ); |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Validate the object | |
| 69 | + * | |
| 70 | + * @param bool $valid The validation state | |
| 71 | + * @param string $param The object parameter | |
| 72 | + * @param \WP_REST_Request $request The request object | |
| 73 | + * @param array $array The activity-object | |
| 74 | + * | |
| 75 | + * @return bool The validation state: true if valid, false if not | |
| 76 | + */ | |
| 77 | + public static function validate_object( $valid, $param, $request ) { | |
| 78 | + $json_params = $request->get_json_params(); | |
| 79 | + | |
| 80 | + if ( | |
| 81 | + 'Create' !== $json_params['type'] || | |
| 82 | + is_wp_error( $request ) | |
| 83 | + ) { | |
| 84 | + return $valid; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $object = $json_params['object']; | |
| 88 | + $required = array( | |
| 89 | + 'id', | |
| 90 | + 'inReplyTo', | |
| 91 | + 'content', | |
| 92 | + ); | |
| 93 | + | |
| 94 | + if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { | |
| 95 | + return false; | |
| 96 | + } | |
| 97 | + | |
| 98 | + return $valid; | |
| 60 | 99 | } |
| 61 | 100 | } |