flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
MethodLogoCheckoutBlocksAssets.php
BlockEditing
1 day ago
Convert
1 day ago
Duplicate
1 day ago
Management
1 day ago
Timestamps
1 day ago
Tracker
1 day ago
views
1 day ago
CommonMethodSettings.php
1 day ago
FreeShippingCalculator.php
1 day ago
FreeShippingThresholdRuleValidator.php
1 day ago
MethodDescription.php
1 day ago
MethodLogo.php
1 day ago
MethodLogoCheckoutBlocksAssets.php
1 day ago
MethodLogoSettingsField.php
1 day ago
MethodSettings.php
1 day ago
MethodTitle.php
1 day ago
RateCalculator.php
1 day ago
RateCalculatorFactory.php
1 day ago
SettingsDisplayPreparer.php
1 day ago
SettingsProcessor.php
1 day ago
SingleMethodSettings.php
1 day ago
MethodLogoCheckoutBlocksAssets.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shipping method logo assets for checkout blocks. |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod |
| 6 | */ |
| 7 | |
| 8 | namespace WPDesk\FS\TableRate\ShippingMethod; |
| 9 | |
| 10 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 11 | use WPDesk\FS\TableRate\ShippingMethodSingle; |
| 12 | use WPDesk_Flexible_Shipping; |
| 13 | |
| 14 | /** |
| 15 | * Enqueue logo assets for WooCommerce checkout blocks. |
| 16 | */ |
| 17 | class MethodLogoCheckoutBlocksAssets implements Hookable { |
| 18 | const SCRIPT_HANDLE = 'flexible-shipping-shipping-method-logo'; |
| 19 | |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | private $plugin_url; |
| 24 | |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | private $scripts_version; |
| 29 | |
| 30 | /** |
| 31 | * @param string $plugin_url . |
| 32 | * @param string $scripts_version . |
| 33 | */ |
| 34 | public function __construct( $plugin_url, $scripts_version ) { |
| 35 | $this->plugin_url = rtrim( (string) $plugin_url, '/' ) . '/'; |
| 36 | $this->scripts_version = $scripts_version; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Hooks. |
| 41 | */ |
| 42 | public function hooks() { |
| 43 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return void |
| 48 | */ |
| 49 | public function enqueue_scripts(): void { |
| 50 | if ( ! $this->should_enqueue_assets() ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 55 | |
| 56 | wp_enqueue_script( |
| 57 | self::SCRIPT_HANDLE, |
| 58 | $this->plugin_url . 'assets/js/shipping-method-block-checkout' . $suffix . '.js', |
| 59 | [], |
| 60 | $this->scripts_version, |
| 61 | true |
| 62 | ); |
| 63 | |
| 64 | wp_localize_script( |
| 65 | self::SCRIPT_HANDLE, |
| 66 | '__fsShippingMethodLogoBlocks', |
| 67 | [ |
| 68 | 'method_ids' => [ |
| 69 | WPDesk_Flexible_Shipping::METHOD_ID, |
| 70 | ShippingMethodSingle::SHIPPING_METHOD_ID, |
| 71 | ], |
| 72 | 'logo_url_key' => RateCalculator::METHOD_LOGO_URL, |
| 73 | 'logo_alt_key' => RateCalculator::METHOD_LOGO_ALT, |
| 74 | 'description_key' => RateCalculator::DESCRIPTION, |
| 75 | 'description_encoded_key' => RateCalculator::DESCRIPTION_BASE64ENCODED, |
| 76 | 'description_class' => 'shipping-method-description flexible-shipping-method-description-block wc-block-components-radio-control__secondary-description', |
| 77 | 'wrapper_class' => 'flexible-shipping-method-logo-block', |
| 78 | ] |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return bool |
| 84 | */ |
| 85 | private function should_enqueue_assets(): bool { |
| 86 | if ( ! is_checkout() ) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if ( function_exists( 'has_block' ) ) { |
| 91 | return has_block( 'woocommerce/checkout' ); |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 |