<?php

$pref='wpf5gdpr-';
 $timenow=date('d-M-Y h:iA');
 global $wpdb;
 $table=$wpdb->prefix."wpfresh5_gdpr_requests";
$settings = array(

    'textarea_rows' =>5, );

	$wpcrmailheaders = "MIME-Version: 1.0" . "\r\n";
    $wpcrmailheaders .= "Content-type:text/html;charset=UTF-8" . "\r\n";
	$msg=get_option($pref.'uns-emailbody');
	$subject="";
	//wp_mail($to,$subject,$msg,$wpcrmailheaders);



 /*if(isset($_POST['notif']))
 {
	 $txt="Please re-consider unsubscribing as you may miss important updates and offers from us. If you really want to unsubscribe, please fill in your email address In the box below";
	 update_option($pref.'uns-notification',stripslashes($txt));
 }
if(isset($_POST['conf']))
 {
	 $txt="Hello,<br>

   This is to notify you that your unsubscribe request has been processed and you have been unsubscribed successfully.
<br>
Thanks";
	 update_option($pref.'uns-emailbody',stripslashes($txt));

 }*/
if(isset($_POST['saveitunsubscribe']))
{
	//upload settings
	update_option($pref.'uns-notification',stripslashes($_POST['unsnotif']));
    update_option($pref.'uns-emailbody',stripslashes($_POST['unsconf']));
}
if(isset($_POST['gdprunstakeaction']))
{
	//confirm single
	$u = new WP_User(email_exists($_POST['unsemail']));
    $u->remove_role( 'subscriber' );
	$wpdb->query($wpdb->prepare("update ".$table." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['unsreqid'])));
	wp_mail($_POST['unsemail'],$subject,$msg,$wpcrmailheaders);
}
if(isset($_POST['gdprunsremove']))
{//single remove
	$wpdb->query($wpdb->query($wpdb->prepare("delete from ".$table." where id=%d ",array($_POST['unsreqid']))));
}
if(isset($_POST['gdprunstakeactionselected']))
{//confirm selected

$arr=explode("@",$_POST['selectedid']);

if($_POST['selectedid']!="")
{
	$arr=explode("@",$_POST['selectedid']);
	//print_r($arr);
	foreach($arr as $test)
	{
		$q=$wpdb->get_row("select * from ".$table." where id=".$test."");
		$u = new WP_User(email_exists($q->email));
    $u->remove_role( 'subscriber' );
	$wpdb->query($wpdb->prepare("update ".$table." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$test)));

	wp_mail($q->email,$subject,$msg,$wpcrmailheaders);
	}
}
}
if(isset($_POST['gdprunsremoveselected']))
{
	//remove selected
if($_POST['selectedid']!="")
{
	$arr=explode("@",$_POST['selectedid']);
	//print_r($arr);
	foreach($arr as $test)
	{
		$wpdb->query($wpdb->query($wpdb->prepare("delete from ".$table." where id=%d ",array($test))));
	}
}
}
if(isset($_POST['gdprunsremoveall']))
{
	//remove all
	$wpdb->query($wpdb->query($wpdb->prepare("delete from ".$table." where type=%s ",array('uns'))));

}
if(isset($_POST['gdprunsconfirmall']))
{
	//confirm all
		$q=get_row("select * from table where id=".$test."");
		$u = new WP_User($user);
    $u->remove_role( 'subscriber' );
	$wpdb->query($wpdb->prepare("update ".$table." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['unseqid'])));
}

?>
  <script type="text/javascript">
  var id='0';
  function collectID(str)
  {
	  if(document.getElementById('id'+str).checked)
	  {
	  id=id+'@'+str;
	    document.getElementById('selectedid').value=id;

	  }
	  else
	  {
		  id=id.replace('@'+str,'');
		  document.getElementById('selectedid').value=id;
	  }
	 // alert(document.getElementById('selectedid').value);
  }
  function gdprunsAction(){
			var conf=confirm("Are you sure you want to delete this");
			if(conf==true)
			{return true;}
			else
			{return false;}
			}
  </script>

  <h3>Unsubscribe Request Form - <a href="https://wpfreshstart5.com/support/knowledgebase/unsubscribe-requests/" target="_blank" style="font-size:0.8em">Support</a></h3>
  Use this short code <b>[WPFS5GDPR]</b> to display an unsubscribe form for your visitors.

      <h4 class="m0">Confirmation Message to people asking to unsusbscribe</h4>
      <?php wp_editor(get_option($pref.'uns-notification'),'unsnotif',$settings) ?>
      	<br/>
      		<button type="button" class="button button-secondary" id="unsub1-default-txt" >Use Default Content</button>
      		<textarea  id="unsub1-def" style="display:none">Hello,<br/><br/>
      Please re-consider unsubscribing as you may miss important updates and offers from us. If you really want to unsubscribe, please fill in your email address in the box below.<br/><br/><br/>
      Thanks</textarea>
<br/>
      <h4 class="m0">Unsubscribe Confirmation Message</h4>
      <?php wp_editor(get_option($pref.'uns-emailbody'),'unsconf',$settings) ?>  
      	<br/>
      		<button type="button" class="button button-secondary" id="unsub2-default-txt" >Use Default Content</button>
      		<textarea  id="unsub2-def" style="display:none">Hello,<br/><br/>
      We would like to notify you that your unsubscribe request has been processed and you have been unsubscribed successfully.<br/><br/><br/>
      Thanks</textarea>
     <!-- <button type="submit" name="conf" class="btn btn-default" style="margin-top: 7px">Use Default</button> -->

     <!-- <button type="submit" name="saveit" class="btn btn-primary btnsave" value="" style="margin-top: 7px"></button> -->
     <input type="hidden" name="saveitunsubscribe" value="1">
<br/>
<br/>
      <table class="tbl">
      	<tbody>
      		<tr><td class="col-lg-2"><label class="mt6">Unsubscribe Requests</label></td>
     <td>
	 <!-- <form action="" method="post"> -->
	 <!-- <div class="dropdown"> -->
<!-- 	 <button class="btn dropdown-toggle" type="button" data-toggle="dropdown">Choose your option
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
  <input type="hidden" id="selectedid" name="selectedid" value="">
    <li><input type="submit" class="btn" value="Send unsubscribe confirmations&nbsp&nbsp" name="gdprunstakeactionselected"></li>
    <li><input type="submit" class="btn"  value="Delete unsubscribe confirmations" name="gdprunsremoveselected"></li>
	</ul> -->
</td>
  </tr>

      	</tbody>
      </table>

    	<table class="tbl widefat gdprrqtable">
    <thead>
      <tr>
        <th>#</th>
        <th>Email</th>
        <th>Name</th>
        <th>Date & Time</th>
        <th>Status</th>
        <th>Options</th>
      </tr>
    </thead>
	<tbody>
	<?php wpfresh5_gdpr_unsubscription_request_table(); ?>
	</tbody>
  </table>