| @@ -2,22 +2,17 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace TierPricingTable\Integrations\Plugins\RankMath; |
| 4 | 4 | |
| 5 | 5 | use RankMath\Helpers\Param; |
| 6 | -use TierPricingTable\Integrations\Plugins\PluginIntegrationAbstract; | |
| 7 | -use TierPricingTable\Managers\FormatPriceManager; | |
| 8 | -use TierPricingTable\PriceManager; | |
| 9 | -use TierPricingTable\TierPricingTablePlugin; | |
| 6 | +use TierPricingTable\Integrations\Plugins\SeoIntegrationAbstract; | |
| 10 | 7 | use WC_Product; |
| 11 | -class RankMath extends PluginIntegrationAbstract { | |
| 12 | - protected $product = null; | |
| 13 | - | |
| 8 | +class RankMath extends SeoIntegrationAbstract { | |
| 14 | 9 | public function getTitle() : string { |
| 15 | 10 | return 'Rank Math SEO'; |
| 16 | 11 | } |
| 17 | 12 | |
| 18 | 13 | public function getDescription() : string { |
| 19 | - return __( 'Enhance the product schema with tiered pricing offers and adds <b>%lowest_price%</b> and <b>%price_range%</b> variables to display the lowest price and price range of products with tiered pricing.', 'tier-pricing-table' ); | |
| 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' ); | |
| 20 | 15 | } |
| 21 | 16 | |
| 22 | 17 | public function getSlug() : string { |
| 23 | 18 | return 'rank-math'; |
| @@ -22,8 +17,16 @@ | ||
| 22 | 17 | public function getSlug() : string { |
| 23 | 18 | return 'rank-math'; |
| 24 | 19 | } |
| 25 | 20 | |
| 21 | + protected function getSettingsPrefix() : string { | |
| 22 | + return 'rank_math'; | |
| 23 | + } | |
| 24 | + | |
| 25 | + protected function getSchemaFilterSlug() : string { | |
| 26 | + return 'rank_math'; | |
| 27 | + } | |
| 28 | + | |
| 26 | 29 | public function run() { |
| 27 | 30 | add_action( 'plugins_loaded', function () { |
| 28 | 31 | if ( !class_exists( 'RankMath\\Helpers\\Param' ) || !function_exists( 'rank_math_register_var_replacement' ) ) { |
| 29 | 32 | return; |
| @@ -32,70 +35,54 @@ | ||
| 32 | 35 | $sections[] = new Settings(); |
| 33 | 36 | return $sections; |
| 34 | 37 | } ); |
| 35 | 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 ); | |
| 36 | 67 | } |
| 37 | 68 | |
| 38 | - protected function getProductJSONldOffers() : array { | |
| 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 ); | |
| 39 | 74 | } |
| 40 | 75 | |
| 41 | - public function getIntegrationCategory() : string { | |
| 42 | - return 'seo'; | |
| 43 | - } | |
| 44 | - | |
| 45 | 76 | public function getIconURL() : ?string { |
| 46 | 77 | return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/rank-math-icon.svg' ); |
| 47 | 78 | } |
| 48 | 79 | |
| 49 | 80 | public function getPriceRange() : ?string { |
| 50 | - $product = $this->get_product(); | |
| 51 | - if ( !$product ) { | |
| 52 | - return ''; | |
| 53 | - } | |
| 54 | - $price = FormatPriceManager::getFormattedPrice( $product, array( | |
| 55 | - 'for_display' => true, | |
| 56 | - 'with_suffix' => false, | |
| 57 | - 'with_default_price' => true, | |
| 58 | - 'with_lowest_prefix' => false, | |
| 59 | - 'html' => true, | |
| 60 | - 'display_type' => 'range', | |
| 61 | - ) ); | |
| 62 | - return ( $price ? $price : '' ); | |
| 81 | + return $this->getFormattedProductPrice( 'range' ); | |
| 63 | 82 | } |
| 64 | 83 | |
| 65 | 84 | public function getLowestPrice() : ?string { |
| 66 | - $product = $this->get_product(); | |
| 67 | - if ( !$product ) { | |
| 68 | - return ''; | |
| 69 | - } | |
| 70 | - $price = FormatPriceManager::getFormattedPrice( $product, array( | |
| 71 | - 'for_display' => true, | |
| 72 | - 'with_suffix' => false, | |
| 73 | - 'with_default_price' => true, | |
| 74 | - 'with_lowest_prefix' => false, | |
| 75 | - 'html' => true, | |
| 76 | - 'display_type' => 'lowest_price', | |
| 77 | - ) ); | |
| 78 | - return ( $price ? $price : '' ); | |
| 79 | - } | |
| 80 | - | |
| 81 | - public function get_product() { | |
| 82 | - if ( !is_null( $this->product ) ) { | |
| 83 | - return $this->product; | |
| 84 | - } | |
| 85 | - if ( !class_exists( 'RankMath\\Helpers\\Param' ) ) { | |
| 86 | - return null; | |
| 87 | - } | |
| 88 | - $product_id = Param::get( 'post', get_queried_object_id(), FILTER_VALIDATE_INT ); | |
| 89 | - $this->product = ( !function_exists( 'wc_get_product' ) || !$product_id || !is_admin() && !is_singular( 'product' ) ? null : wc_get_product( $product_id ) ); | |
| 90 | - return $this->product; | |
| 91 | - } | |
| 92 | - | |
| 93 | - public function isVariablesEnabled() : bool { | |
| 94 | - return $this->getContainer()->getSettings()->get( 'rank_math_enable_variables', 'yes' ) === 'yes'; | |
| 95 | - } | |
| 96 | - | |
| 97 | - public function isEnhancedSchemaEnabled() : bool { | |
| 98 | - return $this->getContainer()->getSettings()->get( 'rank_math_enhance_schema', 'no' ) === 'yes'; | |
| 85 | + return $this->getFormattedProductPrice( 'lowest_price' ); | |
| 99 | 86 | } |
| 100 | 87 | |
| 101 | 88 | } |