Make WordPress Core

Changeset 49992


Ignore:
Timestamp:
01/21/2021 01:35:16 AM (4 years ago)
Author:
flixos90
Message:

Robots: Introduce Robots API.

This changeset introduces a filter-based Robots API, providing central control over the robots meta tag.

  • Introduces wp_robots() function which should be called anywhere a robots meta tag should be included.
  • Introduces wp_robots filter which allows adding or modifying directives for the robots meta tag. The wp_robots() function is entirely filter-based, i.e. if no filter is added to wp_robots, no directives will be present, and therefore the entire robots meta tag will be omitted.
  • Introduces the following wp_robots filter functions which replace similar existing functions that were manually rendering a robots meta tag:
    • wp_robots_noindex() replaces noindex(), which has been deprecated.
    • wp_robots_no_robots() replaces wp_no_robots(), which has been deprecated.
    • wp_robots_sensitive_page() replaces wp_sensitive_page_meta(), which has been deprecated. Its rendering of the referrer meta tag has been moved to another new function wp_strict_cross_origin_referrer().

Migration to the new functions is straightforward. For example, a call to add_action( 'wp_head', 'wp_no_robots' ) should be replaced with add_filter( 'wp_robots', 'wp_robots_no_robots' ).

Plugins and themes that render their own robots meta tags are encouraged to switch to rely on the wp_robots filter in order to use the central management layer now provided by WordPress core.

Props adamsilverstein, flixos90, timothyblynjacobs, westonruter.
See #51511.

Location:
trunk
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-activate.php

    r48672 r49992  
    115115}
    116116add_action( 'wp_head', 'wpmu_activate_stylesheet' );
    117 add_action( 'wp_head', 'wp_sensitive_page_meta' );
     117add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
     118add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
    118119
    119120get_header( 'wp-activate' );
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r49731 r49992  
    19011901            header( 'X-Robots: noindex, nofollow, noarchive' );
    19021902        }
    1903         add_action( 'wp_head', 'wp_no_robots' );
     1903        add_filter( 'wp_robots', 'wp_robots_no_robots' );
    19041904        add_filter( 'wp_headers', array( $this, 'filter_iframe_security_headers' ) );
    19051905
  • trunk/src/wp-includes/default-filters.php

    r49904 r49992  
    231231// Email filters.
    232232add_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
     233
     234// Robots filters.
     235add_filter( 'wp_robots', 'wp_robots_noindex' );
    233236
    234237// Mark site as no longer fresh.
     
    292295add_action( 'wp_head', 'locale_stylesheet' );
    293296add_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
    294 add_action( 'wp_head', 'noindex', 1 );
     297add_action( 'wp_head', 'wp_robots', 1 );
    295298add_action( 'wp_head', 'print_emoji_detection_script', 7 );
    296299add_action( 'wp_head', 'wp_print_styles', 8 );
     
    312315
    313316if ( isset( $_GET['replytocom'] ) ) {
    314     add_action( 'wp_head', 'wp_no_robots' );
     317    add_filter( 'wp_robots', 'wp_robots_no_robots' );
    315318}
    316319
    317320// Login actions.
     321add_action( 'login_head', 'wp_robots', 1 );
    318322add_filter( 'login_head', 'wp_resource_hints', 8 );
    319323add_action( 'login_head', 'wp_print_head_scripts', 9 );
     
    586590add_action( 'embed_head', 'wp_print_head_scripts', 20 );
    587591add_action( 'embed_head', 'wp_print_styles', 20 );
    588 add_action( 'embed_head', 'wp_no_robots' );
     592add_action( 'embed_head', 'wp_robots' );
    589593add_action( 'embed_head', 'rel_canonical' );
    590594add_action( 'embed_head', 'locale_stylesheet', 30 );
     
    597601add_action( 'embed_footer', 'wp_print_footer_scripts', 20 );
    598602
     603add_filter( 'wp_robots', 'wp_embed_no_robots' );
    599604add_filter( 'excerpt_more', 'wp_embed_excerpt_more', 20 );
    600605add_filter( 'the_excerpt_embed', 'wptexturize' );
  • trunk/src/wp-includes/deprecated.php

    r49597 r49992  
    41364136    return is_string( $value ) ? addslashes( $value ) : $value;
    41374137}
     4138
     4139/**
     4140 * Displays a noindex meta tag if required by the blog configuration.
     4141 *
     4142 * If a blog is marked as not being public then the noindex meta tag will be
     4143 * output to tell web robots not to index the page content. Add this to the
     4144 * {@see 'wp_head'} action.
     4145 *
     4146 * Typical usage is as a {@see 'wp_head'} callback:
     4147 *
     4148 *     add_action( 'wp_head', 'noindex' );
     4149 *
     4150 * @see wp_no_robots()
     4151 *
     4152 * @since 2.1.0
     4153 * @deprecated 5.7.0 Use wp_robots_noindex() instead on 'wp_robots' filter.
     4154 */
     4155function noindex() {
     4156    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_noindex()' );
     4157
     4158    // If the blog is not public, tell robots to go away.
     4159    if ( '0' == get_option( 'blog_public' ) ) {
     4160        wp_no_robots();
     4161    }
     4162}
     4163
     4164/**
     4165 * Display a noindex meta tag.
     4166 *
     4167 * Outputs a noindex meta tag that tells web robots not to index the page content.
     4168 * Typical usage is as a {@see 'wp_head'} callback. add_action( 'wp_head', 'wp_no_robots' );
     4169 *
     4170 * @since 3.3.0
     4171 * @since 5.3.0 Echo "noindex,nofollow" if search engine visibility is discouraged.
     4172 * @deprecated 5.7.0 Use wp_robots_no_robots() instead on 'wp_robots' filter.
     4173 */
     4174function wp_no_robots() {
     4175    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_no_robots()' );
     4176
     4177    if ( get_option( 'blog_public' ) ) {
     4178        echo "<meta name='robots' content='noindex,follow' />\n";
     4179        return;
     4180    }
     4181
     4182    echo "<meta name='robots' content='noindex,nofollow' />\n";
     4183}
     4184
     4185/**
     4186 * Display a noindex,noarchive meta tag and referrer origin-when-cross-origin meta tag.
     4187 *
     4188 * Outputs a noindex,noarchive meta tag that tells web robots not to index or cache the page content.
     4189 * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
     4190 * url as a referrer to other sites when cross-origin assets are loaded.
     4191 *
     4192 * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_sensitive_page_meta' );
     4193 *
     4194 * @since 5.0.1
     4195 * @deprecated 5.7.0 Use wp_robots_sensitive_page() instead on 'wp_robots' filter
     4196 *                   and wp_strict_cross_origin_referrer() on 'wp_head' action.
     4197 */
     4198function wp_sensitive_page_meta() {
     4199    _deprecated_function( __FUNCTION__, '5.7.0', 'wp_robots_sensitive_page()' );
     4200
     4201    ?>
     4202    <meta name='robots' content='noindex,noarchive' />
     4203    <?php
     4204    wp_strict_cross_origin_referrer();
     4205}
  • trunk/src/wp-includes/embed.php

    r49936 r49992  
    12441244    return $result;
    12451245}
     1246
     1247/**
     1248 * Adds noindex to the robots meta tag for embeds.
     1249 *
     1250 * Typical usage is as a {@see 'wp_robots'} callback:
     1251 *
     1252 *     add_filter( 'wp_robots', 'wp_embed_no_robots' );
     1253 *
     1254 * @since 5.7.0
     1255 *
     1256 * @param array $robots Associative array of robots directives.
     1257 * @return array Filtered robots directives.
     1258 */
     1259function wp_embed_no_robots( array $robots ) {
     1260    if ( ! is_embed() ) {
     1261        return $robots;
     1262    }
     1263
     1264    return wp_robots_no_robots( $robots );
     1265}
  • trunk/src/wp-includes/functions.php

    r49941 r49992  
    16411641 * @since 2.1.0
    16421642 * @since 5.3.0 Remove the "Disallow: /" output if search engine visiblity is
    1643  *              discouraged in favor of robots meta HTML tag in wp_no_robots().
     1643 *              discouraged in favor of robots meta HTML tag via wp_robots_no_robots()
     1644 *              filter callback.
    16441645 */
    16451646function do_robots() {
     
    34923493    <meta name="viewport" content="width=device-width">
    34933494        <?php
    3494         if ( function_exists( 'wp_no_robots' ) ) {
    3495             wp_no_robots();
     3495        if ( function_exists( 'wp_robots' ) && function_exists( 'wp_robots_no_robots' ) && function_exists( 'add_filter' ) ) {
     3496            add_filter( 'wp_robots', 'wp_robots_no_robots' );
     3497            wp_robots();
    34963498        }
    34973499        ?>
  • trunk/src/wp-includes/general-template.php

    r49977 r49992  
    31923192
    31933193/**
    3194  * Displays a noindex meta tag if required by the blog configuration.
    3195  *
    3196  * If a blog is marked as not being public then the noindex meta tag will be
    3197  * output to tell web robots not to index the page content. Add this to the
    3198  * {@see 'wp_head'} action.
    3199  *
    3200  * Typical usage is as a {@see 'wp_head'} callback:
    3201  *
    3202  *     add_action( 'wp_head', 'noindex' );
    3203  *
    3204  * @see wp_no_robots()
    3205  *
    3206  * @since 2.1.0
    3207  */
    3208 function noindex() {
    3209     // If the blog is not public, tell robots to go away.
    3210     if ( '0' == get_option( 'blog_public' ) ) {
    3211         wp_no_robots();
    3212     }
    3213 }
    3214 
    3215 /**
    3216  * Display a noindex meta tag.
    3217  *
    3218  * Outputs a noindex meta tag that tells web robots not to index the page content.
    3219  * Typical usage is as a {@see 'wp_head'} callback. add_action( 'wp_head', 'wp_no_robots' );
    3220  *
    3221  * @since 3.3.0
    3222  * @since 5.3.0 Echo "noindex,nofollow" if search engine visibility is discouraged.
    3223  */
    3224 function wp_no_robots() {
    3225     if ( get_option( 'blog_public' ) ) {
    3226         echo "<meta name='robots' content='noindex,follow' />\n";
    3227         return;
    3228     }
    3229 
    3230     echo "<meta name='robots' content='noindex,nofollow' />\n";
    3231 }
    3232 
    3233 /**
    3234  * Display a noindex,noarchive meta tag and referrer origin-when-cross-origin meta tag.
    3235  *
    3236  * Outputs a noindex,noarchive meta tag that tells web robots not to index or cache the page content.
     3194 * Displays a referrer strict-origin-when-cross-origin meta tag.
     3195 *
    32373196 * Outputs a referrer origin-when-cross-origin meta tag that tells the browser not to send the full
    32383197 * url as a referrer to other sites when cross-origin assets are loaded.
    32393198 *
    3240  * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_sensitive_page_meta' );
    3241  *
    3242  * @since 5.0.1
    3243  */
    3244 function wp_sensitive_page_meta() {
     3199 * Typical usage is as a wp_head callback. add_action( 'wp_head', 'wp_strict_cross_origin_referrer' );
     3200 *
     3201 * @since 5.7.0
     3202 */
     3203function wp_strict_cross_origin_referrer() {
    32453204    ?>
    3246     <meta name='robots' content='noindex,noarchive' />
    32473205    <meta name='referrer' content='strict-origin-when-cross-origin' />
    32483206    <?php
  • trunk/src/wp-login.php

    r49945 r49992  
    4343
    4444    // Don't index any of these forms.
    45     add_action( 'login_head', 'wp_sensitive_page_meta' );
     45    add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
     46    add_action( 'login_head', 'wp_strict_cross_origin_referrer' );
    4647
    4748    add_action( 'login_head', 'wp_login_viewport_meta' );
  • trunk/src/wp-settings.php

    r49904 r49992  
    182182require ABSPATH . WPINC . '/link-template.php';
    183183require ABSPATH . WPINC . '/author-template.php';
     184require ABSPATH . WPINC . '/robots-template.php';
    184185require ABSPATH . WPINC . '/post.php';
    185186require ABSPATH . WPINC . '/class-walker-page.php';
  • trunk/src/wp-signup.php

    r49078 r49992  
    44require __DIR__ . '/wp-load.php';
    55
    6 add_action( 'wp_head', 'wp_no_robots' );
     6add_filter( 'wp_robots', 'wp_robots_no_robots' );
    77
    88require __DIR__ . '/wp-blog-header.php';
  • trunk/tests/phpunit/tests/customize/manager.php

    r49603 r49992  
    894894        $this->assertSame( $did_action_customize_preview_init + 1, did_action( 'customize_preview_init' ) );
    895895
    896         $this->assertSame( 10, has_action( 'wp_head', 'wp_no_robots' ) );
     896        $this->assertSame( 10, has_filter( 'wp_robots', 'wp_robots_no_robots' ) );
    897897        $this->assertSame( 10, has_action( 'wp_head', array( $wp_customize, 'remove_frameless_preview_messenger_channel' ) ) );
    898898        $this->assertSame( 10, has_filter( 'wp_headers', array( $wp_customize, 'filter_iframe_security_headers' ) ) );
  • trunk/tests/phpunit/tests/general/template.php

    r49847 r49992  
    473473
    474474        $this->assertSame( $expected, $result );
    475     }
    476 
    477     /**
    478      * @ticket 43590
    479      */
    480     function test_wp_no_robots() {
    481         // Simulate private site (search engines discouraged).
    482         update_option( 'blog_public', '0' );
    483         $actual_private = get_echo( 'wp_no_robots' );
    484         $this->assertSame( "<meta name='robots' content='noindex,nofollow' />\n", $actual_private );
    485 
    486         // Simulate public site.
    487         update_option( 'blog_public', '1' );
    488         $actual_public = get_echo( 'wp_no_robots' );
    489         $this->assertSame( "<meta name='robots' content='noindex,follow' />\n", $actual_public );
    490475    }
    491476
Note: See TracChangeset for help on using the changeset viewer.