ClassicVariationGalleryAdmin.php
1 month ago
LegacyVariationGalleryCompatibility.php
1 month ago
Migration.php
1 month ago
Package.php
1 month ago
Telemetry.php
1 month ago
LegacyVariationGalleryCompatibility.php
113 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\VariationGallery; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\RegisterHooksInterface; |
| 7 | use WC_Product_Variation; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Provides read compatibility for variation galleries stored by the |
| 13 | * Additional Variation Images extension. |
| 14 | */ |
| 15 | class LegacyVariationGalleryCompatibility implements RegisterHooksInterface { |
| 16 | |
| 17 | /** |
| 18 | * Legacy meta key used by the retired extension. |
| 19 | */ |
| 20 | private const LEGACY_META_KEY = '_wc_additional_variation_images'; |
| 21 | |
| 22 | /** |
| 23 | * Marks a variation as explicitly managed by core, so legacy fallback stops applying. |
| 24 | */ |
| 25 | private const LEGACY_FALLBACK_DISABLED_META_KEY = '_wc_variation_gallery_legacy_fallback_disabled'; |
| 26 | |
| 27 | /** |
| 28 | * Get the internal meta key used to mark legacy fallback as disabled. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public static function get_core_managed_meta_key(): string { |
| 33 | return self::LEGACY_FALLBACK_DISABLED_META_KEY; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Mark a variation as managed by core so legacy fallback stops applying. |
| 38 | * |
| 39 | * @param WC_Product_Variation $variation Variation managed by core. |
| 40 | * @return void |
| 41 | */ |
| 42 | public static function mark_core_managed( WC_Product_Variation $variation ): void { |
| 43 | if ( ! metadata_exists( 'post', $variation->get_id(), self::LEGACY_META_KEY ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $variation->update_meta_data( self::LEGACY_FALLBACK_DISABLED_META_KEY, 'yes' ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Mark a variation ID as managed by core so legacy fallback stops applying. |
| 52 | * |
| 53 | * @param int $variation_id Variation ID managed by core. |
| 54 | * @return void |
| 55 | */ |
| 56 | public static function mark_variation_id_core_managed( int $variation_id ): void { |
| 57 | if ( ! metadata_exists( 'post', $variation_id, self::LEGACY_META_KEY ) ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | update_post_meta( $variation_id, self::LEGACY_FALLBACK_DISABLED_META_KEY, 'yes' ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Determine whether a variation ID is already managed by core. |
| 66 | * |
| 67 | * @param int $variation_id Variation ID. |
| 68 | * @return bool |
| 69 | */ |
| 70 | public static function is_variation_id_core_managed( int $variation_id ): bool { |
| 71 | return metadata_exists( 'post', $variation_id, self::LEGACY_FALLBACK_DISABLED_META_KEY ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register compatibility hooks. |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function register() { |
| 80 | add_filter( 'woocommerce_product_variation_get_gallery_image_ids', array( $this, 'maybe_read_legacy_gallery_image_ids' ), 10, 2 ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Use legacy variation gallery meta when the core gallery is empty and the |
| 85 | * variation has not been marked as core-managed. |
| 86 | * |
| 87 | * @param array<mixed> $gallery_image_ids Gallery image IDs already resolved by core. |
| 88 | * @param WC_Product_Variation $variation Variation instance. |
| 89 | * @return array<int> |
| 90 | */ |
| 91 | public function maybe_read_legacy_gallery_image_ids( $gallery_image_ids, WC_Product_Variation $variation ): array { |
| 92 | // Core has variation images, just normalize. |
| 93 | if ( ! empty( $gallery_image_ids ) ) { |
| 94 | return array_values( wp_parse_id_list( $gallery_image_ids ) ); |
| 95 | } |
| 96 | |
| 97 | // Sentinel set: respect the explicit "no images" choice; legacy meta remains for BC. |
| 98 | if ( self::is_variation_id_core_managed( $variation->get_id() ) ) { |
| 99 | return array(); |
| 100 | } |
| 101 | |
| 102 | $legacy_gallery_image_ids = get_post_meta( $variation->get_id(), self::LEGACY_META_KEY, true ); |
| 103 | |
| 104 | // Nothing to fall back to. |
| 105 | if ( empty( $legacy_gallery_image_ids ) ) { |
| 106 | return array(); |
| 107 | } |
| 108 | |
| 109 | // Pre-migration variation: fall back to the legacy extension's meta. |
| 110 | return array_values( wp_parse_id_list( $legacy_gallery_image_ids ) ); |
| 111 | } |
| 112 | } |
| 113 |