-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Woo
committed
May 28, 2024
1 parent
ca6a3a1
commit b4f51f2
Showing
15 changed files
with
462 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
ignore: [], | ||
presets: [ '@wordpress/babel-preset-default' ], | ||
plugins: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<?php return array('dependencies' => array('react'), 'version' => 'bba503e536fb0fe8a723'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.