| @@ -1,0 +1,348 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! defined( 'WPSSO_PLUGINDIR' ) ) { | |
| 14 | + | |
| 15 | + die( 'Do. Or do not. There is no try.' ); | |
| 16 | +} | |
| 17 | + | |
| 18 | +if ( ! class_exists( 'SucomUtilRobots' ) ) { | |
| 19 | + | |
| 20 | + require_once WPSSO_PLUGINDIR . 'lib/com/util-robots.php'; | |
| 21 | +} | |
| 22 | + | |
| 23 | +if ( ! class_exists( 'WpssoUtilRobots' ) ) { | |
| 24 | + | |
| 25 | + class WpssoUtilRobots extends SucomUtilRobots { | |
| 26 | + | |
| 27 | + private $p; // Wpsso class object. | |
| 28 | + | |
| 29 | + /* | |
| 30 | + * Instantiated by WpssoUtil->__construct(). | |
| 31 | + */ | |
| 32 | + public function __construct( &$plugin ) { | |
| 33 | + | |
| 34 | + $this->p =& $plugin; | |
| 35 | + | |
| 36 | + if ( $this->p->debug->enabled ) { | |
| 37 | + | |
| 38 | + $this->p->debug->mark(); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + /* | |
| 43 | + * See https://developers.google.com/search/reference/robots_meta_tag. | |
| 44 | + * | |
| 45 | + * Called by WpssoMetaName->maybe_disable_noindex(), and WpssoMetaName->get_array(). | |
| 46 | + */ | |
| 47 | + public function get_content( array $mod ) { | |
| 48 | + | |
| 49 | + if ( $this->p->debug->enabled ) { | |
| 50 | + | |
| 51 | + $this->p->debug->mark(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + $directives = $this->get_directives( $mod ); | |
| 55 | + | |
| 56 | + $content = ''; | |
| 57 | + | |
| 58 | + foreach ( $directives as $directive_key => $directive_value ) { | |
| 59 | + | |
| 60 | + if ( false === $directive_value ) { // Nothing to do. | |
| 61 | + | |
| 62 | + continue; | |
| 63 | + | |
| 64 | + } elseif ( true === $directive_value ) { // Add the directive. | |
| 65 | + | |
| 66 | + $content .= $directive_key . ', '; // index, follow, etc. | |
| 67 | + | |
| 68 | + } else { // Add the directive and its value. | |
| 69 | + | |
| 70 | + $content .= $directive_key . ':' . $directive_value . ', '; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + $content = trim( $content, ', ' ); | |
| 75 | + | |
| 76 | + return apply_filters( 'wpsso_robots_content', $content, $mod, $directives ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /* | |
| 80 | + * Explode a directive string into an associative array. | |
| 81 | + * | |
| 82 | + * Example $content: | |
| 83 | + * | |
| 84 | + * follow, index, max-snippet:-1, max-image-preview:large, max-video-preview:-1 | |
| 85 | + * | |
| 86 | + * Example $directives: | |
| 87 | + * | |
| 88 | + * Array ( | |
| 89 | + * [follow] => | |
| 90 | + * [index] => | |
| 91 | + * [max-snippet] => -1 | |
| 92 | + * [max-image-preview] => large | |
| 93 | + * [max-video-preview] => -1 | |
| 94 | + * ) | |
| 95 | + * | |
| 96 | + * See https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag. | |
| 97 | + */ | |
| 98 | + public function get_content_directives( $content ) { | |
| 99 | + | |
| 100 | + if ( $this->p->debug->enabled ) { | |
| 101 | + | |
| 102 | + $this->p->debug->mark(); | |
| 103 | + } | |
| 104 | + | |
| 105 | + $directives = array(); | |
| 106 | + | |
| 107 | + foreach ( explode( $separator = ', ', $content ) as $el ) { | |
| 108 | + | |
| 109 | + if ( false !== strpos( $el, ':' ) ) { | |
| 110 | + | |
| 111 | + list( $key, $val ) = explode( $separator = ':', $el ); | |
| 112 | + | |
| 113 | + } else { | |
| 114 | + | |
| 115 | + list( $key, $val ) = array( $el, '' ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + $directives[ $key ] = $val; | |
| 119 | + } | |
| 120 | + | |
| 121 | + return $directives; | |
| 122 | + } | |
| 123 | + | |
| 124 | + public function get_directives( array $mod ) { | |
| 125 | + | |
| 126 | + if ( $this->p->debug->enabled ) { | |
| 127 | + | |
| 128 | + $this->p->debug->mark(); | |
| 129 | + } | |
| 130 | + | |
| 131 | + $directives = self::get_default_directives(); | |
| 132 | + | |
| 133 | + if ( $this->p->debug->enabled ) { | |
| 134 | + | |
| 135 | + $this->p->debug->log_arr( 'default directives', $directives ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + $md_opts = array(); | |
| 139 | + | |
| 140 | + if ( ! empty( $mod[ 'obj' ] ) && $mod[ 'id' ] ) { | |
| 141 | + | |
| 142 | + /* | |
| 143 | + * Ignore custom directives if an SEO plugin is active. | |
| 144 | + */ | |
| 145 | + if ( ! empty( $this->p->avail[ 'seo' ][ 'any' ] ) ) { | |
| 146 | + | |
| 147 | + if ( $this->p->debug->enabled ) { | |
| 148 | + | |
| 149 | + $this->p->debug->log( 'skipped custom directives: seo plugin is active' ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + } else { | |
| 153 | + | |
| 154 | + $md_opts = $mod[ 'obj' ]->get_options( $mod[ 'id' ] ); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + foreach ( $directives as $directive_key => $default_value ) { | |
| 159 | + | |
| 160 | + $value = null; | |
| 161 | + $opt_key = str_replace( '-', '_', 'robots_' . $directive_key ); // Convert dashes to underscores. | |
| 162 | + | |
| 163 | + if ( isset( $md_opts[ $opt_key ] ) ) { | |
| 164 | + | |
| 165 | + $value = $md_opts[ $opt_key ]; | |
| 166 | + } | |
| 167 | + | |
| 168 | + /* | |
| 169 | + * Fallback to a default value. | |
| 170 | + */ | |
| 171 | + if ( null === $value ) { | |
| 172 | + | |
| 173 | + /* | |
| 174 | + * Get the default value from the plugin settings for these options: | |
| 175 | + * | |
| 176 | + * 'robots_max_snippet' => -1, | |
| 177 | + * 'robots_max_image_preview' => 'large', | |
| 178 | + * 'robots_max_video_preview' => -1, | |
| 179 | + */ | |
| 180 | + if ( isset( $this->p->options[ $opt_key ] ) ) { | |
| 181 | + | |
| 182 | + $value = $this->p->options[ $opt_key ]; | |
| 183 | + | |
| 184 | + } else { | |
| 185 | + | |
| 186 | + $value = $default_value; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + if ( 'noindex' === $directive_key ) { | |
| 191 | + | |
| 192 | + $value = apply_filters( 'wpsso_robots_is_noindex', $value, $mod ); | |
| 193 | + | |
| 194 | + if ( $this->p->debug->enabled ) { | |
| 195 | + | |
| 196 | + $this->p->debug->log( 'robots is noindex = ' . SucomUtil::get_bool_string( $value ) ); | |
| 197 | + } | |
| 198 | + } | |
| 199 | + | |
| 200 | + if ( $default_value !== $value ) { | |
| 201 | + | |
| 202 | + self::set_directive( $directive_key, $value, $directives ); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 206 | + /* | |
| 207 | + * Sanity check - make sure inverse directives are removed. | |
| 208 | + */ | |
| 209 | + self::sanitize_directives( $directives ); | |
| 210 | + | |
| 211 | + $directives = apply_filters( 'wpsso_robots_directives', $directives, $mod ); | |
| 212 | + | |
| 213 | + if ( $this->p->debug->enabled ) { | |
| 214 | + | |
| 215 | + $this->p->debug->log_arr( 'returning directives', $directives ); | |
| 216 | + } | |
| 217 | + | |
| 218 | + return $directives; | |
| 219 | + } | |
| 220 | + | |
| 221 | + /* | |
| 222 | + * $mixed can be a $mod array, or the name of a module (ie. 'post', 'term', etc.). | |
| 223 | + */ | |
| 224 | + public function is_noimageindex( $mixed, $mod_id = null ) { | |
| 225 | + | |
| 226 | + if ( $this->p->debug->enabled ) { | |
| 227 | + | |
| 228 | + $this->p->debug->mark(); | |
| 229 | + } | |
| 230 | + | |
| 231 | + return $this->is_nokey( 'noimageindex', $mixed, $mod_id ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + /* | |
| 235 | + * $mixed can be a $mod array, or the name of a module (ie. 'post', 'term', etc.). | |
| 236 | + */ | |
| 237 | + public function is_noindex( $mixed, $mod_id = null ) { | |
| 238 | + | |
| 239 | + if ( $this->p->debug->enabled ) { | |
| 240 | + | |
| 241 | + $this->p->debug->mark(); | |
| 242 | + } | |
| 243 | + | |
| 244 | + return $this->is_nokey( 'noindex', $mixed, $mod_id ); | |
| 245 | + } | |
| 246 | + | |
| 247 | + /* | |
| 248 | + * $mixed can be a $mod array, or the name of a module (ie. 'post', 'term', etc.). | |
| 249 | + */ | |
| 250 | + private function is_nokey( $key, $mixed, $mod_id = null ) { | |
| 251 | + | |
| 252 | + if ( $this->p->debug->enabled ) { | |
| 253 | + | |
| 254 | + $this->p->debug->mark(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + $mod = false; | |
| 258 | + $key = SucomUtil::sanitize_key( $key ); // Just in case. | |
| 259 | + | |
| 260 | + if ( ! empty( $mixed[ 'obj' ] ) ) { | |
| 261 | + | |
| 262 | + $mod =& $mixed; | |
| 263 | + | |
| 264 | + } elseif ( is_string( $mixed ) && isset( $this->p->$mixed ) && $mod_id ) { // Just in case. | |
| 265 | + | |
| 266 | + $mod = $this->p->$mixed->get_mod( $mod_id ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + $is_nokey = null; | |
| 270 | + $is_custom = false; | |
| 271 | + | |
| 272 | + if ( ! empty( $mod[ 'obj' ] ) && $mod[ 'id' ] ) { | |
| 273 | + | |
| 274 | + /* | |
| 275 | + * Ignore custom robots keys if an SEO plugin is active. | |
| 276 | + */ | |
| 277 | + if ( ! empty( $this->p->avail[ 'seo' ][ 'any' ] ) ) { | |
| 278 | + | |
| 279 | + if ( $this->p->debug->enabled ) { | |
| 280 | + | |
| 281 | + $this->p->debug->log( 'skipped custom robots keys: seo plugin is active' ); | |
| 282 | + } | |
| 283 | + | |
| 284 | + } else { | |
| 285 | + | |
| 286 | + $md_opts = $mod[ 'obj' ]->get_options( $mod[ 'id' ] ); | |
| 287 | + | |
| 288 | + if ( isset( $md_opts[ 'robots_' . $key ] ) ) { | |
| 289 | + | |
| 290 | + if ( $this->p->debug->enabled ) { | |
| 291 | + | |
| 292 | + $this->p->debug->log( 'robots ' . $key . ' for ' . $mod[ 'name' ] . ' id ' . $mod[ 'id' ] . ' is true' ); | |
| 293 | + } | |
| 294 | + | |
| 295 | + $is_nokey = $md_opts[ 'robots_' . $key ] ? true : false; | |
| 296 | + $is_custom = true; | |
| 297 | + } | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + if ( null === $is_nokey ) { // No custom options found. | |
| 302 | + | |
| 303 | + if ( $this->p->debug->enabled ) { | |
| 304 | + | |
| 305 | + $this->p->debug->log( 'getting default directives' ); | |
| 306 | + } | |
| 307 | + | |
| 308 | + $directives = self::get_default_directives(); | |
| 309 | + $is_nokey = $directives[ $key ] ? true : false; | |
| 310 | + } | |
| 311 | + | |
| 312 | + $filter_name = SucomUtil::sanitize_hookname( 'wpsso_robots_is_' . $key ); // Just in case. | |
| 313 | + $is_nokey = apply_filters( $filter_name, $is_nokey, $mod, $is_custom ); | |
| 314 | + | |
| 315 | + return $is_nokey; | |
| 316 | + } | |
| 317 | + | |
| 318 | + /* | |
| 319 | + * WpssoUtilRobots->is_disabled() returns true if: | |
| 320 | + * | |
| 321 | + * - An SEO plugin is active. | |
| 322 | + * - The 'add_meta_name_robots' option is unchecked. | |
| 323 | + * - The 'wpsso_robots_disabled' filter returns true. | |
| 324 | + */ | |
| 325 | + public function is_disabled() { | |
| 326 | + | |
| 327 | + $disabled = false; | |
| 328 | + | |
| 329 | + if ( ! empty( $this->p->avail[ 'seo' ][ 'any' ] ) ) { | |
| 330 | + | |
| 331 | + $disabled = true; | |
| 332 | + | |
| 333 | + } elseif ( empty( $this->p->options[ 'add_meta_name_robots' ] ) ) { | |
| 334 | + | |
| 335 | + $disabled = true; | |
| 336 | + } | |
| 337 | + | |
| 338 | + $disabled = apply_filters( 'wpsso_robots_disabled', $disabled ); | |
| 339 | + | |
| 340 | + return $disabled; | |
| 341 | + } | |
| 342 | + | |
| 343 | + public function is_enabled() { | |
| 344 | + | |
| 345 | + return $this->is_disabled() ? false : true; | |
| 346 | + } | |
| 347 | + } | |
| 348 | +} | |