diff --git a/js/src/product-attributes/index.js b/js/src/product-attributes/index.js index 881dc893b2..5ca78f4903 100644 --- a/js/src/product-attributes/index.js +++ b/js/src/product-attributes/index.js @@ -6,18 +6,10 @@ import $ from 'jquery'; /** * Internal dependencies */ +import { glaData } from '.~/constants'; import './custom-inputs'; import './index.scss'; -const applicableProductTypes = new Set( [ - // Simple product - 'simple', - // Variable product - 'variable', - // Product bundle from WooCommerce Product Bundles - 'bundle', -] ); - // Originally, this extension relied on a WooCommerce core processing to show or hide // the product data tab and meta box added to the product editing page. // @@ -39,7 +31,9 @@ $( document ).on( 'woocommerce-product-type-change', 'body', ( e, productType ) => { - const shouldDisplay = applicableProductTypes.has( productType ); + const shouldDisplay = + glaData.applicableProductTypes.includes( productType ); + $( '.gla_attributes_tab, .gla_meta_box' ).toggle( shouldDisplay ); } ); diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index 65b8d4abc9..a9d6de9887 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -5,7 +5,6 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Admin\MetaBox\MetaBoxInterface; use Automattic\WooCommerce\GoogleListingsAndAds\Ads\AdsService; -use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminScriptAsset; use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminScriptWithBuiltDependenciesAsset; use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminStyleAsset; use Automattic\WooCommerce\GoogleListingsAndAds\Assets\Asset; @@ -17,6 +16,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\ViewFactory; use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService; use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper; +use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductSyncer; use Automattic\WooCommerce\GoogleListingsAndAds\Value\BuiltScriptDependencyArray; use Automattic\WooCommerce\GoogleListingsAndAds\View\ViewException; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface; @@ -72,8 +72,6 @@ public function __construct( AssetsHandlerInterface $assets_handler, ViewFactory * Register a service. */ public function register(): void { - $this->assets_handler->add_many( $this->get_assets() ); - add_action( 'admin_enqueue_scripts', function() { @@ -82,7 +80,10 @@ function() { wp_enqueue_media(); } - $this->assets_handler->enqueue_many( $this->get_assets() ); + $assets = $this->get_assets(); + + $this->assets_handler->register_many( $assets ); + $this->assets_handler->enqueue_many( $assets ); } ); @@ -112,18 +113,7 @@ protected function get_assets(): array { return wc_admin_is_registered_page(); }; - $assets[] = ( new AdminScriptWithBuiltDependenciesAsset( - 'google-listings-and-ads', - 'js/build/index', - "{$this->get_root_dir()}/js/build/index.asset.php", - new BuiltScriptDependencyArray( - [ - 'dependencies' => [], - 'version' => (string) filemtime( "{$this->get_root_dir()}/js/build/index.js" ), - ] - ), - $wc_admin_condition - ) )->add_inline_script( + $gla_data_inline_script_args = [ 'glaData', [ 'mcSetupComplete' => $this->merchant_center->is_setup_complete(), @@ -135,9 +125,22 @@ protected function get_assets(): array { 'dateFormat' => get_option( 'date_format' ), 'timeFormat' => get_option( 'time_format' ), 'siteLogoUrl' => wp_get_attachment_image_url( get_theme_mod( 'custom_logo' ), 'full' ), + 'applicableProductTypes' => ProductSyncer::get_supported_product_types(), + ], + ]; - ] - ); + $assets[] = ( new AdminScriptWithBuiltDependenciesAsset( + 'google-listings-and-ads', + 'js/build/index', + "{$this->get_root_dir()}/js/build/index.asset.php", + new BuiltScriptDependencyArray( + [ + 'dependencies' => [], + 'version' => (string) filemtime( "{$this->get_root_dir()}/js/build/index.js" ), + ] + ), + $wc_admin_condition + ) )->add_inline_script( ...$gla_data_inline_script_args ); $assets[] = ( new AdminStyleAsset( 'google-listings-and-ads-css', @@ -152,13 +155,19 @@ protected function get_assets(): array { return ( null !== $screen && 'product' === $screen->id ); }; - $assets[] = ( new AdminScriptAsset( + $assets[] = ( new AdminScriptWithBuiltDependenciesAsset( 'gla-product-attributes', 'js/build/product-attributes', - [], - '', + "{$this->get_root_dir()}/js/build/product-attributes.asset.php", + new BuiltScriptDependencyArray( + [ + 'dependencies' => [], + 'version' => (string) filemtime( "{$this->get_root_dir()}/js/build/product-attributes.js" ), + ] + ), $product_condition - ) ); + ) )->add_inline_script( ...$gla_data_inline_script_args ); + $assets[] = ( new AdminStyleAsset( 'gla-product-attributes-css', 'js/build/product-attributes', diff --git a/src/Assets/AssetsHandler.php b/src/Assets/AssetsHandler.php index 46a0227d79..01e8b1d87b 100644 --- a/src/Assets/AssetsHandler.php +++ b/src/Assets/AssetsHandler.php @@ -4,14 +4,13 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Assets; use Automattic\WooCommerce\GoogleListingsAndAds\Exception\InvalidAsset; -use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable; /** * Class AssetsHandler * * @package Automattic\WooCommerce\GoogleListingsAndAds\Assets */ -final class AssetsHandler implements Registerable, AssetsHandlerInterface { +final class AssetsHandler implements AssetsHandlerInterface { /** * Assets known to this asset handler. @@ -21,32 +20,24 @@ final class AssetsHandler implements Registerable, AssetsHandlerInterface { private $assets = []; /** - * Add a single asset to the asset handler. + * Register a single asset. * - * @param Asset $asset Asset to add. + * @param Asset $asset Asset to register. */ - public function add( Asset $asset ): void { + public function register( Asset $asset ): void { $this->validate_handle_not_exists( $asset->get_handle() ); $this->assets[ $asset->get_handle() ] = $asset; + $asset->register(); } /** - * Add multiple assets to the asset handler. + * Register multiple assets. * - * @param Asset[] $assets Array of assets to add. + * @param Asset[] $assets Array of assets to register. */ - public function add_many( array $assets ): void { + public function register_many( array $assets ): void { foreach ( $assets as $asset ) { - $this->add( $asset ); - } - } - - /** - * Register a service. - */ - public function register(): void { - foreach ( $this->assets as $asset ) { - $asset->register(); + $this->register( $asset ); } } @@ -57,8 +48,8 @@ public function register(): void { * * @throws InvalidAsset If the passed-in asset is not valid. * - * @see AssetsHandlerInterface::add To add assets. - * @see AssetsHandlerInterface::add_many To add multiple assets. + * @see AssetsHandlerInterface::register To register assets. + * @see AssetsHandlerInterface::register_many To register multiple assets. */ public function enqueue( Asset $asset ): void { $this->enqueue_handle( $asset->get_handle() ); @@ -71,8 +62,8 @@ public function enqueue( Asset $asset ): void { * * @throws InvalidAsset If any of the passed-in assets are not valid. * - * @see AssetsHandlerInterface::add To add assets. - * @see AssetsHandlerInterface::add_many To add multiple assets. + * @see AssetsHandlerInterface::register To register assets. + * @see AssetsHandlerInterface::register_many To register multiple assets. */ public function enqueue_many( array $assets ): void { foreach ( $assets as $asset ) { diff --git a/src/Assets/AssetsHandlerInterface.php b/src/Assets/AssetsHandlerInterface.php index 57467fd795..52b516a625 100644 --- a/src/Assets/AssetsHandlerInterface.php +++ b/src/Assets/AssetsHandlerInterface.php @@ -13,18 +13,18 @@ interface AssetsHandlerInterface { /** - * Add a single asset to the asset handler. + * Register a single asset. * - * @param Asset $asset Asset to add. + * @param Asset $asset Asset to register. */ - public function add( Asset $asset ): void; + public function register( Asset $asset ): void; /** - * Add multiple assets to the asset handler. + * Register multiple assets. * - * @param Asset[] $assets Array of assets to add. + * @param Asset[] $assets Array of assets to register. */ - public function add_many( array $assets ): void; + public function register_many( array $assets ): void; /** * Enqueue a single asset. @@ -33,8 +33,8 @@ public function add_many( array $assets ): void; * * @throws InvalidAsset If the passed-in asset is not valid. * - * @see AssetsHandlerInterface::add To add assets. - * @see AssetsHandlerInterface::add_many To add multiple assets. + * @see AssetsHandlerInterface::register To register assets. + * @see AssetsHandlerInterface::register_many To register multiple assets. */ public function enqueue( Asset $asset ): void; @@ -45,8 +45,8 @@ public function enqueue( Asset $asset ): void; * * @throws InvalidAsset If any of the passed-in assets are not valid. * - * @see AssetsHandlerInterface::add To add assets. - * @see AssetsHandlerInterface::add_many To add multiple assets. + * @see AssetsHandlerInterface::register To register assets. + * @see AssetsHandlerInterface::register_many To register multiple assets. */ public function enqueue_many( array $assets ): void; diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index c158001063..3f36892e92 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -189,7 +189,7 @@ function () { } ); - $this->assets_handler->add( $gtag_events ); + $this->assets_handler->register( $gtag_events ); add_action( 'wp_footer', diff --git a/src/Infrastructure/GoogleListingsAndAdsPlugin.php b/src/Infrastructure/GoogleListingsAndAdsPlugin.php index 3503d8894d..315d230d67 100644 --- a/src/Infrastructure/GoogleListingsAndAdsPlugin.php +++ b/src/Infrastructure/GoogleListingsAndAdsPlugin.php @@ -3,7 +3,6 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure; -use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AssetsHandlerInterface; use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\JobInitializer; use Automattic\WooCommerce\GoogleListingsAndAds\Internal\Requirements\PluginValidator; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; @@ -102,8 +101,6 @@ function() { add_action( 'init', function() { - $this->container->get( AssetsHandlerInterface::class )->register(); - // register the job initializer only if it is available. see JobInitializer::is_needed. if ( $this->container->has( JobInitializer::class ) ) { $this->container->get( JobInitializer::class )->register();