Robots: Remove contradictory directive check in wp_robots().

Removes the mutually exclusive directives check in `wp_robots()`, ie allow both `follow` and `nofollow` to be specified and for `archive` and `noarchive` to be specified.

This fixes a bug in which WordPress would defer to the most permissive over the least permissive. When contradictory instructions are included, WordPress will defer to the search engine's or archivist's resolution policy: generally this is to observe the least, not most permissive.

Props Cybr, flixos90, SergeyBiryukov.
Merges [50566] to the 5.7 branch.
Fixes #52713.


git-svn-id: https://develop.svn.wordpress.org/branches/5.7@50587 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2021-03-26 00:26:55 +00:00
parent c44eb304c8
commit 84dc8fce67
2 changed files with 1 additions and 104 deletions

View File

@ -15,6 +15,7 @@
* robots meta tag is output if there is at least one relevant directive.
*
* @since 5.7.0
* @since 5.7.1 No longer prevents specific directives to occur together.
*/
function wp_robots() {
/**
@ -30,20 +31,6 @@ function wp_robots() {
*/
$robots = apply_filters( 'wp_robots', array() );
// Don't allow mutually exclusive directives.
if ( ! empty( $robots['follow'] ) ) {
unset( $robots['nofollow'] );
}
if ( ! empty( $robots['nofollow'] ) ) {
unset( $robots['follow'] );
}
if ( ! empty( $robots['archive'] ) ) {
unset( $robots['noarchive'] );
}
if ( ! empty( $robots['noarchive'] ) ) {
unset( $robots['archive'] );
}
$robots_strings = array();
foreach ( $robots as $directive => $value ) {
if ( is_string( $value ) ) {

View File

@ -71,56 +71,6 @@ class Tests_Robots extends WP_UnitTestCase {
$this->assertContains( "'{$expected_directives_string}'", $output );
}
/**
* @ticket 51511
*/
public function test_wp_robots_includes_basic_sanitization_follow_nofollow() {
// Only follow or nofollow can be present, with follow taking precedence.
add_filter( 'wp_robots', array( $this, 'add_follow_directive' ) );
add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ) );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'follow'", $output );
// Consider truthyness of the directive value though.
// Here nofollow is true, follow is false.
add_filter( 'wp_robots', array( $this, 'remove_follow_directive' ), 11 );
add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ), 11 );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'nofollow'", $output );
// Consider truthyness of the directive value though.
// Here follow is true, nofollow is false.
add_filter( 'wp_robots', array( $this, 'add_follow_directive' ), 12 );
add_filter( 'wp_robots', array( $this, 'remove_nofollow_directive' ), 12 );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'follow'", $output );
}
/**
* @ticket 51511
*/
public function test_wp_robots_includes_basic_sanitization_archive_noarchive() {
// Only archive or noarchive can be present, with archive taking precedence.
add_filter( 'wp_robots', array( $this, 'add_archive_directive' ) );
add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ) );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'archive'", $output );
// Consider truthyness of the directive value though.
// Here noarchive is true, archive is false.
add_filter( 'wp_robots', array( $this, 'remove_archive_directive' ), 11 );
add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ), 11 );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'noarchive'", $output );
// Consider truthyness of the directive value though.
// Here archive is true, noarchive is false.
add_filter( 'wp_robots', array( $this, 'add_archive_directive' ), 12 );
add_filter( 'wp_robots', array( $this, 'remove_noarchive_directive' ), 12 );
$output = get_echo( 'wp_robots' );
$this->assertContains( "'archive'", $output );
}
/**
* @ticket 51511
*/
@ -207,44 +157,4 @@ class Tests_Robots extends WP_UnitTestCase {
$robots['noindex'] = false;
return $robots;
}
public function add_follow_directive( array $robots ) {
$robots['follow'] = true;
return $robots;
}
public function remove_follow_directive( array $robots ) {
$robots['follow'] = false;
return $robots;
}
public function add_nofollow_directive( array $robots ) {
$robots['nofollow'] = true;
return $robots;
}
public function remove_nofollow_directive( array $robots ) {
$robots['nofollow'] = false;
return $robots;
}
public function add_archive_directive( array $robots ) {
$robots['archive'] = true;
return $robots;
}
public function remove_archive_directive( array $robots ) {
$robots['archive'] = false;
return $robots;
}
public function add_noarchive_directive( array $robots ) {
$robots['noarchive'] = true;
return $robots;
}
public function remove_noarchive_directive( array $robots ) {
$robots['noarchive'] = false;
return $robots;
}
}