Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert HTML entities in titles before sending to Discourse #504

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,14 +839,14 @@ protected function get_excluded_tag_slugs() {
}

/**
* 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 ) );
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/test-discourse-publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,46 @@ public function test_force_publish_max_age_prevents_older_posts_from_being_publi
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`.
Expand Down
Loading