| @@ -1,7 +1,55 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | + * Emit an inline toast call deferred until EasyInvoiceToast is loaded. | |
| 5 | + * | |
| 6 | + * The toast JS is enqueued in the footer, so toast helper calls that run | |
| 7 | + * mid-body (e.g. during a settings page render) must wait for the global to | |
| 8 | + * appear. We poll for up to ~3s, then fall back to a hidden DOM queue that | |
| 9 | + * the toast script flushes when it boots. | |
| 10 | + */ | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; | |
| 13 | +} | |
| 14 | +function _easy_invoice_emit_toast_script($type, $message, $options) { | |
| 15 | + $type = in_array($type, array('success', 'error', 'warning', 'info'), true) ? $type : 'info'; | |
| 16 | + $message = (string) $message; | |
| 17 | + $payload = wp_json_encode( | |
| 18 | + array( | |
| 19 | + 'type' => $type, | |
| 20 | + 'message' => $message, | |
| 21 | + 'options' => is_array($options) ? $options : array(), | |
| 22 | + ), | |
| 23 | + JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | |
| 24 | + ); | |
| 25 | + if (!$payload) { | |
| 26 | + return; | |
| 27 | + } | |
| 28 | + | |
| 29 | + // Single-shot emitter: wait until BOTH (1) the EasyInvoiceToast API is | |
| 30 | + // exposed by easy-invoice-toast.js AND (2) its container element has been | |
| 31 | + // appended to <body> by its jQuery-ready init() — calling show() before | |
| 32 | + // the container exists crashes on `toastContainer.append(...)`. | |
| 33 | + // The `fired` latch guarantees we fire exactly once across the interval, | |
| 34 | + // DOMContentLoaded, and load triggers. | |
| 35 | + // The payload is JSON built with JSON_HEX_* above, which is what makes it | |
| 36 | + // safe inside <script>; esc_html() would turn its quotes into " and | |
| 37 | + // the script would not parse. | |
| 38 | + echo "<script>(function(){var p=" . $payload . ";var fired=false;" // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 39 | + . "function ready(){return !!(window.EasyInvoiceToast" | |
| 40 | + . "&&typeof window.EasyInvoiceToast[p.type]==='function'" | |
| 41 | + . "&&document.getElementById('easy-invoice-toast-container'));}" | |
| 42 | + . "function fire(){if(fired)return true;if(!ready())return false;" | |
| 43 | + . "fired=true;window.EasyInvoiceToast[p.type](p.message,p.options||{});return true;}" | |
| 44 | + . "if(fire())return;" | |
| 45 | + . "var n=0;var iv=setInterval(function(){n++;if(fire()||n>50){clearInterval(iv);}},100);" | |
| 46 | + . "document.addEventListener('DOMContentLoaded',fire);" | |
| 47 | + . "window.addEventListener('load',fire);" | |
| 48 | + . "})();</script>"; | |
| 49 | +} | |
| 50 | + | |
| 51 | +/** | |
| 4 | 52 | * Display a success toast notification |
| 5 | 53 | * |
| 6 | 54 | * @since 1.0.0 |
| 7 | 55 | * @param string $message The message to display |
| @@ -16,9 +64,9 @@ | ||
| 16 | 64 | 'options' => $options |
| 17 | 65 | ) |
| 18 | 66 | )); |
| 19 | 67 | } else { |
| 20 | - echo "<script>if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.success('" . esc_js($message) . "', " . json_encode($options) . "); }</script>"; | |
| 68 | + _easy_invoice_emit_toast_script('success', $message, $options); | |
| 21 | 69 | } |
| 22 | 70 | } |
| 23 | 71 | |
| 24 | 72 | /** |
| @@ -37,9 +85,9 @@ | ||
| 37 | 85 | 'options' => $options |
| 38 | 86 | ) |
| 39 | 87 | )); |
| 40 | 88 | } else { |
| 41 | - echo "<script>if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.error('" . esc_js($message) . "', " . json_encode($options) . "); }</script>"; | |
| 89 | + _easy_invoice_emit_toast_script('error', $message, $options); | |
| 42 | 90 | } |
| 43 | 91 | } |
| 44 | 92 | |
| 45 | 93 | /** |
| @@ -58,9 +106,9 @@ | ||
| 58 | 106 | 'options' => $options |
| 59 | 107 | ) |
| 60 | 108 | )); |
| 61 | 109 | } else { |
| 62 | - echo "<script>if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.warning('" . esc_js($message) . "', " . json_encode($options) . "); }</script>"; | |
| 110 | + _easy_invoice_emit_toast_script('warning', $message, $options); | |
| 63 | 111 | } |
| 64 | 112 | } |
| 65 | 113 | |
| 66 | 114 | /** |
| @@ -79,7 +127,7 @@ | ||
| 79 | 127 | 'options' => $options |
| 80 | 128 | ) |
| 81 | 129 | )); |
| 82 | 130 | } else { |
| 83 | - echo "<script>if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.info('" . esc_js($message) . "', " . json_encode($options) . "); }</script>"; | |
| 131 | + _easy_invoice_emit_toast_script('info', $message, $options); | |
| 84 | 132 | } |
| 85 | -} | |
| 133 | +} | |