I think <p> and <br /> are fine for all usecases

This commit is contained in:
Matthias Pfefferle 2023-12-05 12:21:29 +01:00
parent eecdb63da3
commit 15179f2c5a
3 changed files with 50 additions and 29 deletions

View file

@ -39,6 +39,8 @@ class Activitypub {
\add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) ); \add_action( 'in_plugin_update_message-' . ACTIVITYPUB_PLUGIN_BASENAME, array( self::class, 'plugin_update_message' ) );
\add_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 15, 2 );
// register several post_types // register several post_types
self::register_post_types(); self::register_post_types();
} }
@ -335,6 +337,32 @@ class Activitypub {
); );
} }
/**
* Adds line breaks to the list of allowed comment tags.
*
* @param array $allowed_tags Allowed HTML tags.
* @param string $context Context.
*
* @return array Filtered tag list.
*/
public static function allowed_comment_html( $allowed_tags, $context = '' ) {
if ( 'pre_comment_content' !== $context ) {
// Do nothing.
return $allowed_tags;
}
// Add `p` and `br` to the list of allowed tags.
if ( ! array_key_exists( 'br', $allowed_tags ) ) {
$allowed_tags['br'] = array();
}
if ( ! array_key_exists( 'p', $allowed_tags ) ) {
$allowed_tags['p'] = array();
}
return $allowed_tags;
}
/** /**
* Register the "Followers" Taxonomy * Register the "Followers" Taxonomy
* *

View file

@ -82,11 +82,9 @@ class Interactions {
return 'inactive'; return 'inactive';
} }
); );
\add_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10, 2 );
$comment = \wp_new_comment( $commentdata, true ); $comment = \wp_new_comment( $commentdata, true );
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10 );
\remove_filter( 'pre_option_require_name_email', '__return_false' ); \remove_filter( 'pre_option_require_name_email', '__return_false' );
// re-add flood control // re-add flood control
@ -132,11 +130,9 @@ class Interactions {
return 'inactive'; return 'inactive';
} }
); );
\add_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10, 2 );
$comment = \wp_update_comment( $commentdata, true ); $comment = \wp_update_comment( $commentdata, true );
\remove_filter( 'wp_kses_allowed_html', array( self::class, 'allowed_comment_html' ), 10 );
\remove_filter( 'pre_option_require_name_email', '__return_false' ); \remove_filter( 'pre_option_require_name_email', '__return_false' );
// re-add flood control // re-add flood control
@ -208,29 +204,4 @@ class Interactions {
$comment_query = new WP_Comment_Query( $args ); $comment_query = new WP_Comment_Query( $args );
return $comment_query->comments; return $comment_query->comments;
} }
/**
* 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;
}
} }

View file

@ -47,6 +47,21 @@ class Test_Activitypub_Interactions extends WP_UnitTestCase {
); );
} }
public function create_test_rich_object( $id = 'https://example.com/123' ) {
return array(
'actor' => $this->user_url,
'id' => 'https://example.com/id/' . microtime( true ),
'to' => [ $this->user_url ],
'cc' => [ 'https://www.w3.org/ns/activitystreams#Public' ],
'object' => array(
'id' => $id,
'url' => 'https://example.com/example',
'inReplyTo' => $this->post_permalink,
'content' => 'Hello<br />example<p>example</p><img src="https://example.com/image.jpg" />',
),
);
}
public function test_handle_create_basic() { public function test_handle_create_basic() {
$comment_id = Activitypub\Collection\Interactions::add_comment( $this->create_test_object() ); $comment_id = Activitypub\Collection\Interactions::add_comment( $this->create_test_object() );
$comment = get_comment( $comment_id, ARRAY_A ); $comment = get_comment( $comment_id, ARRAY_A );
@ -65,6 +80,13 @@ class Test_Activitypub_Interactions extends WP_UnitTestCase {
$this->assertEquals( 'activitypub', get_comment_meta( $comment_id, 'protocol', true ) ); $this->assertEquals( 'activitypub', get_comment_meta( $comment_id, 'protocol', true ) );
} }
public function test_handle_create_rich() {
$comment_id = Activitypub\Collection\Interactions::add_comment( $this->create_test_rich_object() );
$comment = get_comment( $comment_id, ARRAY_A );
$this->assertEquals( 'Hello<br />example<p>example</p>', $comment['comment_content'] );
}
public function test_convert_object_to_comment_not_reply_rejected() { public function test_convert_object_to_comment_not_reply_rejected() {
$object = $this->create_test_object(); $object = $this->create_test_object();
unset( $object['object']['inReplyTo'] ); unset( $object['object']['inReplyTo'] );