Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce module.
|
||||
*
|
||||
* @since 1.0
|
||||
* @package RankMathPro
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Traits\Hooker;
|
||||
use MyThemeShop\Helpers\Param;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Admin class.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class Admin {
|
||||
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->action( 'rank_math/admin/settings/woocommerce', 'add_woocommerce_fields' );
|
||||
$this->action( 'woocommerce_product_options_sku', 'add_gtin_field' );
|
||||
$this->action( 'woocommerce_product_after_variable_attributes', 'add_variation_gtin_field', 10, 3 );
|
||||
$this->action( 'woocommerce_admin_process_product_object', 'save_gtin_data' );
|
||||
$this->action( 'woocommerce_save_product_variation', 'save_variation_gtin_data', 10, 2 );
|
||||
$this->filter( 'rank_math/metabox/post/values', 'updated_localized_data', 99, 2 );
|
||||
$this->filter( 'rank_math/admin/robots', 'update_rank_math_product_robots', 99, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add options to WooCommerce module.
|
||||
*
|
||||
* @param object $cmb CMB object.
|
||||
*/
|
||||
public function add_woocommerce_fields( $cmb ) {
|
||||
$options = Helper::get_object_taxonomies( 'product', 'choices', false );
|
||||
$options['custom'] = esc_html__( 'Custom', 'rank-math-pro' );
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'product_brand',
|
||||
'type' => 'select',
|
||||
'name' => esc_html__( 'Select Brand', 'rank-math-pro' ),
|
||||
'desc' => esc_html__( 'Select Product Brand Taxonomy to use in Schema.org & OpenGraph markup.', 'rank-math-pro' ),
|
||||
'options' => $options,
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'custom_product_brand',
|
||||
'type' => 'text',
|
||||
'name' => esc_html__( 'Brand', 'rank-math-pro' ),
|
||||
'desc' => esc_html__( 'Brand value to use in Schema.org & OpenGraph markup.', 'rank-math-pro' ),
|
||||
'dep' => [ [ 'product_brand', 'custom' ] ],
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'gtin',
|
||||
'type' => 'select',
|
||||
'name' => esc_html__( 'Global Identifier', 'rank-math-pro' ),
|
||||
'desc' => wp_kses_post( __( 'Global Identifier key to use in the Product Schema.', 'rank-math-pro' ) ),
|
||||
'options' => [
|
||||
'gtin8' => esc_html__( 'GTIN-8', 'rank-math-pro' ),
|
||||
'gtin12' => esc_html__( 'GTIN-12', 'rank-math-pro' ),
|
||||
'gtin13' => esc_html__( 'GTIN-13', 'rank-math-pro' ),
|
||||
'gtin14' => esc_html__( 'GTIN-14', 'rank-math-pro' ),
|
||||
'isbn' => esc_html__( 'ISBN', 'rank-math-pro' ),
|
||||
'mpn' => esc_html__( 'MPN', 'rank-math-pro' ),
|
||||
],
|
||||
'default' => 'gtin8',
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'show_gtin',
|
||||
'type' => 'toggle',
|
||||
'name' => esc_html__( 'Show Global Identifier', 'rank-math-pro' ),
|
||||
'desc' => esc_html__( 'Display the Global Identified on Product Page along with other product details.', 'rank-math-pro' ),
|
||||
'default' => 'off',
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'gtin_label',
|
||||
'type' => 'text',
|
||||
'name' => esc_html__( 'Global Identifier label', 'rank-math-pro' ),
|
||||
'desc' => esc_html__( 'Global Identifier label to show on Product Page.', 'rank-math-pro' ),
|
||||
'default' => 'GTIN:',
|
||||
'dep' => [ [ 'show_gtin', 'on' ] ],
|
||||
]
|
||||
);
|
||||
|
||||
$cmb->add_field(
|
||||
[
|
||||
'id' => 'noindex_hidden_products',
|
||||
'type' => 'toggle',
|
||||
'name' => esc_html__( 'Noindex Hidden Products', 'rank-math-pro' ),
|
||||
'desc' => wp_kses_post( __( 'Set Product Pages to noindex when WooCommerce Catalog visibility is set to hidden.', 'rank-math-pro' ) ),
|
||||
'default' => 'on',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add GTIN field in Product Metabox.
|
||||
*/
|
||||
public function add_gtin_field() {
|
||||
$gtin = Helper::get_settings( 'general.gtin', 'gtin8' );
|
||||
$label = 'isbn' === $gtin ? esc_html__( 'ISBN', 'rank-math-pro' ) : ( 'mpn' === $gtin ? esc_html__( 'MPN', 'rank-math-pro' ) : esc_html__( 'GTIN', 'rank-math-pro' ) );
|
||||
?>
|
||||
<div class="options_group">
|
||||
<?php
|
||||
woocommerce_wp_text_input(
|
||||
[
|
||||
'id' => '_rank_math_gtin_code',
|
||||
'label' => $label,
|
||||
'desc_tip' => true,
|
||||
// Translators: Global Identifier name.
|
||||
'description' => sprintf( esc_html__( '%s value to use in the Product schema.', 'rank-math-pro' ), $label ),
|
||||
]
|
||||
);
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Add GTIN field in Variable tab.
|
||||
*
|
||||
* @param int $loop Current variation index.
|
||||
* @param array $variation_data Current variation data.
|
||||
* @param object $variation Current variation object.
|
||||
*/
|
||||
public function add_variation_gtin_field( $loop, $variation_data, $variation ) {
|
||||
$gtin = Helper::get_settings( 'general.gtin', 'gtin8' );
|
||||
$label = 'isbn' === $gtin ? esc_html__( 'ISBN', 'rank-math-pro' ) : ( 'mpn' === $gtin ? esc_html__( 'MPN', 'rank-math-pro' ) : esc_html__( 'GTIN', 'rank-math-pro' ) );
|
||||
$variation_object = wc_get_product( $variation->ID );
|
||||
$value = $variation_object->get_meta( '_rank_math_gtin_code' );
|
||||
|
||||
woocommerce_wp_text_input(
|
||||
[
|
||||
'id' => "_rank_math_gtin_code_variable{$loop}",
|
||||
'name' => "_rank_math_gtin_code_variable[{$loop}]",
|
||||
'value' => $value,
|
||||
'label' => $label,
|
||||
'desc_tip' => true,
|
||||
// Translators: Global Identifier name.
|
||||
'description' => sprintf( esc_html__( '%s value to use in Product schema.', 'rank-math-pro' ), $label ),
|
||||
'wrapper_class' => 'form-row widefat',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save GTIN code.
|
||||
*
|
||||
* @param WC_PRODUCT $product Product Object.
|
||||
*/
|
||||
public function save_gtin_data( $product ) {
|
||||
if ( ! isset( $_POST['_rank_math_gtin_code'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$gtin_code = Param::post( '_rank_math_gtin_code' );
|
||||
$product->update_meta_data( '_rank_math_gtin_code', wc_clean( wp_unslash( $gtin_code ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save GTIN code for curent variation.
|
||||
*
|
||||
* @param int $variation_id Current variation ID.
|
||||
* @param int $id Index of current variation.
|
||||
*/
|
||||
public function save_variation_gtin_data( $variation_id, $id ) {
|
||||
if ( ! isset( $_POST['_rank_math_gtin_code_variable'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$gtin_code = $_POST['_rank_math_gtin_code_variable'][ $id ];
|
||||
$variation = wc_get_product( $variation_id );
|
||||
$variation->update_meta_data( '_rank_math_gtin_code', wc_clean( wp_unslash( $gtin_code ) ) );
|
||||
$variation->save_meta_data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change robots for Hidden Products according to settings.
|
||||
*
|
||||
* @param array $values Metabox values.
|
||||
* @param Screen $screen The current screen.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function updated_localized_data( $values, $screen ) {
|
||||
$object_id = $screen->get_object_id();
|
||||
|
||||
// Early bail if current post type is not Product or the Hidden product option is disabled.
|
||||
if ( 'product' !== get_post_type( $object_id ) || ! Helper::get_settings( 'general.noindex_hidden_products' ) ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
$product = wc_get_product( $object_id );
|
||||
|
||||
// Early bail.
|
||||
if ( 'hidden' !== $product->get_catalog_visibility() ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
if ( isset( $values['assessor']['serpData']['robots']['index'] ) ) {
|
||||
unset( $values['assessor']['serpData']['robots']['index'] );
|
||||
}
|
||||
$values['assessor']['serpData']['robots']['noindex'] = 1;
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change robots for Hidden Products according to settings for post columns.
|
||||
*
|
||||
* @param mixed $robots The rank_math_robots from the FORM post.
|
||||
* @param int $post_id The post id.
|
||||
* @return mixed|string[]
|
||||
*/
|
||||
public function update_rank_math_product_robots( $robots, $post_id ) {
|
||||
if ( 'product' !== get_post_type( $post_id ) || ! Helper::get_settings( 'general.noindex_hidden_products' ) ) {
|
||||
return $robots;
|
||||
}
|
||||
|
||||
$product = \wc_get_product( $post_id );
|
||||
if ( $product && 'hidden' !== $product->get_catalog_visibility() ) {
|
||||
return $robots;
|
||||
}
|
||||
|
||||
return [ 'noindex' ];
|
||||
}
|
||||
}
|
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce module.
|
||||
*
|
||||
* @since 1.0
|
||||
* @package RankMathPro
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Traits\Hooker;
|
||||
use MyThemeShop\Helpers\Conditional;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WooCommerce class.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class WooCommerce {
|
||||
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
if ( is_admin() ) {
|
||||
new Admin();
|
||||
return;
|
||||
}
|
||||
$this->filter( 'rank_math/sitemap/entry', 'remove_hidden_products', 10, 3 );
|
||||
$this->action( 'wp', 'init' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter/Hooks to add GTIN value on Product page.
|
||||
*/
|
||||
public function init() {
|
||||
$this->filter( 'rank_math/frontend/robots', 'robots' );
|
||||
|
||||
if ( ! is_product() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->filter( 'rank_math/snippet/rich_snippet_product_entity', 'add_gtin_in_schema' );
|
||||
$this->filter( 'rank_math/woocommerce/product_brand', 'add_custom_product_brand' );
|
||||
$this->filter( 'rank_math/snippet/rich_snippet_product_entity', 'add_variations_data' );
|
||||
$this->action( 'rank_math/opengraph/facebook', 'og_retailer_id', 60 );
|
||||
$this->filter( 'rank_math/snippet/rich_snippet_product_entity', 'additional_schema_properties' );
|
||||
|
||||
if ( Helper::get_settings( 'general.show_gtin' ) ) {
|
||||
$this->action( 'woocommerce_product_meta_start', 'add_gtin_meta' );
|
||||
$this->filter( 'woocommerce_available_variation', 'add_gtin_to_variation_param', 10, 3 );
|
||||
$this->action( 'wp_footer', 'add_variation_script' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove hidden products from the sitemap.
|
||||
*
|
||||
* @param array $url Array of URL parts.
|
||||
* @param string $type URL type. Can be user, post or term.
|
||||
* @param object $object Data object for the URL.
|
||||
*/
|
||||
public function remove_hidden_products( $url, $type, $object ) {
|
||||
if (
|
||||
'post' !== $type ||
|
||||
! isset( $object->post_type ) ||
|
||||
'product' !== $object->post_type ||
|
||||
! Helper::get_settings( 'general.noindex_hidden_products' ) ||
|
||||
'hidden' !== \wc_get_product( $object->ID )->get_catalog_visibility()
|
||||
) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change robots for WooCommerce pages according to settings
|
||||
*
|
||||
* @param array $robots Array of robots to sanitize.
|
||||
*
|
||||
* @return array Modified robots.
|
||||
*/
|
||||
public function robots( $robots ) {
|
||||
if ( ! Helper::get_settings( 'general.noindex_hidden_products' ) ) {
|
||||
return $robots;
|
||||
}
|
||||
|
||||
if ( is_product() ) {
|
||||
$product = \wc_get_product();
|
||||
$is_hidden = $product && $product->get_catalog_visibility() === 'hidden';
|
||||
if ( $is_hidden ) {
|
||||
return [
|
||||
'noindex' => 'noindex',
|
||||
'nofollow' => 'nofollow',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
global $wp_query;
|
||||
if ( is_product_taxonomy() && ! $wp_query->post_count && $wp_query->queried_object->count ) {
|
||||
return [
|
||||
'noindex' => 'noindex',
|
||||
'nofollow' => 'nofollow',
|
||||
];
|
||||
}
|
||||
|
||||
return $robots;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter to change Product brand value based on the Settings.
|
||||
*
|
||||
* @param string $brand Brand.
|
||||
*
|
||||
* @return string Modified brand.
|
||||
*/
|
||||
public function add_custom_product_brand( $brand ) {
|
||||
return 'custom' === Helper::get_settings( 'general.product_brand' ) ? Helper::get_settings( 'general.custom_product_brand' ) : $brand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter to add url, manufacturer & brand url in Product schema.
|
||||
*
|
||||
* @param array $entity Snippet Data.
|
||||
* @return array
|
||||
*
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public function additional_schema_properties( $entity ) {
|
||||
if ( ! $this->do_filter( 'schema/woocommerce/additional_properties', false ) ) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
$type = 'company' === Helper::get_settings( 'titles.knowledgegraph_type' ) ? 'organization' : 'person';
|
||||
$entity['manufacturer'] = [ '@id' => home_url( "/#{$type}" ) ];
|
||||
$entity['url'] = get_the_permalink();
|
||||
|
||||
$taxonomy = Helper::get_settings( 'general.product_brand' );
|
||||
if ( ! empty( $entity['brand'] ) && $taxonomy && taxonomy_exists( $taxonomy ) ) {
|
||||
$brands = get_the_terms( $product_id, $taxonomy );
|
||||
$entity['brand']['url'] = is_wp_error( $brands ) || empty( $brands[0] ) ? '' : get_term_link( $brands[0], $taxonomy );
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter to add GTIN in Product schema.
|
||||
*
|
||||
* @param array $entity Snippet Data.
|
||||
* @return array
|
||||
*/
|
||||
public function add_gtin_in_schema( $entity ) {
|
||||
$gtin_key = Helper::get_settings( 'general.gtin', 'gtin8' );
|
||||
if ( ! empty( $entity[ $gtin_key ] ) ) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
global $product;
|
||||
if ( ! is_object( $product ) ) {
|
||||
$product = wc_get_product( get_the_ID() );
|
||||
}
|
||||
|
||||
$gtin = $product->get_meta( '_rank_math_gtin_code' );
|
||||
if ( $gtin ) {
|
||||
$entity[ $gtin_key ] = $gtin;
|
||||
}
|
||||
|
||||
if ( ! empty( $entity['isbn'] ) ) {
|
||||
$entity['@type'] = [
|
||||
'Product',
|
||||
'Book',
|
||||
];
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add GTIN data in Product metadata.
|
||||
*/
|
||||
public function add_gtin_meta() {
|
||||
global $product;
|
||||
$gtin_code = $product->get_meta( '_rank_math_gtin_code' );
|
||||
if ( ! $gtin_code ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<span class="rank-math-gtin-wrapper">';
|
||||
echo $this->get_formatted_value( $gtin_code );
|
||||
echo '</span>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add GTIN value to available variations.
|
||||
*
|
||||
* @param array $args Array of variation arguments.
|
||||
* @param Object $product Current Product Object.
|
||||
* @param Object $variation Product variation.
|
||||
*
|
||||
* @return array Modified robots.
|
||||
*/
|
||||
public function add_gtin_to_variation_param( $args, $product, $variation ) {
|
||||
$args['rank_math_gtin'] = $this->get_formatted_value( $variation->get_meta( '_rank_math_gtin_code' ) );
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variation script to change GTIN when variation is changed from the dropdown.
|
||||
*/
|
||||
public function add_variation_script() {
|
||||
global $product;
|
||||
if ( ! $product->is_type( 'variable' ) ) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<script>
|
||||
const $form = jQuery( '.variations_form' );
|
||||
const wrapper = jQuery( '.rank-math-gtin-wrapper' );
|
||||
const gtin_code = wrapper.text();
|
||||
if ( $form.length ) {
|
||||
$form.on( 'found_variation', function( event, variation ) {
|
||||
if ( variation.rank_math_gtin ) {
|
||||
wrapper.text( variation.rank_math_gtin );
|
||||
}
|
||||
} );
|
||||
|
||||
$form.on( 'reset_data', function() {
|
||||
wrapper.text( gtin_code );
|
||||
} );
|
||||
}
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter to add Offers array in Product schema.
|
||||
*
|
||||
* @param array $entity Snippet Data.
|
||||
* @return array
|
||||
*/
|
||||
public function add_variations_data( $entity ) {
|
||||
$product = wc_get_product( get_the_ID() );
|
||||
if ( ! $product->is_type( 'variable' ) ) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
if ( empty( $entity['offers']['@type'] ) || 'AggregateOffer' !== $entity['offers']['@type'] ) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
$variations = $product->get_available_variations( 'object' );
|
||||
if ( empty( $variations ) ) {
|
||||
return $entity;
|
||||
}
|
||||
|
||||
$this->add_variable_gtin( get_the_ID(), $entity['offers'] );
|
||||
|
||||
$offers = [];
|
||||
foreach ( $variations as $variation ) {
|
||||
$price_valid_until = get_post_meta( $variation->get_id(), '_sale_price_dates_to', true );
|
||||
if ( ! $price_valid_until ) {
|
||||
$price_valid_until = strtotime( ( date( 'Y' ) + 1 ) . '-12-31' );
|
||||
}
|
||||
|
||||
$offer_entity = [
|
||||
'@type' => 'Offer',
|
||||
'description' => wp_strip_all_tags( $variation->get_description() ),
|
||||
'price' => wc_get_price_to_display( $variation ),
|
||||
'priceCurrency' => get_woocommerce_currency(),
|
||||
'availability' => 'outofstock' === $variation->get_stock_status() ? 'https://schema.org/OutOfStock' : 'https://schema.org/InStock',
|
||||
'itemCondition' => 'NewCondition',
|
||||
'priceValidUntil' => date_i18n( 'Y-m-d', $price_valid_until ),
|
||||
'url' => $product->get_permalink(),
|
||||
];
|
||||
|
||||
$this->add_variable_gtin( $variation->get_id(), $offer_entity );
|
||||
|
||||
$offers[] = $offer_entity;
|
||||
}
|
||||
|
||||
$entity['offers']['offers'] = $offers;
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product retailer ID to the OpenGraph output.
|
||||
*
|
||||
* @param OpenGraph $opengraph The current opengraph network object.
|
||||
*/
|
||||
public function og_retailer_id( $opengraph ) {
|
||||
$product = wc_get_product( get_the_ID() );
|
||||
if ( empty( $product ) || ! $product->get_sku() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$opengraph->tag( 'product:retailer_item_id', $product->get_sku() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add gtin value in variable offer datta.
|
||||
*
|
||||
* @param int $variation_id Variation ID.
|
||||
* @param array $entity Offer entity.
|
||||
*/
|
||||
private function add_variable_gtin( $variation_id, &$entity ) {
|
||||
$gtin_key = Helper::get_settings( 'general.gtin', 'gtin8' );
|
||||
$gtin = get_post_meta( $variation_id, '_rank_math_gtin_code', true );
|
||||
if ( ! $gtin || 'isbn' === $gtin_key ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entity[ $gtin_key ] = $gtin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted GTIN value with label.
|
||||
*
|
||||
* @param string $gtin GTIN code.
|
||||
*
|
||||
* @return string Formatted GTIN value with label.
|
||||
*/
|
||||
private function get_formatted_value( $gtin ) {
|
||||
$label = Helper::get_settings( 'general.gtin_label' );
|
||||
$label = $label ? $label . ' ' : '';
|
||||
|
||||
return esc_html( $this->do_filter( 'woocommerce/gtin_label', $label ) . $gtin );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user