NoIndexService.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\Abstracts; |
| 4 | |
| 5 | /** |
| 6 | * Abstract base class for SEO plugin integrations that handle noindex robots meta. |
| 7 | */ |
| 8 | abstract class NoIndexService { |
| 9 | /** |
| 10 | * The filter hook name for the robots meta. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $hook_name = ''; |
| 15 | |
| 16 | /** |
| 17 | * The query vars that should trigger noindex. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $query_vars = [ |
| 22 | 'products-search', |
| 23 | 'products-order', |
| 24 | 'products-orderby', |
| 25 | 'line_items', |
| 26 | 'currency', |
| 27 | ]; |
| 28 | |
| 29 | /** |
| 30 | * The noindex robots. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $noindex_robots = [ |
| 35 | 'noindex' => 'noindex', |
| 36 | 'nofollow' => 'nofollow', |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * Bootstrap the service. |
| 41 | * |
| 42 | * @throws \RuntimeException If hook_name is not set. |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | public function bootstrap(): void { |
| 47 | if ( empty( $this->hook_name ) ) { |
| 48 | throw new \RuntimeException( 'Missing hook_name for noindex service: ' . static::class ); |
| 49 | } |
| 50 | |
| 51 | add_filter( $this->hook_name, [ $this, 'addNoindexForQueryVars' ] ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Modify robots to add noindex for SureCart query vars. |
| 56 | * |
| 57 | * @param array $robots Robots array. |
| 58 | * |
| 59 | * @return array Modified robots. |
| 60 | */ |
| 61 | public function addNoindexForQueryVars( array $robots ): array { |
| 62 | if ( $this->hasNoIndexQueryVars() ) { |
| 63 | return $this->noindex_robots; |
| 64 | } |
| 65 | |
| 66 | return $robots; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if the current request has any SureCart query variables. |
| 71 | * |
| 72 | * @return bool True if any SureCart query var is present. |
| 73 | */ |
| 74 | protected function hasNoIndexQueryVars(): bool { |
| 75 | $query_vars = $this->getNoIndexQueryVars(); |
| 76 | |
| 77 | foreach ( $query_vars as $query_var ) { |
| 78 | // Safe to use $_GET directly as we only check existence, not values. |
| 79 | if ( isset( $_GET[ $query_var ] ) || get_query_var( $query_var ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get SureCart query variables that should trigger noindex. |
| 89 | * |
| 90 | * @return array List of query variable names. |
| 91 | */ |
| 92 | protected function getNoIndexQueryVars(): array { |
| 93 | $query_vars = $this->query_vars; |
| 94 | |
| 95 | // Add all registered taxonomies for sc_product. |
| 96 | $product_taxonomies = get_object_taxonomies( 'sc_product', 'names' ); |
| 97 | if ( ! empty( $product_taxonomies ) ) { |
| 98 | foreach ( $product_taxonomies as $taxonomy ) { |
| 99 | $query_vars[] = 'products-' . $taxonomy; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return apply_filters( 'surecart/noindex_query_vars', array_unique( $query_vars ), $this ); |
| 104 | } |
| 105 | } |
| 106 |