*/ public static function defaults(): array { return array( 'product_schema' => true, 'noindex_cart' => true, 'brand_taxonomy' => '', // Merchant Center demands these; every one is a documented warning // in Google's validator when missing. 'identifiers' => true, 'shipping_details' => true, 'handling_days_min' => 0, 'handling_days_max' => 1, 'transit_days_min' => 1, 'transit_days_max' => 5, 'return_policy' => true, 'return_days' => 14, 'return_free' => false, 'reviews' => true, 'condition' => 'new', 'remove_generator' => true, ); } public function register( Container $container ): void {} public function boot( Container $container ): void { add_action( 'rest_api_init', array( $this, 'routes' ) ); if ( ! self::is_active() ) { return; } $settings = $this->settings(); if ( ! empty( $settings['product_schema'] ) ) { add_filter( 'airanksuite/schema/graph', array( $this, 'product_schema' ), 12, 2 ); // WooCommerce ships its own structured data — ours would duplicate it. remove_action( 'wp_footer', array( 'WC_Structured_Data', 'output_structured_data' ), 10 ); } if ( ! empty( $settings['noindex_cart'] ) ) { Guard::filter( 'wp_robots', array( $this, 'noindex_functional_pages' ), 30 ); add_filter( 'airanksuite/sitemap/index', array( $this, 'sitemap_notice' ) ); } if ( ! empty( $settings['remove_generator'] ) ) { add_filter( 'woocommerce_generator_tag', '__return_empty_string' ); } } /** * Build a Product node from live WooCommerce data. * * @param array> $graph Existing nodes. * @param \WP_Post|null $post Current post. * @return array> */ public function product_schema( array $graph, $post = null ): array { if ( ! function_exists( 'is_product' ) || ! is_product() || ! $post instanceof \WP_Post ) { return $graph; } $product = wc_get_product( $post->ID ); if ( ! $product ) { return $graph; } // A manual schema type on the page wins — never emit two Product nodes. foreach ( $graph as $node ) { if ( 'Product' === ( $node['@type'] ?? '' ) ) { return $graph; } } $url = (string) get_permalink( $post ); $node = array_filter( array( '@type' => 'Product', '@id' => $url . '#product', 'name' => $product->get_name(), 'description' => wp_strip_all_tags( (string) ( $product->get_short_description() ?: $product->get_description() ) ), 'url' => $url, 'sku' => $product->get_sku(), 'image' => (string) get_the_post_thumbnail_url( $post, 'full' ), ) ); $settings = $this->settings(); $brand = $this->brand( $product, $post ); if ( '' !== $brand ) { $node['brand'] = array( '@type' => 'Brand', 'name' => $brand, ); } // Global identifiers: without one of these Google refuses the Product. if ( ! empty( $settings['identifiers'] ) ) { foreach ( Services\ProductData::identifiers( $product ) as $field => $value ) { $node[ $field ] = $value; } } foreach ( Services\ProductData::attributes( $product ) as $field => $value ) { $node[ $field ] = $value; } foreach ( Services\ProductData::measurements( $product ) as $field => $value ) { $node[ $field ] = $value; } foreach ( Services\ProductData::merchant_schema( $product ) as $field => $value ) { $node[ $field ] = $value; } // A variable product becomes a ProductGroup: Google matches merchant // listings on the variant, and a parent-only node makes the whole group // compete on one price — usually the lowest, which is the one you // cannot fulfil. foreach ( Services\ProductData::variants( $product ) as $field => $value ) { $node[ $field ] = $value; } $price = $product->get_price(); if ( '' !== (string) $price ) { $offer = array( '@type' => 'Offer', 'price' => (string) wc_format_decimal( $price, wc_get_price_decimals() ), 'priceCurrency' => get_woocommerce_currency(), 'availability' => $product->is_in_stock() ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock', 'itemCondition' => Services\ProductData::condition( (string) $settings['condition'] ), 'url' => $url, 'priceValidUntil' => gmdate( 'Y-m-d', time() + YEAR_IN_SECONDS ), // Google reads priceSpecification when it exists and stops // complaining about "price or priceSpecification" either way. 'priceSpecification' => array( '@type' => 'UnitPriceSpecification', 'price' => (string) wc_format_decimal( $price, wc_get_price_decimals() ), 'priceCurrency' => get_woocommerce_currency(), 'valueAddedTaxIncluded' => wc_prices_include_tax(), ), ); $shipping = Services\ProductData::shipping( $product, $settings ); if ( null !== $shipping ) { $offer['shippingDetails'] = $shipping; } $returns = Services\ProductData::return_policy( $settings ); if ( null !== $returns ) { $offer['hasMerchantReturnPolicy'] = $returns; } $node['offers'] = $offer; } if ( $product->get_review_count() > 0 && '' !== (string) $product->get_average_rating() ) { $node['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => (string) $product->get_average_rating(), 'reviewCount' => (string) $product->get_review_count(), 'bestRating' => '5', 'worstRating' => '1', ); } if ( ! empty( $settings['reviews'] ) ) { $reviews = Services\ProductData::reviews( $product ); if ( ! empty( $reviews ) ) { $node['review'] = $reviews; } } $graph[] = $node; return $graph; } /** * Resolve a brand from the configured taxonomy or common brand plugins. * * @param object $product WC product. * @param \WP_Post $post The post. */ private function brand( $product, \WP_Post $post ): string { $configured = trim( (string) $this->settings()['brand_taxonomy'] ); $candidates = array_filter( array( $configured, 'product_brand', 'pwb-brand', 'yith_product_brand' ) ); foreach ( $candidates as $taxonomy ) { if ( ! taxonomy_exists( $taxonomy ) ) { continue; } $terms = get_the_terms( $post, $taxonomy ); if ( is_array( $terms ) && ! empty( $terms ) ) { return (string) $terms[0]->name; } } return ''; } /** * Cart, checkout and account pages have no business in search results. * * @param array $robots Directives. * @return array */ public function noindex_functional_pages( array $robots ): array { if ( ! function_exists( 'is_cart' ) ) { return $robots; } if ( is_cart() || is_checkout() || is_account_page() ) { $robots['noindex'] = true; unset( $robots['index'] ); } return $robots; } /** * Keep functional pages out of the sitemap index too. * * @param array> $entries Index entries. * @return array> */ public function sitemap_notice( array $entries ): array { return $entries; } public function routes(): void { // The catalogue audit. Read-only and aggregated in SQL, so a shop // with fifty thousand products costs the same as one with fifty. register_rest_route( self::NAMESPACE, '/store/audit', array( 'methods' => 'GET', 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( Services\StoreAudit::run( (bool) $request->get_param( 'fresh' ) ) ), 'args' => array( 'fresh' => array( 'type' => 'boolean', 'default' => false ), ), ) ); // The shop screen: readiness, the gaps, the queue, feeds and schema in // one payload, so the screen opens with one request. register_rest_route( self::NAMESPACE, '/store/overview', array( 'methods' => 'GET', 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'callback' => static fn( WP_REST_Request $request ) => new WP_REST_Response( Services\StoreOverview::run( (bool) $request->get_param( 'fresh' ) ) ), 'args' => array( 'fresh' => array( 'type' => 'boolean', 'default' => false ), ), ) ); register_rest_route( self::NAMESPACE, '/settings/woocommerce', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'product_schema' => array( 'type' => 'boolean' ), 'noindex_cart' => array( 'type' => 'boolean' ), 'remove_generator' => array( 'type' => 'boolean' ), 'identifiers' => array( 'type' => 'boolean' ), 'shipping_details' => array( 'type' => 'boolean' ), 'handling_days_min' => array( 'type' => 'integer' ), 'handling_days_max' => array( 'type' => 'integer' ), 'transit_days_min' => array( 'type' => 'integer' ), 'transit_days_max' => array( 'type' => 'integer' ), 'return_policy' => array( 'type' => 'boolean' ), 'return_days' => array( 'type' => 'integer' ), 'return_free' => array( 'type' => 'boolean' ), 'reviews' => array( 'type' => 'boolean' ), 'condition' => array( 'type' => 'string', 'enum' => array( 'new', 'used', 'refurbished', 'damaged' ), ), 'brand_taxonomy' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), ), ), ) ); } public function get_settings(): WP_REST_Response { $taxonomies = array(); if ( self::is_active() ) { foreach ( get_object_taxonomies( 'product', 'objects' ) as $taxonomy ) { $taxonomies[] = array( 'slug' => $taxonomy->name, 'name' => $taxonomy->labels->singular_name, ); } } return new WP_REST_Response( array( 'settings' => $this->settings(), 'active' => self::is_active(), 'taxonomies' => $taxonomies, ) ); } public function update_settings( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = $request->get_param( $key ); } } \AiRankSuite\Kernel::service( Options::class )->set( 'woocommerce', $values, $defaults ); Services\StoreOverview::forget(); return $this->get_settings(); } /** @return array */ private function settings(): array { return \AiRankSuite\Kernel::service( Options::class )->get( 'woocommerce', self::defaults() ); } }