| 1 |
<?php |
| 2 |
|
| 3 |
namespace TierPricingTable\Integrations\Plugins\RankMath; |
| 4 |
|
| 5 |
use RankMath\Helpers\Param; |
| 6 |
use TierPricingTable\Integrations\Plugins\SeoIntegrationAbstract; |
| 7 |
use WC_Product; |
| 8 |
class RankMath extends SeoIntegrationAbstract { |
| 9 |
public function getTitle() : string { |
| 10 |
return 'Rank Math SEO'; |
| 11 |
} |
| 12 |
|
| 13 |
public function getDescription() : string { |
| 14 |
return __( 'Add tiered pricing data to the product schema and introduce <b>%lowest_price%</b> and <b>%price_range%</b> variables for SEO.', 'tier-pricing-table' ); |
| 15 |
} |
| 16 |
|
| 17 |
public function getSlug() : string { |
| 18 |
return 'rank-math'; |
| 19 |
} |
| 20 |
|
| 21 |
protected function getSettingsPrefix() : string { |
| 22 |
return 'rank_math'; |
| 23 |
} |
| 24 |
|
| 25 |
protected function getSchemaFilterSlug() : string { |
| 26 |
return 'rank_math'; |
| 27 |
} |
| 28 |
|
| 29 |
public function run() { |
| 30 |
add_action( 'plugins_loaded', function () { |
| 31 |
if ( !class_exists( 'RankMath\\Helpers\\Param' ) || !function_exists( 'rank_math_register_var_replacement' ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
add_filter( 'tiered_pricing_table/settings/sections', function ( $sections ) { |
| 35 |
$sections[] = new Settings(); |
| 36 |
return $sections; |
| 37 |
} ); |
| 38 |
} ); |
| 39 |
return; |
| 40 |
add_action( 'rank_math/vars/register_extra_replacements', function () { |
| 41 |
if ( !function_exists( 'rank_math_register_var_replacement' ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
if ( !$this->isVariablesEnabled() ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
rank_math_register_var_replacement( 'lowest_price', [ |
| 48 |
'name' => esc_html__( 'Lowest Price', 'rank-math' ), |
| 49 |
'description' => esc_html__( 'Tiered Pricing: The lowest price of a product.', 'rank-math' ), |
| 50 |
'variable' => 'lowest_price', |
| 51 |
'example' => $this->getLowestPrice(), |
| 52 |
], array($this, 'getLowestPrice') ); |
| 53 |
rank_math_register_var_replacement( 'price_range', [ |
| 54 |
'name' => esc_html__( 'Price range', 'rank-math' ), |
| 55 |
'description' => esc_html__( 'Tiered Pricing: A price range from the lowest to the highest.', 'rank-math' ), |
| 56 |
'variable' => 'price_range', |
| 57 |
'example' => $this->getPriceRange(), |
| 58 |
], array($this, 'getPriceRange') ); |
| 59 |
} ); |
| 60 |
add_filter( 'rank_math/snippet/rich_snippet_product_entity', function ( $data ) { |
| 61 |
global $product; |
| 62 |
if ( !$this->isEnhancedSchemaEnabled() || !$product instanceof WC_Product || !is_array( $data ) ) { |
| 63 |
return $data; |
| 64 |
} |
| 65 |
return $this->enhanceProductSchema( $data, $product ); |
| 66 |
}, 10 ); |
| 67 |
} |
| 68 |
|
| 69 |
protected function resolveProductId() { |
| 70 |
if ( !class_exists( 'RankMath\\Helpers\\Param' ) ) { |
| 71 |
return get_queried_object_id(); |
| 72 |
} |
| 73 |
return Param::get( 'post', get_queried_object_id(), FILTER_VALIDATE_INT ); |
| 74 |
} |
| 75 |
|
| 76 |
public function getIconURL() : ?string { |
| 77 |
return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/rank-math-icon.svg' ); |
| 78 |
} |
| 79 |
|
| 80 |
public function getPriceRange() : ?string { |
| 81 |
return $this->getFormattedProductPrice( 'range' ); |
| 82 |
} |
| 83 |
|
| 84 |
public function getLowestPrice() : ?string { |
| 85 |
return $this->getFormattedProductPrice( 'lowest_price' ); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|