CollectionSiteMap.php
2 years ago
ProductSiteMap.php
2 years ago
RankMathService.php
2 years ago
RankMathServiceProvider.php
2 years ago
ProductSiteMap.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\RankMath; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | /** |
| 8 | * Controls the Product Sitemap integration. |
| 9 | */ |
| 10 | class ProductSiteMap implements \RankMath\Sitemap\Providers\Provider { |
| 11 | /** |
| 12 | * What type of content this provider handles. |
| 13 | * |
| 14 | * @param string $type Type of content. |
| 15 | */ |
| 16 | public function handles_type( $type ) { |
| 17 | return 'sc_product' === $type; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Get the index links. |
| 22 | * |
| 23 | * @param int $max_entries Maximum number of entries per sitemap. |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | public function get_index_links( $max_entries ) { |
| 28 | return [ |
| 29 | [ |
| 30 | 'loc' => \RankMath\Sitemap\Router::get_base_url( 'sc_product-sitemap.xml' ), |
| 31 | 'lastmod' => date( 'c', time() ), |
| 32 | ], |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the sitemap links. |
| 38 | * |
| 39 | * @param string $type Type of content. |
| 40 | * @param int $max_entries Maximum number of entries per sitemap. |
| 41 | * @param int $current_page Current page. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function get_sitemap_links( $type, $max_entries, $current_page ) { |
| 46 | // get products. |
| 47 | $products = \SureCart\Models\Product::where( [ 'status' => 'published' ] )->paginate( |
| 48 | [ |
| 49 | 'page' => $current_page, |
| 50 | 'per_page' => $max_entries, |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | $links = array_map( |
| 55 | function( $product ) { |
| 56 | $lastmod = new \DateTime( '@' . $product->updated_at ); |
| 57 | $lastmod_w3c = $lastmod->format( 'c' ); |
| 58 | return [ |
| 59 | 'loc' => $product->permalink, |
| 60 | 'mod' => $lastmod_w3c, |
| 61 | ]; |
| 62 | }, |
| 63 | $products->data |
| 64 | ); |
| 65 | |
| 66 | return $links; |
| 67 | } |
| 68 | } |
| 69 |