Home / Admin / Charitable Email Preview Test – PHP 8.4
Duplicate Snippet

Embed Snippet on Your Site

Charitable Email Preview Test – PHP 8.4

Code Preview
php
<?php
<?php
/**
 * Charitable Email Preview Test - PHP 8.4
 *
 * Use this ONLY if email preview works on PHP 8.2 but breaks on PHP 8.4
 *
 * WPCODE INSTRUCTIONS:
 * - Type: PHP Snippet
 * - Location: Run Everywhere
 * - Title: Charitable Preview Test - PHP 8.4
 */
// Override email preview handling with PHP 8.4 compatibility
add_action('template_redirect', function() {
    // Only intercept email preview requests
    if (isset($_GET['charitable_action']) && $_GET['charitable_action'] === 'preview_email') {
        if (!isset($_GET['email_id'])) {
            wp_die('Missing email ID for preview.');
        }
        // PHP 8.4 safe parameter handling with null coalescing
        $email_id = $_GET['email_id'] ?? '';
        $email_id = sanitize_key($email_id); // More appropriate than esc_html for keys
        if (empty($email_id)) {
            wp_die('Invalid email ID provided.');
        }
        // Get the email system with PHP 8.4 error handling
        if (function_exists('charitable_get_helper')) {
            $emails_helper = charitable_get_helper('emails');
            if ($emails_helper && method_exists($emails_helper, 'register_emails')) {
                // Force email registration
                $emails_helper->register_emails();
                $email_class = $emails_helper->get_email($email_id);
                if ($email_class && class_exists($email_class)) {
                    try {
                        // Create email object with PHP 8.4 exception handling
                        $email_object = new $email_class();
                        echo $email_object->preview();
                        exit;
                    } catch (Throwable $e) {
                        // PHP 8.4 compatible error handling
                        wp_die('PHP 8.4 Preview Error: ' . esc_html($e->getMessage()));
                    }
                } else {
                    wp_die('Email class not found: ' . esc_html($email_id));
                }
            }
        }
        wp_die('Email preview system unavailable.');
    }
}, 1); // Very high priority to run before other plugins

Comments

Add a Comment