2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-10-03 08:59:21 +08:00

Convert HTML entities in titles before sending to Discourse (#504)

* Convert HTML entities in titles before sending to Discourse

* Fix linting
This commit is contained in:
Angus McLeod 2024-03-22 21:34:41 +01:00 committed by GitHub
parent 16a44d2f1b
commit 7d34ae81c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View file

@ -839,14 +839,14 @@ class DiscoursePublish extends DiscourseBase {
}

/**
* Strip html tags from titles before passing them to Discourse.
* Strip html tags and convert HTML entities before passing them to Discourse.
*
* @param string $title The title of the post.
*
* @return string
*/
protected function sanitize_title( $title ) {
return wp_strip_all_tags( $title );
return wp_specialchars_decode( wp_strip_all_tags( $title ) );
}

/**

View file

@ -854,6 +854,46 @@ class DiscoursePublishTest extends UnitTest {
wp_delete_post( $post_id );
}

/**
* Test that HTML entities are converted to their special characters.
*/
public function test_conversion_of_html_entities_in_title() {
$title_with_entities = 'Title with &';
$title_with_decoded_entities = 'Title with &';
self::$post_atts['post_title'] = $title_with_entities;

$response = $this->build_response( 'success' );
$response['body'] = $this->response_body_json( 'post_create' );

add_filter(
'pre_http_request',
function( $prempt, $args, $url ) use ( $response, $title_with_decoded_entities ) {
$body = json_decode( $args['body'] );

if ( $body->title !== $title_with_decoded_entities ) {
return new \WP_Error( 'http_request_failed', 'Failed to decode title' );
} else {
return $response;
}
},
10,
3
);

// Setup post.
$post_id = wp_insert_post( self::$post_atts, false, false );

// Run the publication.
$post = get_post( $post_id );
$this->publish->publish_post_after_save( $post_id, $post );

// Ensure publication occurs.
$this->assertEquals( get_post_meta( $post_id, 'wpdc_publishing_response', true ), 'success' );

// Cleanup.
wp_delete_post( $post_id );
}

/**
* Posts can only be published via XMLRPC by hooking into the wp_discourse_before_xmlrpc_publish filter with a function
* that returns `true`.