<?php

/**
 * Provide a admin area view for the sitemap settings
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://www.cspgeeks.com
 * @since      1.0.3
 *
 * @package    Basic_SEO_Helper
 * @subpackage Basic_SEO_Helper/admin/partials
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variables are scoped to this included file, not global scope

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

$sitemap_settings = get_option( 'cspgbase_sitemap_settings', array() );

// Handle form submission
if ( isset( $_POST['submit'] ) && isset( $_POST['basic_seo_helper_sitemap_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['basic_seo_helper_sitemap_nonce'] ) ), 'basic_seo_helper_sitemap_settings' ) ) {
	$sitemap_settings = array(
		'enabled'                    => isset( $_POST['sitemap_enabled'] ) ? '1' : '0',
		'include_post_types'         => isset( $_POST['include_post_types'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['include_post_types'] ) ) : array(),
		'max_entries'                => isset( $_POST['max_entries'] ) ? intval( wp_unslash( $_POST['max_entries'] ) ) : 50000,
		'exclude_private'            => isset( $_POST['exclude_private'] ) ? '1' : '0',
		'exclude_password_protected' => isset( $_POST['exclude_password_protected'] ) ? '1' : '0',
	);

	update_option( 'cspgbase_sitemap_settings', $sitemap_settings );
	echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Sitemap settings saved successfully!', 'cspg-basic-seo-helper' ) . '</p></div>';
}

// Get all public post types
$post_types = get_post_types( array( 'public' => true ), 'objects' );

// Default settings
$defaults = array(
	'enabled'                    => '1',
	'include_post_types'         => array( 'post', 'page' ),
	'max_entries'                => 1000,
	'exclude_private'            => '1',
	'exclude_password_protected' => '1',
);

$sitemap_settings = wp_parse_args( $sitemap_settings, $defaults );
?>

<div class="wrap">
	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
	
	<div class="basic-seo-helper-admin-wrapper">
		<!-- Sitemap Settings -->
		<div class="postbox">
			<div class="postbox-header">
				<h2 class="hndle"><?php esc_html_e( 'Sitemap Settings', 'cspg-basic-seo-helper' ); ?></h2>
			</div>
			<div class="inside">
				<p><?php esc_html_e( 'Configure your XML sitemap settings to help search engines discover and index your content effectively.', 'cspg-basic-seo-helper' ); ?></p>
				
				<form method="post" action="">
					<?php wp_nonce_field( 'basic_seo_helper_sitemap_settings', 'basic_seo_helper_sitemap_nonce' ); ?>
					
					<table class="form-table">
						<tr>
							<th scope="row">
								<?php esc_html_e( 'Enable Sitemap', 'cspg-basic-seo-helper' ); ?>
							</th>
							<td>
								<label for="sitemap_enabled">
									<input type="checkbox" id="sitemap_enabled" name="sitemap_enabled" value="1" <?php checked( $sitemap_settings['enabled'], '1' ); ?> />
									<?php esc_html_e( 'Generate XML sitemap', 'cspg-basic-seo-helper' ); ?>
								</label>
								<p class="description"><?php esc_html_e( 'Enable or disable the XML sitemap generation.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="sitemap_url"><?php esc_html_e( 'Sitemap URL', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<input type="text" id="sitemap_url" value="<?php echo esc_url( home_url( '/sitemap.xml' ) ); ?>" class="large-text" readonly />
								<p class="description">
									<?php esc_html_e( 'Your sitemap will be available at this URL.', 'cspg-basic-seo-helper' ); ?>
									<a href="<?php echo esc_url( home_url( '/sitemap.xml' ) ); ?>" target="_blank" class="button button-secondary" style="margin-left: 10px;">
										<?php esc_html_e( 'View Sitemap', 'cspg-basic-seo-helper' ); ?>
									</a>
								</p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<?php esc_html_e( 'Include Post Types', 'cspg-basic-seo-helper' ); ?>
							</th>
							<td>
								<fieldset>
									<legend class="screen-reader-text"><?php esc_html_e( 'Select post types to include in sitemap', 'cspg-basic-seo-helper' ); ?></legend>
									<?php foreach ( $post_types as $post_type ) : ?>
										<?php if ( 'attachment' !== $post_type->name ) : // Skip attachments ?>
											<label for="include_post_type_<?php echo esc_attr( $post_type->name ); ?>">
												<input type="checkbox" 
														id="include_post_type_<?php echo esc_attr( $post_type->name ); ?>" 
														name="include_post_types[]" 
														value="<?php echo esc_attr( $post_type->name ); ?>"
												<?php checked( in_array( $post_type->name, $sitemap_settings['include_post_types'], true ) ); ?> />
												<?php echo esc_html( $post_type->label ); ?> 
												<code>(<?php echo esc_html( $post_type->name ); ?>)</code>
											</label><br />
										<?php endif; ?>
									<?php endforeach; ?>
									<p class="description"><?php esc_html_e( 'Select which post types to include in your sitemap.', 'cspg-basic-seo-helper' ); ?></p>
								</fieldset>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="max_entries"><?php esc_html_e( 'Maximum Entries', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<input type="number" id="max_entries" name="max_entries" value="<?php echo esc_attr( $sitemap_settings['max_entries'] ); ?>" min="1" max="50000" class="small-text" />
								<p class="description"><?php esc_html_e( 'Maximum number of entries to include in the sitemap. Recommended: 1000-10000.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<?php esc_html_e( 'Exclusions', 'cspg-basic-seo-helper' ); ?>
							</th>
							<td>
								<fieldset>
									<legend class="screen-reader-text"><?php esc_html_e( 'Content exclusion options', 'cspg-basic-seo-helper' ); ?></legend>
									
									<label for="exclude_private">
										<input type="checkbox" id="exclude_private" name="exclude_private" value="1" <?php checked( $sitemap_settings['exclude_private'], '1' ); ?> />
										<?php esc_html_e( 'Exclude private posts', 'cspg-basic-seo-helper' ); ?>
									</label><br />
									
									<label for="exclude_password_protected">
										<input type="checkbox" id="exclude_password_protected" name="exclude_password_protected" value="1" <?php checked( $sitemap_settings['exclude_password_protected'], '1' ); ?> />
										<?php esc_html_e( 'Exclude password-protected posts', 'cspg-basic-seo-helper' ); ?>
									</label><br />
									
									<p class="description"><?php esc_html_e( 'Choose which types of content to exclude from your sitemap.', 'cspg-basic-seo-helper' ); ?></p>
								</fieldset>
							</td>
						</tr>
					</table>
					
					<?php submit_button(); ?>
				</form>
			</div>
		</div>
		
		<!-- Sitemap Guide -->
		<div class="bsh-seo-elements-guide">
			<h3><span class="bsh-guide-icon">🗺️</span><?php esc_html_e( 'Sitemap Guide', 'cspg-basic-seo-helper' ); ?></h3>
			<div class="bsh-guide-wrapper">
				<div class="bsh-guide-columns">
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">📋</span><?php esc_html_e( 'What is an XML Sitemap?', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'What It Is:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'An XML sitemap is a structured file that lists all the important pages, posts, and content on your website. It serves as a roadmap for search engines to discover and index your content.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Key Benefits:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Helps search engines find all your pages', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Speeds up content discovery and indexing', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Indicates page priority and update frequency', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Improves SEO for large or complex sites', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Your sitemap is automatically generated and available at /sitemap.xml. Configure which content types to include and set entry limits above.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">⚙️</span><?php esc_html_e( 'Configuration Best Practices', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Post Types to Include:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Always include: Posts and Pages (main content)', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Consider: Public custom post types', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Exclude: Attachments, private content, admin-only types', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Entry Limits by Site Size:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Small sites (< 500 pages): 500-1000 entries', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Medium sites (500-5000 pages): 1000-5000 entries', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Large sites (> 5000 pages): 5000-10000 entries', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Content Exclusions:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Always exclude private posts and password-protected content as they\'re not accessible to search engines.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">📊</span><?php esc_html_e( 'Sitemap Statistics', 'cspg-basic-seo-helper' ); ?></h4>
							
							<?php if ( '1' === $sitemap_settings['enabled'] ) : ?>
								<?php
								$total_posts      = 0;
								$post_type_counts = array();
								foreach ( $sitemap_settings['include_post_types'] as $post_type ) {
									$count = wp_count_posts( $post_type );
									if ( isset( $count->publish ) ) {
										$type_count                     = $count->publish;
										$total_posts                   += $type_count;
										$post_type_obj                  = get_post_type_object( $post_type );
										$post_type_counts[ $post_type ] = array(
											'count' => $type_count,
											'label' => $post_type_obj ? $post_type_obj->label : ucfirst( $post_type ),
										);
									}
								}
								$capped_entries = min( $total_posts, $sitemap_settings['max_entries'] );
								?>
								
								<p><strong><?php esc_html_e( 'Current Statistics:', 'cspg-basic-seo-helper' ); ?></strong><br>
								• <?php esc_html_e( 'Total Entries:', 'cspg-basic-seo-helper' ); ?> <?php echo esc_html( number_format( $capped_entries ) ); ?><br>
								• <?php esc_html_e( 'Post Types:', 'cspg-basic-seo-helper' ); ?> <?php echo esc_html( count( $sitemap_settings['include_post_types'] ) ); ?><br>
								• <?php esc_html_e( 'Max Limit:', 'cspg-basic-seo-helper' ); ?> <?php echo esc_html( number_format( $sitemap_settings['max_entries'] ) ); ?></p>

								<p><strong><?php esc_html_e( 'Content Breakdown:', 'cspg-basic-seo-helper' ); ?></strong><br>
								<?php foreach ( $post_type_counts as $post_type => $data ) : ?>
									• <?php echo esc_html( $data['label'] ); ?>: <?php echo esc_html( number_format( $data['count'] ) ); ?><br>
								<?php endforeach; ?></p>

								<p><strong><?php esc_html_e( 'Sitemap URL:', 'cspg-basic-seo-helper' ); ?></strong><br>
								<a href="<?php echo esc_url( home_url( '/sitemap.xml' ) ); ?>" target="_blank"><?php echo esc_url( home_url( '/sitemap.xml' ) ); ?></a></p>

								<?php if ( $total_posts > $sitemap_settings['max_entries'] ) : ?>
									<p><strong><?php esc_html_e( 'Note:', 'cspg-basic-seo-helper' ); ?></strong><br>
									<?php
									printf(
										/* translators: %1$s is the total number of entries, %2$s is the maximum entries limit */
										esc_html__( 'Your site has %1$s total entries, but your sitemap is limited to %2$s. Consider increasing the limit for better coverage.', 'cspg-basic-seo-helper' ),
										number_format( $total_posts ),
										number_format( $sitemap_settings['max_entries'] )
									);
									?>
									</p>
								<?php endif; ?>

							<?php else : ?>
								<p><strong><?php esc_html_e( 'Sitemap Status:', 'cspg-basic-seo-helper' ); ?></strong><br>
								<?php esc_html_e( 'Your XML sitemap is currently disabled. Enable it above to start generating your sitemap and improve your SEO.', 'cspg-basic-seo-helper' ); ?></p>
							<?php endif; ?>
						</div>
					</div>
					
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">📤</span><?php esc_html_e( 'How to Submit Your Sitemap', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Google Search Console:', 'cspg-basic-seo-helper' ); ?></strong><br>
							1. <?php esc_html_e( 'Go to search.google.com/search-console', 'cspg-basic-seo-helper' ); ?><br>
							2. <?php esc_html_e( 'Verify your website ownership', 'cspg-basic-seo-helper' ); ?><br>
							3. <?php esc_html_e( 'Navigate to Sitemaps in the left sidebar', 'cspg-basic-seo-helper' ); ?><br>
							4. <?php esc_html_e( 'Enter "sitemap.xml" and click Submit', 'cspg-basic-seo-helper' ); ?><br>
							5. <?php esc_html_e( 'Monitor for errors in the Sitemaps report', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Bing Webmaster Tools:', 'cspg-basic-seo-helper' ); ?></strong><br>
							1. <?php esc_html_e( 'Visit bing.com/webmasters', 'cspg-basic-seo-helper' ); ?><br>
							2. <?php esc_html_e( 'Verify your site ownership', 'cspg-basic-seo-helper' ); ?><br>
							3. <?php esc_html_e( 'Go to Configure → Sitemaps', 'cspg-basic-seo-helper' ); ?><br>
							4. <?php esc_html_e( 'Submit your full sitemap URL', 'cspg-basic-seo-helper' ); ?><br>
							5. <?php esc_html_e( 'Monitor indexing progress', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Once submitted, your sitemap will be automatically updated as you publish new content. Check search console regularly for any issues.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">📈</span><?php esc_html_e( 'Monitoring Your Sitemap', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Weekly Checks:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Review Google Search Console for sitemap errors', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Check the Coverage report for indexing status', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Monitor for "Sitemap could not be read" warnings', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Verify new content appears in search results', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Performance Tips:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Update settings when adding new post types', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Consider lower entry limits for faster generation', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Use caching plugins to optimize sitemap delivery', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Re-submit after making major site changes', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'In Basic SEO Helper:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Your sitemap automatically updates when content changes. The statistics above help you monitor coverage and performance.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🔧</span><?php esc_html_e( 'Troubleshooting Common Issues', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Sitemap Not Accessible:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Check if sitemap is enabled above', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Verify permalinks are not set to "Plain"', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Clear caching plugins and try again', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Check for .htaccess conflicts', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Missing Pages:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Ensure post type is selected in settings', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Check if content is published (not draft)', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Verify max entries limit isn\'t reached', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Review exclusion settings', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Search Console Errors:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Re-submit sitemap after changes', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Check for broken URLs or redirects', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Wait 24-48 hours for processing', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Ensure sitemap is accessible to Googlebot', 'cspg-basic-seo-helper' ); ?></p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

