Make WordPress Core

Ticket #52536: 52536-poc.diff

File 52536-poc.diff, 1.9 KB (added by peterwilsoncc, 7 months ago)
  • src/wp-includes/class-wp.php

    diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php
    index ce88c102cf..9003cef029 100644
    a b class WP { 
    547547                        }
    548548                }
    549549
     550                $x_robots_tag_value = wp_x_robots_tag_header_string();
     551                if ( is_string( $x_robots_tag_value ) ) {
     552                        $headers['X-Robots-Tag'] = $x_robots_tag_value;
     553                }
     554
    550555                /**
    551556                 * Filters the HTTP headers before they're sent to the browser.
    552557                 *
  • src/wp-includes/robots-template.php

    diff --git a/src/wp-includes/robots-template.php b/src/wp-includes/robots-template.php
    index e719e745d6..ec7c2e59dc 100644
    a b function wp_robots() { 
    4949        echo "<meta name='robots' content='" . esc_attr( implode( ', ', $robots_strings ) ) . "' />\n";
    5050}
    5151
     52/**
     53 * Returns the necessary X-Robots-Tag value.
     54 *
     55 * Gathers robots directives to include for the current context, using the
     56 * {@see 'wp_robots'} filter. The directives are then sanitized, and the
     57 * X-Robots-Tag header is output if there is at least one relevant directive.
     58 *
     59 * @since x.x.x
     60 *
     61 * @return string|null The X-Robots-Tag header value, or null if no relevant directives.
     62 */
     63function wp_x_robots_tag_header_string() {
     64        /** This filter is documented in wp-includes/robots-template.php */
     65        $robots = apply_filters( 'wp_robots', array() );
     66
     67        $robots_strings = array();
     68        foreach ( $robots as $directive => $value ) {
     69                if ( is_string( $value ) ) {
     70                        // If a string value, include it as value for the directive.
     71                        $robots_strings[] = "{$directive}={$value}";
     72                } elseif ( $value ) {
     73                        // Otherwise, include the directive if it is truthy.
     74                        $robots_strings[] = $directive;
     75                }
     76        }
     77
     78        if ( empty( $robots_strings ) ) {
     79                return null;
     80        }
     81
     82        return implode( ', ', $robots_strings );
     83}
     84
    5285/**
    5386 * Adds `noindex` to the robots meta tag if required by the site configuration.
    5487 *