2018-09-27 22:28:49 +02:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-05-19 12:00:11 +02:00
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
2023-04-21 08:51:38 +02:00
|
|
|
use WP_REST_Response;
|
2023-07-03 17:59:42 +02:00
|
|
|
use Activitypub\Activity\Activity;
|
2023-07-03 19:56:06 +02:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2023-04-20 15:22:11 +02:00
|
|
|
|
2023-05-19 12:00:11 +02:00
|
|
|
use function Activitypub\get_context;
|
|
|
|
use function Activitypub\url_to_authorid;
|
2023-05-12 22:31:53 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
2023-05-19 12:00:11 +02:00
|
|
|
use function Activitypub\get_remote_metadata_by_actor;
|
2023-04-20 15:22:11 +02:00
|
|
|
|
2018-09-27 22:28:49 +02:00
|
|
|
/**
|
2019-02-24 13:01:28 +01:00
|
|
|
* ActivityPub Inbox REST-Class
|
2018-09-27 22:28:49 +02:00
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
2019-02-24 13:01:28 +01:00
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#inbox
|
2018-09-27 22:28:49 +02:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Inbox {
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
public static function init() {
|
2023-10-04 18:15:53 +02:00
|
|
|
self::register_routes();
|
2023-04-24 20:46:51 +02:00
|
|
|
|
2023-04-20 15:22:11 +02:00
|
|
|
\add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 2 );
|
2019-02-24 12:07:41 +01:00
|
|
|
}
|
2019-02-28 19:31:55 +01:00
|
|
|
|
2018-09-27 22:28:49 +02:00
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-01-27 13:09:11 +01:00
|
|
|
'/inbox',
|
|
|
|
array(
|
2018-09-27 22:28:49 +02:00
|
|
|
array(
|
2023-10-12 11:00:58 +02:00
|
|
|
'methods' => WP_REST_Server::CREATABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'shared_inbox_post' ),
|
2022-12-27 14:03:10 +01:00
|
|
|
'args' => self::shared_inbox_post_parameters(),
|
2020-09-18 16:36:09 +02:00
|
|
|
'permission_callback' => '__return_true',
|
2018-09-27 22:28:49 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2023-05-26 16:08:08 +02:00
|
|
|
'/users/(?P<user_id>[\w\-\.]+)/inbox',
|
2022-01-27 13:09:11 +01:00
|
|
|
array(
|
2018-09-27 22:28:49 +02:00
|
|
|
array(
|
2023-10-12 11:00:58 +02:00
|
|
|
'methods' => WP_REST_Server::CREATABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'user_inbox_post' ),
|
2022-12-27 14:03:10 +01:00
|
|
|
'args' => self::user_inbox_post_parameters(),
|
2020-09-18 16:36:09 +02:00
|
|
|
'permission_callback' => '__return_true',
|
2018-09-27 22:28:49 +02:00
|
|
|
),
|
2021-01-05 21:56:38 +01:00
|
|
|
array(
|
2023-05-19 12:00:11 +02:00
|
|
|
'methods' => WP_REST_Server::READABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'user_inbox_get' ),
|
2022-12-27 14:03:10 +01:00
|
|
|
'args' => self::user_inbox_get_parameters(),
|
2021-01-05 21:56:38 +01:00
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
2018-09-27 22:28:49 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
/**
|
|
|
|
* Renders the user-inbox
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2021-01-05 21:56:38 +01:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2021-07-26 09:48:51 +02:00
|
|
|
public static function user_inbox_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-07-03 19:56:06 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-01-05 21:56:38 +01:00
|
|
|
$page = $request->get_param( 'page', 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
2023-07-25 10:47:59 +02:00
|
|
|
\do_action( 'activitypub_rest_inbox_pre' );
|
2021-01-05 21:56:38 +01:00
|
|
|
|
|
|
|
$json = new \stdClass();
|
|
|
|
|
2023-05-19 12:00:11 +02:00
|
|
|
$json->{'@context'} = get_context();
|
2023-07-25 13:47:49 +02:00
|
|
|
$json->id = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) );
|
2021-01-05 21:56:38 +01:00
|
|
|
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
|
|
|
|
$json->type = 'OrderedCollectionPage';
|
2023-06-28 14:22:27 +02:00
|
|
|
$json->partOf = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) ); // phpcs:ignore
|
2021-01-05 21:56:38 +01:00
|
|
|
|
|
|
|
$json->totalItems = 0; // phpcs:ignore
|
|
|
|
|
|
|
|
$json->orderedItems = array(); // phpcs:ignore
|
|
|
|
|
|
|
|
$json->first = $json->partOf; // phpcs:ignore
|
|
|
|
|
|
|
|
// filter output
|
2023-08-09 13:07:30 +02:00
|
|
|
$json = \apply_filters( 'activitypub_rest_inbox_array', $json );
|
2021-01-05 21:56:38 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd after the ActivityPub profile has been created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_inbox_post' );
|
|
|
|
|
2023-10-23 14:57:58 +02:00
|
|
|
$rest_response = new WP_REST_Response( $json, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
2021-01-05 21:56:38 +01:00
|
|
|
|
2023-10-23 14:57:58 +02:00
|
|
|
return $rest_response;
|
2021-01-05 21:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles user-inbox requests
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2019-02-28 19:31:55 +01:00
|
|
|
*
|
2018-12-20 11:33:08 +01:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2021-01-05 21:56:38 +01:00
|
|
|
public static function user_inbox_post( $request ) {
|
2020-02-21 11:11:03 +01:00
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-07-05 15:33:07 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
2019-11-27 08:25:04 +01:00
|
|
|
|
2023-09-05 21:03:25 +02:00
|
|
|
$data = $request->get_json_params();
|
2020-02-21 11:11:03 +01:00
|
|
|
$type = $request->get_param( 'type' );
|
2021-11-17 21:11:34 +01:00
|
|
|
$type = \strtolower( $type );
|
2018-10-02 22:16:47 +02:00
|
|
|
|
2023-06-28 14:22:27 +02:00
|
|
|
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type );
|
|
|
|
\do_action( "activitypub_inbox_{$type}", $data, $user->get__id() );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2023-10-23 14:57:58 +02:00
|
|
|
$rest_response = new WP_REST_Response( array(), 202 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2018-09-27 22:28:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* The shared inbox
|
|
|
|
*
|
2020-07-21 09:23:35 +02:00
|
|
|
* @param WP_REST_Request $request
|
2018-09-30 22:51:22 +02:00
|
|
|
*
|
2020-07-21 09:23:35 +02:00
|
|
|
* @return WP_REST_Response
|
2018-09-30 22:51:22 +02:00
|
|
|
*/
|
2021-01-05 21:56:38 +01:00
|
|
|
public static function shared_inbox_post( $request ) {
|
2023-09-05 21:03:25 +02:00
|
|
|
$data = $request->get_json_params();
|
2021-01-09 01:26:26 +01:00
|
|
|
$type = $request->get_param( 'type' );
|
|
|
|
$users = self::extract_recipients( $data );
|
|
|
|
|
|
|
|
if ( ! $users ) {
|
2023-05-19 12:00:11 +02:00
|
|
|
return new WP_Error(
|
2022-01-27 13:09:11 +01:00
|
|
|
'rest_invalid_param',
|
|
|
|
\__( 'No recipients found', 'activitypub' ),
|
|
|
|
array(
|
2023-10-24 14:54:03 +02:00
|
|
|
'status' => 400,
|
2022-01-27 13:09:11 +01:00
|
|
|
'params' => array(
|
|
|
|
'to' => \__( 'Please check/validate "to" field', 'activitypub' ),
|
|
|
|
'bto' => \__( 'Please check/validate "bto" field', 'activitypub' ),
|
|
|
|
'cc' => \__( 'Please check/validate "cc" field', 'activitypub' ),
|
|
|
|
'bcc' => \__( 'Please check/validate "bcc" field', 'activitypub' ),
|
|
|
|
'audience' => \__( 'Please check/validate "audience" field', 'activitypub' ),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2021-01-09 01:26:26 +01:00
|
|
|
}
|
2020-07-21 09:23:35 +02:00
|
|
|
|
|
|
|
foreach ( $users as $user ) {
|
2023-09-01 18:32:56 +02:00
|
|
|
$user = User_Collection::get_by_various( $user );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:11:34 +01:00
|
|
|
$type = \strtolower( $type );
|
|
|
|
|
2021-01-09 01:26:26 +01:00
|
|
|
\do_action( 'activitypub_inbox', $data, $user->ID, $type );
|
|
|
|
\do_action( "activitypub_inbox_{$type}", $data, $user->ID );
|
2020-07-21 09:23:35 +02:00
|
|
|
}
|
2019-11-18 20:57:00 +01:00
|
|
|
|
2023-10-23 14:57:58 +02:00
|
|
|
$rest_response = new WP_REST_Response( array(), 202 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2018-09-27 22:28:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
2022-12-27 14:03:10 +01:00
|
|
|
public static function user_inbox_get_parameters() {
|
2018-09-27 22:28:49 +02:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
2020-02-21 11:11:03 +01:00
|
|
|
$params['user_id'] = array(
|
2018-09-27 22:28:49 +02:00
|
|
|
'required' => true,
|
2023-05-24 16:32:00 +02:00
|
|
|
'type' => 'string',
|
2022-12-27 14:03:10 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function user_inbox_post_parameters() {
|
2018-09-27 22:28:49 +02:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
2020-02-21 11:11:03 +01:00
|
|
|
$params['user_id'] = array(
|
2018-09-27 22:28:49 +02:00
|
|
|
'required' => true,
|
2023-05-24 16:32:00 +02:00
|
|
|
'type' => 'string',
|
2018-09-27 22:28:49 +02:00
|
|
|
);
|
|
|
|
|
2020-02-21 11:11:03 +01:00
|
|
|
$params['id'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'sanitize_callback' => 'esc_url_raw',
|
2023-04-15 07:53:43 +02:00
|
|
|
);
|
|
|
|
|
2020-02-21 11:11:03 +01:00
|
|
|
$params['actor'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
2023-11-21 15:05:12 +01:00
|
|
|
if ( \is_array( $param ) ) {
|
|
|
|
if ( isset( $param['id'] ) ) {
|
|
|
|
$param = $param['id'];
|
|
|
|
} else {
|
|
|
|
$param = $param['url'];
|
|
|
|
}
|
2020-02-21 11:11:03 +01:00
|
|
|
}
|
|
|
|
return \esc_url_raw( $param );
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['type'] = array(
|
|
|
|
'required' => true,
|
2020-05-04 00:06:48 +02:00
|
|
|
//'type' => 'enum',
|
|
|
|
//'enum' => array( 'Create' ),
|
2021-11-17 21:11:34 +01:00
|
|
|
//'sanitize_callback' => function( $param, $request, $key ) {
|
2023-07-20 03:39:58 +02:00
|
|
|
// return \strtolower( $param );
|
2021-11-17 21:11:34 +01:00
|
|
|
//},
|
2020-07-21 09:23:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$params['object'] = array(
|
|
|
|
'required' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
2022-12-27 14:03:10 +01:00
|
|
|
public static function shared_inbox_post_parameters() {
|
2020-07-21 09:23:35 +02:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['id'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
|
|
|
'sanitize_callback' => 'esc_url_raw',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['actor'] = array(
|
|
|
|
'required' => true,
|
|
|
|
//'type' => array( 'object', 'string' ),
|
2020-02-21 11:11:03 +01:00
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
2020-07-21 09:23:35 +02:00
|
|
|
if ( ! \is_string( $param ) ) {
|
|
|
|
$param = $param['id'];
|
|
|
|
}
|
|
|
|
return \esc_url_raw( $param );
|
2020-02-21 11:11:03 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
$params['type'] = array(
|
|
|
|
'required' => true,
|
|
|
|
//'type' => 'enum',
|
|
|
|
//'enum' => array( 'Create' ),
|
2021-11-17 21:11:34 +01:00
|
|
|
//'sanitize_callback' => function( $param, $request, $key ) {
|
2023-07-20 03:39:58 +02:00
|
|
|
// return \strtolower( $param );
|
2021-11-17 21:11:34 +01:00
|
|
|
//},
|
2020-07-21 09:23:35 +02:00
|
|
|
);
|
|
|
|
|
2020-02-21 11:11:03 +01:00
|
|
|
$params['object'] = array(
|
|
|
|
'required' => true,
|
2020-05-04 00:06:48 +02:00
|
|
|
//'type' => 'object',
|
2020-02-21 11:11:03 +01:00
|
|
|
);
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
$params['to'] = array(
|
2021-01-09 01:26:26 +01:00
|
|
|
'required' => false,
|
2020-07-21 09:23:35 +02:00
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['cc'] = array(
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['bcc'] = array(
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-27 22:28:49 +02:00
|
|
|
return $params;
|
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
|
|
|
|
/**
|
2019-09-08 04:32:58 +02:00
|
|
|
* Handles "Create" requests
|
2018-12-20 11:33:08 +01:00
|
|
|
*
|
|
|
|
* @param array $object The activity-object
|
|
|
|
* @param int $user_id The id of the local blog-user
|
|
|
|
*/
|
|
|
|
public static function handle_create( $object, $user_id ) {
|
2023-05-19 12:00:11 +02:00
|
|
|
$meta = get_remote_metadata_by_actor( $object['actor'] );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2022-11-09 15:08:32 +01:00
|
|
|
if ( ! isset( $object['object']['inReplyTo'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-27 14:43:37 +01:00
|
|
|
// check if Activity is public or not
|
|
|
|
if ( ! self::is_activity_public( $object ) ) {
|
|
|
|
// @todo maybe send email
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:46:03 +01:00
|
|
|
$comment_post_id = \url_to_postid( $object['object']['inReplyTo'] );
|
|
|
|
|
|
|
|
// save only replys and reactions
|
|
|
|
if ( ! $comment_post_id ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
$commentdata = array(
|
2020-12-18 17:46:03 +01:00
|
|
|
'comment_post_ID' => $comment_post_id,
|
2019-09-27 10:12:59 +02:00
|
|
|
'comment_author' => \esc_attr( $meta['name'] ),
|
|
|
|
'comment_author_url' => \esc_url_raw( $object['actor'] ),
|
2023-11-13 10:19:35 +01:00
|
|
|
'comment_content' => addslashes( \wp_kses( $object['object']['content'], 'pre_comment_content' ) ),
|
2023-08-02 18:19:21 +02:00
|
|
|
'comment_type' => 'comment',
|
2018-12-20 11:33:08 +01:00
|
|
|
'comment_author_email' => '',
|
|
|
|
'comment_parent' => 0,
|
|
|
|
'comment_meta' => array(
|
2019-09-27 10:12:59 +02:00
|
|
|
'source_url' => \esc_url_raw( $object['object']['url'] ),
|
|
|
|
'avatar_url' => \esc_url_raw( $meta['icon']['url'] ),
|
2018-12-20 11:33:08 +01:00
|
|
|
'protocol' => 'activitypub',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2020-12-28 23:33:26 +01:00
|
|
|
// disable flood control
|
|
|
|
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
|
|
|
|
|
2020-10-09 13:19:17 +02:00
|
|
|
// do not require email for AP entries
|
2020-12-18 17:36:07 +01:00
|
|
|
\add_filter( 'pre_option_require_name_email', '__return_false' );
|
|
|
|
|
2023-08-16 17:12:31 +02:00
|
|
|
// No nonce possible for this submission route
|
|
|
|
\add_filter(
|
|
|
|
'akismet_comment_nonce',
|
|
|
|
function() {
|
|
|
|
return 'inactive';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-13 10:19:35 +01:00
|
|
|
\add_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10, 2 );
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$state = \wp_new_comment( $commentdata, true );
|
2020-10-09 13:19:17 +02:00
|
|
|
|
2023-11-13 10:19:35 +01:00
|
|
|
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ) );
|
2020-12-18 17:36:07 +01:00
|
|
|
\remove_filter( 'pre_option_require_name_email', '__return_false' );
|
2020-12-28 23:33:26 +01:00
|
|
|
|
|
|
|
// re-add flood control
|
|
|
|
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
|
2023-08-02 19:03:32 +02:00
|
|
|
|
|
|
|
do_action( 'activitypub_handled_create', $object, $user_id, $state, $commentdata );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
2021-01-09 01:26:26 +01:00
|
|
|
|
2022-12-27 14:43:37 +01:00
|
|
|
/**
|
|
|
|
* Extract recipient URLs from Activity object
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array The list of user URLs
|
|
|
|
*/
|
2021-01-09 01:26:26 +01:00
|
|
|
public static function extract_recipients( $data ) {
|
2022-12-27 14:43:37 +01:00
|
|
|
$recipient_items = array();
|
2021-01-09 01:26:26 +01:00
|
|
|
|
|
|
|
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
|
|
|
|
if ( array_key_exists( $i, $data ) ) {
|
2023-02-16 09:12:01 +01:00
|
|
|
if ( is_array( $data[ $i ] ) ) {
|
|
|
|
$recipient = $data[ $i ];
|
|
|
|
} else {
|
|
|
|
$recipient = array( $data[ $i ] );
|
|
|
|
}
|
|
|
|
$recipient_items = array_merge( $recipient_items, $recipient );
|
2021-01-09 01:26:26 +01:00
|
|
|
}
|
|
|
|
|
2023-10-21 11:23:05 +02:00
|
|
|
if ( is_array( $data['object'] ) && array_key_exists( $i, $data['object'] ) ) {
|
2023-02-16 09:12:01 +01:00
|
|
|
if ( is_array( $data['object'][ $i ] ) ) {
|
|
|
|
$recipient = $data['object'][ $i ];
|
|
|
|
} else {
|
|
|
|
$recipient = array( $data['object'][ $i ] );
|
|
|
|
}
|
|
|
|
$recipient_items = array_merge( $recipient_items, $recipient );
|
2021-01-09 01:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 14:43:37 +01:00
|
|
|
$recipients = array();
|
|
|
|
|
|
|
|
// flatten array
|
|
|
|
foreach ( $recipient_items as $recipient ) {
|
|
|
|
if ( is_array( $recipient ) ) {
|
|
|
|
// check if recipient is an object
|
|
|
|
if ( array_key_exists( 'id', $recipient ) ) {
|
|
|
|
$recipients[] = $recipient['id'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$recipients[] = $recipient;
|
2021-01-09 01:26:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 14:43:37 +01:00
|
|
|
return array_unique( $recipients );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get local user recipients
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array The list of local users
|
|
|
|
*/
|
|
|
|
public static function get_recipients( $data ) {
|
|
|
|
$recipients = self::extract_recipients( $data );
|
|
|
|
$users = array();
|
2021-01-09 01:26:26 +01:00
|
|
|
|
|
|
|
foreach ( $recipients as $recipient ) {
|
2023-05-19 12:00:11 +02:00
|
|
|
$user_id = url_to_authorid( $recipient );
|
2021-01-09 01:26:26 +01:00
|
|
|
|
|
|
|
$user = get_user_by( 'id', $user_id );
|
|
|
|
|
|
|
|
if ( $user ) {
|
|
|
|
$users[] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
2022-12-27 14:43:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if passed Activity is Public
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_activity_public( $data ) {
|
|
|
|
$recipients = self::extract_recipients( $data );
|
|
|
|
|
|
|
|
return in_array( 'https://www.w3.org/ns/activitystreams#Public', $recipients, true );
|
|
|
|
}
|
2023-11-13 10:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds line breaks to the list of allowed comment tags.
|
|
|
|
*
|
|
|
|
* @param array $allowedtags Allowed HTML tags.
|
|
|
|
* @param string $context Context.
|
|
|
|
* @return array Filtered tag list.
|
|
|
|
*/
|
|
|
|
public static function allowed_comment_html( $allowedtags, $context = '' ) {
|
|
|
|
if ( 'pre_comment_content' !== $context ) {
|
|
|
|
// Do nothing.
|
|
|
|
return $allowedtags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add `p` and `br` to the list of allowed tags.
|
|
|
|
if ( ! array_key_exists( 'br', $allowedtags ) ) {
|
|
|
|
$allowedtags['br'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! array_key_exists( 'p', $allowedtags ) ) {
|
|
|
|
$allowedtags['p'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $allowedtags;
|
|
|
|
}
|
2018-09-27 22:28:49 +02:00
|
|
|
}
|