diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index e4eb2c2e15..a3ef57b6fb 100644 --- a/src/Admin/Input/Input.php +++ b/src/Admin/Input/Input.php @@ -51,6 +51,16 @@ class Input extends Form implements InputInterface { */ protected $value; + /** + * @var bool + */ + protected $is_readonly = false; + + /** + * @var bool + */ + protected $is_hidden = false; + /** * Input constructor. * @@ -142,6 +152,34 @@ public function set_value( $value ): InputInterface { return $this; } + /** + * @param bool $value + * + * @return InputInterface + */ + public function set_readonly( $value ): InputInterface { + $this->is_readonly = $value; + + return $this; + } + /** + * @param bool $value + * + * @return InputInterface + */ + public function set_hidden( $value ): InputInterface { + $this->is_hidden = $value; + + return $this; + } + + /** + * @return bool + */ + public function is_hidden(): bool { + return $this->is_hidden; + } + /** * Return the data used for the input's view. * @@ -157,6 +195,12 @@ public function get_view_data(): array { 'desc_tip' => true, ]; + if ( $this->is_readonly ) { + $view_data['custom_attributes'] = [ + 'readonly' => 'readonly', + ]; + } + return array_merge( parent::get_view_data(), $view_data ); } diff --git a/src/Admin/Product/Attributes/AttributesForm.php b/src/Admin/Product/Attributes/AttributesForm.php index e816b7071d..9688e2d409 100644 --- a/src/Admin/Product/Attributes/AttributesForm.php +++ b/src/Admin/Product/Attributes/AttributesForm.php @@ -166,10 +166,13 @@ public function add_attribute( string $attribute_type, ?string $input_type = nul $this->validate_interface( $input_type, InputInterface::class ); $attribute_input = self::init_input( new $input_type(), new $attribute_type() ); - $this->add( $attribute_input ); - $attribute_id = call_user_func( [ $attribute_type, 'get_id' ] ); - $this->attribute_types[ $attribute_id ] = $attribute_type; + if ( ! $attribute_input->is_hidden() ) { + $this->add( $attribute_input ); + + $attribute_id = call_user_func( [ $attribute_type, 'get_id' ] ); + $this->attribute_types[ $attribute_id ] = $attribute_type; + } return $this; } diff --git a/src/Admin/Product/Attributes/Input/GTINInput.php b/src/Admin/Product/Attributes/Input/GTINInput.php index 9c43f0fc41..c79393b640 100644 --- a/src/Admin/Product/Attributes/Input/GTINInput.php +++ b/src/Admin/Product/Attributes/Input/GTINInput.php @@ -4,6 +4,8 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Product\Attributes\Input; use Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input\Text; +use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait; +use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; defined( 'ABSPATH' ) || exit; @@ -16,6 +18,15 @@ */ class GTINInput extends Text { + use OptionsAwareTrait; + + /** + * Version since we start hiding the GTIN field. + * + * @var string + */ + private $hidden_since_version = '2.8.5'; + /** * GTINInput constructor. */ @@ -24,5 +35,28 @@ public function __construct() { $this->set_label( __( 'Global Trade Item Number (GTIN)', 'google-listings-and-ads' ) ); $this->set_description( __( 'Global Trade Item Number (GTIN) for your item. These identifiers include UPC (in North America), EAN (in Europe), JAN (in Japan), and ISBN (for books)', 'google-listings-and-ads' ) ); + $this->set_options_object( woogle_get_container()->get( OptionsInterface::class ) ); + $this->set_field_visibility(); + } + + /** + * Controls the inputs visibility based on the WooCommerce version and the + * initial version of Google for WooCommerce at the time of installation. + * + * @since x.x.x + * @return void + */ + public function set_field_visibility(): void { + $initial_version = $this->options->get( OptionsInterface::INSTALL_VERSION, false ); + // 9.2 is the version when GTIN field was added in Woo Core. So we need to hide or set the field as read-only since then. + if ( version_compare( WC_VERSION, '9.2', '>=' ) ) { + // For versions after 2.8.5 hide the GTIN field from G4W tab. Otherwise, set as readonly. + if ( $initial_version && version_compare( $initial_version, $this->hidden_since_version, '>=' ) ) { + $this->set_hidden( true ); + } else { + $this->set_readonly( true ); + $this->set_description( __( 'The Global Trade Item Number (GTIN) for your item can now be entered on the "Inventory" tab', 'google-listings-and-ads' ) ); + } + } } } diff --git a/src/Internal/InstallTimestamp.php b/src/Internal/InstallTimestamp.php index fddf16ab8f..81688c5f69 100644 --- a/src/Internal/InstallTimestamp.php +++ b/src/Internal/InstallTimestamp.php @@ -9,6 +9,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait; use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; +use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper; defined( 'ABSPATH' ) || exit; @@ -21,11 +22,13 @@ class InstallTimestamp implements Conditional, FirstInstallInterface, OptionsAwa use AdminConditional; use OptionsAwareTrait; + use PluginHelper; /** * Logic to run when the plugin is first installed. */ public function first_install(): void { $this->options->add( OptionsInterface::INSTALL_TIMESTAMP, time() ); + $this->options->add( OptionsInterface::INSTALL_VERSION, $this->get_version() ); } } diff --git a/src/Options/OptionsInterface.php b/src/Options/OptionsInterface.php index d52df9cc1e..fc2b4b3605 100644 --- a/src/Options/OptionsInterface.php +++ b/src/Options/OptionsInterface.php @@ -28,6 +28,7 @@ interface OptionsInterface { public const GOOGLE_CONNECTED = 'google_connected'; public const GOOGLE_WPCOM_AUTH_NONCE = 'google_wpcom_auth_nonce'; public const INSTALL_TIMESTAMP = 'install_timestamp'; + public const INSTALL_VERSION = 'install_version'; public const JETPACK_CONNECTED = 'jetpack_connected'; public const MC_SETUP_COMPLETED_AT = 'mc_setup_completed_at'; public const MERCHANT_ACCOUNT_STATE = 'merchant_account_state'; @@ -61,6 +62,7 @@ interface OptionsInterface { self::FILE_VERSION => true, self::GOOGLE_CONNECTED => true, self::INSTALL_TIMESTAMP => true, + self::INSTALL_VERSION => true, self::JETPACK_CONNECTED => true, self::MC_SETUP_COMPLETED_AT => true, self::MERCHANT_ACCOUNT_STATE => true, diff --git a/tests/Unit/Admin/Input/InputTest.php b/tests/Unit/Admin/Input/InputTest.php index 1277477410..0c76a5c02e 100644 --- a/tests/Unit/Admin/Input/InputTest.php +++ b/tests/Unit/Admin/Input/InputTest.php @@ -184,4 +184,18 @@ public function test_get_block_config() { $this->input->get_block_config() ); } + + public function test_hidden_prop() { + $this->assertFalse( $this->input->is_hidden() ); + $this->input->set_hidden( true ); + $this->assertTrue( $this->input->is_hidden() ); + $this->input->set_hidden( false ); + } + + public function test_readonly_prop() { + $this->assertFalse( isset( $this->input->get_view_data()['custom_attributes']['readonly'] ) ); + $this->input->set_readonly( true ); + $this->assertEquals( $this->input->get_view_data()['custom_attributes']['readonly'], 'readonly' ); + $this->input->set_readonly( false ); + } } diff --git a/tests/Unit/Admin/Product/Attributes/Input/AttributeInputCollectionTest.php b/tests/Unit/Admin/Product/Attributes/Input/AttributeInputCollectionTest.php index db09281024..2cf127adf7 100644 --- a/tests/Unit/Admin/Product/Attributes/Input/AttributeInputCollectionTest.php +++ b/tests/Unit/Admin/Product/Attributes/Input/AttributeInputCollectionTest.php @@ -310,23 +310,32 @@ public function test_gender_input() { } public function test_gtin_input() { + $description = 'Global Trade Item Number (GTIN) for your item. These identifiers include UPC (in North America), EAN (in Europe), JAN (in Japan), and ISBN (for books)'; + $data = [ + 'id' => 'gla_gtin', + 'type' => 'text', + 'label' => 'Global Trade Item Number (GTIN)', + 'description' => $description, + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_gtin', + 'is_root' => true, + 'children' => [], + ]; + + if ( version_compare( WC_VERSION, '9.2', '>=' ) ) { + $description = 'The Global Trade Item Number (GTIN) for your item can now be entered on the "Inventory" tab'; + $data['description'] = $description; + $data['custom_attributes'] = [ 'readonly' => 'readonly' ]; + } + $input = new GTINInput(); $input ->set_id( GTIN::get_id() ) ->set_name( GTIN::get_id() ); $this->assertEquals( - [ - 'id' => 'gla_gtin', - 'type' => 'text', - 'label' => 'Global Trade Item Number (GTIN)', - 'description' => 'Global Trade Item Number (GTIN) for your item. These identifiers include UPC (in North America), EAN (in Europe), JAN (in Japan), and ISBN (for books)', - 'desc_tip' => true, - 'value' => null, - 'name' => 'gla_gtin', - 'is_root' => true, - 'children' => [], - ], + $data, $input->get_view_data() ); @@ -337,7 +346,7 @@ public function test_gtin_input() { 'attributes' => [ 'property' => 'meta_data._wc_gla_gtin', 'label' => 'Global Trade Item Number (GTIN)', - 'tooltip' => 'Global Trade Item Number (GTIN) for your item. These identifiers include UPC (in North America), EAN (in Europe), JAN (in Japan), and ISBN (for books)', + 'tooltip' => $description, ], ], $input->get_block_config() diff --git a/tests/Unit/Product/Attributes/GtinMappingTest.php b/tests/Unit/Product/Attributes/GtinMappingTest.php index 91fcf8674f..21ed83828d 100644 --- a/tests/Unit/Product/Attributes/GtinMappingTest.php +++ b/tests/Unit/Product/Attributes/GtinMappingTest.php @@ -34,8 +34,13 @@ class GtinMappingTest extends TestCase { * @return void */ public function test_gtin_populated_from_wc_core_global_unique_id() { - $mock_product = WC_Helper_Product::create_simple_product( false ); - $mock_product->set_global_unique_id( $this->core_gtin ); + $expected_gtin = $this->gla_gtin; + $mock_product = WC_Helper_Product::create_simple_product( false ); + + if ( version_compare( WC_VERSION, '9.2', '>=' ) ) { + $mock_product->set_global_unique_id( $this->core_gtin ); + $expected_gtin = $this->core_gtin; + } $adapter = new WCProductAdapter(); $adapter->mapTypes( @@ -48,7 +53,7 @@ public function test_gtin_populated_from_wc_core_global_unique_id() { ] ); - $this->assertEquals( $this->core_gtin, $adapter->getGtin() ); + $this->assertEquals( $expected_gtin, $adapter->getGtin() ); } /**