<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
/**
* Ajax routine to delete device id from database after user opts to un-subscribe push notifications.
*
* @param string   $_POST['device_id'] Device id
*
*
* @since 1.0.0
*/

// Check the nonce first
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['nonce'])), 'pnfpbpushnonce' ) ) {
	echo 'Security validation failed.';
} else {
	/** Sanitize the device id generated by Google's Firebase for the user who subscribed push notification  **/
	
    $bpdeviceid = sanitize_text_field($_POST['device_id']);
    
    // phpcs:ignoreFile WordPress.DB.DirectDatabaseQuery
	
	/** securing data from Firebase who subscribed push notification  **/
	
	$bpdeviceid = esc_html($bpdeviceid);
	
	if ($bpdeviceid != '') {
	
		global $wpdb;
	
		$table = $wpdb->prefix.'pnfpb_ic_subscribed_deviceids_web';
		
		$newbpdeviceid = $bpdeviceid.'@N';
		
		$deviceid_update_status = $wpdb->query($wpdb->prepare("UPDATE %i SET device_id = %s WHERE device_id = %s",$table,$newbpdeviceid,$bpdeviceid)) ;
		
		if($deviceid_update_status > 0) {
		    
			echo "unsubscribed";
			
		}
		else
		{
			echo "...failed to delete/unsubscribe device id ";
		}
	}
	else
	{
		echo "Device id empty...failed to unsubscribe device id";
	}
}
wp_die();
	
?>