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
Package.php
131 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Package class file for the variation gallery feature. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\VariationGallery; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Variation gallery package entry point. |
| 14 | * |
| 15 | * Registered in `\Automattic\WooCommerce\Packages::$merged_packages` against |
| 16 | * the `woocommerce-additional-variation-images` slug, so this class is the |
| 17 | * single bootstrap surface for the merged variation gallery feature. |
| 18 | */ |
| 19 | class Package { |
| 20 | |
| 21 | /** |
| 22 | * Action Scheduler hook for DB update callbacks. |
| 23 | */ |
| 24 | private const UPDATE_CALLBACK_HOOK = 'woocommerce_run_update_callback'; |
| 25 | |
| 26 | /** |
| 27 | * Action Scheduler group for DB update callbacks. |
| 28 | */ |
| 29 | private const UPDATE_CALLBACK_GROUP = 'woocommerce-db-updates'; |
| 30 | |
| 31 | /** |
| 32 | * The feature id used by `FeaturesController` (Settings → Advanced → Features). |
| 33 | */ |
| 34 | public const FEATURE_ID = 'variation_gallery'; |
| 35 | |
| 36 | /** |
| 37 | * Option backing the variation gallery feature toggle. |
| 38 | */ |
| 39 | public const ENABLE_OPTION_NAME = 'wc_feature_woocommerce_additional_variation_images_enabled'; |
| 40 | |
| 41 | /** |
| 42 | * Whether the merged variation gallery feature is enabled for the current |
| 43 | * request. |
| 44 | * |
| 45 | * Reads the same option as the Features toggles, so the `FeaturesController` |
| 46 | * and the merged-package machinery share a single source of truth. Defaults |
| 47 | * to off for the 10.9 canary period. |
| 48 | * |
| 49 | * @return bool |
| 50 | */ |
| 51 | public static function is_enabled() { |
| 52 | return 'yes' === get_option( self::ENABLE_OPTION_NAME, 'no' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Early bootstrap hook fired by `Packages::prepare_packages` at |
| 57 | * plugins_loaded priority -100. No-op for the variation gallery feature. |
| 58 | * |
| 59 | * @internal |
| 60 | */ |
| 61 | public static function prepare(): void { |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Initialize the merged variation gallery feature. |
| 66 | * |
| 67 | * @internal |
| 68 | */ |
| 69 | final public static function init(): void { |
| 70 | if ( ! self::is_enabled() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $container = wc_get_container(); |
| 75 | $container->get( ClassicVariationGalleryAdmin::class )->register(); |
| 76 | $container->get( LegacyVariationGalleryCompatibility::class )->register(); |
| 77 | |
| 78 | // Action Scheduler initializes on `init`, not `plugins_loaded`. |
| 79 | add_action( 'init', array( __CLASS__, 'maybe_schedule_migration' ), 20 ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Schedule the legacy variation gallery migration if it hasn't already |
| 84 | * completed and isn't already pending or running. |
| 85 | * |
| 86 | * @internal |
| 87 | */ |
| 88 | public static function maybe_schedule_migration(): void { |
| 89 | if ( get_option( Migration::COMPLETED_OPTION ) ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $args = array( 'update_callback' => array( Migration::class, 'run' ) ); |
| 94 | |
| 95 | if ( self::has_pending_or_running_migration( $args ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | WC()->queue()->add( |
| 100 | self::UPDATE_CALLBACK_HOOK, |
| 101 | $args, |
| 102 | self::UPDATE_CALLBACK_GROUP |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Determine whether the migration is already pending or running. |
| 108 | * |
| 109 | * @param array<string, array<int, string>> $args Exact callback args for the migration action. |
| 110 | * @return bool |
| 111 | */ |
| 112 | private static function has_pending_or_running_migration( array $args ): bool { |
| 113 | if ( null !== WC()->queue()->get_next( self::UPDATE_CALLBACK_HOOK, $args, self::UPDATE_CALLBACK_GROUP ) ) { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | $running_actions = WC()->queue()->search( |
| 118 | array( |
| 119 | 'hook' => self::UPDATE_CALLBACK_HOOK, |
| 120 | 'args' => $args, |
| 121 | 'status' => \ActionScheduler_Store::STATUS_RUNNING, |
| 122 | 'per_page' => 1, |
| 123 | 'group' => self::UPDATE_CALLBACK_GROUP, |
| 124 | ), |
| 125 | 'ids' |
| 126 | ); |
| 127 | |
| 128 | return ! empty( $running_actions ); |
| 129 | } |
| 130 | } |
| 131 |