Skip to content

Commit

Permalink
Improve compat with cloudflare rocket loader
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbills authored Jul 20, 2023
1 parent a5f3b14 commit b1977d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions assets/css/form.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#wc-shipping-sim {
display: none;
padding: 1.5em 0;
border-top: 0.1em solid #e0e0e0;
font-size: 16px;
Expand Down
10 changes: 8 additions & 2 deletions assets/js/form.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
window.addEventListener('DOMContentLoaded', () => {
const $ = (s, root = document) => root.querySelector(s);
const on = (el, evt, cb) => el.addEventListener(evt, cb);
const form = $('#wc-shipping-sim-form');

const root = $('#wc-shipping-sim');

if (!form) return console.error('Shipping Simulator not found');
if (!root) return console.error('Shipping Simulator not found');

const form = $('#wc-shipping-sim-form');
const params = JSON.parse(form.dataset.params);

const input = $('.input-postcode', form);
const button = $('.button.submit', form);
const results = $('#wc-shipping-sim-results');

const I = (val) => val;
const hooks = {
filterFormData: I,
Expand Down Expand Up @@ -142,6 +146,8 @@ window.addEventListener('DOMContentLoaded', () => {
window.wc_shipping_simulator = config;
document.dispatchEvent(new Event('wc_shipping_simulator:init'));

root.style.display = config.root_display;

config.hooks.inputMaskHandler && config.hooks.inputMaskHandler();

on(form, 'submit', config.hooks.submitHandler);
Expand Down
19 changes: 17 additions & 2 deletions classes/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ final class Shortcode {
protected $product;

public function __start () {
add_action( 'wc_shipping_simulator_form_after', [ $this, 'display_results_wrapper' ] );
add_shortcode( self::get_tag(), [ $this, 'render_shortcode' ] );

add_action( 'wc_shipping_simulator_form_after', [ $this, 'display_results_wrapper' ] );
add_filter( 'script_loader_tag', [ $this, 'prepare_script_tag' ], 10, 3 );
}

public function display_results_wrapper () {
Expand Down Expand Up @@ -75,6 +77,18 @@ public function render_shortcode ( $atts ) {
return '';
}

public function prepare_script_tag ( $tag, $handle, $src ) {
if ( h::prefix( 'form' ) !== $handle) return $tag;

$atts = [];

if ( apply_filters( 'wc_shipping_simulator_script_disable_cfrocket', true ) ) {
$atts[] = 'data-cfasync="false"';
}

return str_replace( ' src=', ' ' . implode( ' ', $atts ) . ' src=', $tag );
}

protected function get_script_params () {
return [
'ajax_url' => \admin_url( 'admin-ajax.php' ),
Expand All @@ -91,7 +105,8 @@ protected function get_script_params () {
'postcode_mask' => apply_filters(
'wc_shipping_simulator_form_input_mask',
'' // no input mask by default
)
),
'root_display' => 'block'
];
}

Expand Down

0 comments on commit b1977d5

Please sign in to comment.