Skip to content

Commit

Permalink
Use the same source of the supported product types as the backend sid…
Browse files Browse the repository at this point in the history
…e on the front-end side.

Address #2087 (comment)
  • Loading branch information
eason9487 committed Sep 8, 2023
1 parent d013eaa commit d13cdf6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 68 deletions.
14 changes: 4 additions & 10 deletions js/src/product-attributes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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 );
}
);
53 changes: 31 additions & 22 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -82,7 +80,10 @@ function() {
wp_enqueue_media();
}

$this->assets_handler->enqueue_many( $this->get_assets() );
$assets = $this->get_assets();

Check warning on line 83 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L83

Added line #L83 was not covered by tests

$this->assets_handler->register_many( $assets );
$this->assets_handler->enqueue_many( $assets );

Check warning on line 86 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L85-L86

Added lines #L85 - L86 were not covered by tests
}
);

Expand Down Expand Up @@ -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 = [

Check warning on line 116 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L116

Added line #L116 was not covered by tests
'glaData',
[
'mcSetupComplete' => $this->merchant_center->is_setup_complete(),
Expand All @@ -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(),
],
];

Check warning on line 130 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L128-L130

Added lines #L128 - L130 were not covered by tests

]
);
$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 );

Check warning on line 143 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L132-L143

Added lines #L132 - L143 were not covered by tests

$assets[] = ( new AdminStyleAsset(
'google-listings-and-ads-css',
Expand All @@ -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" ),
]
),

Check warning on line 167 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L158-L167

Added lines #L158 - L167 were not covered by tests
$product_condition
) );
) )->add_inline_script( ...$gla_data_inline_script_args );

Check warning on line 169 in src/Admin/Admin.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Admin.php#L169

Added line #L169 was not covered by tests

$assets[] = ( new AdminStyleAsset(
'gla-product-attributes-css',
'js/build/product-attributes',
Expand Down
35 changes: 13 additions & 22 deletions src/Assets/AssetsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {

Check warning on line 27 in src/Assets/AssetsHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Assets/AssetsHandler.php#L27

Added line #L27 was not covered by tests
$this->validate_handle_not_exists( $asset->get_handle() );
$this->assets[ $asset->get_handle() ] = $asset;
$asset->register();

Check warning on line 30 in src/Assets/AssetsHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Assets/AssetsHandler.php#L30

Added line #L30 was not covered by tests
}

/**
* 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 {

Check warning on line 38 in src/Assets/AssetsHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Assets/AssetsHandler.php#L38

Added line #L38 was not covered by tests
foreach ( $assets as $asset ) {
$this->add( $asset );
}
}

/**
* Register a service.
*/
public function register(): void {
foreach ( $this->assets as $asset ) {
$asset->register();
$this->register( $asset );

Check warning on line 40 in src/Assets/AssetsHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Assets/AssetsHandler.php#L40

Added line #L40 was not covered by tests
}
}

Expand All @@ -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() );
Expand All @@ -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 ) {
Expand Down
20 changes: 10 additions & 10 deletions src/Assets/AssetsHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Google/GlobalSiteTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function () {
}
);

$this->assets_handler->add( $gtag_events );
$this->assets_handler->register( $gtag_events );

Check warning on line 192 in src/Google/GlobalSiteTag.php

View check run for this annotation

Codecov / codecov/patch

src/Google/GlobalSiteTag.php#L192

Added line #L192 was not covered by tests

add_action(
'wp_footer',
Expand Down
3 changes: 0 additions & 3 deletions src/Infrastructure/GoogleListingsAndAdsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d13cdf6

Please sign in to comment.