From a32ce730222017629b60ec5cfaca0fc1f085e8dd Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 14:39:51 +0100 Subject: [PATCH 1/7] Add method to Input to disable field --- src/Admin/Input/Input.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index e4eb2c2e15..efa3f2c0b9 100644 --- a/src/Admin/Input/Input.php +++ b/src/Admin/Input/Input.php @@ -51,6 +51,11 @@ class Input extends Form implements InputInterface { */ protected $value; + /** + * @var bool + */ + protected $is_disabled = false; + /** * Input constructor. * @@ -142,6 +147,17 @@ public function set_value( $value ): InputInterface { return $this; } + /** + * @param bool $disabled + * + * @return InputInterface + */ + public function set_disabled( $value = false ) { + $this->is_disabled = $value; + + return $this; + } + /** * Return the data used for the input's view. * @@ -157,6 +173,12 @@ public function get_view_data(): array { 'desc_tip' => true, ]; + if ( $this->is_disabled ) { + $view_data['custom_attributes'] = array( + 'disabled' => 'disabled', + ); + } + return array_merge( parent::get_view_data(), $view_data ); } From 5f4d2f8e4439fbd2ca580fc4b6a76470d49eef40 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 17:02:31 +0100 Subject: [PATCH 2/7] Add Utilities trait to Input class and add readonly setting --- src/Admin/Input/Input.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index efa3f2c0b9..c981037e45 100644 --- a/src/Admin/Input/Input.php +++ b/src/Admin/Input/Input.php @@ -4,6 +4,8 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input; use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper; +use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; +use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\Utilities; defined( 'ABSPATH' ) || exit; @@ -15,6 +17,7 @@ class Input extends Form implements InputInterface { use PluginHelper; + use Utilities; /** * @var string @@ -54,7 +57,7 @@ class Input extends Form implements InputInterface { /** * @var bool */ - protected $is_disabled = false; + protected $is_readonly = false; /** * Input constructor. @@ -65,6 +68,9 @@ class Input extends Form implements InputInterface { public function __construct( string $type, string $block_name ) { $this->type = $type; $this->block_name = $block_name; + + $this->set_options_object( woogle_get_container()->get( OptionsInterface::class ) ); + parent::__construct(); } @@ -148,12 +154,12 @@ public function set_value( $value ): InputInterface { } /** - * @param bool $disabled + * @param bool $value * * @return InputInterface */ - public function set_disabled( $value = false ) { - $this->is_disabled = $value; + public function set_readonly( $value = false ) { + $this->is_readonly = $value; return $this; } @@ -173,9 +179,9 @@ public function get_view_data(): array { 'desc_tip' => true, ]; - if ( $this->is_disabled ) { + if ( $this->is_readonly ) { $view_data['custom_attributes'] = array( - 'disabled' => 'disabled', + 'readonly' => 'readonly', ); } From 7384a0a5cedbcf58d3e7021e675b7a09afc89940 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 17:24:13 +0100 Subject: [PATCH 3/7] Add utility to test if extension was installed after a specific date --- src/HelperTraits/Utilities.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/HelperTraits/Utilities.php b/src/HelperTraits/Utilities.php index 1840a20cb9..cbc399e4a7 100644 --- a/src/HelperTraits/Utilities.php +++ b/src/HelperTraits/Utilities.php @@ -68,6 +68,22 @@ protected function gla_setup_for( $seconds ): bool { return ( ( time() - $gla_completed_setup ) >= $seconds ); } + /** + * Test if the extension was installed after a specific date + * + * @param string $date Date to compare against the installation date + * @return bool + */ + protected function gla_installed_after( $date ): bool { + $gla_installed = $this->options->get( OptionsInterface::INSTALL_TIMESTAMP, false ); + + if ( false === $gla_installed ) { + return false; + } + + return ( $gla_installed >= strtotime( $date ) ); + } + /** * Is Jetpack connected? * From 2893750c1a0411d3edff5a94293cc31c7fad9a8c Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 17:36:22 +0100 Subject: [PATCH 4/7] Add functionality to disable form attribute --- src/Admin/Input/Input.php | 22 +++++++++++++++++++ .../Product/Attributes/AttributesForm.php | 9 +++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index c981037e45..5b30002427 100644 --- a/src/Admin/Input/Input.php +++ b/src/Admin/Input/Input.php @@ -59,6 +59,11 @@ class Input extends Form implements InputInterface { */ protected $is_readonly = false; + /** + * @var bool + */ + protected $is_disabled = false; + /** * Input constructor. * @@ -163,6 +168,23 @@ public function set_readonly( $value = false ) { return $this; } + /** + * @param bool $value + * + * @return InputInterface + */ + public function set_disabled( $value = false ) { + $this->is_disabled = $value; + + return $this; + } + + /** + * @return bool + */ + public function is_disabled(): bool { + return $this->is_disabled; + } /** * Return the data used for the input's view. diff --git a/src/Admin/Product/Attributes/AttributesForm.php b/src/Admin/Product/Attributes/AttributesForm.php index e816b7071d..9fda4020a4 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_disabled() ) { + $this->add( $attribute_input ); + + $attribute_id = call_user_func( [ $attribute_type, 'get_id' ] ); + $this->attribute_types[ $attribute_id ] = $attribute_type; + } return $this; } From 37edb294d89fb5504081cfc7685897f816a11ed7 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 17:45:17 +0100 Subject: [PATCH 5/7] Disable or make GTIN field readonly depending on when GLA was installed and what version of WC is installed --- .../Product/Attributes/Input/GTINInput.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Admin/Product/Attributes/Input/GTINInput.php b/src/Admin/Product/Attributes/Input/GTINInput.php index 9c43f0fc41..e40c83f191 100644 --- a/src/Admin/Product/Attributes/Input/GTINInput.php +++ b/src/Admin/Product/Attributes/Input/GTINInput.php @@ -16,6 +16,11 @@ */ class GTINInput extends Text { + /** + * @var string + */ + private $disabled_from = '2024-09-20'; + /** * GTINInput constructor. */ @@ -24,5 +29,29 @@ 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->conditionally_restrict(); + } + + /** + * If Google for WooCommerce was installed after $this->disabled_from then + * this field will be disabled and not added to the product form. + * + * If Google for WooCommerce was installed before $this->disabled_from and + * WooCommerce version 9.2 or higher is installed then it will be readonly + * + * @since x.x.x + * @return void + */ + public function conditionally_restrict(): void { + if ( $this->gla_installed_after( $this->disabled_from ) ) { + $this->set_disabled( true ); + return; + } + + if ( version_compare( WC_VERSION, '9.2', '>=' ) ) { + $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' ) ); + } } } From 3c5007cf86e5dfcab010aec00a7a87ca44c03973 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 17:55:01 +0100 Subject: [PATCH 6/7] Update conditional restrictions --- .../Product/Attributes/Input/GTINInput.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Admin/Product/Attributes/Input/GTINInput.php b/src/Admin/Product/Attributes/Input/GTINInput.php index e40c83f191..d70261980b 100644 --- a/src/Admin/Product/Attributes/Input/GTINInput.php +++ b/src/Admin/Product/Attributes/Input/GTINInput.php @@ -34,22 +34,20 @@ public function __construct() { } /** - * If Google for WooCommerce was installed after $this->disabled_from then - * this field will be disabled and not added to the product form. - * - * If Google for WooCommerce was installed before $this->disabled_from and - * WooCommerce version 9.2 or higher is installed then it will be readonly + * If WooCommerce >= 9.2 is installed then the field will be: + * - Disabled if GLA was installed after this feature was added + * - Readonly if GLA was installed before this feature was added * * @since x.x.x * @return void */ public function conditionally_restrict(): void { - if ( $this->gla_installed_after( $this->disabled_from ) ) { - $this->set_disabled( true ); - return; - } - if ( version_compare( WC_VERSION, '9.2', '>=' ) ) { + if ( $this->gla_installed_after( $this->disabled_from ) ) { + $this->set_disabled( true ); + return; + } + $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' ) ); } From 44ae293b2828833a4c571ff332e8da79b6752282 Mon Sep 17 00:00:00 2001 From: Martyn Jones Date: Fri, 20 Sep 2024 18:00:57 +0100 Subject: [PATCH 7/7] phpcs fix --- src/Admin/Input/Input.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index 5b30002427..53a802cd6b 100644 --- a/src/Admin/Input/Input.php +++ b/src/Admin/Input/Input.php @@ -202,9 +202,9 @@ public function get_view_data(): array { ]; if ( $this->is_readonly ) { - $view_data['custom_attributes'] = array( + $view_data['custom_attributes'] = [ 'readonly' => 'readonly', - ); + ]; } return array_merge( parent::get_view_data(), $view_data );