WordPress.org

Make WordPress Core

Ticket #52457: 52457.diff

File 52457.diff, 2.5 KB (added by peterwilsoncc, 7 months ago)
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index e8e0a84d45..783606df4b 100644
    a b add_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); 
    238238
    239239// Robots filters.
    240240add_filter( 'wp_robots', 'wp_robots_noindex' );
     241add_filter( 'wp_robots', 'wp_robots_noindex_search' );
    241242add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
    242243
    243244// Mark site as no longer fresh.
  • src/wp-includes/robots-template.php

    diff --git a/src/wp-includes/robots-template.php b/src/wp-includes/robots-template.php
    index dc425686c2..61931a25e5 100644
    a b function wp_robots_noindex( array $robots ) { 
    8787        return $robots;
    8888}
    8989
     90/**
     91 * Adds noindex to the robots meta tag if a search is being performed.
     92 *
     93 * If a search is being performed then noindex will be output to
     94 * tell web robots not to index the page content. Add this to the
     95 * {@see 'wp_robots'} filter.
     96 *
     97 * Typical usage is as a {@see 'wp_robots'} callback:
     98 *
     99 *     add_filter( 'wp_robots', 'wp_robots_noindex_search' );
     100 *
     101 * @since 5.7.0
     102 * @see wp_robots_no_robots()
     103 *
     104 * @param array $robots Associative array of robots directives.
     105 * @return array Filtered robots directives.
     106 */
     107function wp_robots_noindex_search( array $robots ) {
     108        if ( is_search() ) {
     109                return wp_robots_no_robots( $robots );
     110        }
     111
     112        return $robots;
     113}
     114
    90115/**
    91116 * Adds noindex to the robots meta tag.
    92117 *
  • tests/phpunit/tests/robots.php

    diff --git a/tests/phpunit/tests/robots.php b/tests/phpunit/tests/robots.php
    index a8073b36f3..f826b3bf11 100644
    a b class Tests_Robots extends WP_UnitTestCase { 
    176176                $this->assertEmpty( $output );
    177177        }
    178178
     179        /**
     180         * @ticket 52457
     181         */
     182        public function test_wp_robots_search_page() {
     183                add_filter( 'wp_robots', 'wp_robots_noindex_search' );
     184                $this->go_to( home_url( '?s=ticket+52457+core.trac.wordpress.org' ) );
     185
     186                $output = get_echo( 'wp_robots' );
     187                $this->assertContains( 'noindex', $output );
     188        }
     189
     190        /**
     191         * @ticket 52457
     192         */
     193        public function test_wp_robots_non_search_page() {
     194                add_filter( 'wp_robots', 'wp_robots_noindex_search' );
     195                $this->go_to( home_url() );
     196
     197                $output = get_echo( 'wp_robots' );
     198                $this->assertNotContains( 'noindex', $output );
     199        }
     200
    179201        public function add_noindex_directive( array $robots ) {
    180202                $robots['noindex'] = true;
    181203                return $robots;