diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 7aa50f7862..81be17b79d 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -2885,10 +2885,20 @@ final class WP_Customize_Manager { $this->store_changeset_revision = $allow_revision; add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 ); - // Update the changeset post. The publish_customize_changeset action will cause the settings in the changeset to be saved via WP_Customize_Setting::save(). + /* + * Update the changeset post. The publish_customize_changeset action + * will cause the settings in the changeset to be saved via + * WP_Customize_Setting::save(). + */ + + // Prevent content filters from corrupting JSON in post_content. $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) ); if ( $has_kses ) { - kses_remove_filters(); // Prevent KSES from corrupting JSON in post_content. + kses_remove_filters(); + } + $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) ); + if ( $has_targeted_link_rel_filters ) { + wp_remove_targeted_link_rel_filters(); } // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values(). @@ -2918,9 +2928,15 @@ final class WP_Customize_Manager { $this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset. } } + + // Restore removed content filters. if ( $has_kses ) { kses_init_filters(); } + if ( $has_targeted_link_rel_filters ) { + wp_init_targeted_link_rel_filters(); + } + $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents. remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) ); diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 66d64485d6..1319826212 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -128,19 +128,7 @@ foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pr } // Add proper rel values for links with target. -foreach ( array( - 'title_save_pre', - 'content_save_pre', - 'excerpt_save_pre', - 'content_filtered_save_pre', - 'pre_comment_content', - 'pre_term_description', - 'pre_link_description', - 'pre_link_notes', - 'pre_user_description', -) as $filter ) { - add_filter( $filter, 'wp_targeted_link_rel' ); -}; +add_action( 'init', 'wp_init_targeted_link_rel_filters' ); // Format strings for display. foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'widget_title' ) as $filter ) { diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index dc4bf24648..4beb33cb8f 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3095,6 +3095,52 @@ function wp_targeted_link_rel_callback( $matches ) { return ""; } +/** + * Adds all filters modifying the rel attribute of targeted links. + * + * @since 5.1.0 + */ +function wp_init_targeted_link_rel_filters() { + $filters = array( + 'title_save_pre', + 'content_save_pre', + 'excerpt_save_pre', + 'content_filtered_save_pre', + 'pre_comment_content', + 'pre_term_description', + 'pre_link_description', + 'pre_link_notes', + 'pre_user_description', + ); + + foreach ( $filters as $filter ) { + add_filter( $filter, 'wp_targeted_link_rel' ); + }; +} + +/** + * Removes all filters modifying the rel attribute of targeted links. + * + * @since 5.1.0 + */ +function wp_remove_targeted_link_rel_filters() { + $filters = array( + 'title_save_pre', + 'content_save_pre', + 'excerpt_save_pre', + 'content_filtered_save_pre', + 'pre_comment_content', + 'pre_term_description', + 'pre_link_description', + 'pre_link_notes', + 'pre_user_description', + ); + + foreach ( $filters as $filter ) { + remove_filter( $filter, 'wp_targeted_link_rel' ); + }; +} + /** * Convert one smiley code to the icon graphic file equivalent. * diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php index 5693f02973..08f8ac1021 100644 --- a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php +++ b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php @@ -83,4 +83,22 @@ class Tests_Targeted_Link_Rel extends WP_UnitTestCase { $expected = '

Links: Do not change me

'; $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); } + + /** + * Ensure default content filters are added. + * + * @ticket 45292. + */ + public function test_wp_targeted_link_rel_filters_run() { + $content = '

Links: No rel

'; + $expected = '

Links: No rel

'; + + $post = $this->factory()->post->create_and_get( + array( + 'post_content' => $content, + ) + ); + + $this->assertEquals( $expected, $post->post_content ); + } }