| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Discovery; |
| 4 |
|
| 5 |
/** |
| 6 |
* Finds posts and terms that an SEO plugin marks as "noindex". |
| 7 |
* |
| 8 |
* @since 1.8.0 |
| 9 |
*/ |
| 10 |
class RobotsMeta { |
| 11 |
|
| 12 |
/** |
| 13 |
* Yoast SEO post meta: value "1" is noindex. |
| 14 |
*/ |
| 15 |
const YOAST_NOINDEX = '_yoast_wpseo_meta-robots-noindex'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Rank Math post and term meta: serialized list that can contain "noindex". |
| 19 |
*/ |
| 20 |
const RANK_MATH_ROBOTS = 'rank_math_robots'; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param array<int, array{post_id:int, meta_key:string, meta_value:string}> $rows Post meta rows. |
| 24 |
* |
| 25 |
* @return array<int, bool> Post ID => true for each noindex post. |
| 26 |
*/ |
| 27 |
public static function noindex_post_ids( array $rows ) { |
| 28 |
$ids = []; |
| 29 |
|
| 30 |
foreach ( $rows as $row ) { |
| 31 |
$value = (string) $row['meta_value']; |
| 32 |
$yoast = self::YOAST_NOINDEX === $row['meta_key'] && '1' === $value; |
| 33 |
$rank_math = self::RANK_MATH_ROBOTS === $row['meta_key'] && self::is_rank_math_noindex( $value ); |
| 34 |
|
| 35 |
if ( $yoast || $rank_math ) { |
| 36 |
$ids[ (int) $row['post_id'] ] = true; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $ids; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Terms with "wpseo_noindex" = "noindex" in the Yoast SEO option wpseo_taxonomy_meta. |
| 45 |
* |
| 46 |
* @param array $option Option value: taxonomy => term ID => meta. |
| 47 |
* |
| 48 |
* @return array<int, array{id:int, taxonomy:string}> |
| 49 |
*/ |
| 50 |
public static function yoast_noindex_terms( array $option ) { |
| 51 |
$terms = []; |
| 52 |
|
| 53 |
foreach ( $option as $taxonomy => $term_meta ) { |
| 54 |
foreach ( (array) $term_meta as $term_id => $meta ) { |
| 55 |
if ( is_array( $meta ) && 'noindex' === ( $meta['wpseo_noindex'] ?? '' ) ) { |
| 56 |
$terms[] = [ |
| 57 |
'id' => (int) $term_id, |
| 58 |
'taxonomy' => (string) $taxonomy, |
| 59 |
]; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $terms; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param string $value Raw rank_math_robots meta value. |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public static function is_rank_math_noindex( $value ) { |
| 73 |
return false !== strpos( (string) $value, 'noindex' ); |
| 74 |
} |
| 75 |
} |
| 76 |
|