Skip to content

Commit

Permalink
Updates to 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Woo committed May 28, 2024
1 parent ca6a3a1 commit b4f51f2
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 32 deletions.
2 changes: 1 addition & 1 deletion assets/css/admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion assets/js/frontend-checkout.min.js

This file was deleted.

1 change: 0 additions & 1 deletion assets/js/table-rate-rows.min.js

This file was deleted.

5 changes: 5 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
ignore: [],
presets: [ '@wordpress/babel-preset-default' ],
plugins: [],
};
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*** Table Rate Shipping Changelog ***

2024-05-27 - version 3.2.0
* Add - Abort messages compatibility with cart and checkout blocks.

2024-04-15 - version 3.1.9
* Fix - Notice is displayed when rate minimum and rate maximum is equal.

Expand Down
108 changes: 108 additions & 0 deletions client/abort-notices/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Block Notices
*
* This file is responsible for rendering Abort Messages from the Table Rate Shipping plugin.
*
* @package WooCommerce_Table_Rate_Shipping
*/

const { useSelect } = window.wp.data;
const { registerPlugin } = window.wp.plugins;
const { ExperimentalOrderShippingPackages, StoreNotice } = window.wc.blocksCheckout;
const { RawHTML } = window.wp.element;

/**
* Create a store notice component.
*
* @param notice
* @param index
* @param type
* @returns {JSX.Element}
*/
const createStoreNotice = ( notice, index, type = 'info' ) => {
if ( 'debug' === type ) {
type = 'info';
}

const message = <RawHTML>{notice}</RawHTML>;

return (
<StoreNotice key={index} status={type} isDismissible={false}>
{message}
</StoreNotice>
);
};

/**
* Utility function to get the abort message for the current package hashes.
*
* @param messages
* @param packageHashes
* @returns {string|null}
*/
const getAbortMessageForCurrentPackage = ( messages, packageHashes ) => {
if ( !messages || !packageHashes ) {
return null;
}

for ( const hash of packageHashes ) {
if ( messages[hash] ) {
return messages[hash];
}
}
return null;
};

/**
* Notices component.
*
* @param messages
* @param packageHashes
* @returns {JSX.Element}
* @constructor
*/
const Notices = ({ messages, packageHashes }) => {
const currentMessage = getAbortMessageForCurrentPackage(messages, packageHashes);

if ( !currentMessage ) {
return null;
}

return (
<div className="woocommerce-table-rate-shipping-block-notices">
{createStoreNotice( currentMessage, 0, 'info' )}
</div>
);
};

const render = () => {
const { abortMessages, hasShippingRates, packageHashes } = useSelect((select) => {
const storeCartData = select( 'wc/store/cart' ).getCartData();
const shippingRates = storeCartData?.shippingRates || [];
const hasShippingRates = shippingRates.some( rate => rate.shipping_rates.length > 0 );
const abortMessages = storeCartData?.extensions?.['woocommerce_table_rate_shipping']?.abort_messages;
const packageHashes = storeCartData?.extensions?.['woocommerce_table_rate_shipping']?.package_hashes;

return {
abortMessages,
hasShippingRates,
packageHashes,
};
}, []);

// Ensure we only show abort messages if no shipping rates are available.
if (hasShippingRates) {
return null;
}

return (
<ExperimentalOrderShippingPackages>
<Notices messages={abortMessages} packageHashes={packageHashes} />
</ExperimentalOrderShippingPackages>
);
};

registerPlugin('woocommerce-trs-abort-notices', {
render,
scope: 'woocommerce-checkout',
});
1 change: 1 addition & 0 deletions dist/woocommerce-trs-abort-notices.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react'), 'version' => 'bba503e536fb0fe8a723');
1 change: 1 addition & 0 deletions dist/woocommerce-trs-abort-notices.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions includes/class-blocks-integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Blocks Integration class.
*
* @package WooCommerce_Table_Rate_Shipping
*/

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;

class Blocks_Integration implements IntegrationInterface {

/**
* The name of the integration.
*
* @return string
*/
public function get_name(): string {
return 'woocommerce-table-rate-shipping';
}

/**
* When called invokes any initialization/setup for the integratidon.
*/
public function initialize() {
$this->register_scripts();
}

/**
* Returns an array of script handles to enqueue in the frontend context.
*
* @return string[]
*/
public function get_script_handles(): array {
return array( 'woocommerce-trs-abort-notices' );
}

/**
* Returns an array of script handles to enqueue in the editor context.
*
* @return string[]
*/
public function get_editor_script_handles(): array {
return array();
}

/**
* An array of key, value pairs of data made available to the block on the client side.
*
* @return array
*/
public function get_script_data(): array {
return array();
}

/**
* Registers the scripts and styles for the integration.
*/
public function register_scripts() {

foreach ( $this->get_script_handles() as $handle ) {
$this->register_script( $handle );
}
}

/**
* Register a script for the integration.
*
* @param string $handle Script handle.
*/
protected function register_script( string $handle ) {
$script_path = $handle . '.js';
$script_url = WC_TABLE_RATE_SHIPPING_DIST_URL . $script_path;

$script_asset_path = WC_TABLE_RATE_SHIPPING_DIST_DIR . $handle . '.asset.php';
$script_asset = file_exists( $script_asset_path )
? require $script_asset_path // nosemgrep: audit.php.lang.security.file.inclusion-arg --- This is a safe file inclusion.
: array(
'dependencies' => array(),
'version' => $this->get_file_version( WC_TABLE_RATE_SHIPPING_DIST_DIR . $script_path ),
);

wp_register_script(
$handle,
$script_url,
$script_asset['dependencies'],
$script_asset['version'],
true
);

wp_set_script_translations(
$handle,
'woocommerce-table-rate-shipping',
WC_TABLE_RATE_SHIPPING_MAIN_ABSPATH . '/languages'
);
}

/**
* Get the file modified time as a cache buster if we're in dev mode.
*
* @param string $file Local path to the file.
*
* @return string The cache buster value to use for the given file.
*/
protected function get_file_version( string $file ): string {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) {
return filemtime( $file );
}

return TABLE_RATE_SHIPPING_VERSION;
}
}
100 changes: 100 additions & 0 deletions includes/class-store-api-extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Store_API_Extension class.
*
* A class to extend the store public API with Table Rate Shipping Abort Message functionality.
*
* @package WooCommerce_Table_Rate_Shipping
*/

use Automattic\WooCommerce\StoreApi\StoreApi;
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema;

/**
* Store API Extension.
*/
class Store_API_Extension {
/**
* Stores Rest Extending instance.
*
* @var ExtendSchema
*/
private static $extend;

/**
* Plugin Identifier, unique to each plugin.
*
* @var string
*/
const IDENTIFIER = 'woocommerce_table_rate_shipping';

/**
* Bootstraps the class and hooks required data.
*
* @since 1.0.0
*/
public static function init() {
self::$extend = StoreApi::container()->get( ExtendSchema::class );
self::extend_store();
}

/**
* Registers the data into each endpoint.
*/
public static function extend_store() {

self::$extend->register_endpoint_data(
array(
'endpoint' => CartSchema::IDENTIFIER,
'namespace' => self::IDENTIFIER,
'data_callback' => array( static::class, 'data' ),
'schema_callback' => array( static::class, 'schema' ),
'schema_type' => ARRAY_A,
)
);
}

/**
* Store API extension data callback.
*
* @return array
*/
public static function data() {
$abort = WC()->session->get( WC_Table_Rate_Shipping::$abort_key );
$abort = is_array( $abort ) ? $abort : array();

$packages = WC()->cart->get_shipping_packages();
$package_hashes = array();
foreach ( $packages as $package ) {
$package_hashes[] = WC_Table_Rate_Shipping::create_package_hash( $package );
}

return array(
'abort_messages' => $abort,
'package_hashes' => $package_hashes,
);
}

/**
* Store API extension schema callback.
*
* @return array Registered schema.
*/
public static function schema() {
return array(
'abort_messages' => array(
'description' => __( 'Abort messages from Table Rate Shipping.', 'woocommerce-table-rate-shipping' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'package_hashes' => array(
'description' => __( 'Current package hashes.', 'woocommerce-table-rate-shipping' ),
'type' => 'array',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
);
}
}
25 changes: 14 additions & 11 deletions includes/class-wc-shipping-table-rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,21 +1376,24 @@ public function maybe_add_error_notices_on_settings_saved() {
return;
}

add_action( 'woocommerce_settings_shipping', function () {
add_action(
'woocommerce_settings_shipping',
function () {

$message = sprintf(
$message = sprintf(
/* translators: %s: message */
'<div class="notice notice-warning inline"><p><strong>%s</strong></p></div>',
esc_html__(
'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.',
'woocommerce-table-rate-shipping'
)
);
'<div class="notice notice-warning inline"><p><strong>%s</strong></p></div>',
esc_html__(
'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.',
'woocommerce-table-rate-shipping'
)
);

echo wp_kses_post( $message );
}, 0 );
echo wp_kses_post( $message );
},
0
);

// phpcs:enable WordPress.Security.NonceVerification.Recommended
}

}
Loading

0 comments on commit b4f51f2

Please sign in to comment.