diff --git a/src/Admin/Input/Input.php b/src/Admin/Input/Input.php index e4eb2c2e15..53a802cd6b 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 @@ -51,6 +54,16 @@ class Input extends Form implements InputInterface { */ protected $value; + /** + * @var bool + */ + protected $is_readonly = false; + + /** + * @var bool + */ + protected $is_disabled = false; + /** * Input constructor. * @@ -60,6 +73,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(); } @@ -142,6 +158,34 @@ public function set_value( $value ): InputInterface { return $this; } + /** + * @param bool $value + * + * @return InputInterface + */ + public function set_readonly( $value = false ) { + $this->is_readonly = $value; + + 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. * @@ -157,6 +201,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..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; } diff --git a/src/Admin/Product/Attributes/Input/GTINInput.php b/src/Admin/Product/Attributes/Input/GTINInput.php index 9c43f0fc41..d70261980b 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,27 @@ 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 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 ( 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' ) ); + } } } 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? *