Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide GTIN field for new installs make readonly for existing installs #2622

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
50 changes: 50 additions & 0 deletions src/Admin/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,6 +17,7 @@
class Input extends Form implements InputInterface {

use PluginHelper;
use Utilities;

/**
* @var string
Expand Down Expand Up @@ -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.
*
Expand All @@ -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();
}

Expand Down Expand Up @@ -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.
*
Expand All @@ -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 );
}

Expand Down
9 changes: 6 additions & 3 deletions src/Admin/Product/Attributes/AttributesForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Admin/Product/Attributes/Input/GTINInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
class GTINInput extends Text {

/**
* @var string
*/
private $disabled_from = '2024-09-20';

/**
* GTINInput constructor.
*/
Expand All @@ -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' ) );
}
}
}
16 changes: 16 additions & 0 deletions src/HelperTraits/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
Expand Down
Loading