Make WordPress Core

Ticket #45292: 45292.diff

File 45292.diff, 5.2 KB (added by peterwilsoncc, 6 years ago)
  • src/wp-includes/class-wp-customize-manager.php

    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 b final class WP_Customize_Manager { 
    28852885                $this->store_changeset_revision = $allow_revision;
    28862886                add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 5, 3 );
    28872887
    2888                 // Update the changeset post. The publish_customize_changeset action will cause the settings in the changeset to be saved via WP_Customize_Setting::save().
     2888                /*
     2889                 * Update the changeset post. The publish_customize_changeset action
     2890                 * will cause the settings in the changeset to be saved via
     2891                 * WP_Customize_Setting::save().
     2892                 */
     2893
     2894                // Prevent content filters from corrupting JSON in post_content.
    28892895                $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
    28902896                if ( $has_kses ) {
    2891                         kses_remove_filters(); // Prevent KSES from corrupting JSON in post_content.
     2897                        kses_remove_filters();
     2898                }
     2899                $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
     2900                if ( $has_targeted_link_rel_filters ) {
     2901                        wp_remove_targeted_link_rel_filters();
    28922902                }
    28932903
    28942904                // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
    final class WP_Customize_Manager { 
    29182928                                $this->_changeset_post_id = $r; // Update cached post ID for the loaded changeset.
    29192929                        }
    29202930                }
     2931
     2932                // Restore removed content filters.
    29212933                if ( $has_kses ) {
    29222934                        kses_init_filters();
    29232935                }
     2936                if ( $has_targeted_link_rel_filters ) {
     2937                        wp_init_targeted_link_rel_filters();
     2938                }
     2939
    29242940                $this->_changeset_data = null; // Reset so WP_Customize_Manager::changeset_data() will re-populate with updated contents.
    29252941
    29262942                remove_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ) );
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index 66d64485d6..1319826212 100644
    a b foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pr 
    128128}
    129129
    130130// Add proper rel values for links with target.
    131 foreach ( array(
    132         'title_save_pre',
    133         'content_save_pre',
    134         'excerpt_save_pre',
    135         'content_filtered_save_pre',
    136         'pre_comment_content',
    137         'pre_term_description',
    138         'pre_link_description',
    139         'pre_link_notes',
    140         'pre_user_description',
    141 ) as $filter ) {
    142         add_filter( $filter, 'wp_targeted_link_rel' );
    143 };
     131add_action( 'init', 'wp_init_targeted_link_rel_filters' );
    144132
    145133// Format strings for display.
    146134foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'widget_title' ) as $filter ) {
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index dc4bf24648..4beb33cb8f 100644
    a b function wp_targeted_link_rel_callback( $matches ) { 
    30953095        return "<a $link_html>";
    30963096}
    30973097
     3098/**
     3099 * Adds all filters modifying the rel attribute of targeted links.
     3100 *
     3101 * @since 5.1.0
     3102 */
     3103function wp_init_targeted_link_rel_filters() {
     3104        $filters = array(
     3105                'title_save_pre',
     3106                'content_save_pre',
     3107                'excerpt_save_pre',
     3108                'content_filtered_save_pre',
     3109                'pre_comment_content',
     3110                'pre_term_description',
     3111                'pre_link_description',
     3112                'pre_link_notes',
     3113                'pre_user_description',
     3114        );
     3115
     3116        foreach ( $filters as $filter ) {
     3117                add_filter( $filter, 'wp_targeted_link_rel' );
     3118        };
     3119}
     3120
     3121/**
     3122 * Removes all filters modifying the rel attribute of targeted links.
     3123 *
     3124 * @since 5.1.0
     3125 */
     3126function wp_remove_targeted_link_rel_filters() {
     3127        $filters = array(
     3128                'title_save_pre',
     3129                'content_save_pre',
     3130                'excerpt_save_pre',
     3131                'content_filtered_save_pre',
     3132                'pre_comment_content',
     3133                'pre_term_description',
     3134                'pre_link_description',
     3135                'pre_link_notes',
     3136                'pre_user_description',
     3137        );
     3138
     3139        foreach ( $filters as $filter ) {
     3140                remove_filter( $filter, 'wp_targeted_link_rel' );
     3141        };
     3142}
     3143
    30983144/**
    30993145 * Convert one smiley code to the icon graphic file equivalent.
    31003146 *
  • tests/phpunit/tests/formatting/WPTargetedLinkRel.php

    diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php
    index 5693f02973..08f8ac1021 100644
    a b class Tests_Targeted_Link_Rel extends WP_UnitTestCase { 
    8383                $expected = '<p>Links: <a href="/" target="_blank">Do not change me</a></p>';
    8484                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
    8585        }
     86
     87        /**
     88         * Ensure default content filters are added.
     89         *
     90         * @ticket 45292.
     91         */
     92        public function test_wp_targeted_link_rel_filters_run() {
     93                $content  = '<p>Links: <a href="/" target="_blank">No rel</a></p>';
     94                $expected = '<p>Links: <a href="/" target="_blank" rel="noopener noreferrer">No rel</a></p>';
     95
     96                $post = $this->factory()->post->create_and_get(
     97                        array(
     98                                'post_content' => $content,
     99                        )
     100                );
     101
     102                $this->assertEquals( $expected, $post->post_content );
     103        }
    86104}