| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2020-2026 Jean-Sebastien Morisset (https://surniaulula.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'SucomUtilRobots' ) ) { |
| 14 |
|
| 15 |
class SucomUtilRobots { |
| 16 |
|
| 17 |
private static $default_directives = array( |
| 18 |
'follow' => true, // Follow by default. |
| 19 |
'index' => true, // Index by default. |
| 20 |
'noarchive' => false, |
| 21 |
'nofollow' => false, // Do not follow links on this webpage. |
| 22 |
'noimageindex' => false, // Do not index images on this webpage. |
| 23 |
'noindex' => false, // Do not show this webpage in search results. |
| 24 |
'nosnippet' => false, // Do not show a text snippet or a video preview in search results. |
| 25 |
'notranslate' => false, |
| 26 |
'max-snippet' => -1, // Max characters for textual snippet (-1 = no limit). |
| 27 |
'max-image-preview' => 'large', // Max size for image preview. |
| 28 |
'max-video-preview' => -1, // Max seconds for video snippet (-1 = no limit). |
| 29 |
); |
| 30 |
|
| 31 |
private static $inverse_directives = array( |
| 32 |
'nofollow' => array( 'follow' ), // Do not follow links on this webpage. |
| 33 |
'noimageindex' => array( 'max-image-preview' ), // Do not index images on this webpage. |
| 34 |
'noindex' => array( 'index' ), // Do not show this webpage in search results. |
| 35 |
'nosnippet' => array( 'max-snippet', 'max-video-preview' ), // Do not show a text snippet or a video preview in search results. |
| 36 |
); |
| 37 |
|
| 38 |
/* |
| 39 |
* See https://developers.google.com/search/reference/robots_meta_tag. |
| 40 |
*/ |
| 41 |
public static function get_default_directives() { |
| 42 |
|
| 43 |
$is_public = get_option( 'blog_public' ); |
| 44 |
$directives = self::$default_directives; |
| 45 |
|
| 46 |
/* |
| 47 |
* If the site is not public, discourage robots from indexing the site. |
| 48 |
*/ |
| 49 |
if ( ! $is_public ) { |
| 50 |
|
| 51 |
$directives[ 'follow' ] = false; // No follow. |
| 52 |
$directives[ 'index' ] = false; // No index. |
| 53 |
$directives[ 'noarchive' ] = true; |
| 54 |
$directives[ 'nofollow' ] = true; |
| 55 |
$directives[ 'noimageindex' ] = true; |
| 56 |
$directives[ 'noindex' ] = true; |
| 57 |
$directives[ 'nosnippet' ] = true; |
| 58 |
$directives[ 'notranslate' ] = true; |
| 59 |
|
| 60 |
/* |
| 61 |
* The webpage should not be indexed, but allow robots to follow links. |
| 62 |
*/ |
| 63 |
} elseif ( isset( $_GET[ 'replytocom' ] ) || is_embed() || is_404() || is_search() ) { |
| 64 |
|
| 65 |
$directives[ 'index' ] = false; // No index. |
| 66 |
$directives[ 'noarchive' ] = true; |
| 67 |
$directives[ 'noindex' ] = true; |
| 68 |
$directives[ 'nosnippet' ] = true; |
| 69 |
} |
| 70 |
|
| 71 |
return apply_filters( 'sucom_robots_default_directives', $directives ); |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Properly set boolean directives and their inverse boolean directives. |
| 76 |
*/ |
| 77 |
public static function set_directive( $key, $value, array &$directives ) { |
| 78 |
|
| 79 |
if ( isset( self::$default_directives[ $key ] ) ) { // Directive must be known. |
| 80 |
|
| 81 |
if ( is_bool( self::$default_directives[ $key ] ) ) { // Default boolean, so set as boolean. |
| 82 |
|
| 83 |
$directives[ $key ] = $value ? true : false; // Convert to boolean. |
| 84 |
|
| 85 |
/* |
| 86 |
* Check for inverse directives. |
| 87 |
*/ |
| 88 |
if ( isset( self::$inverse_directives[ $key ] ) ) { |
| 89 |
|
| 90 |
foreach ( self::$inverse_directives[ $key ] as $inverse_key ) { |
| 91 |
|
| 92 |
/* |
| 93 |
* If the inverse is also a boolean, then set the inverse boolean value. |
| 94 |
*/ |
| 95 |
if ( isset( self::$default_directives[ $inverse_key ] ) ) { // Directive must be known. |
| 96 |
|
| 97 |
if ( is_bool( self::$default_directives[ $inverse_key ] ) ) { // Also a boolean. |
| 98 |
|
| 99 |
$directives[ $inverse_key ] = $value ? false : true; // Inverse boolean. |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} else { |
| 106 |
|
| 107 |
$directives[ $key ] = $value; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* Sanity check - make sure inverse directives are removed. |
| 114 |
*/ |
| 115 |
public static function sanitize_directives( array &$directives ) { |
| 116 |
|
| 117 |
foreach ( self::$inverse_directives as $key => $inverse_keys ) { |
| 118 |
|
| 119 |
if ( ! empty( $directives[ $key ] ) ) { // $key exists and is true. |
| 120 |
|
| 121 |
foreach ( $inverse_keys as $inverse_key ) { // Unset each inverse directive. |
| 122 |
|
| 123 |
if ( isset( $directives[ $inverse_key ] ) ) { // Just in case. |
| 124 |
|
| 125 |
unset( $directives[ $inverse_key ] ); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|