$value ) { if ( ( isset( $value['reviewer-name'] ) && ! empty( $value['reviewer-name'] ) ) && ( isset( $value['product-rating'] ) && ! empty( $value['product-rating'] ) ) ) { $schema['review'][ $key ]['@type'] = 'Review'; $schema['review'][ $key ]['author']['name'] = wp_strip_all_tags( (string) $value['reviewer-name'] ); if ( isset( $value['reviewer-type'] ) && ! empty( $value['reviewer-type'] ) ) { $schema['review'][ $key ]['author']['@type'] = wp_strip_all_tags( (string) $value['reviewer-type'] ); } else { $schema['review'][ $key ]['author']['@type'] = 'Person'; } if ( isset( $value['product-rating'] ) && ! empty( $value['product-rating'] ) ) { $schema['review'][ $key ]['reviewRating']['@type'] = 'Rating'; $schema['review'][ $key ]['reviewRating']['ratingValue'] = wp_strip_all_tags( (string) $value['product-rating'] ); } $schema['review'][ $key ]['reviewBody'] = ! empty( $value['review-body'] ) ? wp_strip_all_tags( (string) $value['review-body'] ) : null; } } } if ( isset($data['merchant-return-policy']) && !empty($data['merchant-return-policy'] ) ) { // Initialize hasMerchantReturnPolicy array $schema['offers']['hasMerchantReturnPolicy'] = array(); foreach ($data['merchant-return-policy'] as $policy) { // Add each policy to hasMerchantReturnPolicy $return_policy = array(); $return_policy['@type'] = 'MerchantReturnPolicy'; $return_policy['applicableCountry'] = isset($policy['applicableCountry']) ? wp_strip_all_tags((string)$policy['applicableCountry']) : null; // Validate length of applicableCountry if ($return_policy['applicableCountry'] && strlen($return_policy['applicableCountry']) > 2) { // Truncate or handle the string appropriately if it exceeds the maximum length $return_policy['applicableCountry'] = substr($return_policy['applicableCountry'], 0, 2); } $return_policy['returnPolicyCategory'] = isset($policy['returnPolicyCategory']) ? esc_url($policy['returnPolicyCategory']) : null; $return_policy['merchantReturnDays'] = isset($policy['merchantReturnDays']) ? intval($policy['merchantReturnDays']) : null; $return_policy['returnFees'] = isset($policy['returnFees']) ? esc_url($policy['returnFees']) : null; $return_policy['returnMethod'] = isset($policy['returnMethod']) ? esc_url($policy['returnMethod']) : null; // Add returnShippingFeesAmount if returnFees is set to ReturnShippingFees if ( isset($policy['returnFees']) && $policy['returnFees'] === 'https://schema.org/ReturnShippingFees' && isset($policy['returnShippingFeesAmount']) && !empty($policy['returnShippingFeesAmount']) ) { // Initialize returnShippingFeesAmount array $return_shipping_fees_amount = array(); // Add value and currency to returnShippingFeesAmount $return_shipping_fees_amount['@type'] = 'MonetaryAmount'; $return_shipping_fees_amount['value'] = floatval($policy['returnShippingFeesAmount']); $return_shipping_fees_amount['currency'] = !empty($policy['merchantCurrency']) ? $policy['merchantCurrency'] : null; // Default to USD if currency not provided $return_policy['returnShippingFeesAmount'] = $return_shipping_fees_amount; } // Push the policy to hasMerchantReturnPolicy array $schema['offers']['hasMerchantReturnPolicy'][] = $return_policy; } } // Fetch woocommerce review. if ( defined( 'WC_VERSION' ) && apply_filters( 'wp_schema_pro_add_woocommerce_review', false ) ) { $comments = get_comments( array( 'number' => 5, 'post_id' => $post['ID'], 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', 'parent' => 0, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => 'rating', 'type' => 'NUMERIC', 'compare' => '>', 'value' => 0, ), ), ) ); if ( $comments ) { foreach ( $comments as $key => $comment ) { $schema['review'][ $key ]['@type'] = 'Review'; $schema['review'][ $key ]['reviewRating']['@type'] = 'Rating'; $schema['review'][ $key ]['reviewRating']['ratingValue'] = get_comment_meta( $comment->comment_ID, 'rating', true ); $schema['review'][ $key ]['author']['@type'] = 'Person'; $schema['review'][ $key ]['author']['name'] = get_comment_author( $comment ); $schema['review'][ $key ]['reviewBody'] = get_comment_text( $comment ); } } } return apply_filters( 'wp_schema_pro_schema_product', $schema, $data, $post ); } } }