| @@ -20,8 +20,15 @@ | ||
| 20 | 20 | array( self::class, 'handle_create' ), |
| 21 | 21 | 10, |
| 22 | 22 | 3 |
| 23 | 23 | ); |
| 24 | + | |
| 25 | + \add_filter( | |
| 26 | + 'activitypub_validate_object', | |
| 27 | + array( self::class, 'validate_object' ), | |
| 28 | + 10, | |
| 29 | + 3 | |
| 30 | + ); | |
| 24 | 31 | } |
| 25 | 32 | |
| 26 | 33 | /** |
| 27 | 34 | * Handles "Create" requests |
| @@ -32,19 +39,8 @@ | ||
| 32 | 39 | * |
| 33 | 40 | * @return void |
| 34 | 41 | */ |
| 35 | 42 | public static function handle_create( $array, $user_id, $object = null ) { |
| 36 | - if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { | |
| 37 | - return; | |
| 38 | - } | |
| 39 | - | |
| 40 | - if ( | |
| 41 | - ! isset( $array['object'] ) || | |
| 42 | - ! isset( $array['object']['id'] ) | |
| 43 | - ) { | |
| 44 | - return; | |
| 45 | - } | |
| 46 | - | |
| 47 | 43 | // check if Activity is public or not |
| 48 | 44 | if ( ! is_activity_public( $array ) ) { |
| 49 | 45 | // @todo maybe send email |
| 50 | 46 | return; |
| @@ -60,11 +56,45 @@ | ||
| 60 | 56 | |
| 61 | 57 | $state = Interactions::add_comment( $array ); |
| 62 | 58 | $reaction = null; |
| 63 | 59 | |
| 64 | - if ( $state && ! \is_wp_error( $reaction ) ) { | |
| 60 | + if ( $state && ! \is_wp_error( $state ) ) { | |
| 65 | 61 | $reaction = \get_comment( $state ); |
| 66 | 62 | } |
| 67 | 63 | |
| 68 | 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; | |
| 69 | 99 | } |
| 70 | 100 | } |