Home / Admin / Charitable Email Preview Debug Tool
Duplicate Snippet

Embed Snippet on Your Site

Charitable Email Preview Debug Tool

Code Preview
php
<?php
/**
 * Charitable Email Preview Debug Tool (WPCode-ready)
 *
 * WPCODE INSTRUCTIONS:
 * 1. Install WPCode plugin (Insert Headers and Footers)
 * 2. Code Snippets → + Add Snippet → "Add Your Custom Code (New Snippet)"
 * 3. Title: "Charitable Email Debug Tool"
 * 4. Code Type: PHP Snippet
 * 5. Location: Run Everywhere (or Frontend Only)
 * 6. Paste this code and ACTIVATE
 * 7. Visit: yoursite.com/?charitable_debug=1
 * 8. Copy the report and share with support
 * 9. Deactivate/delete this snippet when done
 *
 * WHY THIS VERSION WORKS:
 * - Registers on template_redirect so it runs AFTER WordPress + Charitable load
 * - Outputs full HTML and then exit; so the theme never loads (no redirect to homepage)
 */
if ( ! isset( $_GET['charitable_debug'] ) ) {
	return;
}
if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
	return;
}
// Run on template_redirect so we have full WP + Charitable, then take over the request
add_action( 'template_redirect', function () {
	if ( ! isset( $_GET['charitable_debug'] ) ) {
		return;
	}
	ob_start();
	// Use global so charitable_debug_add_to_report() can append to the same array
	global $report_lines;
	$report_lines   = array();
	$report_lines[] = 'CHARITABLE EMAIL PREVIEW DEBUG REPORT';
	$report_lines[] = 'Generated: ' . date( 'Y-m-d H:i:s T' );
	$report_lines[] = 'Site: ' . home_url();
	$report_lines[] = str_repeat( '=', 50 );
	$report_lines[] = '';
	function charitable_debug_add_to_report( $section, $details = '' ) {
		global $report_lines;
		$report_lines[] = $section;
		if ( $details ) {
			$report_lines[] = $details;
		}
		$report_lines[] = '';
	}
	function charitable_debug_display_test( $name, $result, $details = '', $add_to_text = true ) {
		$icon  = $result ? '✅' : '❌';
		$class = $result ? 'pass' : 'fail';
		echo '<p class="' . esc_attr( $class ) . '">' . $icon . ' ' . esc_html( $name ) . '</p>';
		if ( $details ) {
			echo '<pre>' . esc_html( $details ) . '</pre>';
		}
		if ( $add_to_text ) {
			charitable_debug_add_to_report( $icon . ' ' . $name, $details );
		}
	}
	function charitable_debug_display_info( $message, $details = '', $is_warning = true ) {
		$icon  = $is_warning ? '⚠️' : 'ℹ️';
		$class = $is_warning ? 'warning' : 'info';
		echo '<p class="' . esc_attr( $class ) . '">' . $icon . ' ' . esc_html( $message ) . '</p>';
		if ( $details ) {
			echo '<pre>' . esc_html( $details ) . '</pre>';
		}
		charitable_debug_add_to_report( $icon . ' ' . $message, $details );
	}
	?>
	<!DOCTYPE html>
	<html>
	<head>
		<title>Charitable Email Preview Debug Report</title>
		<style>
			body { font-family: Arial, sans-serif; margin: 20px; line-height: 1.6; }
			.header { background: #0073aa; color: white; padding: 20px; border-radius: 5px; margin-bottom: 20px; }
			.section { background: #f9f9f9; border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 5px; }
			.pass { color: #46b450; font-weight: bold; }
			.fail { color: #dc3232; font-weight: bold; }
			.warning { color: #ffb900; font-weight: bold; }
			.info { color: #0073aa; }
			pre { background: #f0f0f0; padding: 10px; overflow-x: auto; border-radius: 3px; font-size: 12px; }
			.plugin.conflict { padding: 8px; margin: 4px 0; border-left: 4px solid #dc3232; background: #ffe6e6; }
			.copy-box { background: #e7f3ff; border: 2px solid #0073aa; padding: 15px; margin: 20px 0; border-radius: 5px; }
			.button { background: #0073aa; color: white; padding: 8px 15px; text-decoration: none; border-radius: 3px; }
			textarea { width: 100%; height: 300px; font-family: monospace; font-size: 11px; }
		</style>
	</head>
	<body>
		<div class="header">
			<h1>🔍 Charitable Email Preview Debug Report</h1>
			<p>Diagnosing why email preview redirects to homepage</p>
			<p><strong>Generated:</strong> <?php echo esc_html( date( 'Y-m-d H:i:s T' ) ); ?></p>
			<p><strong>Site:</strong> <?php echo esc_url( home_url() ); ?></p>
		</div>
	<?php
	// 1. Environment
	echo '<div class="section"><h2>1. Environment</h2>';
	charitable_debug_display_info( 'PHP Version', PHP_VERSION, false );
	charitable_debug_display_info( 'WordPress Version', get_bloginfo( 'version' ), false );
	charitable_debug_display_info( 'Home URL', home_url(), false );
	$cdn_detected = '';
	if ( ! empty( $_SERVER['HTTP_CF_RAY'] ) ) {
		$cdn_detected .= 'Cloudflare detected. ';
	}
	if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
		$cdn_detected .= 'Proxy/CDN detected. ';
	}
	if ( $cdn_detected ) {
		charitable_debug_display_info( 'CDN/Proxy Status', trim( $cdn_detected ), true );
	}
	echo '</div>';
	// 2. Charitable
	echo '<div class="section"><h2>2. Charitable Plugin</h2>';
	if ( ! function_exists( 'charitable' ) ) {
		charitable_debug_display_test( 'Charitable Active', false, 'Plugin not found - install/activate Charitable' );
		echo '</div></body></html>';
		$report_text = implode( "\n", $report_lines );
		echo '<div class="copy-box"><h2>📋 COPY THIS REPORT</h2><textarea readonly>' . esc_textarea( $report_text ) . '</textarea></div>';
		exit;
	}
	charitable_debug_display_test( 'Charitable Active', true, 'Version: ' . ( defined( 'CHARITABLE_VERSION' ) ? CHARITABLE_VERSION : 'Unknown' ) );
	$emails_helper = charitable_get_helper( 'emails' );
	charitable_debug_display_test( 'Email System', (bool) $emails_helper );
	if ( $emails_helper ) {
		$emails     = $emails_helper->get_available_emails();
		$has_emails = is_array( $emails ) && count( $emails ) > 0;
		charitable_debug_display_test( 'Emails Registered', $has_emails, $has_emails ? count( $emails ) . ' emails found' : 'No emails - timing issue' );
		if ( $has_emails ) {
			$offline_email = $emails_helper->get_email( 'offline_donation_receipt' );
			charitable_debug_display_test( 'Offline Email Available', (bool) $offline_email, $offline_email ?: 'Class not found' );
		}
	}
	echo '</div>';
	// 3. Conflicts
	echo '<div class="section"><h2>3. Potential Conflicts</h2>';
	$current_theme = wp_get_theme();
	charitable_debug_display_info( 'Active Theme', $current_theme->get( 'Name' ), false );
	$problematic = array(
		'safe-redirect-manager'   => 'Safe Redirect Manager - HIGH RISK',
		'ninja-firewall'          => 'NinjaFirewall - HIGH RISK',
		'really-simple-security'  => 'Really Simple Security - MEDIUM RISK',
		'wp-rocket'               => 'WP Rocket - LOW RISK',
	);
	$active_plugins = get_option( 'active_plugins', array() );
	$conflicts      = array();
	foreach ( $active_plugins as $plugin ) {
		foreach ( $problematic as $slug => $name ) {
			if ( stripos( $plugin, $slug ) !== false ) {
				$conflicts[] = $name;
				echo '<div class="plugin conflict">⚠️ ' . esc_html( $name ) . '</div>';
			}
		}
	}
	if ( empty( $conflicts ) ) {
		charitable_debug_display_test( 'Plugin Conflicts', true, 'No known problematic plugins detected' );
	} else {
		charitable_debug_display_test( 'Plugin Conflicts', false, implode( "\n", $conflicts ) );
	}
	echo '</div>';
	// 4. URL Testing
	echo '<div class="section"><h2>4. URL Testing</h2>';
	$test_url = add_query_arg(
		array(
			'charitable_action' => 'preview_email',
			'email_id'         => 'offline_donation_receipt',
		),
		home_url()
	);
	charitable_debug_display_info( 'Generated Preview URL', $test_url, false );
	$url_issues = array();
	if ( strpos( $test_url, '//?' ) !== false ) {
		$url_issues[] = 'Double slash detected';
	}
	if ( ! parse_url( $test_url, PHP_URL_SCHEME ) ) {
		$url_issues[] = 'Missing URL scheme';
	}
	charitable_debug_display_test( 'URL Generation', empty( $url_issues ), implode( ', ', $url_issues ) );
	echo '<p><strong>🧪 LIVE TEST:</strong> <a href="' . esc_url( $test_url ) . '" target="_blank" class="button">Click to Test Preview URL</a></p>';
	echo '<p><small>This should show email preview, NOT redirect to homepage</small></p>';
	echo '</div>';
	// 5. Live Preview Test (if this IS a preview request)
	if ( isset( $_GET['charitable_action'] ) && $_GET['charitable_action'] === 'preview_email' && isset( $_GET['email_id'] ) ) {
		echo '<div class="section"><h2>5. Live Preview Test Results</h2>';
		$email_id = sanitize_text_field( wp_unslash( $_GET['email_id'] ) );
		charitable_debug_display_info( 'Email ID Received', $email_id, false );
		if ( $emails_helper ) {
			$email_class = $emails_helper->get_email( $email_id );
			charitable_debug_display_test( 'Email Class Found', (bool) $email_class, $email_class ?: 'Not found' );
			if ( $email_class && class_exists( $email_class ) ) {
				$email_obj = new $email_class();
				charitable_debug_display_test( 'Email Object Created', true, 'Name: ' . $email_obj->get_name() );
				echo '<h3>📧 Email Preview Content:</h3>';
				echo '<div style="border: 2px solid green; padding: 15px; background: white;">';
				echo $email_obj->preview(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo '</div>';
			} else {
				charitable_debug_display_test( 'Email Object Created', false, 'Class not found or invalid' );
			}
		}
		echo '</div>';
	}
	$report_text = implode( "\n", $report_lines );
	?>
		<div class="copy-box">
			<h2>📋 COPY THIS REPORT</h2>
			<p><strong>Instructions:</strong></p>
			<ol>
				<li><strong>Select all text in the box below</strong> (Ctrl+A or Cmd+A)</li>
				<li><strong>Copy it</strong> (Ctrl+C or Cmd+C)</li>
				<li><strong>Test the Preview URL button above</strong> – note what happens</li>
				<li><strong>Paste the report + test results in your support ticket</strong></li>
			</ol>
			<textarea readonly><?php echo esc_textarea( $report_text ); ?></textarea>
		</div>
	</body>
	</html>
	<?php
	// CRITICAL: Stop WordPress from loading the theme (prevents redirect/homepage)
	exit;
}, 1 );
'; // 3. Conflicts echo '

3. Potential Conflicts

'; $current_theme = wp_get_theme(); charitable_debug_display_info( 'Active Theme', $current_theme->get( 'Name' ), false ); $problematic = array( 'safe-redirect-manager' => 'Safe Redirect Manager - HIGH RISK', 'ninja-firewall' => 'NinjaFirewall - HIGH RISK', 'really-simple-security' => 'Really Simple Security - MEDIUM RISK', 'wp-rocket' => 'WP Rocket - LOW RISK', ); $active_plugins = get_option( 'active_plugins', array() ); $conflicts = array(); foreach ( $active_plugins as $plugin ) { foreach ( $problematic as $slug => $name ) { if ( stripos( $plugin, $slug ) !== false ) { $conflicts[] = $name; echo '
⚠️ ' . esc_html( $name ) . '
'; } } } if ( empty( $conflicts ) ) { charitable_debug_display_test( 'Plugin Conflicts', true, 'No known problematic plugins detected' ); } else { charitable_debug_display_test( 'Plugin Conflicts', false, implode( "\n", $conflicts ) ); } echo '
'; // 4. URL Testing echo '

4. URL Testing

'; $test_url = add_query_arg( array( 'charitable_action' => 'preview_email', 'email_id' => 'offline_donation_receipt', ), home_url() ); charitable_debug_display_info( 'Generated Preview URL', $test_url, false ); $url_issues = array(); if ( strpos( $test_url, '//?' ) !== false ) { $url_issues[] = 'Double slash detected'; } if ( ! parse_url( $test_url, PHP_URL_SCHEME ) ) { $url_issues[] = 'Missing URL scheme'; } charitable_debug_display_test( 'URL Generation', empty( $url_issues ), implode( ', ', $url_issues ) ); echo '

🧪 LIVE TEST: Click to Test Preview URL

'; echo '

This should show email preview, NOT redirect to homepage

'; echo '
'; // 5. Live Preview Test (if this IS a preview request) if ( isset( $_GET['charitable_action'] ) && $_GET['charitable_action'] === 'preview_email' && isset( $_GET['email_id'] ) ) { echo '

5. Live Preview Test Results

'; $email_id = sanitize_text_field( wp_unslash( $_GET['email_id'] ) ); charitable_debug_display_info( 'Email ID Received', $email_id, false ); if ( $emails_helper ) { $email_class = $emails_helper->get_email( $email_id ); charitable_debug_display_test( 'Email Class Found', (bool) $email_class, $email_class ?: 'Not found' ); if ( $email_class && class_exists( $email_class ) ) { $email_obj = new $email_class(); charitable_debug_display_test( 'Email Object Created', true, 'Name: ' . $email_obj->get_name() ); echo '

📧 Email Preview Content:

'; echo '
'; echo $email_obj->preview(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '
'; } else { charitable_debug_display_test( 'Email Object Created', false, 'Class not found or invalid' ); } } echo '
'; } $report_text = implode( "\n", $report_lines ); ?>

📋 COPY THIS REPORT

Instructions:

  1. Select all text in the box below (Ctrl+A or Cmd+A)
  2. Copy it (Ctrl+C or Cmd+C)
  3. Test the Preview URL button above – note what happens
  4. Paste the report + test results in your support ticket

Comments

Add a Comment