<?php
/**
 * Admin View: Page - Status Report.
 *
 * @package CardOracle
 * @since 1.1.3
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( 'status' === $active_tab ) {
	require_once CARD_ORACLE_DIR . 'admin/class-card-oracle-admin-status.php';

	global $co_logs, $wpdb;

	if ( isset( $_POST['co_clear_logs_button'] ) && check_admin_referer( 'co_clear_logs_button' ) ) {
		// Clear Logs button pressed, delete the error log files.
		$co_logs->delete_logs( 0, 'error' );
	}

	if ( isset( $_POST['co_clear_debug_log_button'] ) && check_admin_referer( 'co_clear_debug_log_button' ) ) {
		// Clear Debug Log button pressed, clear the debug log file.
		$co_logs->clear_log_file();
	}

	$co_cache_clear_result = null;
	if ( isset( $_POST['co_clear_cache_button'] ) && check_admin_referer( 'co_clear_cache_button' ) && current_user_can( 'manage_options' ) ) {
		$co_cache_clear_result = card_oracle_clear_all_caches();
	}

	$environment         = card_oracle_get_environment_info();
	$active_plugins      = $environment['active_plugins'];
	$active_theme        = wp_get_theme();
	$api_request_counts  = $environment['api_request_counts'];
	$card_oracle_options = isset( $environment['card_oracle_options'] ) ? $environment['card_oracle_options'] : array();
	$domain_name         = isset( $environment['domain_name'] ) ? $environment['domain_name'] : 'unknown-domain';
	$dropins_mu_plugins  = get_dropins_mu_plugins();
	$error_log_counts    = $environment['error_log_counts'];
	$memory_usage        = isset( $environment['memory_usage'] ) ? $environment['memory_usage'] : array();
	$opcache_status      = isset( $environment['opcache_status'] ) ? $environment['opcache_status'] : array();
	$wp_constants        = isset( $environment['wp_constants'] ) ? $environment['wp_constants'] : array();
	$error_log_rows      = '5';
	if ( isset( $_POST['error_rows'] ) && isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'co_error_rows' ) ) {
		$error_log_rows = sanitize_text_field( wp_unslash( $_POST['error_rows'] ) );
	}
	$inactive_plugins = $environment['inactive_plugins'];
	$md5_files        = isset( $environment['md5_files'] ) ? $environment['md5_files'] : array();
	$post_type_counts = isset( $environment['post_type_counts'] ) ? $environment['post_type_counts'] : array();
	$theme            = array(
		'name'           => $active_theme->name,
		'version'        => $active_theme->version,
		'author_url'     => esc_url_raw( $active_theme->{'Author URI'} ),
		'is_child_theme' => is_child_theme(),
	);
	?>

	<div class="co-dashboard">
		<header class="co-dashboard__hero co-dashboard__hero--subtle">
			<div class="co-dashboard__hero-text">
				<span class="co-dashboard__badge"><?php esc_html_e( 'Diagnostics', 'card-oracle' ); ?></span>
				<h1 class="co-dashboard__hero-title"><?php esc_html_e( 'System Status', 'card-oracle' ); ?></h1>
				<p class="co-dashboard__hero-description">
					<?php esc_html_e( 'Review environment checks, request logs, and integrations before escalating support tickets.', 'card-oracle' ); ?>
				</p>
			</div>
			<div class="co-dashboard__hero-actions">
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=troubleshooting' ) ); ?>">
					<?php esc_html_e( 'Troubleshooting Guide', 'card-oracle' ); ?>
				</a>
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=help' ) ); ?>">
					<?php esc_html_e( 'Help Documentation', 'card-oracle' ); ?>
				</a>
				<a class="button button-secondary" href="mailto:support@chillichalli.com">
					<?php esc_html_e( 'Contact Support', 'card-oracle' ); ?>
				</a>
			</div>
		</header>

		<section class="co-dashboard__section co-dashboard__section--panel">
			<div class="co-dashboard__section-heading">
				<h2><?php esc_html_e( 'Snapshot', 'card-oracle' ); ?></h2>
				<p><?php esc_html_e( 'Collect the latest diagnostic information to include with your support conversations.', 'card-oracle' ); ?></p>
			</div>
			<div class="co-dashboard__panel-body">

	<div class="updated card-oracle-message inline">
		<p>
			<?php esc_html_e( 'Please copy and paste this information in your ticket when contacting support:', 'card-oracle' ); ?>
		</p>
		<p class="submit">
			<a href="#" class="button-primary debug-report" data-domain="<?php echo esc_attr( $domain_name ); ?>"><?php esc_html_e( 'Email system report', 'card-oracle' ); ?></a>
			<a class="button-secondary docs" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=status-report' ) ); ?>">
				<?php esc_html_e( 'Understanding the status report', 'card-oracle' ); ?>
			</a>
		</p>
		<div id="debug-report">
			<textarea readonly="readonly"></textarea>
			<p class="submit">
				<button id="copy-for-support" class="button-primary" href="#" data-tip="<?php esc_attr_e( 'Copied!', 'card-oracle' ); ?>">
					<?php esc_html_e( 'Copy for support', 'card-oracle' ); ?>
				</button>
			</p>
			<div class="copy-success hidden">
				<p class="copy-success hidden">
					<?php esc_html_e( 'Copied!', 'card-oracle' ); ?>
				</p>
				<p class="copy-error hidden">
					<?php esc_html_e( 'Copying to clipboard failed. Please press Ctrl/Cmd+C to copy.', 'card-oracle' ); ?>
				</p>
			</div>
		</div>
	</div>
	<?php if ( null !== $co_cache_clear_result ) : ?>
		<div class="notice notice-success inline" style="margin: 12px 0;">
			<p>
				<?php
				if ( $co_cache_clear_result['cleared'] > 0 ) {
					printf(
						/* translators: %d: number of caches cleared. */
						esc_html( _n( '%d cache cleared.', '%d caches cleared.', $co_cache_clear_result['cleared'], 'card-oracle' ) ),
						(int) $co_cache_clear_result['cleared']
					);
					if ( ! empty( $co_cache_clear_result['items'] ) ) {
						echo ' <span class="description">(' . esc_html( implode( ', ', $co_cache_clear_result['items'] ) ) . ')</span>';
					}
				} else {
					esc_html_e( 'No active caches to clear.', 'card-oracle' );
				}
				?>
			</p>
		</div>
	<?php endif; ?>

	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Cache"><h2><?php esc_html_e( 'Cache', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="Cache Status"><?php esc_html_e( 'Card Oracle caches', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Clears the daily card, random card, orphaned shortcode scan, and reading lookup caches. Use this if changes to readings or cards are not appearing on the site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<form action="" method="post" style="margin: 0;">
						<input type="hidden" value="true" name="co_clear_cache_button" />
						<?php wp_nonce_field( 'co_clear_cache_button' ); ?>
						<?php submit_button( esc_attr__( 'Clear cache', 'card-oracle' ), 'secondary', '', false ); ?>
					</form>
				</td>
			</tr>
		</tbody>
	</table>

	<?php
	// Transient Status: scan wp_options for Card Oracle transients and surface their TTL.
	// External object cache backends (Redis, Memcached) store transients off-DB; show a notice instead.
	$co_transient_rows                   = array();
	$co_uses_object_cache_for_transients = wp_using_ext_object_cache();
	if ( ! $co_uses_object_cache_for_transients ) {
		$co_transient_rows = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
			"SELECT option_name, LENGTH(option_value) AS size_bytes
			   FROM {$wpdb->options}
			  WHERE ( option_name LIKE '\\_transient\\_card\\_oracle\\_%'
			       OR option_name LIKE '\\_transient\\_co\\_cards\\_for\\_%'
			       OR option_name LIKE '\\_transient\\_co\\_positions\\_for\\_%' )
			    AND option_name NOT LIKE '\\_transient\\_timeout\\_%'
			  ORDER BY option_name"
		);
	}
	?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Transient Status"><h2><?php esc_html_e( 'Transient Status', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( $co_uses_object_cache_for_transients ) : ?>
				<tr>
					<td data-export-label="Transient Storage"><?php esc_html_e( 'Transient storage', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'An external object cache (Redis, Memcached) is active. Transients are stored there instead of the database, so per-transient TTLs are not visible from this page.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><mark class="yes"><?php esc_html_e( 'External object cache', 'card-oracle' ); ?></mark></td>
				</tr>
			<?php elseif ( empty( $co_transient_rows ) ) : ?>
				<tr>
					<td data-export-label="Transient Status"><?php esc_html_e( 'Card Oracle transients', 'card-oracle' ); ?>:</td>
					<td class="card-oracle-help-blank">&nbsp;</td>
					<td><?php esc_html_e( 'No active transients found.', 'card-oracle' ); ?></td>
				</tr>
			<?php else : ?>
				<tr>
					<td><strong><?php esc_html_e( 'Transient', 'card-oracle' ); ?></strong></td>
					<td><strong><?php esc_html_e( 'Expiry', 'card-oracle' ); ?></strong></td>
					<td><strong><?php esc_html_e( 'Size', 'card-oracle' ); ?></strong></td>
				</tr>
				<?php
				$now = time();
				foreach ( $co_transient_rows as $co_t ) :
					$co_t_key     = str_replace( '_transient_', '', $co_t->option_name );
					$co_t_timeout = (int) get_option( '_transient_timeout_' . $co_t_key );
					$co_t_size    = (int) $co_t->size_bytes;
					if ( 0 === $co_t_timeout ) {
						$co_t_expiry = '<mark class="no">' . esc_html__( 'No expiry', 'card-oracle' ) . '</mark>';
					} elseif ( $co_t_timeout < $now ) {
						$co_t_expiry = '<mark class="error">' . esc_html__( 'Expired', 'card-oracle' ) . '</mark>';
					} else {
						$co_t_expiry = '<mark class="yes">' . esc_html( human_time_diff( $now, $co_t_timeout ) ) . '</mark>';
					}
					?>
					<tr>
						<td data-export-label="Transient"><code><?php echo esc_html( $co_t_key ); ?></code></td>
						<td><?php echo wp_kses_post( $co_t_expiry ); ?></td>
						<td><?php echo esc_html( size_format( $co_t_size ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>

	<?php
	// Cron Jobs: surface any scheduled card_oracle_* / co_* events.
	$co_cron_jobs = array();
	if ( function_exists( '_get_cron_array' ) ) {
		$co_cron_array = _get_cron_array();
		if ( is_array( $co_cron_array ) ) {
			foreach ( $co_cron_array as $co_cron_ts => $co_cron_hooks ) {
				if ( ! is_array( $co_cron_hooks ) ) {
					continue;
				}
				foreach ( $co_cron_hooks as $co_cron_hook => $co_cron_events ) {
					if ( 0 !== strpos( $co_cron_hook, 'card_oracle_' ) && 0 !== strpos( $co_cron_hook, 'co_' ) ) {
						continue;
					}
					if ( ! is_array( $co_cron_events ) ) {
						continue;
					}
					foreach ( $co_cron_events as $co_cron_event ) {
						$co_cron_jobs[] = array(
							'hook'     => $co_cron_hook,
							'next'     => (int) $co_cron_ts,
							'schedule' => isset( $co_cron_event['schedule'] ) && $co_cron_event['schedule'] ? (string) $co_cron_event['schedule'] : 'one-off',
						);
					}
				}
			}
		}
	}
	?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Cron Jobs"><h2><?php esc_html_e( 'Cron Jobs', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( empty( $co_cron_jobs ) ) : ?>
				<tr>
					<td data-export-label="Cron Jobs"><?php esc_html_e( 'Card Oracle cron jobs', 'card-oracle' ); ?>:</td>
					<td class="card-oracle-help-blank">&nbsp;</td>
					<td><?php esc_html_e( 'No Card Oracle cron jobs are scheduled.', 'card-oracle' ); ?></td>
				</tr>
			<?php else : ?>
				<tr>
					<td><strong><?php esc_html_e( 'Hook', 'card-oracle' ); ?></strong></td>
					<td><strong><?php esc_html_e( 'Schedule', 'card-oracle' ); ?></strong></td>
					<td><strong><?php esc_html_e( 'Next run', 'card-oracle' ); ?></strong></td>
				</tr>
				<?php foreach ( $co_cron_jobs as $co_job ) : ?>
					<tr>
						<td data-export-label="Cron Hook"><code><?php echo esc_html( $co_job['hook'] ); ?></code></td>
						<td><?php echo esc_html( $co_job['schedule'] ); ?></td>
						<td>
							<?php
							echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $co_job['next'] ) );
							echo ' <span class="description">(' . esc_html( human_time_diff( time(), $co_job['next'] ) ) . ')</span>';
							?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>

	<?php
	// Database Table Sizes: report wp_posts/wp_postmeta totals plus the Card Oracle row footprint.
	$co_db_rows  = array();
	$co_db_error = '';
	if ( isset( $wpdb->dbname ) ) {
		$co_db_name = $wpdb->dbname;
	} elseif ( defined( 'DB_NAME' ) ) {
		$co_db_name = DB_NAME;
	} else {
		$co_db_name = '';
	}
	$co_post_types = array( 'co_readings', 'co_positions', 'co_cards', 'co_descriptions', 'co_order', 'co_transaction', 'co_log' );
	if ( '' !== $co_db_name ) {
		$co_db_tables = array( $wpdb->posts, $wpdb->postmeta );
		foreach ( $co_db_tables as $co_db_t ) {
			$co_db_info = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
				$wpdb->prepare(
					'SELECT table_rows, data_length, index_length
					   FROM information_schema.TABLES
					  WHERE table_schema = %s AND table_name = %s',
					$co_db_name,
					$co_db_t
				)
			);
			if ( $co_db_info ) {
				$co_db_rows[ $co_db_t ] = array(
					'rows'  => (int) $co_db_info->table_rows,
					'data'  => (int) $co_db_info->data_length,
					'index' => (int) $co_db_info->index_length,
				);
			}
		}
		if ( empty( $co_db_rows ) ) {
			$co_db_error = $wpdb->last_error;
		}
	}
	$co_post_in       = implode( "','", array_map( 'esc_sql', $co_post_types ) );
	$co_posts_count   = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('{$co_post_in}')" ); // phpcs:ignore WordPress.DB
	$co_postmeta_rows = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.post_type IN ('{$co_post_in}')" ); // phpcs:ignore WordPress.DB
	?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Database Table Sizes"><h2><?php esc_html_e( 'Database Table Sizes', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<?php if ( '' === $co_db_name || ! empty( $co_db_error ) || empty( $co_db_rows ) ) : ?>
				<tr>
					<td data-export-label="Database Sizes"><?php esc_html_e( 'Table sizes', 'card-oracle' ); ?>:</td>
					<td class="card-oracle-help-blank">&nbsp;</td>
					<td>
						<mark class="no">
							<?php esc_html_e( 'Unable to read table sizes (information_schema not accessible).', 'card-oracle' ); ?>
						</mark>
					</td>
				</tr>
			<?php else : ?>
				<?php foreach ( $co_db_rows as $co_db_t => $co_db_stats ) : ?>
					<tr>
						<td data-export-label="<?php echo esc_attr( $co_db_t ); ?>"><code><?php echo esc_html( $co_db_t ); ?></code></td>
						<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Approximate row count and storage size reported by MySQL. Card Oracle stores all its content as rows in wp_posts/wp_postmeta.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
						<td>
							<?php
							/* translators: 1: row count, 2: data size, 3: index size */
							printf(
								esc_html__( '%1$s rows · %2$s data · %3$s index', 'card-oracle' ),
								esc_html( number_format_i18n( $co_db_stats['rows'] ) ),
								esc_html( size_format( $co_db_stats['data'] ) ),
								esc_html( size_format( $co_db_stats['index'] ) )
							);
							?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
			<tr>
				<td data-export-label="Card Oracle Posts"><?php esc_html_e( 'Card Oracle post rows', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Total rows in wp_posts across all Card Oracle post types (readings, positions, cards, descriptions, orders, transactions, logs).', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( number_format_i18n( $co_posts_count ) ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Card Oracle Postmeta"><?php esc_html_e( 'Card Oracle postmeta rows', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Total rows in wp_postmeta attached to Card Oracle posts.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( number_format_i18n( $co_postmeta_rows ) ); ?></td>
			</tr>
		</tbody>
	</table>

	<table class="card_oracle_status_table widefat" id="status">
		<thead>
			<tr>
				<th colspan="3" data-export-label="WordPress Environment"><h2><?php esc_html_e( 'WordPress environment', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="WordPress address (URL)"><?php esc_html_e( 'WordPress address (URL)', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><div class="card-oracle-help-tip"><p><?php esc_html_e( 'The root URL of your site.', 'card-oracle' ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></p></div></td>
				<td><?php echo esc_html( $environment['site_url'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Site address (URL)"><?php esc_html_e( 'Site address (URL)', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The homepage URL of your site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $environment['home_url'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Card Oracle Version"><?php esc_html_e( 'Card Oracle version', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of Card Oracle installed on your site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $environment['card_oracle_version'] ); ?></td>
			</tr>
			<?php if ( $environment['rss_feed'] ) : ?>
				<tr>
					<td data-export-label="Card Oracle RSS"><?php esc_html_e( 'Card Oracle RSS', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Is Card Oracle RSS registered on your site?', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					</td>
				</tr>
			<?php endif; ?>
			<tr>
				<td data-export-label="Freemius Version"><?php esc_html_e( 'Freemius version', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of Freemius installed on your site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( WP_FS__SDK_VERSION ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Log Directory Writable"><?php esc_html_e( 'Log directory writable', 'card-oracle' ); ?>:</td>
				<td class="help"><?php echo card_oracle_help_tip( esc_html__( 'Card Oracle can write logs which makes debugging problems easier. The directory must be writable for this to happen.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( $environment['log_directory_writable'] ) {
						echo '<mark class="yes"><span class="dashicons dashicons-yes"></span> <code class="private">' . esc_html( $environment['log_directory'] ) . '</code></mark> ';
					} else {
						/* Translators: %1$s: Log directory, %2$s: Log directory constant */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( 'To allow logging, make %1$s writable or define a custom %2$s.', 'card-oracle' ), '<code>' . esc_html( $environment['log_directory'] ) . '</code>', '<code>CARD_ORACLE_LOG_DIR</code>' ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<tr>
				<td data-export-label="WP Version"><?php esc_html_e( 'WordPress version', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of WordPress installed on your site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					$latest_version = get_transient( 'card_oracle_system_status_wp_version_check' );

					if ( false === $latest_version ) {
						$version_check = wp_remote_get( 'https://api.wordpress.org/core/version-check/1.7/' );

						if ( is_wp_error( $version_check ) ) {
							$latest_version = $environment['wp_version'];
							set_transient( 'card_oracle_system_status_wp_version_check', $latest_version, DAY_IN_SECONDS );
						}

						$api_response = ! is_wp_error( $version_check ) ? json_decode( wp_remote_retrieve_body( $version_check ), true ) : null;

						if ( $api_response && isset( $api_response['offers'], $api_response['offers'][0], $api_response['offers'][0]['version'] ) ) {
							$latest_version = $api_response['offers'][0]['version'];
						} else {
							$latest_version = $environment['wp_version'];
						}
						set_transient( 'card_oracle_system_status_wp_version_check', $latest_version, DAY_IN_SECONDS );
					}

					if ( version_compare( $environment['wp_version'], $latest_version, '<' ) ) {
						/* Translators: %1$s: Current version, %2$s: New version */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( '%1$s - There is a newer version of WordPress available (%2$s)', 'card-oracle' ), esc_html( $environment['wp_version'] ), esc_html( $latest_version ) ) . '</mark>';
					} else {
						echo '<mark class="yes">' . esc_html( $environment['wp_version'] ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<tr>
				<td data-export-label="WP Multisite"><?php esc_html_e( 'WordPress multisite', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Whether or not you have WordPress Multisite enabled.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo ( $environment['wp_multisite'] ) ? '<span class="dashicons dashicons-yes"></span>' : '&ndash;'; ?></td>
			</tr>
			<tr>
				<td data-export-label="WP Memory Limit"><?php esc_html_e( 'WordPress memory limit', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The maximum amount of memory (RAM) that your site can use at one time.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( $environment['wp_memory_limit'] < 67108864 ) {
						/* Translators: %1$s: Memory limit, %2$s: Docs link. */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( '%1$s - We recommend setting memory to at least 64MB. See: %2$s', 'card-oracle' ), esc_html( size_format( $environment['wp_memory_limit'] ) ), '<a href="https://wordpress.org/support/article/editing-wp-config-php/#increasing-memory-allocated-to-php" target="_blank">' . esc_html__( 'Increasing memory allocated to PHP', 'card-oracle' ) . '</a>' ) . '</mark>';
					} else {
						echo '<mark class="yes">' . esc_html( size_format( $environment['wp_memory_limit'] ) ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<tr>
				<td data-export-label="WP Debug Mode"><?php esc_html_e( 'WordPress debug mode', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Displays whether or not WordPress is in Debug Mode.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $environment['wp_debug_mode'] ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="no">&ndash;</mark>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<td data-export-label="WP Cron"><?php esc_html_e( 'WordPress cron', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Displays whether or not WP Cron Jobs are enabled.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $environment['wp_cron'] ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="no">&ndash;</mark>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<td data-export-label="Database Prefix"><?php esc_html_e( 'Database prefix', 'card-oracle' ); ?></td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Displays the table prefix used in your WordPress database.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( strlen( $environment['database_prefix'] ) > 20 ) {
						/* Translators: %1$s: Database prefix. */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( '%1$s - We recommend using a prefix with less than 20 characters.', 'card-oracle' ), esc_html( $environment['database_prefix'] ) ) . '</mark>';
					} else {
						echo '<mark class="yes">' . esc_html( $environment['database_prefix'] ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<tr>
				<td data-export-label="Language"><?php esc_html_e( 'Language', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The current language used by WordPress. Default = English', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $environment['language'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="External object cache"><?php esc_html_e( 'External object cache', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Displays whether or not WordPress is using an external object cache.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $environment['external_object_cache'] ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="no">&ndash;</mark>
					<?php endif; ?>
				</td>
			</tr>
			<?php
			// REST API check: loopback request, cached for an hour so the Status tab stays cheap.
			$co_rest_status = get_transient( 'card_oracle_rest_api_status' );
			if ( ! is_array( $co_rest_status ) ) {
				$co_rest_url      = rest_url();
				$co_rest_response = wp_remote_get(
					$co_rest_url,
					array(
						'timeout'   => 5,
						'sslverify' => false,
					)
				);
				if ( is_wp_error( $co_rest_response ) ) {
					$co_rest_status = array(
						'ok'      => false,
						'code'    => 0,
						'message' => $co_rest_response->get_error_message(),
						'url'     => $co_rest_url,
					);
				} else {
					$co_rest_code   = (int) wp_remote_retrieve_response_code( $co_rest_response );
					$co_rest_status = array(
						'ok'      => ( 200 === $co_rest_code ),
						'code'    => $co_rest_code,
						'message' => '',
						'url'     => $co_rest_url,
					);
				}
				set_transient( 'card_oracle_rest_api_status', $co_rest_status, HOUR_IN_SECONDS );
			}
			?>
			<tr>
				<td data-export-label="REST API"><?php esc_html_e( 'REST API', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'A loopback request to wp-json verifies the WordPress REST API is reachable from this server. Required for the block editor and several Card Oracle features. Result cached for one hour.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( ! empty( $co_rest_status['ok'] ) ) {
						echo '<mark class="yes"><span class="dashicons dashicons-yes"></span> ' . esc_html( $co_rest_status['url'] ) . '</mark>';
					} elseif ( ! empty( $co_rest_status['message'] ) ) {
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html( $co_rest_status['message'] ) . '</mark>';
					} else {
						/* translators: %d: HTTP status code returned by the REST API loopback. */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html( sprintf( __( 'Unexpected response code: %d', 'card-oracle' ), (int) $co_rest_status['code'] ) ) . '</mark>';
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Server Environment"><h2><?php esc_html_e( 'Server environment', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="Server Info"><?php esc_html_e( 'Server info', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Information about the web server that is currently hosting your site.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $environment['server_info'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="PHP Version"><?php esc_html_e( 'PHP version', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of PHP installed on your hosting server.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( version_compare( $environment['php_version'], '7.4', '>=' ) ) {
						echo '<mark class="yes">' . esc_html( $environment['php_version'] ) . '</mark>';
					} else {
						$update_link = ' <a href="https://docs.woocommerce.com/document/how-to-update-your-php-version/" target="_blank">' . esc_html__( 'How to update your PHP version', 'card-oracle' ) . '</a>';
						$notice      = '<span class="dashicons dashicons-warning"></span> ' . __( 'Card Oracle requires PHP 7.4 or above.', 'card-oracle' ) . $update_link;
						echo '<mark class="error">' . esc_html( $environment['php_version'] ) . ' - ' . wp_kses_post( $notice ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<?php if ( function_exists( 'ini_get' ) ) : ?>
				<tr>
					<td data-export-label="PHP Post Max Size"><?php esc_html_e( 'PHP post max size', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The largest filesize that can be contained in one post.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( size_format( $environment['php_post_max_size'] ) ); ?></td>
				</tr>
				<tr>
					<td data-export-label="PHP Time Limit"><?php esc_html_e( 'PHP time limit', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The amount of time (in seconds) that your site will spend on a single operation before timing out (to avoid server lockups)', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $environment['php_max_execution_time'] ); ?></td>
				</tr>
				<tr>
					<td data-export-label="PHP Max Input Vars"><?php esc_html_e( 'PHP max input vars', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The maximum number of variables your server can use for a single function to avoid overloads.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $environment['php_max_input_vars'] ); ?></td>
				</tr>
				<tr>
					<td data-export-label="PHP Sendmail Path"><?php esc_html_e( 'PHP sendmail path', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The path to sendmail on your server.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $environment['php_sendmail_path'] ); ?></td>
				</tr>
				<tr>
					<td data-export-label="cURL Version"><?php esc_html_e( 'cURL version', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of cURL installed on your server.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $environment['curl_version'] ); ?></td>
				</tr>
				<tr>
					<td data-export-label="SUHOSIN Installed"><?php esc_html_e( 'SUHOSIN installed', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Suhosin is an advanced protection system for PHP installations. If enabled on your server, Suhosin may need to be configured to increase its data submission limits.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo $environment['suhosin_installed'] ? '<span class="dashicons dashicons-yes"></span>' : '&ndash;'; ?></td>
				</tr>
			<?php endif; ?>
			<?php if ( $environment['mysql_version'] ) : ?>
				<tr>
					<td data-export-label="MySQL Version"><?php esc_html_e( 'MySQL version', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The version of MySQL installed on your hosting server.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td>
						<?php
						if ( version_compare( $environment['mysql_version'], '5.6', '<' ) && ! strstr( $environment['mysql_version_string'], 'MariaDB' ) ) {
							/* Translators: %1$s: MySQL version, %2$s: Recommended MySQL version. */
							echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( '%1$s - We recommend a minimum MySQL version of 5.6. See: %2$s', 'card-oracle' ), esc_html( $environment['mysql_version_string'] ), '<a href="https://wordpress.org/about/requirements/" target="_blank">' . esc_html__( 'WordPress requirements', 'card-oracle' ) . '</a>' ) . '</mark>';
						} else {
							echo '<mark class="yes">' . esc_html( $environment['mysql_version_string'] ) . '</mark>';
						}
						?>
					</td>
				</tr>
			<?php endif; ?>
			<tr>
				<td data-export-label="Max Upload Size"><?php esc_html_e( 'Max upload size', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The largest filesize that can be uploaded to your WordPress installation.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( size_format( $environment['max_upload_size'] ) ); ?></td>
			</tr>
			<tr>
				<td data-export-label="DOMDocument"><?php esc_html_e( 'DOMDocument', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'HTML/Multipart emails use DOMDocument to generate inline CSS in templates.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( $environment['domdocument_enabled'] ) {
						echo '<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>';
					} else {
						/* Translators: %s: classname and link. */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( 'Your server does not have the %s class enabled - HTML/Multipart emails, and also some extensions, will not work without DOMDocument.', 'card-oracle' ), '<a href="https://php.net/manual/en/class.domdocument.php">DOMDocument</a>' ) . '</mark>';
					}
					?>
				</td>
			</tr>
			<tr>
				<td data-export-label="Multibyte String"><?php esc_html_e( 'Multibyte string', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Multibyte String (mbstring) is used to convert character encoding, like for emails or converting characters to lowercase.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( $environment['mbstring_enabled'] ) {
						echo '<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>';
					} else {
						/* Translators: %s: classname and link. */
						echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . sprintf( esc_html__( 'Your server does not support the %s functions - this is required for better character encoding. Some fallbacks will be used instead for it.', 'card-oracle' ), '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>' ) . '</mark>';
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Image Processing"><h2><?php esc_html_e( 'Image Processing', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<?php
			$img_upload_dir = wp_upload_dir();
			$img_gd         = extension_loaded( 'gd' );
			$img_imagick    = extension_loaded( 'imagick' );
			$img_gd_jpeg    = false;
			if ( $img_gd ) {
				$gd_info     = gd_info();
				$img_gd_jpeg = ! empty( $gd_info['JPEG Support'] );
			}
			?>
			<tr>
				<td data-export-label="Uploads Directory"><?php esc_html_e( 'Uploads directory', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The path where WordPress stores uploaded media files.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><code><?php echo esc_html( $img_upload_dir['basedir'] ); ?></code></td>
			</tr>
			<tr>
				<td data-export-label="Uploads URL"><?php esc_html_e( 'Uploads URL', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The web URL for the uploads directory.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><code><?php echo esc_html( $img_upload_dir['baseurl'] ); ?></code></td>
			</tr>
			<tr>
				<td data-export-label="Uploads Writable"><?php esc_html_e( 'Uploads writable', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Whether the uploads directory is writable by the web server.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( is_writable( $img_upload_dir['basedir'] ) ) : // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- diagnostic-only check on Status tab. ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'Not writable', 'card-oracle' ); ?></mark>
					<?php endif; ?>
				</td>
			</tr>
			<?php if ( ! empty( $img_upload_dir['error'] ) ) : ?>
			<tr>
				<td data-export-label="Uploads Error"><?php esc_html_e( 'Uploads error', 'card-oracle' ); ?>:</td>
				<td class="help-tip">&nbsp;</td>
				<td><mark class="error"><span class="dashicons dashicons-warning"></span> <?php echo esc_html( $img_upload_dir['error'] ); ?></mark></td>
			</tr>
			<?php endif; ?>
			<tr>
				<td data-export-label="GD Library"><?php esc_html_e( 'GD library', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'PHP image processing library used by WordPress to resize and crop images.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $img_gd ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'Not installed', 'card-oracle' ); ?></mark>
					<?php endif; ?>
				</td>
			</tr>
			<?php if ( $img_gd ) : ?>
			<tr>
				<td data-export-label="GD JPEG Support"><?php esc_html_e( 'GD JPEG support', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Whether GD can process JPEG images. Required for card image imports.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $img_gd_jpeg ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'Not supported', 'card-oracle' ); ?></mark>
					<?php endif; ?>
				</td>
			</tr>
			<?php endif; ?>
			<tr>
				<td data-export-label="Imagick"><?php esc_html_e( 'Imagick', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Alternative PHP image processing library. WordPress prefers Imagick over GD when available.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $img_imagick ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="no">&ndash;</mark>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<td data-export-label="WP Image Editor"><?php esc_html_e( 'WordPress image editor', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Whether WordPress has a working image editor for thumbnail generation.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( wp_image_editor_supports() ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'No supported image editor found', 'card-oracle' ); ?></mark>
					<?php endif; ?>
				</td>
			</tr>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Performance"><h2><?php esc_html_e( 'Performance', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="PHP Memory Current Usage"><?php esc_html_e( 'PHP memory current usage', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Current memory usage of this PHP process.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $memory_usage['current_usage'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="PHP Memory Peak Usage"><?php esc_html_e( 'PHP memory peak usage', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Peak memory usage of this PHP process.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $memory_usage['peak_usage'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="OPcache Status"><?php esc_html_e( 'OPcache status', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Status of PHP OPcache for improved performance.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( isset( $opcache_status['enabled'] ) ) {
						if ( $opcache_status['enabled'] === 'Yes' ) {
							echo '<mark class="yes"><span class="dashicons dashicons-yes"></span> ' . esc_html__( 'Enabled', 'card-oracle' ) . '</mark>';
							if ( isset( $opcache_status['cache_full'] ) && $opcache_status['cache_full'] === 'Yes' ) {
								echo '<br><mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Cache is full', 'card-oracle' ) . '</mark>';
							}
							if ( isset( $opcache_status['restart_pending'] ) && $opcache_status['restart_pending'] === 'Yes' ) {
								echo '<br><mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Restart pending', 'card-oracle' ) . '</mark>';
							}
						} else {
							echo '<mark class="no">' . esc_html( $opcache_status['enabled'] ) . '</mark>';
						}
					} else {
						echo '<mark class="no">' . esc_html__( 'N/A', 'card-oracle' ) . '</mark>';
					}
					?>
				</td>
			</tr>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="WordPress Constants"><h2><?php esc_html_e( 'WordPress Constants', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<?php foreach ( $wp_constants as $constant_name => $constant_value ) : ?>
				<tr>
					<td data-export-label="<?php echo esc_attr( $constant_name ); ?>"><?php echo esc_html( $constant_name ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'WordPress configuration constant.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td>
						<?php
						if ( is_bool( $constant_value ) ) {
							echo $constant_value ? '<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>' : '<mark class="no">&ndash;</mark>';
						} else {
							echo esc_html( $constant_value );
						}
						?>
					</td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Security"><h2><?php esc_html_e( 'Security', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="Secure connection (HTTPS)"><?php esc_html_e( 'Secure connection (HTTPS)', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Is the connection secure?', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $environment['secure_connection'] ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span>
						<?php
						/* Translators: %s: docs link. */
						echo wp_kses_post( sprintf( __( 'Your site is not using HTTPS. <a href="%s" target="_blank">Learn more about HTTPS and SSL Certificates</a>.', 'card-oracle' ), 'https://blog.hubspot.com/marketing/what-is-ssl' ) );
						?>
						</mark>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<td data-export-label="Hide errors from visitors"><?php esc_html_e( 'Hide errors from visitors', 'card-oracle' ); ?></td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Error messages can contain sensitive information about your site. These should be hidden from untrusted visitors.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php if ( $environment['hide_errors'] ) : ?>
						<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>
					<?php else : ?>
						<mark class="error"><span class="dashicons dashicons-warning"></span><?php esc_html_e( 'Error messages should not be shown to visitors.', 'card-oracle' ); ?></mark>
					<?php endif; ?>
				</td>
			</tr>
		</tbody>
	</table>
	<?php if ( $card_oracle_options ) : ?>
		<table class="card_oracle_status_table widefat">
			<thead>
				<tr>
					<th colspan="3" data-export-label="Card Oracle Options"><h2><?php esc_html_e( 'Card Oracle Options', 'card-oracle' ); ?></h2></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach ( $card_oracle_options as $name => $value ) { ?>
				<?php if ( '_key' === substr( $name, -4, 4 ) ) : ?>
					<tr>
						<td><?php echo esc_html( $name ); ?></td>
						<td class="card-oracle-help-blank">&nbsp;</td>
						<td><?php echo $value ? '*' . esc_html( substr( $value, -4 ) ) . ' <strong style="color:red;">' . esc_html__( '(Last 4 characters)', 'card-oracle' ) . '</strong>' : '&ndash;'; ?></td>
					</tr>
				<?php else : ?>
					<tr>
						<td><?php echo esc_html( $name ); ?></td>
						<td class="card-oracle-help-blank">&nbsp;</td>
						<td><?php echo $value ? esc_html( $value ) : '&ndash;'; ?></td>
					</tr>
				<?php endif; ?>

			<?php } ?>
			</tbody>
		</table>
	<?php endif; ?>
	<?php if ( $post_type_counts ) : ?>
		<table class="card_oracle_status_table widefat">
			<thead>
			<tr>
				<th colspan="3" data-export-label="Post Type Counts"><h2><?php esc_html_e( 'Post Type Counts', 'card-oracle' ); ?></h2></th>
			</tr>
			</thead>
			<tbody>
			<?php foreach ( $post_type_counts as $post_type_count ) { ?>
				<tr>
					<td><?php echo esc_html( $post_type_count->type ); ?></td>
					<td class="card-oracle-help-blank">&nbsp;</td>
					<td><?php echo absint( $post_type_count->count ); ?></td>
				</tr>
			<?php } ?>
			</tbody>
		</table>
	<?php endif; ?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Active Plugins (<?php echo count( $active_plugins ); ?>)"><h2><?php esc_html_e( 'Active plugins', 'card-oracle' ); ?> (<?php echo count( $active_plugins ); ?>)</h2></th>
			</tr>
		</thead>
		<tbody>
			<?php CardOracleAdminStatus::output_plugins_info( $active_plugins ); ?>
		</tbody>
	</table>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th colspan="3" data-export-label="Inactive Plugins (<?php echo count( $inactive_plugins ); ?>)"><h2><?php esc_html_e( 'Inactive plugins', 'card-oracle' ); ?> (<?php echo count( $inactive_plugins ); ?>)</h2></th>
			</tr>
		</thead>
		<tbody>
			<?php CardOracleAdminStatus::output_plugins_info( $inactive_plugins ); ?>
		</tbody>
	</table>
	<?php
	if ( 0 < count( $dropins_mu_plugins['dropins'] ) ) :
		?>
		<table class="card_oracle_status_table widefat">
			<thead>
				<tr>
					<th colspan="3" data-export-label="Dropin Plugins (<?php echo count( $dropins_mu_plugins['dropins'] ); ?>)"><h2><?php esc_html_e( 'Dropin Plugins', 'card-oracle' ); ?> (<?php echo count( $dropins_mu_plugins['dropins'] ); ?>)</h2></th>
				</tr>
			</thead>
			<tbody>
				<?php
				foreach ( $dropins_mu_plugins['dropins'] as $dropin ) {
					?>
					<tr>
						<td><?php echo wp_kses_post( $dropin['plugin'] ); ?></td>
						<td class="card-oracle-help-blank">&nbsp;</td>
						<td><?php echo wp_kses_post( $dropin['name'] ); ?>
					</tr>
					<?php
				}
				?>
			</tbody>
		</table>
		<?php
	endif;
	if ( 0 < count( $dropins_mu_plugins['mu_plugins'] ) ) :
		?>
		<table class="card_oracle_status_table widefat">
			<thead>
				<tr>
					<th colspan="3" data-export-label="Must Use Plugins (<?php echo count( $dropins_mu_plugins['mu_plugins'] ); ?>)"><h2><?php esc_html_e( 'Must Use Plugins', 'card-oracle' ); ?> (<?php echo count( $dropins_mu_plugins['mu_plugins'] ); ?>)</h2></th>
				</tr>
			</thead>
			<tbody>
				<?php
				foreach ( $dropins_mu_plugins['mu_plugins'] as $mu_plugin ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
					$plugin_name = esc_html( $mu_plugin['name'] );
					if ( ! empty( $mu_plugin['url'] ) ) {
						$plugin_name = '<a href="' . esc_url( $mu_plugin['url'] ) . '" aria-label="' . esc_attr__( 'Visit plugin homepage', 'card-oracle' ) . '" target="_blank">' . $plugin_name . '</a>';
					}
					?>
					<tr>
						<td><?php echo wp_kses_post( $plugin_name ); ?></td>
						<td class="card-oracle-help-blank">&nbsp;</td>
						<td>
						<?php
							/* translators: %s: plugin author */
							printf( esc_html__( 'by %s', 'card-oracle' ), esc_html( $mu_plugin['author_name'] ) );
							echo ' &ndash; ' . esc_html( $mu_plugin['version'] );
						?>
					</tr>
					<?php
				}
				?>
			</tbody>
		</table>
	<?php endif; ?>
	<table class="card_oracle_status_table widefat" >
		<thead>
			<tr>
				<th colspan="3" data-export-label="Theme"><h2><?php esc_html_e( 'Theme', 'card-oracle' ); ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td data-export-label="Name"><?php esc_html_e( 'Name', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The name of the current active theme.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $theme['name'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Version"><?php esc_html_e( 'Version', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The installed version of the current active theme.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $theme['version'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Author URL"><?php esc_html_e( 'Author URL', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The theme developers URL.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td><?php echo esc_html( $theme['author_url'] ); ?></td>
			</tr>
			<tr>
				<td data-export-label="Child Theme"><?php esc_html_e( 'Child theme', 'card-oracle' ); ?>:</td>
				<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'Displays whether or not the current theme is a child theme.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
				<td>
					<?php
					if ( $theme['is_child_theme'] ) {
						echo '<mark class="yes"><span class="dashicons dashicons-yes"></span></mark>';
					} else {
						/* Translators: %s docs link. */
						echo '<span class="dashicons dashicons-no-alt"></span> &ndash; ' . wp_kses_post( sprintf( __( 'If you are thinking about modifying a parent theme, we recommend using a child theme. See: <a href="%s" target="_blank">How to create a child theme</a>', 'card-oracle' ), 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ) );
					}
					?>
					</td>
			</tr>
			<?php if ( $theme['is_child_theme'] ) : ?>
				<tr>
					<td data-export-label="Parent Theme Name"><?php esc_html_e( 'Parent theme name', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The name of the parent theme.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $theme['parent_name'] ); ?></td>
				</tr>
				<tr>
					<td data-export-label="Parent Theme Version"><?php esc_html_e( 'Parent theme version', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The installed version of the parent theme.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td>
						<?php
						echo esc_html( $theme['parent_version'] );
						if ( version_compare( $theme['parent_version'], $theme['parent_version_latest'], '<' ) ) {
							/* translators: %s: parent theme latest version */
							echo ' &ndash; <strong style="color:red;">' . sprintf( esc_html__( '%s is available', 'card-oracle' ), esc_html( $theme['parent_version_latest'] ) ) . '</strong>';
						}
						?>
					</td>
				</tr>
				<tr>
					<td data-export-label="Parent Theme Author URL"><?php esc_html_e( 'Parent theme author URL', 'card-oracle' ); ?>:</td>
					<td class="help-tip"><?php echo card_oracle_help_tip( esc_html__( 'The parent theme developers URL.', 'card-oracle' ) ); /* phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped */ ?></td>
					<td><?php echo esc_html( $theme['parent_author_url'] ); ?></td>
				</tr>
			<?php endif; ?>
		</tbody>
	</table>
	<?php if ( $md5_files ) : ?>
		<table class="card_oracle_status_table widefat">
			<thead>
				<tr>
					<th colspan="3" data-export-label="Card Oracle MD5 Sums"><h2><?php esc_html_e( 'Card Oracle MD5 Sums', 'card-oracle' ); ?></h2></th>
				</tr>
			</thead>
			<tbody>
			<?php foreach ( $md5_files as $md5_file ) { ?>
				<?php $md5 = md5_file( $md5_file['filename'] ); ?>
				<tr>
					<td><?php echo esc_html( $md5_file['name'] ); ?></td>
					<td class="card-oracle-help-blank">&nbsp;</td>
					<?php if ( strcmp( $md5, $md5_file['md5sum'] ) !== 0 ) : ?>
						<td style="color:red;"><?php echo esc_html( $md5 ); ?></td>
					<?php else : ?>
						<td><?php echo esc_html( $md5 ); ?></td>
					<?php endif; ?>
				</tr>
			<?php } ?>
		</tbody>
	</table>
	<?php endif; ?>
	<?php if ( $error_log_counts > 0 ) : ?>
		<table class="card_oracle_status_table widefat">
			<thead>
				<tr>
					<th colspan="3" data-export-label="Error Logs (<?php echo esc_attr( $error_log_counts ); ?>)"><h2><?php esc_html_e( 'Error Logs', 'card-oracle' ); ?> (<?php echo esc_attr( $error_log_counts ); ?>)</h2></th>
				</tr>
			</thead>
			<tbody>
				<?php
				$total_rows = $error_log_rows > $error_log_counts ? $error_log_counts : $error_log_rows;
				$logs       = $co_logs->get_connected_logs(
					array(
						'posts_per_page' => $total_rows,
						'log_type'       => 'error',
					)
				);

				if ( $logs ) {
					echo '<tr><td>' . esc_html__( 'Date', 'card-oracle' ) . '</td>';
					echo '<td>' . esc_html__( 'Title', 'card-oracle' ) . '</td>';
					echo '<td>' . esc_html__( 'Message', 'card-oracle' ) . '</td></tr>';
					foreach ( $logs as $log ) {
						?>
					<tr>
						<td><?php echo wp_kses_post( date_i18n( 'Y-M-d G:i:s', strtotime( $log->post_date ) ) ); ?></td>
						<td><?php echo wp_kses_post( $log->post_title ); ?></td>
						<td><?php echo wp_kses_post( $log->post_content ); ?></td>
					</tr>
						<?php
					}
				}
				?>
			</tbody>
			<tfoot>
				<tr>
					<td colspan="2" >
						<form action="" method="post">
							<input type="hidden" value="true" name="co_clear_logs_button" />
							<?php wp_nonce_field( 'co_clear_logs_button' ); ?>
							<?php submit_button( esc_attr( __( 'Clear Error Logs', 'card-oracle' ) ), 'primary', '', '' ); ?>
						</form>
					</td>
					<td style="text-align: right;">
						<form action="" method="post" id="error_rows_form" autocomplete="off">
						<?php wp_nonce_field( 'co_error_rows' ); ?>
						<span><?php esc_html_e( 'Rows to display:', 'card-oracle' ); ?></span>&nbsp;
						<select id="error_rows" name="error_rows">
							<?php
							$numrows_arr = array( '5', '10', '25', '50' );
							foreach ( $numrows_arr as $nrow ) {
								$selected = $error_log_rows === $nrow ? 'selected' : '';

								echo '<option value="' . esc_attr( $nrow ) . '" ' . esc_attr( $selected ) . '>' . esc_attr( $nrow ) . '</option>';
							}
							?>
							</select>
						</form>
					</td>
				</tr>
			</tfoot>
		</table>
	<?php endif; ?>
	<?php
	// Show Card Oracle debug log if it exists.
	$co_log_file = $co_logs->get_log_file_path();
	if ( $co_log_file && file_exists( $co_log_file ) && filesize( $co_log_file ) > 0 ) :
		?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th id="card-oracle-log-header" colspan="3" data-export-label="Card Oracle Log">
					<h2><?php echo esc_html__( 'Card Oracle Debug Log', 'card-oracle' ); ?></h2>
					<p class="description"><?php echo esc_html( basename( $co_log_file ) ); ?></p>
				</th>
			</tr>
		</thead>
		<tbody>
			<tr><td>
			<?php
			// Parse and display the log file (last 100 lines).
			$log_content = $co_logs->parse_log( $co_log_file, 100, 512 );
			$log_content = nl2br( esc_html( trim( $log_content ) ) );
			echo '<p class="codeblock"><code>' . wp_kses_post( $log_content ) . '</code></p>';
			?>
			</td></tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="3">
					<form action="" method="post">
						<input type="hidden" value="true" name="co_clear_debug_log_button" />
						<?php wp_nonce_field( 'co_clear_debug_log_button' ); ?>
						<?php submit_button( esc_attr( __( 'Clear Debug Log', 'card-oracle' ) ), 'secondary', '', false ); ?>
					</form>
				</td>
			</tr>
		</tfoot>
	</table>
	<?php endif; ?>
	<?php if ( $co_logs->has_debug_constant() && $co_logs->check_file_data() ) : ?>
	<table class="card_oracle_status_table widefat">
		<thead>
			<tr>
				<th id="card-oracle-debug-header" colspan="3" data-export-label="Debug Log"><h2><?php echo esc_html__( 'WordPress Debug Log', 'card-oracle' ) . ' (' . esc_attr( $environment['log_file'] ) . ')'; ?></h2></th>
			</tr>
		</thead>
		<tbody>
			<tr><td>
			<?php $co_logs->view_log( $environment['log_file'] ); ?>
			</td></tr>
		</tbody>
	</table>
	<?php endif; ?>
			</div>
		</section>
	</div>
<?php } ?>
