RankMathService.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\RankMath; |
| 4 | |
| 5 | use SureCart\Integrations\Abstracts\NoIndexService; |
| 6 | |
| 7 | /** |
| 8 | * Controls the Rank Math integration. |
| 9 | */ |
| 10 | class RankMathService extends NoIndexService { |
| 11 | /** |
| 12 | * The filter hook name for the robots meta. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $hook_name = 'rank_math/frontend/robots'; |
| 17 | |
| 18 | /** |
| 19 | * The filter hook name for the robots meta. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | public function bootstrap(): void { |
| 24 | parent::bootstrap(); |
| 25 | |
| 26 | // Skip product model filter registration during sitemap generation to prevent memory exhaustion. |
| 27 | add_filter( 'surecart/product/skip_filters', [ $this, 'skipFiltersOnSitemap' ] ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Skip model filters during sitemap generation. |
| 32 | * |
| 33 | * @param bool $skip Whether to skip filters. |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function skipFiltersOnSitemap( $skip ): bool { |
| 38 | if ( $skip ) { |
| 39 | return $skip; |
| 40 | } |
| 41 | |
| 42 | return $this->isSitemapRequest(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Check if current request is a sitemap request. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | private function isSitemapRequest(): bool { |
| 51 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 52 | $uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 53 | if ( false !== strpos( $uri, 'sitemap' ) && '.xml' === substr( $uri, -4 ) ) { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 |