<?php
@set_time_limit(0);

//check if paction is set and is not empty and user is logged in and is admin
if(!isset($_POST['paction']) || empty($_POST['paction'])){
	return;
}

if ( !is_user_logged_in() || !current_user_can( 'administrator' ) ) {
	wp_send_json_error( 'You do not have permission to perform this action.' );
}



// identify synonyms
if($_POST['paction'] == 'identifysyn'){

	wp_auto_spinner_log_new ('Manual Spin', 'Admin clicked on rewrite button for post '. $_POST['post_id']);
	
	
	//spintax
	require_once('inc/class.spintax.php');
	
	//spin it now 
	require_once('inc/class.spin.php');
	$spin=new wp_auto_spin_spin($_POST['post_id'],$_POST['title'],$_POST['postCnt']);
	$return=$spin->spin_wrap();

	//is return is boolean and == false, then return error
	if(is_bool($return) && $return == false){
		$return['status']='error';
		$return['message']='Error while spinning the post, Check log page for more details';
		print_r(json_encode($return));
		return;
	}

	$return['status']='success';

	print_r(json_encode($return));

//REWRITE	
}elseif($_POST['paction'] == 'rewrite'){
	
	//spintax
	require_once('inc/class.spintax.php');
	
	
	//generate new spintax
	$content = stripslashes( $_POST['title'].'**9999**'.$_POST['postCnt']);
	$spintax=new wp_auto_spinner_Spintax;
	$spintaxed=$spintax->spin($content);
	$spintaxed_arr=explode('**9999**',$spintaxed);
	$spintaxed_ttl=$spintaxed_arr[0];
	$spintaxed_cnt=$spintaxed_arr[1];
	
	$post_id=$_POST['post_id'];
	$spintaxed2=$spintax->editor_form;
	$spintaxed2_arr=explode('**9999**',$spintaxed2);
	$spintaxed2_cnt=$spintaxed2_arr[1];
	update_post_meta($post_id, 'spintaxed_cnt2', $spintaxed2_cnt);
	
	//update post meta
	$post_id=$_POST['post_id'];
	update_post_meta($post_id, 'spintaxed_ttl', $spintaxed_ttl);
	update_post_meta($post_id, 'spintaxed_cnt', $spintaxed_cnt);
	
	$return['spintaxed_ttl']=$spintaxed_arr[0];
	$return['spintaxed_cnt2']=$spintaxed2_arr[1];
	$return['status']='success';
	
	
	
	print_r(json_encode($return));
}elseif ($_POST['paction']=='delete'){
	
	
	$id=$_POST['id'];
	$thesaurs=$_POST['thesaurs'];
	
	$deleted=get_option('wp_auto_spinner_deleted_'.$thesaurs ,array() );
	$deleted[]=$id;
	
	update_option('wp_auto_spinner_deleted_'.$thesaurs , $deleted );
	 
	 
}elseif ($_POST['paction']=='edit'){
	$id=$_POST['id'];
	$thesaurs=$_POST['thesaurs'];
	$value=$_POST['value'];
	
	$modified=get_option('wp_auto_spinner_modified_'.$thesaurs ,array() );
	$modified[$id]=$value;
	update_option('wp_auto_spinner_modified_'.$thesaurs , $modified );
	
	print_r(get_option('wp_auto_spinner_modified_'.$thesaurs ,array() ));
	
}elseif ($_POST['paction']=='custom_add' || $_POST['paction']=='custom_edit' ){
	$id=$_POST['id'];
	$thesaurs=$_POST['thesaurs'];
	$value=$_POST['value'];
	
	$custom=get_option('wp_auto_spinner_custom_'.$thesaurs ,array() );
	
	if(is_numeric($id)){
		$custom[$id]=$value;
	}else{
		$custom[]=$value;
	}
	
	
	update_option('wp_auto_spinner_custom_'.$thesaurs , $custom );
}elseif ($_POST['paction'] == 'custom_delete'){
	$id=$_POST['id'];
	$thesaurs=$_POST['thesaurs'];
	 
	
	$custom=get_option('wp_auto_spinner_custom_'.$thesaurs ,array() );
	unset($custom[$id]);
	update_option('wp_auto_spinner_custom_'.$thesaurs , $custom );
}elseif($_POST['paction'] == 'getstatus'){

	//get the last log message from the log table
	global $wpdb;
	$last_log = $wpdb->get_row("SELECT * FROM wp_auto_spinner_log ORDER BY id DESC LIMIT 1");

	//if there is no log message, return empty string
	if($last_log == null){
		$last_log = '';

	//else return the log message
	}else{
		$last_log = $last_log->data;
	}

	//build the return array
	$return['status'] = 'success';
	$return['message'] = $last_log;

	//return the json encoded array
	print_r(json_encode($return));
	
	
}