AsyncProductEditorCategoryField
3 years ago
Blueprint
11 months ago
Fulfillments
4 weeks ago
MarketingRecommendations
3 months ago
Navigation
1 year ago
OnboardingTasks
4 weeks ago
PaymentGatewaySuggestions
1 year ago
ProductBlockEditor
4 weeks ago
ProductDataViews
4 weeks ago
ShippingPartnerSuggestions
1 month ago
Features.php
4 weeks ago
LaunchYourStore.php
4 weeks ago
Onboarding.php
1 year ago
ProductVariationsClassicRedesign.php
4 weeks ago
TransientNotices.php
3 years ago
ProductVariationsClassicRedesign.php
100 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Product Variations Classic Redesign |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\Features; |
| 9 | |
| 10 | /** |
| 11 | * Loads assets for the product variations classic redesign feature. |
| 12 | */ |
| 13 | class ProductVariationsClassicRedesign { |
| 14 | const FEATURE_ID = 'product-variations-classic-redesign'; |
| 15 | const SCRIPT_HANDLE = 'wc-experimental-products-app'; |
| 16 | const SCRIPT_PATH = 'experimental-products-app'; |
| 17 | const ROOT_ID = 'woocommerce-variations-classic-root'; |
| 18 | const ATTRIBUTES_ROOT_ID = 'woocommerce-product-attributes-classic-root'; |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | if ( ! is_admin() ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 ); |
| 29 | add_filter( 'woocommerce_product_data_tabs', array( $this, 'handle_woocommerce_product_data_tabs' ), 5 ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Returns true if we are on a product edit screen. |
| 34 | */ |
| 35 | public static function is_product_edit_page(): bool { |
| 36 | $screen = get_current_screen(); |
| 37 | return $screen && 'product' === $screen->post_type && 'post' === $screen->base; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns true if the user has requested legacy editing for a specific variation. |
| 42 | */ |
| 43 | public static function is_legacy_variation_edit(): bool { |
| 44 | // phpcs:disable WordPress.Security.NonceVerification |
| 45 | return isset( $_GET['edit_variation'] ) && is_numeric( $_GET['edit_variation'] ); |
| 46 | // phpcs:enable WordPress.Security.NonceVerification |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Handle the woocommerce_product_data_tabs filter. |
| 51 | * |
| 52 | * @internal |
| 53 | * |
| 54 | * @param array $tabs Product data tabs. |
| 55 | * @return array Product data tabs. |
| 56 | */ |
| 57 | public function handle_woocommerce_product_data_tabs( array $tabs ): array { |
| 58 | if ( isset( $tabs['variations'] ) && is_array( $tabs['variations'] ) ) { |
| 59 | $tabs['variations']['priority'] = 40; |
| 60 | } |
| 61 | |
| 62 | if ( isset( $tabs['linked_product'] ) && is_array( $tabs['linked_product'] ) ) { |
| 63 | $tabs['linked_product']['priority'] = 60; |
| 64 | } |
| 65 | |
| 66 | return $tabs; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Enqueue scripts and styles for the variations table. |
| 71 | */ |
| 72 | public function enqueue_scripts(): void { |
| 73 | if ( ! self::is_product_edit_page() || self::is_legacy_variation_edit() ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | wp_enqueue_script( self::SCRIPT_HANDLE ); |
| 78 | wp_enqueue_style( self::SCRIPT_HANDLE ); |
| 79 | |
| 80 | global $post; |
| 81 | $product_id = $post ? $post->ID : 0; |
| 82 | $script = sprintf( |
| 83 | 'window.wc.experimentalProductsApp.initializeVariationView( %s, %d );', |
| 84 | wp_json_encode( self::ROOT_ID ), |
| 85 | $product_id |
| 86 | ); |
| 87 | $script .= sprintf( |
| 88 | ' window.wc.experimentalProductsApp.initializeProductAttributesView( %s, %d );', |
| 89 | wp_json_encode( self::ATTRIBUTES_ROOT_ID ), |
| 90 | $product_id |
| 91 | ); |
| 92 | |
| 93 | wp_add_inline_script( |
| 94 | self::SCRIPT_HANDLE, |
| 95 | $script, |
| 96 | 'after' |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 |