PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.4.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.4.0
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Helpers / ToastHelper.php

ToastHelper.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.4.0, at includes/Helpers/ToastHelper.php

134 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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 &quot; 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 /**
52 * Display a success toast notification
53 *
54 * @since 1.0.0
55 * @param string $message The message to display
56 * @param array $options Additional options
57 */
58 function easy_invoice_toast_success($message, $options = array()) {
59 if (wp_doing_ajax()) {
60 wp_send_json_success(array(
61 'toast' => array(
62 'type' => 'success',
63 'message' => $message,
64 'options' => $options
65 )
66 ));
67 } else {
68 _easy_invoice_emit_toast_script('success', $message, $options);
69 }
70 }
71
72 /**
73 * Display an error toast notification
74 *
75 * @since 1.0.0
76 * @param string $message The message to display
77 * @param array $options Additional options
78 */
79 function easy_invoice_toast_error($message, $options = array()) {
80 if (wp_doing_ajax()) {
81 wp_send_json_error(array(
82 'toast' => array(
83 'type' => 'error',
84 'message' => $message,
85 'options' => $options
86 )
87 ));
88 } else {
89 _easy_invoice_emit_toast_script('error', $message, $options);
90 }
91 }
92
93 /**
94 * Display a warning toast notification
95 *
96 * @since 1.0.0
97 * @param string $message The message to display
98 * @param array $options Additional options
99 */
100 function easy_invoice_toast_warning($message, $options = array()) {
101 if (wp_doing_ajax()) {
102 wp_send_json_error(array(
103 'toast' => array(
104 'type' => 'warning',
105 'message' => $message,
106 'options' => $options
107 )
108 ));
109 } else {
110 _easy_invoice_emit_toast_script('warning', $message, $options);
111 }
112 }
113
114 /**
115 * Display an info toast notification
116 *
117 * @since 1.0.0
118 * @param string $message The message to display
119 * @param array $options Additional options
120 */
121 function easy_invoice_toast_info($message, $options = array()) {
122 if (wp_doing_ajax()) {
123 wp_send_json_success(array(
124 'toast' => array(
125 'type' => 'info',
126 'message' => $message,
127 'options' => $options
128 )
129 ));
130 } else {
131 _easy_invoice_emit_toast_script('info', $message, $options);
132 }
133 }
134