Skip to content

Commit b4f51f2

Browse files
author
Woo
committed
Updates to 3.2.0
1 parent ca6a3a1 commit b4f51f2

15 files changed

+462
-32
lines changed

assets/css/admin.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/frontend-checkout.min.js

-1
This file was deleted.

assets/js/table-rate-rows.min.js

-1
This file was deleted.

babel.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
ignore: [],
3+
presets: [ '@wordpress/babel-preset-default' ],
4+
plugins: [],
5+
};

changelog.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** Table Rate Shipping Changelog ***
22

3+
2024-05-27 - version 3.2.0
4+
* Add - Abort messages compatibility with cart and checkout blocks.
5+
36
2024-04-15 - version 3.1.9
47
* Fix - Notice is displayed when rate minimum and rate maximum is equal.
58

client/abort-notices/index.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Block Notices
3+
*
4+
* This file is responsible for rendering Abort Messages from the Table Rate Shipping plugin.
5+
*
6+
* @package WooCommerce_Table_Rate_Shipping
7+
*/
8+
9+
const { useSelect } = window.wp.data;
10+
const { registerPlugin } = window.wp.plugins;
11+
const { ExperimentalOrderShippingPackages, StoreNotice } = window.wc.blocksCheckout;
12+
const { RawHTML } = window.wp.element;
13+
14+
/**
15+
* Create a store notice component.
16+
*
17+
* @param notice
18+
* @param index
19+
* @param type
20+
* @returns {JSX.Element}
21+
*/
22+
const createStoreNotice = ( notice, index, type = 'info' ) => {
23+
if ( 'debug' === type ) {
24+
type = 'info';
25+
}
26+
27+
const message = <RawHTML>{notice}</RawHTML>;
28+
29+
return (
30+
<StoreNotice key={index} status={type} isDismissible={false}>
31+
{message}
32+
</StoreNotice>
33+
);
34+
};
35+
36+
/**
37+
* Utility function to get the abort message for the current package hashes.
38+
*
39+
* @param messages
40+
* @param packageHashes
41+
* @returns {string|null}
42+
*/
43+
const getAbortMessageForCurrentPackage = ( messages, packageHashes ) => {
44+
if ( !messages || !packageHashes ) {
45+
return null;
46+
}
47+
48+
for ( const hash of packageHashes ) {
49+
if ( messages[hash] ) {
50+
return messages[hash];
51+
}
52+
}
53+
return null;
54+
};
55+
56+
/**
57+
* Notices component.
58+
*
59+
* @param messages
60+
* @param packageHashes
61+
* @returns {JSX.Element}
62+
* @constructor
63+
*/
64+
const Notices = ({ messages, packageHashes }) => {
65+
const currentMessage = getAbortMessageForCurrentPackage(messages, packageHashes);
66+
67+
if ( !currentMessage ) {
68+
return null;
69+
}
70+
71+
return (
72+
<div className="woocommerce-table-rate-shipping-block-notices">
73+
{createStoreNotice( currentMessage, 0, 'info' )}
74+
</div>
75+
);
76+
};
77+
78+
const render = () => {
79+
const { abortMessages, hasShippingRates, packageHashes } = useSelect((select) => {
80+
const storeCartData = select( 'wc/store/cart' ).getCartData();
81+
const shippingRates = storeCartData?.shippingRates || [];
82+
const hasShippingRates = shippingRates.some( rate => rate.shipping_rates.length > 0 );
83+
const abortMessages = storeCartData?.extensions?.['woocommerce_table_rate_shipping']?.abort_messages;
84+
const packageHashes = storeCartData?.extensions?.['woocommerce_table_rate_shipping']?.package_hashes;
85+
86+
return {
87+
abortMessages,
88+
hasShippingRates,
89+
packageHashes,
90+
};
91+
}, []);
92+
93+
// Ensure we only show abort messages if no shipping rates are available.
94+
if (hasShippingRates) {
95+
return null;
96+
}
97+
98+
return (
99+
<ExperimentalOrderShippingPackages>
100+
<Notices messages={abortMessages} packageHashes={packageHashes} />
101+
</ExperimentalOrderShippingPackages>
102+
);
103+
};
104+
105+
registerPlugin('woocommerce-trs-abort-notices', {
106+
render,
107+
scope: 'woocommerce-checkout',
108+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react'), 'version' => 'bba503e536fb0fe8a723');

dist/woocommerce-trs-abort-notices.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/class-blocks-integration.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Blocks Integration class.
4+
*
5+
* @package WooCommerce_Table_Rate_Shipping
6+
*/
7+
8+
defined( 'ABSPATH' ) || exit;
9+
10+
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
11+
12+
class Blocks_Integration implements IntegrationInterface {
13+
14+
/**
15+
* The name of the integration.
16+
*
17+
* @return string
18+
*/
19+
public function get_name(): string {
20+
return 'woocommerce-table-rate-shipping';
21+
}
22+
23+
/**
24+
* When called invokes any initialization/setup for the integratidon.
25+
*/
26+
public function initialize() {
27+
$this->register_scripts();
28+
}
29+
30+
/**
31+
* Returns an array of script handles to enqueue in the frontend context.
32+
*
33+
* @return string[]
34+
*/
35+
public function get_script_handles(): array {
36+
return array( 'woocommerce-trs-abort-notices' );
37+
}
38+
39+
/**
40+
* Returns an array of script handles to enqueue in the editor context.
41+
*
42+
* @return string[]
43+
*/
44+
public function get_editor_script_handles(): array {
45+
return array();
46+
}
47+
48+
/**
49+
* An array of key, value pairs of data made available to the block on the client side.
50+
*
51+
* @return array
52+
*/
53+
public function get_script_data(): array {
54+
return array();
55+
}
56+
57+
/**
58+
* Registers the scripts and styles for the integration.
59+
*/
60+
public function register_scripts() {
61+
62+
foreach ( $this->get_script_handles() as $handle ) {
63+
$this->register_script( $handle );
64+
}
65+
}
66+
67+
/**
68+
* Register a script for the integration.
69+
*
70+
* @param string $handle Script handle.
71+
*/
72+
protected function register_script( string $handle ) {
73+
$script_path = $handle . '.js';
74+
$script_url = WC_TABLE_RATE_SHIPPING_DIST_URL . $script_path;
75+
76+
$script_asset_path = WC_TABLE_RATE_SHIPPING_DIST_DIR . $handle . '.asset.php';
77+
$script_asset = file_exists( $script_asset_path )
78+
? require $script_asset_path // nosemgrep: audit.php.lang.security.file.inclusion-arg --- This is a safe file inclusion.
79+
: array(
80+
'dependencies' => array(),
81+
'version' => $this->get_file_version( WC_TABLE_RATE_SHIPPING_DIST_DIR . $script_path ),
82+
);
83+
84+
wp_register_script(
85+
$handle,
86+
$script_url,
87+
$script_asset['dependencies'],
88+
$script_asset['version'],
89+
true
90+
);
91+
92+
wp_set_script_translations(
93+
$handle,
94+
'woocommerce-table-rate-shipping',
95+
WC_TABLE_RATE_SHIPPING_MAIN_ABSPATH . '/languages'
96+
);
97+
}
98+
99+
/**
100+
* Get the file modified time as a cache buster if we're in dev mode.
101+
*
102+
* @param string $file Local path to the file.
103+
*
104+
* @return string The cache buster value to use for the given file.
105+
*/
106+
protected function get_file_version( string $file ): string {
107+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
108+
return filemtime( $file );
109+
}
110+
111+
return TABLE_RATE_SHIPPING_VERSION;
112+
}
113+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Store_API_Extension class.
4+
*
5+
* A class to extend the store public API with Table Rate Shipping Abort Message functionality.
6+
*
7+
* @package WooCommerce_Table_Rate_Shipping
8+
*/
9+
10+
use Automattic\WooCommerce\StoreApi\StoreApi;
11+
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
12+
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema;
13+
14+
/**
15+
* Store API Extension.
16+
*/
17+
class Store_API_Extension {
18+
/**
19+
* Stores Rest Extending instance.
20+
*
21+
* @var ExtendSchema
22+
*/
23+
private static $extend;
24+
25+
/**
26+
* Plugin Identifier, unique to each plugin.
27+
*
28+
* @var string
29+
*/
30+
const IDENTIFIER = 'woocommerce_table_rate_shipping';
31+
32+
/**
33+
* Bootstraps the class and hooks required data.
34+
*
35+
* @since 1.0.0
36+
*/
37+
public static function init() {
38+
self::$extend = StoreApi::container()->get( ExtendSchema::class );
39+
self::extend_store();
40+
}
41+
42+
/**
43+
* Registers the data into each endpoint.
44+
*/
45+
public static function extend_store() {
46+
47+
self::$extend->register_endpoint_data(
48+
array(
49+
'endpoint' => CartSchema::IDENTIFIER,
50+
'namespace' => self::IDENTIFIER,
51+
'data_callback' => array( static::class, 'data' ),
52+
'schema_callback' => array( static::class, 'schema' ),
53+
'schema_type' => ARRAY_A,
54+
)
55+
);
56+
}
57+
58+
/**
59+
* Store API extension data callback.
60+
*
61+
* @return array
62+
*/
63+
public static function data() {
64+
$abort = WC()->session->get( WC_Table_Rate_Shipping::$abort_key );
65+
$abort = is_array( $abort ) ? $abort : array();
66+
67+
$packages = WC()->cart->get_shipping_packages();
68+
$package_hashes = array();
69+
foreach ( $packages as $package ) {
70+
$package_hashes[] = WC_Table_Rate_Shipping::create_package_hash( $package );
71+
}
72+
73+
return array(
74+
'abort_messages' => $abort,
75+
'package_hashes' => $package_hashes,
76+
);
77+
}
78+
79+
/**
80+
* Store API extension schema callback.
81+
*
82+
* @return array Registered schema.
83+
*/
84+
public static function schema() {
85+
return array(
86+
'abort_messages' => array(
87+
'description' => __( 'Abort messages from Table Rate Shipping.', 'woocommerce-table-rate-shipping' ),
88+
'type' => 'array',
89+
'context' => array( 'view', 'edit' ),
90+
'readonly' => true,
91+
),
92+
'package_hashes' => array(
93+
'description' => __( 'Current package hashes.', 'woocommerce-table-rate-shipping' ),
94+
'type' => 'array',
95+
'context' => array( 'view', 'edit' ),
96+
'readonly' => true,
97+
),
98+
);
99+
}
100+
}

includes/class-wc-shipping-table-rate.php

+14-11
Original file line numberDiff line numberDiff line change
@@ -1376,21 +1376,24 @@ public function maybe_add_error_notices_on_settings_saved() {
13761376
return;
13771377
}
13781378

1379-
add_action( 'woocommerce_settings_shipping', function () {
1379+
add_action(
1380+
'woocommerce_settings_shipping',
1381+
function () {
13801382

1381-
$message = sprintf(
1383+
$message = sprintf(
13821384
/* translators: %s: message */
1383-
'<div class="notice notice-warning inline"><p><strong>%s</strong></p></div>',
1384-
esc_html__(
1385-
'You have overlapping shipping rates defined, which may offer multiple options for the same criteria at checkout. If this is not your intention please review and adjust your rates and verify it on the cart/checkout pages.',
1386-
'woocommerce-table-rate-shipping'
1387-
)
1388-
);
1385+
'<div class="notice notice-warning inline"><p><strong>%s</strong></p></div>',
1386+
esc_html__(
1387+
'You have overlapping shipping rates defined, which may offer multiple options for the same criteria at checkout. If this is not your intention please review and adjust your rates and verify it on the cart/checkout pages.',
1388+
'woocommerce-table-rate-shipping'
1389+
)
1390+
);
13891391

1390-
echo wp_kses_post( $message );
1391-
}, 0 );
1392+
echo wp_kses_post( $message );
1393+
},
1394+
0
1395+
);
13921396

13931397
// phpcs:enable WordPress.Security.NonceVerification.Recommended
13941398
}
1395-
13961399
}

0 commit comments

Comments
 (0)