/* Plugin Name: WooCommerce Variation Swatches Optimized Loader (v1.6 Ultra Fast Fix - Enhanced Error Handling) Description: Ultra-light, true ultra-fast JSON-LD for variations (100+ supported). Single DB query for variation SKU+image, preserves full-size images, safe for high-load hosts. Version: 1.6.1 Author: Custom (Enhanced by Gemini) */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * 1) Front-end swatch: small image + full srcset (unchanged, safe) */ add_filter('woocommerce_swatches_image_html', function($html, $image_id){ if ( ! $image_id ) return $html; // Use thumbnail for swatch display (fast) and full in srcset for crawlers $swatch_size = 'thumbnail'; $src = wp_get_attachment_image_url($image_id, $swatch_size); $srcset = wp_get_attachment_image_srcset($image_id, 'full'); return sprintf( '', esc_url($src), esc_attr($srcset) ); }, 10, 2); /** * 2) v1.6.1 Ultra-fast JSON-LD for variations - Enhanced Error Handling * * Key ideas: * - Added explicit $wpdb error checks after queries for robustness. * - Single optimized DB query to fetch variation ID -> sku + thumbnail_id */ add_filter('woocommerce_structured_data_product', function($data, $product){ // Safety guards: skip background / REST / CLI / admin-lists if ( wp_doing_cron() || ( defined('WP_CLI') && WP_CLI ) || ( defined('REST_REQUEST') && REST_REQUEST ) ) { return $data; } if ( is_admin() && ! wp_doing_ajax() ) { return $data; } // only for variable products with variant JSON-LD present if ( ! $product || ! $product->is_type('variable') || empty($data['hasVariant']) || ! is_array($data['hasVariant']) ) { return $data; } static $processed = []; $product_id = intval( $product->get_id() ); if ( isset( $processed[ $product_id ] ) ) { return $data; } $processed[ $product_id ] = true; // Build SKU -> index map from the existing structured data (cheap) $variant_index = []; foreach ( $data['hasVariant'] as $i => $v ) { if ( isset( $v['sku'] ) && $v['sku'] !== '' ) { $variant_index[ (string) $v['sku'] ] = $i; } } if ( empty( $variant_index ) ) { return $data; } global $wpdb; // --- 查詢 1: 獲取所有變體 ID --- // Fetch variation IDs (published) for this parent product $variation_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = 'product_variation' AND post_status IN ('publish','private','draft')", $product_id ) ); // 錯誤檢查 1: 確保查詢成功且結果是陣列 if ( ! is_array( $variation_ids ) || $wpdb->last_error ) { // Log the error if necessary, but safely return original data // error_log( "Variation Swatch JSON-LD DB Error 1: " . $wpdb->last_error ); return $data; } if ( empty( $variation_ids ) ) { return $data; } // --- 查詢 2: 批次獲取 SKUs 和 Thumbnail IDs --- // Prepare to fetch meta for those variation IDs: _sku and _thumbnail_id $ids_placeholders = implode( ',', array_fill(0, count($variation_ids), '%d') ); $meta_query = " SELECT post_id, meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id IN ($ids_placeholders) AND meta_key IN ('_sku','_thumbnail_id') "; // Build args for prepare: array( query_string, id1, id2, ... ) $prepare_args = array_merge( array( $meta_query ), $variation_ids ); $sql = array_shift($prepare_args); $rows = $wpdb->get_results( $wpdb->prepare( $sql, $prepare_args ), ARRAY_A ); // 錯誤檢查 2: 確保查詢成功且結果是陣列 if ( ! is_array( $rows ) || $wpdb->last_error ) { // Log the error if necessary, but safely return original data // error_log( "Variation Swatch JSON-LD DB Error 2: " . $wpdb->last_error ); return $data; } if ( empty( $rows ) ) { return $data; } // Reduce rows into a map: variation_id => ['sku'=>..., 'image_id'=>...] $map = []; foreach ( $rows as $r ) { $vid = intval( $r['post_id'] ); if ( ! isset( $map[ $vid ] ) ) $map[ $vid ] = [ 'sku' => '', 'image_id' => 0 ]; if ( $r['meta_key'] === '_sku' ) { $map[ $vid ]['sku'] = (string) $r['meta_value']; } elseif ( $r['meta_key'] === '_thumbnail_id' ) { $map[ $vid ]['image_id'] = intval( $r['meta_value'] ); } } // Parent product fallback full image $parent_image_id = $product->get_image_id(); $parent_full_url = $parent_image_id ? wp_get_attachment_image_url( $parent_image_id, 'full' ) : ''; // Iterate the map and fill JSON-LD foreach ( $map as $vid => $info ) { $sku = (string) $info['sku']; if ( $sku === '' ) { continue; } if ( ! isset( $variant_index[ $sku ] ) ) { // SKU not present in original structured data (skip) continue; } $idx = $variant_index[ $sku ]; $image_id = intval( $info['image_id'] ); $full_url = ''; if ( $image_id > 0 ) { // minimal call to produce final URL; this is safe and cheap $full_url = wp_get_attachment_image_url( $image_id, 'full' ); } else { $full_url = $parent_full_url; } if ( $full_url ) { // 確保結構:使用陣列形式以符合 JSON-LD 規範,並使用 esc_url_raw $data['hasVariant'][ $idx ]['image'] = array( esc_url_raw( $full_url ) ); } } return $data; }, 10, 2); https://cottonsweetshop.com/post-sitemap.xml 2024-08-13T16:33:38+00:00 https://cottonsweetshop.com/page-sitemap.xml 2025-11-15T11:32:05+00:00 https://cottonsweetshop.com/product-sitemap1.xml 2025-11-19T09:26:39+00:00 https://cottonsweetshop.com/product-sitemap2.xml 2025-11-19T09:22:47+00:00 https://cottonsweetshop.com/product-sitemap3.xml 2025-11-19T08:55:36+00:00 https://cottonsweetshop.com/category-sitemap.xml 2024-08-13T16:33:38+00:00 https://cottonsweetshop.com/product_brand-sitemap.xml 2025-11-19T09:26:39+00:00 https://cottonsweetshop.com/product_cat-sitemap.xml 2025-11-19T09:26:39+00:00 https://cottonsweetshop.com/local-sitemap.xml 2025-10-08T01:00:59+00:00