| 1 |
<?php |
| 2 |
|
| 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 |
function _easy_invoice_emit_toast_script($type, $message, $options) { |
| 12 |
$type = in_array($type, array('success', 'error', 'warning', 'info'), true) ? $type : 'info'; |
| 13 |
$message = (string) $message; |
| 14 |
$payload = wp_json_encode( |
| 15 |
array( |
| 16 |
'type' => $type, |
| 17 |
'message' => $message, |
| 18 |
'options' => is_array($options) ? $options : array(), |
| 19 |
), |
| 20 |
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT |
| 21 |
); |
| 22 |
if (!$payload) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Single-shot emitter: wait until BOTH (1) the EasyInvoiceToast API is |
| 27 |
// exposed by easy-invoice-toast.js AND (2) its container element has been |
| 28 |
// appended to <body> by its jQuery-ready init() — calling show() before |
| 29 |
// the container exists crashes on `toastContainer.append(...)`. |
| 30 |
// The `fired` latch guarantees we fire exactly once across the interval, |
| 31 |
// DOMContentLoaded, and load triggers. |
| 32 |
echo "<script>(function(){var p=" . $payload . ";var fired=false;" |
| 33 |
. "function ready(){return !!(window.EasyInvoiceToast" |
| 34 |
. "&&typeof window.EasyInvoiceToast[p.type]==='function'" |
| 35 |
. "&&document.getElementById('easy-invoice-toast-container'));}" |
| 36 |
. "function fire(){if(fired)return true;if(!ready())return false;" |
| 37 |
. "fired=true;window.EasyInvoiceToast[p.type](p.message,p.options||{});return true;}" |
| 38 |
. "if(fire())return;" |
| 39 |
. "var n=0;var iv=setInterval(function(){n++;if(fire()||n>50){clearInterval(iv);}},100);" |
| 40 |
. "document.addEventListener('DOMContentLoaded',fire);" |
| 41 |
. "window.addEventListener('load',fire);" |
| 42 |
. "})();</script>"; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Display a success toast notification |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* @param string $message The message to display |
| 50 |
* @param array $options Additional options |
| 51 |
*/ |
| 52 |
function easy_invoice_toast_success($message, $options = array()) { |
| 53 |
if (wp_doing_ajax()) { |
| 54 |
wp_send_json_success(array( |
| 55 |
'toast' => array( |
| 56 |
'type' => 'success', |
| 57 |
'message' => $message, |
| 58 |
'options' => $options |
| 59 |
) |
| 60 |
)); |
| 61 |
} else { |
| 62 |
_easy_invoice_emit_toast_script('success', $message, $options); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Display an error toast notification |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @param string $message The message to display |
| 71 |
* @param array $options Additional options |
| 72 |
*/ |
| 73 |
function easy_invoice_toast_error($message, $options = array()) { |
| 74 |
if (wp_doing_ajax()) { |
| 75 |
wp_send_json_error(array( |
| 76 |
'toast' => array( |
| 77 |
'type' => 'error', |
| 78 |
'message' => $message, |
| 79 |
'options' => $options |
| 80 |
) |
| 81 |
)); |
| 82 |
} else { |
| 83 |
_easy_invoice_emit_toast_script('error', $message, $options); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Display a warning toast notification |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* @param string $message The message to display |
| 92 |
* @param array $options Additional options |
| 93 |
*/ |
| 94 |
function easy_invoice_toast_warning($message, $options = array()) { |
| 95 |
if (wp_doing_ajax()) { |
| 96 |
wp_send_json_error(array( |
| 97 |
'toast' => array( |
| 98 |
'type' => 'warning', |
| 99 |
'message' => $message, |
| 100 |
'options' => $options |
| 101 |
) |
| 102 |
)); |
| 103 |
} else { |
| 104 |
_easy_invoice_emit_toast_script('warning', $message, $options); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Display an info toast notification |
| 110 |
* |
| 111 |
* @since 1.0.0 |
| 112 |
* @param string $message The message to display |
| 113 |
* @param array $options Additional options |
| 114 |
*/ |
| 115 |
function easy_invoice_toast_info($message, $options = array()) { |
| 116 |
if (wp_doing_ajax()) { |
| 117 |
wp_send_json_success(array( |
| 118 |
'toast' => array( |
| 119 |
'type' => 'info', |
| 120 |
'message' => $message, |
| 121 |
'options' => $options |
| 122 |
) |
| 123 |
)); |
| 124 |
} else { |
| 125 |
_easy_invoice_emit_toast_script('info', $message, $options); |
| 126 |
} |
| 127 |
} |
| 128 |
|