<?php

class Rover_IDX_ADMINCALLBACKS
	{
	private Rover_IDX $rover_idx;

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

		#	G E N E R A L
		add_action('wp_ajax_rover_idx_save_setup',				[ $this, 'rover_idx_save_setup_callback']);
		add_action('wp_ajax_rover_idx_save_slug_excludes',		[ $this, 'rover_idx_save_slug_excludes_callback']);
		add_action('wp_ajax_rover_idx_save_style_settings',		[ $this, 'rover_idx_save_style_settings_callback']);
		add_action('wp_ajax_rover_idx_reset',					[ $this, 'rover_idx_reset_callback']);
		add_action('wp_ajax_rover_idx_quick_start_create',		[ $this, 'rover_idx_quick_start_create_callback']);
		add_action('wp_ajax_rover_idx_quick_start_info',		[ $this, 'rover_idx_quick_start_info_callback']);
		add_action('wp_ajax_rover_idx_quick_start_reset',		[ $this, 'rover_idx_quick_start_reset_callback']);

		add_action('wp_ajax_rover_idx_refresh_js_ver',			[ $this, 'rover_idx_refresh_js_ver_callback']);
		add_action('wp_ajax_rover_idx_show_settings',			[ $this, 'rover_idx_show_settings_callback']);

		#	C U S T O M I Z E
		add_action('wp_ajax_rover_idx_theme',					[ $this, 'rover_idx_theme_callback']);
		add_action('wp_ajax_rover_idx_fetch_theme_settings',	[ $this, 'rover_idx_fetch_theme_settings_callback']);
		add_action('wp_ajax_rover_idx_menu_add',				[ $this, 'rover_idx_menu_add_callback']);
		add_action('wp_ajax_rover_idx_menu_remove',				[ $this, 'rover_idx_menu_remove_callback']);
		add_action('wp_ajax_rover_idx_overwrite_theme_settings',[ $this, 'rover_idx_overwrite_theme_settings_callback']);


		#	S E O
		add_action('wp_ajax_rover_idx_seo',					[ $this, 'rover_idx_seo_callback']);
		add_action('wp_ajax_rover_idx_do_sitemap',			[ $this, 'rover_idx_do_sitemap_callback']);
		add_action('wp_ajax_rover_idx_sitemap_history',		[ $this, 'rover_idx_sitemap_history_callback']);
		add_action('wp_ajax_rover_idx_create_city_dynamic_definitions',			[ $this, 'rover_idx_create_city_dynamic_definitions_callback']);
		add_action('wp_ajax_rover_idx_create_subdivision_dynamic_definitions',	[ $this, 'rover_idx_create_subdivision_dynamic_definitions_callback']);

		#	S O C I A L
		add_action('wp_ajax_rover_idx_social',				[ $this, 'rover_idx_social_callback']);
		add_action('wp_ajax_rover_idx_refresh_social',		[ $this, 'rover_idx_refresh_social_callback']);
		}


	########################################################
	#	G E N E R A L
	########################################################

	public function rover_idx_save_setup_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

	#	error_log(
	#		sprintf( '[%s] [%s] DEBUG - starting - [%s]', __FUNCTION__, __LINE__, print_r($_POST, true))
	#		);

		if ( ! current_user_can('manage_options') )
			{
	#		error_log(
	#			sprintf( '[%s] [%s] DEBUG - cannot manage_options - [%s]', __FUNCTION__, __LINE__)
	#			);

			echo json_encode(array(
													'error'			=> 'cannot manage_options',
													'success'		=> false		//	Folks are getting confused when we say 'Settings were not updated'
													));

			die();
			}

		if (isset($_POST['reset']) && (isset($_POST['regions'])))
			{
	#		$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, ' resetting regions - '.$_POST['regions']);
	#		error_log(
	#			sprintf( '[%s] [%s] DEBUG - resetting regions - [%s]', __FUNCTION__, __LINE__, print_r($_POST['regions'], true))
	#			);

			if (isset($_POST['did']))
				$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'domain_id', intval($_POST['did']));

			$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'regions', $_POST['regions']);
			}
		else
			{
			if (isset($_POST['did']))
				$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'domain_id', intval($_POST['did']));

			if (count($this->rover_idx->all_selected_regions) == 0)
				$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'regions', $_POST['region']);
			else if (isset($_POST['region']))
				$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'regions', $_POST['region']);

			if (!is_array($region_options = get_option(roverOPTIONS::REGIONS)))
				$region_options						= array();

			if (isset($region_options['region']) && !isset($region_options['regions']))
				$this->rover_idx->update_region_settings(__FUNCTION__, __LINE__, 'regions', $region_options['region']);
			}

		$responseVar								= array(
	#														'region_data'	=> $_POST['region_data'],
															'success'		=> true		//	Folks are getting confused when we say 'Settings were not updated'
															);

		flush_rewrite_rules();

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_save_slug_excludes_callback()		{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return;

		if (empty($_POST['exclude']))
			{
			echo json_encode(array(
													'exclude'		=> null,
													'success'		=> false
													));
			}
		else
			{
			if (!is_array($rr = get_option(roverOPTIONS::REGIONS)))
				$rr									= array();
			$rr['exclude_slugs']					= sanitize_text_field( $_POST['exclude'] );

			$r										= update_option(roverOPTIONS::REGIONS, $rr);

			$responseVar							= array(
															'exclude'		=> $_POST['exclude'],
															'success'		=> true
															);

			flush_rewrite_rules();

			echo json_encode($responseVar);
			}

		die();
		}

	public function rover_idx_save_style_settings_callback()	{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('edit_posts') )
			return;

		if (!is_array($theme_options = get_option(roverOPTIONS::THEME)))
			$theme_options							= array();

		$one_key									= (isset($_POST['key']))
															? $_POST['key']
															: null;
		$one_val									= (isset($_POST['val']))
															? $_POST['val']
															: null;

		$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, ' key ['.$one_key.'] / val ['.$one_val.']');

		if (!is_null($one_key) && !is_null($one_val))
			{
			if (in_array($one_key, array('load_admin_bootstrap', 'load_emojis', 'load_google_api')))	/*	'Yes' / 'No' values	*/
				$theme_options[$one_key]			= $this->rover_idx->COMMON->validate_yes_no($one_val);
			else if (is_bool($one_val) || (is_string($one_val) && in_array($one_val, array('true', 'false'))))
				$theme_options[$one_key]			= $this->rover_idx->COMMON->validate_bool($one_val);
			else
				$theme_options[$one_key]			= sanitize_text_field($one_val);
			}
		else
			{
			foreach($_POST as $one_key => $one_val)
				{
				if (!in_array($one_key, array('action', 'security')))
					$theme_options[$one_key]		= sanitize_text_field($one_val);
				}
			}

		$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, ' post sanitize ['.$one_key.'] => ['.$theme_options[$one_key].']');

		$r											= update_option(roverOPTIONS::THEME, $theme_options);

		$responseVar								= array(
															'success'		=> true		//	Folks are getting confused when we say 'Settings were not updated'
															);

															echo json_encode($responseVar);

		die();
		}

	public function rover_idx_reset_callback(){

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return;

		roveridx_uninstall();

		echo json_encode(array('success'	=> true));

		die();
		}

	public function rover_idx_quick_start_create_callback()		{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return;

		$responseVar	= array(
								'html'				=> Rover_IDX_Database::create_quick_setup_pages($this->rover_idx, $_POST)
								);

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_quick_start_info_callback()	{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return;

		$responseVar	= array(
								'html'				=> Rover_IDX_Database::fetch_quick_setup_pages()
								);

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_quick_start_reset_callback()	{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return;

		$responseVar	= array(
								'html'				=> Rover_IDX_Database::reset_quick_setup_pages()
								);

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_refresh_js_ver_callback()	{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('edit_posts') )
			return;

		require_once __DIR__ . '/../rover-scheduled.php';

		$scheduled				= new Rover_IDX_Schedule($this->rover_idx);
		$responseVar	= array(
								'ver'				=> $scheduled->refresh_js_ver($force_refresh = true)
								);

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_show_settings_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( ! current_user_can('manage_options') )
			return json_encode(['html'	=> 'Apologies - you are not authorized to see this data']);

		$the_html[]				= '<div>';

		foreach(array(
					'Region'	=> roverOPTIONS::REGIONS,
					'Theme'		=> roverOPTIONS::THEME,
					'SEO'		=> roverOPTIONS::SEO,
					'Social'	=> roverOPTIONS::SOCIAL,
					)
				as $label => $option_key)
			{
			$the_html[]			=	'<div class="idx-row">';
			$the_html[]			=		'<div class="idx-col-12"><strong>'.$label.'</strong></div>';
			$the_html[]			=	'</div>';

			if (!is_array($opts = get_option($option_key)))
				$opts			= array();

			foreach ($opts as $key => $val) {
				$the_html[]		=	'<div class="idx-row">';
				$the_html[]		=		'<div class="idx-col-3">'.$key.'</div>';
				$the_html[]		=		(is_array($val) || is_object($val))
											? '<div class="idx-col-9" style="hyphens: auto;word-wrap: break-word;">'.print_r($val, true).'</div>'
											: '<div class="idx-col-9" style="hyphens: auto;word-wrap: break-word;">'.$val.'</div>';
				$the_html[]		=	'</div>';
				}
			}

		$the_html[]				=	'</div>';

		echo json_encode(array(
							'html'		=> implode('', $the_html)
							));

		die();
		}


	########################################################
	#	C U S T O M I Z E
	########################################################

	public function rover_idx_theme_callback() {

		if ( current_user_can('manage_options') )
			{
			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Starting');

			check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

			if (!is_array($theme_options = get_option(roverOPTIONS::THEME)))
				$theme_options								= array();

			foreach($_POST as $key => $val)
				$theme_options[$key]						= sanitize_text_field( $val );

			if (isset($theme_options['action']))
				unset($theme_options['action']);
			if (isset($theme_options['security']))
				unset($theme_options['security']);

			$r 												= update_option(roverOPTIONS::THEME, $theme_options);
			if ($r === true)
				$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Theme options were changed');
			else
				$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Theme options were not changed');

			Rover_IDX_Database::set_template_meta($theme_options['rover_post_id'], sanitize_text_field( $_POST['template'] ) );

			$responseVar = array(
							'theme'							=> (isset($theme_options['theme']))	? $theme_options['theme']	: null,
							'css'							=> (isset($theme_options['css']))	? $theme_options['css']		: null,
							'success'						=> $r
							);

			echo json_encode($responseVar);
			}

		die();
		}

	public function rover_idx_fetch_theme_settings_callback() {

		if ( current_user_can('manage_options') )
			{
			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Starting');

			check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

			echo json_encode(array(
							'success'						=> true,
							'settings'						=> get_option(roverOPTIONS::THEME)
							));
			}

		die();
		}


	public function rover_idx_menu_add_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		$menus_found										= [];
		if (is_array($tt = get_option(roverOPTIONS::THEME)))
			{
			#	Add the Menu

	#$output		= [];
	#foreach (wp_get_nav_menus() as $menu) {
	#	$menu_items											= wp_get_nav_menu_items( $menu->term_id );
	#	$output[]			= sprintf('[%s] => [%s]', $menu->term_id, $menu->name);
	#	}
	#echo json_encode(array(
	#			'done'		=> false,
	#			'output'	=> implode('<br>', $output)
	#			));
	#die();

			foreach (wp_get_nav_menus() as $menu) {
				$menu_items									= wp_get_nav_menu_items( $menu->term_id );
				$menus_found[]								= $menu->name;
				if ($_POST['wp_menu'] == $menu->name)
					{
					require_once __DIR__ . '/../rover-menus.php';

					$menus									= new Rover_IDX_Menus($this->rover_idx);
					$menus->add_login_menu($menu->term_id, $menu->name);

					echo json_encode(array(
									'menu_id'				=> $menu->term_id,
									'menu_name'				=> $menu->name,
									'done'					=> true,
									));

					die();
					}
				}

			#	Record that we have added the menu
			$tt['menu_has_been_added']						= $_POST['wp_menu'];

			update_option(roverOPTIONS::THEME, $tt);

			$this->rover_idx->roveridx_theming				= get_option(roverOPTIONS::THEME);
			}

		echo json_encode(array(
					'done'			=> false,
					'menus_found'	=> implode(',', $menus_found),
					'menu_name'		=> $_POST['wp_menu'],
					));

		die();
		}

	public function rover_idx_menu_remove_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		$deleted											= [];
		foreach (wp_get_nav_menus() as $menu) {

			$menu_items										= wp_get_nav_menu_items( $menu->term_id );
			if (!empty( $menu_items ) ) {
				foreach ( $menu_items as $menu_item ) {
					if (isset($menu_item->classes) && in_array('idx-menu-item', $menu_item->classes)) {
	#					echo '<pre>'.print_r($menu_item, true).'</pre>';
						wp_delete_post( $menu_item->db_id );
						$deleted[$menu_item->ID]			= $menu_item->title;
						}
					}
				}
			}

		if (is_array($tt = get_option(roverOPTIONS::THEME)))
			{
			if (isset($tt['menu_has_been_added']))
				unset($tt['menu_has_been_added']);

			update_option(roverOPTIONS::THEME, $tt);

			$this->rover_idx->roveridx_theming				= get_option(roverOPTIONS::THEME);
			}

		echo json_encode(array(
					'deleted'				=> $deleted,
					'done'					=> true,
					));

		die();
		}

	public function rover_idx_overwrite_theme_settings_callback() {

		if ( current_user_can('manage_options') )
			{
			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Starting');

			check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

			$source_wp_theme_options							= $_POST['source_wp_theme_options'];

			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'source_wp_theme_options ['.$source_wp_theme_options.']');

			$unsanitized_object									= $source_wp_theme_options;

			if (is_array($unsanitized_object))
				$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'unsanitized_object is an array of ['.count($unsanitized_object).'] items');
			else
				$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'unsanitized_object is NOT an array ');

			$new_theme_options									= array();
			foreach($unsanitized_object as $option_key => $option_val)
				$new_theme_options[$option_key]					= sanitize_text_field( $unsanitized_object[$option_key] );

			$r 													= update_option(roverOPTIONS::THEME, $new_theme_options);

			echo json_encode(array(
								'success'						=> true,
								'msg'							=> 'WordPress settings have been updated'
								));
			die();
			}

		echo json_encode(array(
								'success'						=> false,
								'msg'							=> 'The action requires administrator access'
								));
		die();
		}

	########################################################
	#	S E O
	########################################################

	public function rover_idx_seo_callback()	{

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( current_user_can('edit_posts') )
			{
			if (!is_array($seo_opts = get_option(roverOPTIONS::SEO)))
				$seo_opts				= array();

			$seo_opts['sitemap_enabled']	= sanitize_text_field( $_POST['sitemap_enabled'] );
			$seo_opts['crawler_redirect']	= sanitize_text_field( $_POST['crawler_redirect'] );

			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'POST[sitemap_enabled] ['.$_POST['sitemap_enabled'].']');
			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'seo_opts[disabled] ['.($seo_opts['sitemap_enabled']).']');
			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'seo_opts[crawler_redirect] ['.$seo_opts['crawler_redirect'].']');

			$r							= update_option(roverOPTIONS::SEO, $seo_opts);

			$responseVar = array(
										'success'	=> true
										);

			echo json_encode($responseVar);
			}

		die();
		}

	public function rover_idx_do_sitemap_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( current_user_can('edit_posts') )
			{
			$sitemap					= new Rover_IDX_SITEMAP($this->rover_idx);
			$ret						= $sitemap->build($force_refresh = true);

			$history					= $sitemap->history();

			if (is_array($ret))
				$ret['html']			= $history['html'];
			else
				$ret['html']			= 'sitemap refresh failed for an unknown reason.';

			echo json_encode($ret);
			}

		die();
		}


	public function rover_idx_sitemap_history_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		$sitemap						= new Rover_IDX_SITEMAP($this->rover_idx);

		$history						= $sitemap->history();

		$responseVar					= array(
										'html'		=> $history['html'],
										'never'		=> $history['never'],
										'domain'	=> get_site_url(),
										'success'	=> true
										);

		echo json_encode($responseVar);

		die();
		}

	public function rover_idx_create_city_dynamic_definitions_callback()	{

		global							$wpdb;

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( current_user_can('edit_posts') )
			{
			$added						= 0;
			$cities						= array();
			foreach (explode('&', sanitize_text_field( $_POST['cities'] ) ) as $post_key => $post_val)
				{
				$one_row				= explode('=', $post_val);
				$one_location_data		= explode('__', $one_row[1]);
				$state					= $one_location_data[0];
				$city					= $one_location_data[1];

				$dyn_meta				= $wpdb->get_row(
										"SELECT ID FROM $wpdb->posts
										WHERE post_type = '".ROVER_IDX_CUSTOM_POST_DYNAMIC_META."'
										AND post_status = 'publish'
										AND post_name = '".preg_replace('/[^a-zA-Z0-9]/', '', $state.$city)."'"
										);

				if (is_null($dyn_meta))
					{
					//	Doesn't exist - add it

					$id = wp_insert_post( array(
										'comment_status' => 'closed',
										'ping_status'	=> 'closed',
										'post_content'   => '[rover_crm_inbound]',
										'post_name'	  => preg_replace('/[^a-zA-Z0-9]/', '', $state.'/'.$city),
										'post_status'	=> 'publish',
										'post_title'	 => $state.'/'.$city,
										'post_type'	  => ROVER_IDX_CUSTOM_POST_DYNAMIC_META
										) );

					if (is_wp_error($id)) {
						$errors			= $id->get_error_messages();
						foreach ($errors as $error) {
							$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'wp_error: '.$error);
							}
						}
					else
						{
						add_post_meta($id, 'rover_idx_page_title', 'Homes for sale in '.$city);
						$added++;
						}
					}
				}

			$responseVar				= array(
										'success'	=> $added,
										'tbody'		=> $this->refresh_dynamic_definitions()
										);

			echo json_encode($responseVar);
			}

		die();
		}

	public function rover_idx_create_subdivision_dynamic_definitions_callback()	{

		global							$wpdb;

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( current_user_can('edit_posts') )
			{
			$added						= 0;
			$cities						= array();
			foreach (explode('&', sanitize_text_field( $_POST['subdivisions'] ) ) as $post_key => $post_val)
				{
				$one_row				= explode('=', $post_val);
				$one_location_data		= explode('__', $one_row[1]);
				$state					= $one_location_data[0];
				$city					= $one_location_data[1];
				$subdivision			= $one_location_data[2];

				$dyn_meta				= $wpdb->get_row(
										"SELECT ID FROM $wpdb->posts
										WHERE post_type = '".ROVER_IDX_CUSTOM_POST_DYNAMIC_META."'
										AND post_status = 'publish'
										AND post_name = '".preg_replace('/[^a-zA-Z0-9]/', '', $state.'subdivision'.$subdivision)."'"
										);

				if (is_null($dyn_meta))
					{
					//	Doesn't exist - add it

					$id = wp_insert_post( array(
										'comment_status' => 'closed',
										'ping_status'	=> 'closed',
										'post_content'   => '[rover_crm_inbound]',
										'post_name'	  => preg_replace('/[^a-zA-Z0-9]/', '', $state.'subdivision'.$subdivision),
										'post_status'	=> 'publish',
										'post_title'	 => $state.'/subdivision/'.$subdivision,
										'post_type'	  => ROVER_IDX_CUSTOM_POST_DYNAMIC_META
										) );

					if (is_wp_error($id)) {
						$errors			= $id->get_error_messages();
						foreach ($errors as $error) {
							$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'wp_error: '.$error);
							}
						}
					else
						{
						add_post_meta($id, 'rover_idx_page_title', 'Homes for sale in '.$city.' subdivision of '.$subdivision);
						$added++;
						}
					}
				}

			$responseVar				= array(
										'success'	=> $added,
										'tbody'		=> $this->refresh_dynamic_definitions()
										);

			echo json_encode($responseVar);
			}

		die();
		}

	private function refresh_dynamic_definitions()
		{
		global						$wpdb;

		$wp_list_table				= new Rover_List_Table($this->rover_idx);

		$wp_list_table->prepare_items("SELECT * FROM $wpdb->posts WHERE post_type = '".ROVER_IDX_CUSTOM_POST_DYNAMIC_META."' AND post_status = 'publish'");

		ob_start();
		$wp_list_table->display_rows_or_placeholder();

		return ob_get_clean();
		}

	########################################################
	#	S O C I A L
	########################################################

	public function rover_idx_social_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		if ( current_user_can('edit_posts') )
			{
			$text_fields				= array(
										'post_to_wp',
										'post_new', 'post_price_change', 'post_sold',
										'post_open_houses', 'post_monthly_data',
										'post_to_wp_comments', 'publish_office_listings',
										'email_on_error', 'email_on_post',
										'facebook_app', 'fb_app_id', 'fb_access_token', 'fb_name'
										);
			$int_fields					= array(
										'post_to_wp_as_user', 'email_on_error_to_user'
										);
			$settings					= get_option(roverOPTIONS::SOCIAL);

			foreach($_POST as $key => $val)
				{
				#	'enabled' or 'disabled'

				if (in_array($key, $text_fields))
					$settings[$key]		= sanitize_text_field($_POST[$key]);
				else if (in_array($key, $int_fields))
					$settings[$key]		= intval($_POST[$key]);
				}

			$settings['rand']			= rand(0,1500);

			$r							= update_option(roverOPTIONS::SOCIAL, $settings);

			$responseVar 				= array(
										'success'					=> $r,
										'post_to_wp'				=> $settings['post_to_wp'],
										'facebook_app'				=> $settings['facebook_app']
										);

			echo json_encode($responseVar);
			}

		die();
		}

	public function rover_idx_refresh_social_callback() {

		check_ajax_referer(roverKEYS::NONCE_DEF, 'security');

		$responseVar		= array(
							'success'	=> false,
							'response'	=> 'Apologies - not authorized'
							);

		if ( current_user_can('edit_posts') )
			{
			require_once __DIR__ . '/../rover-social-common.php';

			$social						= new Rover_IDX_SOCIAL($this->rover_idx);
			$refresh_ret				= $social->refresh($fix_missing_images = true);

			$responseVar				= array(
										'success'				=> true,
										'response'				=> $refresh_ret
										);
			}

		echo json_encode($responseVar);

		die();
		}
	}

?>