<?php

require_once 'rover-templates.php';
require_once 'rover-panel-lists.php';
require_once __DIR__ . '/../rover-sitemap.php';

class Rover_IDX_ADMINSEO {

	private Rover_IDX $rover_idx;

	public function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx						= $rover_idx;
		}

	public function roveridx_panel_seo_form() {

		if (!is_array($seo_options = get_option(roverOPTIONS::SEO)))
			$seo_options						= array();

		$rover_content							= $this->rover_idx->CONTENT->rover_content(
																	'ROVER_COMPONENT_SEO_PANEL',
																	array(
																		'region'	=> $this->rover_idx->get_first_region(),
																		'regions'	=> implode(',', array_keys($this->rover_idx->all_selected_regions)),
																		'settings'	=> $seo_options
																		)
																	);

		$TEMPLATES								= new Rover_IDX_ADMINTEMPLATES($this->rover_idx);
		?>
		<div class="wrap" data-page="rover-panel-seo">

			<?php echo $TEMPLATES->roveridx_panel_header();	?>

			<div id="rover-seo-panel">
				<?php echo $rover_content['the_html']; ?>

				<div class="rover-site-pages" style="display:none;"><?php echo $this->roveridx_site_pages(); ?></div>
				<input type="hidden" id="sitemap_enabled_in_options" value="<?php echo $this->roveridx_sitemap_enabled(); ?>">
				<input type="hidden" id="sitemap_updated" value="<?php echo $this->roveridx_sitemap_updated(); ?>">
				<input type="hidden" id="redirect_crawler" value="<?php echo $this->roveridx_crawler_redirect(); ?>">

				<?php echo $TEMPLATES->roveridx_panel_footer($panel = 'seo');	?>

			</div>

		</div><!-- wrap	-->

		<?php

		}


	private function roveridx_site_pages()
		{
		$the_html								= array();
		foreach ( get_pages(array('post_status' => 'publish')) as $page )
			{
			$the_html[]							= '<option value="' . get_page_link( $page->ID ) . '">'.$page->post_title.'</option>';
			}

		return implode('', $the_html);
		}

	private function roveridx_sitemap_enabled()
		{
		if (!is_array($seo_options = get_option(roverOPTIONS::SEO)))
			$seo_options		= array();
		$enabled								= (isset($seo_options['sitemap_enabled']) && ($seo_options['sitemap_enabled'] == 'disabled'))
														? 'disabled'
														: 'enabled';

		return $enabled;
		}

	private function roveridx_sitemap_updated()
		{
		if (!is_array($seo_options = get_option(roverOPTIONS::SEO)))
			$seo_options		= array();

		if ($this->roveridx_sitemap_enabled() === "enabled")
			{
			if (isset($seo_options['updated']) && !empty($seo_options['updated']))
				return $seo_options['updated'];
			}

		return null;
		}

	private function roveridx_crawler_redirect()
		{
		if (!is_array($seo_options = get_option(roverOPTIONS::SEO)))
			$seo_options					= array();

		$crawler_redirect						= (isset($seo_options['redirect_crawler']))
														? $seo_options['redirect_crawler']
														: 'home';

		return $crawler_redirect;
		}


	}


?>