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

Add support for wrapping rss titles in CDATA #3104

Merged
merged 1 commit into from
May 9, 2024
Merged
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
32 changes: 32 additions & 0 deletions includes/optional-modules/class-rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static function init() {
add_filter( 'the_content_feed', [ __CLASS__, 'maybe_remove_content_featured_image' ], 1 );
add_filter( 'wpseo_include_rss_footer', [ __CLASS__, 'maybe_suppress_yoast' ] );
add_action( 'rss2_ns', [ __CLASS__, 'maybe_inject_yahoo_namespace' ] );
add_filter( 'the_title_rss', [ __CLASS__, 'maybe_wrap_titles_in_cdata' ] );
}

/**
Expand Down Expand Up @@ -76,6 +77,7 @@ public static function get_feed_settings( $feed_post = null ) {
'yahoo_namespace' => false,
'update_frequency' => false,
'use_post_id_as_guid' => false,
'cdata_titles' => false,
];

if ( ! $feed_post ) {
Expand Down Expand Up @@ -401,6 +403,13 @@ public static function render_technical_settings_metabox( $feed_post ) {
<input type="checkbox" name="yahoo_namespace" value="1" <?php checked( $settings['yahoo_namespace'] ); ?> />
</td>
</tr>
<tr>
<th><?php esc_html_e( 'Wrap the content of <title> elements in CDATA tags', 'newspack-plugin' ); ?></th>
<td>
<input type="hidden" name="cdata_titles" value="0" />
<input type="checkbox" name="cdata_titles" value="1" <?php checked( $settings['cdata_titles'] ); ?> />
</td>
</tr>
<?php if ( defined( 'WPSEO_VERSION' ) && WPSEO_VERSION ) : ?>
<tr>
<th>
Expand Down Expand Up @@ -478,6 +487,9 @@ public static function save_settings( $feed_post_id ) {
$use_post_id_as_guid = filter_input( INPUT_POST, 'use_post_id_as_guid', FILTER_SANITIZE_NUMBER_INT );
$settings['use_post_id_as_guid'] = (bool) $use_post_id_as_guid;

$cdata_titles = filter_input( INPUT_POST, 'cdata_titles', FILTER_SANITIZE_NUMBER_INT );
$settings['cdata_titles'] = (bool) $cdata_titles;

$category_settings = filter_input_array(
INPUT_POST,
[
Expand Down Expand Up @@ -695,5 +707,25 @@ public static function maybe_inject_yahoo_namespace() {
<?php
}
}

/**
* Wrap titles in CDATA tags if checked e.g. "<title><![CDATA[Post title]]></title>".
* This is useful for certain parsers that don't support titles with special characters in them.
*
* @param string $title Post title for RSS feed.
* @return string Modified $title.
*/
public static function maybe_wrap_titles_in_cdata( $title ) {
$settings = self::get_feed_settings();
if ( ! $settings ) {
return $title;
}

if ( $settings['cdata_titles'] ) {
$title = '<![CDATA[' . $title . ']]>';
}

return $title;
}
}
RSS::init();