Merge pull request #26566 from woocommerce/update/26510-generate-order-key

Allow custom values in wc_generate_order_key()
This commit is contained in:
Claudio Sanches 2020-07-01 12:41:24 -03:00 committed by GitHub
commit 54f91a5a59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -149,13 +149,18 @@ function wc_get_order_status_name( $status ) {
}
/**
* Generate an order key.
* Generate an order key with prefix.
*
* @since 3.5.4
* @param string $key Order key without a prefix. By default generates a 13 digit secret.
* @return string The order key.
*/
function wc_generate_order_key() {
return 'wc_' . apply_filters( 'woocommerce_generate_order_key', 'order_' . wp_generate_password( 13, false ) );
function wc_generate_order_key( $key = '' ) {
if ( '' === $key ) {
$key = wp_generate_password( 13, false );
}
return 'wc_' . apply_filters( 'woocommerce_generate_order_key', 'order_' . $key );
}
/**

View File

@ -1520,4 +1520,23 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
remove_filter( 'woocommerce_hold_stock_for_checkout', '__return_false' );
}
/**
* Test wc_generate_order_key().
*
* @since 4.3.0
*/
public function test_wc_generate_order_key() {
// Test custom key.
$key = 'foo123bar';
$order_key = wc_generate_order_key( $key );
$expected = 'wc_' . apply_filters( 'woocommerce_generate_order_key', 'order_' . $key );
$this->assertEquals( $expected, $order_key );
// Test default key.
$order_key = wc_generate_order_key();
$prefix = 'wc_' . apply_filters( 'woocommerce_generate_order_key', 'order_' );
$this->assertStringStartsWith( $prefix, $order_key );
$this->assertEquals( 13, strlen( str_replace( $prefix, '', $order_key ) ) );
}
}