# easy-invoice/2.3.8/includes/Helpers/ToastHelper.php

Easy Invoice – Invoice Generator, PDF Quotes &amp; Payments, version 2.3.8. 128 lines.

- Page: https://pluginprobe.com/plugins/easy-invoice/2.3.8/code/includes/Helpers/ToastHelper.php
- Raw: https://pluginprobe.com/plugins/easy-invoice/2.3.8/raw/includes/Helpers/ToastHelper.php
- Modified: 2026-05-21T08:29:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/easy-invoice/2.3.8/code/includes/Helpers/ToastHelper.php#L10-L20`.

```php
<?php

/**
 * Emit an inline toast call deferred until EasyInvoiceToast is loaded.
 *
 * The toast JS is enqueued in the footer, so toast helper calls that run
 * mid-body (e.g. during a settings page render) must wait for the global to
 * appear. We poll for up to ~3s, then fall back to a hidden DOM queue that
 * the toast script flushes when it boots.
 */
function _easy_invoice_emit_toast_script($type, $message, $options) {
    $type    = in_array($type, array('success', 'error', 'warning', 'info'), true) ? $type : 'info';
    $message = (string) $message;
    $payload = wp_json_encode(
        array(
            'type'    => $type,
            'message' => $message,
            'options' => is_array($options) ? $options : array(),
        ),
        JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
    );
    if (!$payload) {
        return;
    }

    // Single-shot emitter: wait until BOTH (1) the EasyInvoiceToast API is
    // exposed by easy-invoice-toast.js AND (2) its container element has been
    // appended to <body> by its jQuery-ready init() — calling show() before
    // the container exists crashes on `toastContainer.append(...)`.
    // The `fired` latch guarantees we fire exactly once across the interval,
    // DOMContentLoaded, and load triggers.
    echo "<script>(function(){var p=" . $payload . ";var fired=false;"
        . "function ready(){return !!(window.EasyInvoiceToast"
        . "&&typeof window.EasyInvoiceToast[p.type]==='function'"
        . "&&document.getElementById('easy-invoice-toast-container'));}"
        . "function fire(){if(fired)return true;if(!ready())return false;"
        . "fired=true;window.EasyInvoiceToast[p.type](p.message,p.options||{});return true;}"
        . "if(fire())return;"
        . "var n=0;var iv=setInterval(function(){n++;if(fire()||n>50){clearInterval(iv);}},100);"
        . "document.addEventListener('DOMContentLoaded',fire);"
        . "window.addEventListener('load',fire);"
        . "})();</script>";
}

/**
 * Display a success toast notification
 *
 * @since 1.0.0
 * @param string $message The message to display
 * @param array $options Additional options
 */
function easy_invoice_toast_success($message, $options = array()) {
    if (wp_doing_ajax()) {
        wp_send_json_success(array(
            'toast' => array(
                'type' => 'success',
                'message' => $message,
                'options' => $options
            )
        ));
    } else {
        _easy_invoice_emit_toast_script('success', $message, $options);
    }
}

/**
 * Display an error toast notification
 *
 * @since 1.0.0
 * @param string $message The message to display
 * @param array $options Additional options
 */
function easy_invoice_toast_error($message, $options = array()) {
    if (wp_doing_ajax()) {
        wp_send_json_error(array(
            'toast' => array(
                'type' => 'error',
                'message' => $message,
                'options' => $options
            )
        ));
    } else {
        _easy_invoice_emit_toast_script('error', $message, $options);
    }
}

/**
 * Display a warning toast notification
 *
 * @since 1.0.0
 * @param string $message The message to display
 * @param array $options Additional options
 */
function easy_invoice_toast_warning($message, $options = array()) {
    if (wp_doing_ajax()) {
        wp_send_json_error(array(
            'toast' => array(
                'type' => 'warning',
                'message' => $message,
                'options' => $options
            )
        ));
    } else {
        _easy_invoice_emit_toast_script('warning', $message, $options);
    }
}

/**
 * Display an info toast notification
 *
 * @since 1.0.0
 * @param string $message The message to display
 * @param array $options Additional options
 */
function easy_invoice_toast_info($message, $options = array()) {
    if (wp_doing_ajax()) {
        wp_send_json_success(array(
            'toast' => array(
                'type' => 'info',
                'message' => $message,
                'options' => $options
            )
        ));
    } else {
        _easy_invoice_emit_toast_script('info', $message, $options);
    }
}

```
