WordPress.org

Make WordPress Core

Ticket #43590: 43590.2.diff

File 43590.2.diff, 2.7 KB (added by donmhico, 4 months ago)

Additional changes + unit test.

  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 857d16fd60..42daee1833 100644
    function do_feed_atom( $for_comments ) { 
    15221522 * robots.txt file.
    15231523 *
    15241524 * @since 2.1.0
     1525 * @since 5.3.0 Remove the `Disallow` output if "Search Engine Visiblity" is discouraged.
    15251526 */
    15261527function do_robots() {
    15271528        header( 'Content-Type: text/plain; charset=utf-8' );
    function do_robots() { 
    15351536
    15361537        $output = "User-agent: *\n";
    15371538        $public = get_option( 'blog_public' );
    1538         if ( '0' == $public ) {
    1539                 $output .= "Disallow: /\n";
    1540         } else {
    1541                 $site_url = parse_url( site_url() );
    1542                 $path     = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
    1543                 $output  .= "Disallow: $path/wp-admin/\n";
    1544                 $output  .= "Allow: $path/wp-admin/admin-ajax.php\n";
    1545         }
     1539       
     1540        $site_url = parse_url( site_url() );
     1541        $path     = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : '';
     1542        $output  .= "Disallow: $path/wp-admin/\n";
     1543        $output  .= "Allow: $path/wp-admin/admin-ajax.php\n";
    15461544
    15471545        /**
    15481546         * Filters the robots.txt output.
  • src/wp-includes/general-template.php

    diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
    index c2356376ca..20f1e14a64 100644
    function noindex() { 
    29882988 * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_no_robots' );
    29892989 *
    29902990 * @since 3.3.0
     2991 * @since 5.3.0 Echo `noindex,follow` as robot meta content for public site.
    29912992 */
    29922993function wp_no_robots() {
    2993         echo "<meta name='robots' content='noindex,follow' />\n";
     2994        if ( '0' == get_option( 'blog_public' ) ) {
     2995                echo "<meta name='robots' content='noindex,nofollow' />\n";
     2996        }
     2997        else {
     2998                echo "<meta name='robots' content='noindex,follow' />\n";
     2999        }
    29943000}
    29953001
    29963002/**
  • tests/phpunit/tests/general/template.php

    diff --git tests/phpunit/tests/general/template.php tests/phpunit/tests/general/template.php
    index daa77da5b9..d78ac60f29 100644
    class Tests_General_Template extends WP_UnitTestCase { 
    612612
    613613                $this->assertSame( $expected, $result );
    614614        }
     615
     616        /**
     617         * @ticket 43590
     618         */
     619        function test_wp_no_robots() {
     620                // Simulate private site.
     621                update_option( 'blog_public', '0');
     622
     623                $robots_meta_private = get_echo( 'wp_no_robots' );
     624
     625                $this->assertEquals( "<meta name='robots' content='noindex,nofollow' />\n", $robots_meta_private );
     626
     627                // Simulate public site.
     628                update_option( 'blog_public', '1');
     629
     630                $robots_meta_public = get_echo( 'wp_no_robots' );
     631
     632                $this->assertEquals( "<meta name='robots' content='noindex,follow' />\n", $robots_meta_public );
     633
     634        }
    615635}