Your IP : 216.73.217.79


Current Path : /proc/thread-self/root/var/www/cesa.co.za/
Upload File :
Current File : //proc/thread-self/root/var/www/cesa.co.za/wordpress-integration.php

<?php

/**
 * WordPress Integration for CESA Error Handler
 * 
 * This file provides WordPress-specific integration for the CESA error handler.
 * It can be included in wp-config.php or functions.php.
 * 
 * Usage in wp-config.php:
 * require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/wordpress-integration.php');
 * 
 * Usage in functions.php:
 * include_once($_SERVER['DOCUMENT_ROOT'] . '/includes/wordpress-integration.php');
 */

// Prevent direct access
if (!defined('CESA_WP_INTEGRATION_LOADED')) {
    define('CESA_WP_INTEGRATION_LOADED', true);
}

// Only load if WordPress is available
if (function_exists('add_action')) {

    /**
     * Initialize CESA error handler for WordPress
     */
    function cesa_init_error_handler()
    {
        // Include the main error handler
        require_once($_SERVER['DOCUMENT_ROOT'] . '/php/includes/error_handling/error_handler.php');

        // Override WordPress default error handling
        if (!WP_DEBUG) {
            // In production, suppress WordPress error display
            add_filter('wp_php_error_message', '__return_empty_string');
        }

        // Add custom error logging for WordPress
        add_action('wp_error_added', 'cesa_log_wordpress_error', 10, 4);

        // Log fatal errors
        add_action('shutdown', 'cesa_check_for_fatal_errors');
    }

    /**
     * Log WordPress errors
     */
    function cesa_log_wordpress_error($code, $message, $data, $wp_error)
    {
        $errorData = array(
            'type' => 'WordPress Error',
            'message' => $message,
            'code' => $code,
            'data' => $data,
            'timestamp' => date('Y-m-d H:i:s'),
            'url' => $_SERVER['REQUEST_URI'] ?? 'CLI',
            'ip' => $_SERVER['REMOTE_ADDR'] ?? 'CLI',
            'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'CLI'
        );

        // Log to file
        $logFile = $_SERVER['DOCUMENT_ROOT'] . '/logs/wordpress-errors.log';
        $logEntry = sprintf(
            "[%s] WordPress Error [%s]: %s\n",
            $errorData['timestamp'],
            $code,
            $message
        );

        error_log($logEntry, 3, $logFile);
    }

    /**
     * Check for fatal errors on shutdown
     */
    function cesa_check_for_fatal_errors()
    {
        $error = error_get_last();

        if ($error && in_array($error['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR))) {
            $errorData = array(
                'type' => 'WordPress Fatal Error',
                'message' => $error['message'],
                'file' => $error['file'],
                'line' => $error['line'],
                'timestamp' => date('Y-m-d H:i:s'),
                'url' => $_SERVER['REQUEST_URI'] ?? 'CLI',
                'ip' => $_SERVER['REMOTE_ADDR'] ?? 'CLI',
                'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'CLI'
            );

            // Log to file
            $logFile = $_SERVER['DOCUMENT_ROOT'] . '/logs/wordpress-fatal-errors.log';
            $logEntry = sprintf(
                "[%s] WordPress Fatal Error: %s in %s on line %d\n",
                $errorData['timestamp'],
                $errorData['message'],
                $errorData['file'],
                $errorData['line']
            );

            error_log($logEntry, 3, $logFile);

            // Send email notification
            if (function_exists('wp_mail')) {
                $subject = "WordPress Fatal Error on CESA Site";
                $message = "A fatal error has occurred on the WordPress site:\n\n";
                $message .= "Error: {$errorData['message']}\n";
                $message .= "File: {$errorData['file']}\n";
                $message .= "Line: {$errorData['line']}\n";
                $message .= "URL: {$errorData['url']}\n";
                $message .= "Time: {$errorData['timestamp']}\n";

                wp_mail('cesa@igotafrica.com', $subject, $message);
            }
        }
    }

    /**
     * Custom error page for WordPress
     */
    function cesa_wordpress_error_page($errorData)
    {
        // Clear any output
        while (ob_get_level()) {
            ob_end_clean();
        }

        // Set status code
        http_response_code(500);

        // Display custom error page
?>
        <!DOCTYPE html>
        <html lang="en">

        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>WordPress Error - CESA</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    margin: 0;
                    padding: 0;
                    min-height: 100vh;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }

                .error-container {
                    background: white;
                    border-radius: 10px;
                    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
                    padding: 40px;
                    max-width: 600px;
                    width: 90%;
                    text-align: center;
                }

                .error-icon {
                    font-size: 64px;
                    color: #e74c3c;
                    margin-bottom: 20px;
                }

                .error-title {
                    color: #2c3e50;
                    font-size: 28px;
                    margin-bottom: 15px;
                    font-weight: bold;
                }

                .error-message {
                    color: #7f8c8d;
                    font-size: 16px;
                    line-height: 1.6;
                    margin-bottom: 30px;
                }

                .error-details {
                    background: #f8f9fa;
                    border: 1px solid #e9ecef;
                    border-radius: 5px;
                    padding: 20px;
                    margin: 20px 0;
                    text-align: left;
                    font-family: 'Courier New', monospace;
                    font-size: 12px;
                    color: #495057;
                }

                .error-id {
                    background: #e9ecef;
                    padding: 5px 10px;
                    border-radius: 3px;
                    font-family: monospace;
                    font-size: 12px;
                    color: #495057;
                    margin-top: 20px;
                }

                .back-button {
                    background: #3498db;
                    color: white;
                    padding: 12px 24px;
                    border: none;
                    border-radius: 5px;
                    text-decoration: none;
                    display: inline-block;
                    margin-top: 20px;
                    transition: background 0.3s;
                }

                .back-button:hover {
                    background: #2980b9;
                }

                .contact-info {
                    margin-top: 30px;
                    padding-top: 20px;
                    border-top: 1px solid #e9ecef;
                    color: #7f8c8d;
                    font-size: 14px;
                }
            </style>
        </head>

        <body>
            <div class="error-container">
                <div class="error-icon">⚠️</div>
                <h1 class="error-title">WordPress Error</h1>
                <p class="error-message">
                    We're sorry, but something went wrong with our website. Our technical team has been notified and is working to resolve the issue.
                </p>

                <div class="error-details">
                    <strong>Error Type:</strong> <?php echo htmlspecialchars($errorData['type']); ?><br>
                    <strong>Message:</strong> <?php echo htmlspecialchars($errorData['message']); ?><br>
                    <?php if (isset($errorData['file'])): ?>
                        <strong>File:</strong> <?php echo htmlspecialchars($errorData['file']); ?><br>
                    <?php endif; ?>
                    <?php if (isset($errorData['line'])): ?>
                        <strong>Line:</strong> <?php echo htmlspecialchars($errorData['line']); ?><br>
                    <?php endif; ?>
                    <strong>URL:</strong> <?php echo htmlspecialchars($errorData['url']); ?><br>
                    <strong>Time:</strong> <?php echo htmlspecialchars($errorData['timestamp']); ?><br>
                </div>

                <div class="error-id">Error ID: <?php echo uniqid(); ?></div>

                <a href="/" class="back-button">Return to Homepage</a>

                <div class="contact-info">
                    <p>If this problem persists, please contact our support team at <a href="mailto:info@cesa.co.za">info@cesa.co.za</a></p>
                </div>
            </div>
        </body>

        </html>
<?php
        exit;
    }

    // Initialize the error handler
    add_action('init', 'cesa_init_error_handler');
} else {
    // WordPress not loaded, just include the main error handler
    require_once($_SERVER['DOCUMENT_ROOT'] . '/php/includes/error_handling/error_handler.php');
}
?>