ClassicVariationGalleryAdmin.php
4 weeks ago
LegacyVariationGalleryCompatibility.php
4 weeks ago
Migration.php
4 weeks ago
Package.php
4 weeks ago
Telemetry.php
4 weeks ago
Migration.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Batched migration of legacy variation gallery meta into core's native prop. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\VariationGallery; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Migrate legacy variation gallery meta into WooCommerce's native variation gallery prop. |
| 14 | */ |
| 15 | class Migration { |
| 16 | |
| 17 | /** |
| 18 | * Number of variations processed per batch. |
| 19 | */ |
| 20 | private const BATCH_SIZE = 250; |
| 21 | |
| 22 | /** |
| 23 | * Option name recording when the migration finished. |
| 24 | */ |
| 25 | public const COMPLETED_OPTION = 'wc_variation_gallery_migration_completed_at'; |
| 26 | |
| 27 | /** |
| 28 | * Run one batch of the migration. |
| 29 | * |
| 30 | * @return bool Whether there are pending migration records. |
| 31 | */ |
| 32 | public static function run(): bool { |
| 33 | global $wpdb; |
| 34 | |
| 35 | if ( get_option( self::COMPLETED_OPTION ) ) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | $legacy_meta_key = '_wc_additional_variation_images'; |
| 40 | $core_gallery_meta = '_product_image_gallery'; |
| 41 | $fallback_disabled = LegacyVariationGalleryCompatibility::get_core_managed_meta_key(); |
| 42 | $select_variation_ids = static function ( int $limit ) use ( $wpdb, $legacy_meta_key, $fallback_disabled ): array { |
| 43 | $query = $wpdb->prepare( |
| 44 | "SELECT legacy.post_id |
| 45 | FROM {$wpdb->postmeta} AS legacy |
| 46 | INNER JOIN {$wpdb->posts} AS posts |
| 47 | ON posts.ID = legacy.post_id |
| 48 | AND posts.post_type = 'product_variation' |
| 49 | LEFT JOIN {$wpdb->postmeta} AS disabled |
| 50 | ON disabled.post_id = legacy.post_id |
| 51 | AND disabled.meta_key = %s |
| 52 | WHERE legacy.meta_key = %s |
| 53 | AND legacy.meta_value <> '' |
| 54 | AND disabled.post_id IS NULL |
| 55 | GROUP BY legacy.post_id |
| 56 | ORDER BY legacy.post_id ASC |
| 57 | LIMIT %d", |
| 58 | $fallback_disabled, |
| 59 | $legacy_meta_key, |
| 60 | $limit |
| 61 | ); |
| 62 | |
| 63 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared immediately above. |
| 64 | return array_map( 'intval', $wpdb->get_col( $query ) ); |
| 65 | }; |
| 66 | |
| 67 | $variation_ids = $select_variation_ids( self::BATCH_SIZE ); |
| 68 | |
| 69 | foreach ( $variation_ids as $variation_id ) { |
| 70 | $legacy_gallery_image_ids = array_values( |
| 71 | array_filter( wp_parse_id_list( get_post_meta( $variation_id, $legacy_meta_key, true ) ) ) |
| 72 | ); |
| 73 | $core_gallery_image_ids = wp_parse_id_list( get_post_meta( $variation_id, $core_gallery_meta, true ) ); |
| 74 | |
| 75 | if ( empty( $core_gallery_image_ids ) && ! empty( $legacy_gallery_image_ids ) ) { |
| 76 | update_post_meta( $variation_id, $core_gallery_meta, implode( ',', $legacy_gallery_image_ids ) ); |
| 77 | } |
| 78 | |
| 79 | // Keep legacy meta for third-party readers; disable fallback via the sentinel instead. |
| 80 | LegacyVariationGalleryCompatibility::mark_variation_id_core_managed( $variation_id ); |
| 81 | } |
| 82 | |
| 83 | $has_more = ! empty( $select_variation_ids( 1 ) ); |
| 84 | |
| 85 | // Guard against duplicate completion events if this runner is invoked twice. |
| 86 | if ( ! $has_more && ! get_option( self::COMPLETED_OPTION ) ) { |
| 87 | update_option( self::COMPLETED_OPTION, time() ); |
| 88 | Telemetry::record_event( Telemetry::EVENT_MIGRATION_COMPLETED ); |
| 89 | } |
| 90 | |
| 91 | return $has_more; |
| 92 | } |
| 93 | } |
| 94 |