Add pattern validation for global_unique_id (#50501)

* Add validation for global_unique_id in classic editor

* Fix an issue with validations in the text block

* Replace everything that is not number or hyphen in the product API

* Add pattern validation in the product and variation templates

* Add changelogs

* Increment regex to also accept empty string

* Fix e2e test

* Update update-stricter-global-unique-id
This commit is contained in:
Nathan Silveira 2024-08-12 08:27:06 -03:00 committed by GitHub
parent 9bc69246ff
commit f7595c6725
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 59 additions and 9 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix an issue in the text block where the validation was not being run due to missing dependencies in the parameters

View File

@ -137,7 +137,7 @@ export function Edit( {
};
}
},
[ type, required, pattern, minLength, maxLength, min, max ]
[ type, required, pattern, minLength, maxLength, min, max, value ]
);
function getSuffix() {

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Add pattern validation for global_unique_id

View File

@ -292,16 +292,49 @@
'i18n_sale_less_than_regular_error',
] );
} else {
$(
document.body
).triggerHandler( 'wc_remove_error_tip', [
$( this ),
'i18n_sale_less_than_regular_error',
] );
$( document.body ).triggerHandler(
'wc_remove_error_tip',
[ $( this ), 'i18n_sale_less_than_regular_error' ]
);
}
}
)
.on(
'keyup',
'input[type=text][name*=_global_unique_id]',
function () {
var global_unique_id = $( this ).val();
if ( /[^0-9\-]/.test( global_unique_id ) ) {
$( document.body ).triggerHandler( 'wc_add_error_tip', [
$( this ),
'i18n_global_unique_id_error',
] );
} else {
$( document.body ).triggerHandler(
'wc_remove_error_tip',
[ $( this ), 'i18n_global_unique_id_error' ]
);
}
}
)
.on(
'change',
'input[type=text][name*=_global_unique_id]',
function () {
var global_unique_id = $( this ).val();
$( this ).val(
global_unique_id.replace( /[^0-9\-]/g, '' )
);
$( document.body ).triggerHandler(
'wc_remove_error_tip',
[ $( this ), 'i18n_global_unique_id_error' ]
);
}
)
.on( 'init_tooltips', function () {
$( '.tips, .help_tip, .woocommerce-help-tip' ).tipTip( {
attribute: 'data-tip',

View File

@ -854,7 +854,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @param string $global_unique_id Unique ID.
*/
public function set_global_unique_id( $global_unique_id ) {
$global_unique_id = (string) $global_unique_id;
$global_unique_id = preg_replace( '/[^0-9\-]/', '', (string) $global_unique_id );
if ( $this->get_object_read() && ! empty( $global_unique_id ) && ! wc_product_has_global_unique_id( $this->get_id(), $global_unique_id ) ) {
$global_unique_id_found = wc_get_product_id_by_global_unique_id( $global_unique_id );

View File

@ -220,6 +220,7 @@ if ( ! class_exists( 'WC_Admin_Assets', false ) ) :
'i18n_delete_product_notice' => __( 'This product has produced sales and may be linked to existing orders. Are you sure you want to delete it?', 'woocommerce' ),
'i18n_remove_personal_data_notice' => __( 'This action cannot be reversed. Are you sure you wish to erase personal data from the selected orders?', 'woocommerce' ),
'i18n_confirm_delete' => __( 'Are you sure you wish to delete this item?', 'woocommerce' ),
'i18n_global_unique_id_error' => __( 'Please enter only numbers and hyphens (-).', 'woocommerce' ),
'decimal_point' => $decimal,
'mon_decimal_point' => wc_get_price_decimal_separator(),
'ajax_url' => admin_url( 'admin-ajax.php' ),

View File

@ -342,6 +342,10 @@ class ProductVariationTemplate extends AbstractProductFormTemplate implements Pr
// translators: %1$s GTIN %2$s UPC %3$s EAN %4$s ISBN.
'label' => sprintf( __( '%1$s, %2$s, %3$s, or %4$s', 'woocommerce' ), '<abbr title="' . esc_attr__( 'Global Trade Item Number', 'woocommerce' ) . '">' . esc_html__( 'GTIN', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'Universal Product Code', 'woocommerce' ) . '">' . esc_html__( 'UPC', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'European Article Number', 'woocommerce' ) . '">' . esc_html__( 'EAN', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'International Standard Book Number', 'woocommerce' ) . '">' . esc_html__( 'ISBN', 'woocommerce' ) . '</abbr>' ),
'tooltip' => __( 'Enter a barcode or any other identifier unique to this product. It can help you list this product on other channels or marketplaces.', 'woocommerce' ),
'pattern' => array(
'value' => '[0-9\-]*',
'message' => __( 'Please enter only numbers and hyphens (-).', 'woocommerce' ),
),
),
)
);

View File

@ -768,6 +768,10 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
// translators: %1$s GTIN %2$s UPC %3$s EAN %4$s ISBN.
'label' => sprintf( __( '%1$s, %2$s, %3$s, or %4$s', 'woocommerce' ), '<abbr title="' . esc_attr__( 'Global Trade Item Number', 'woocommerce' ) . '">' . esc_html__( 'GTIN', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'Universal Product Code', 'woocommerce' ) . '">' . esc_html__( 'UPC', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'European Article Number', 'woocommerce' ) . '">' . esc_html__( 'EAN', 'woocommerce' ) . '</abbr>', '<abbr title="' . esc_attr__( 'International Standard Book Number', 'woocommerce' ) . '">' . esc_html__( 'ISBN', 'woocommerce' ) . '</abbr>' ),
'tooltip' => __( 'Enter a barcode or any other identifier unique to this product. It can help you list this product on other channels or marketplaces.', 'woocommerce' ),
'pattern' => array(
'value' => '[0-9\-]*',
'message' => __( 'Please enter only numbers and hyphens (-).', 'woocommerce' ),
),
),
'disableConditions' => array(
array(

View File

@ -24,7 +24,7 @@ const productData = {
{ name: `custom-field_${ Date.now() }`, value: 'custom_1' },
],
sku: `sku_${ Date.now() }`,
gtin: `gtin_${ Date.now() }`,
gtin: `${ Date.now() }`,
shipping: {
shippingClassName: `shipping-class_${ Date.now() }`,
weight: '2',