SEOPressService.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\SEOPress; |
| 4 | |
| 5 | use SureCart\Integrations\Abstracts\NoIndexService; |
| 6 | |
| 7 | /** |
| 8 | * Controls the SEOPress integration. |
| 9 | */ |
| 10 | class SEOPressService extends NoIndexService { |
| 11 | /** |
| 12 | * The filter hook name for the robots meta. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $hook_name = 'seopress_titles_robots_attrs'; |
| 17 | |
| 18 | /** |
| 19 | * Modify robots to add noindex for SureCart query vars. |
| 20 | * SEOPress uses indexed array format instead of associative. |
| 21 | * |
| 22 | * @param array $robots Robots array. |
| 23 | * |
| 24 | * @return array Modified robots. |
| 25 | */ |
| 26 | public function addNoindexForQueryVars( array $robots ): array { |
| 27 | if ( ! $this->hasNoIndexQueryVars() ) { |
| 28 | return $robots; |
| 29 | } |
| 30 | |
| 31 | // Remove values that conflict with noindex/nofollow. |
| 32 | // SEOPress adds 'index, follow' as a combined string. |
| 33 | $robots = array_filter( |
| 34 | $robots, |
| 35 | function ( $value ) { |
| 36 | // Skip non-string values. |
| 37 | if ( ! is_string( $value ) ) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | // Remove 'index, follow' combined string. |
| 42 | if ( strpos( $value, 'index' ) !== false && strpos( $value, 'noindex' ) === false ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // Remove standalone 'follow' (when noindex was set but nofollow wasn't). |
| 47 | if ( 'follow' === $value ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | ); |
| 54 | |
| 55 | // Re-index array after filtering. |
| 56 | $robots = array_values( $robots ); |
| 57 | |
| 58 | // Add noindex and nofollow. |
| 59 | $robots[] = 'noindex'; |
| 60 | $robots[] = 'nofollow'; |
| 61 | |
| 62 | return $robots; |
| 63 | } |
| 64 | } |
| 65 |