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

Open
wants to merge 20 commits into
base: add/support-core-gtin-field
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Admin/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 );
}

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_hidden() ) {
$this->add( $attribute_input );

$attribute_id = call_user_func( [ $attribute_type, 'get_id' ] );
$this->attribute_types[ $attribute_id ] = $attribute_type;
}

return $this;
}
Expand Down
34 changes: 34 additions & 0 deletions src/Admin/Product/Attributes/Input/GTINInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand All @@ -24,5 +35,28 @@

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

Check warning on line 55 in src/Admin/Product/Attributes/Input/GTINInput.php

View check run for this annotation

Codecov / codecov/patch

src/Admin/Product/Attributes/Input/GTINInput.php#L55

Added line #L55 was not covered by tests
} 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' ) );
}
}
}
}
3 changes: 3 additions & 0 deletions src/Internal/InstallTimestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,11 +22,13 @@

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() );

Check warning on line 32 in src/Internal/InstallTimestamp.php

View check run for this annotation

Codecov / codecov/patch

src/Internal/InstallTimestamp.php#L32

Added line #L32 was not covered by tests
}
}
2 changes: 2 additions & 0 deletions src/Options/OptionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Admin/Input/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand All @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions tests/Unit/Product/Attributes/GtinMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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() );
}

/**
Expand Down