wordpress add logout rest api - dathalongbay/notes GitHub Wiki

add_action( 'init', 'gg_oauth_logout2' );

add_action( 'rest_api_init', function () {
	// https://thegoldentimes.com.vn/wp-json/wpoauthserver/v1/logout
	register_rest_route( 'wpoauthserver/v1', '/logout/', array(
		'methods'             => 'GET',
		'callback'            => 'wp_oauth_server_logout'
	) );
	
	register_rest_route( 'wpoauthserver/v1', '/logout/', array(
		'methods'             => 'POST',
		'callback'            => 'wp_oauth_server_logout'
	) );
} );



function wp_oauth_server_logout() {
	
	//update_option('oauth_force_logout',1);
	//update_option('oauth_force_logout_data',json_encode($_REQUEST));
	//update_option('oauth_force_logout_header',json_encode(getallheaders()));
		

	$fileLog = date("d_m_Y");
$pluginlog=ABSPATH."/logs/$fileLog.log";
$time = time();
$meg_log='['.date("Y-m-d H:i:s", $time).' request UTC] : '.print_r(" -- log $time --",true).PHP_EOL;
error_log($meg_log, 3,$pluginlog);

$rawData = file_get_contents("php://input");
$meg_log='['.date("Y-m-d H:i:s", $time).' php://input UTC] : '.print_r($rawData,true).PHP_EOL;
error_log($meg_log, 3,$pluginlog);

$meg_log='['.date("Y-m-d H:i:s", $time).' request UTC] : '.print_r($_REQUEST,true).PHP_EOL;
error_log($meg_log, 3,$pluginlog);

$meg_log='['.date("Y-m-d H:i:s", $time).' header UTC] : '.print_r(getallheaders(),true).PHP_EOL;
error_log($meg_log, 3,$pluginlog);

$meg_log='['.date("Y-m-d H:i:s", $time).' server UTC] : '.print_r($_SERVER,true).PHP_EOL;
error_log($meg_log, 3,$pluginlog);
	
	
	// wp_logout();
	// wp_redirect('https://redirect-location.com');
	
	http_response_code(200);
		 $return = array(
			'status' => 200,
			'message' => "Logout Successful."
		);
		echo json_encode($return); exit;
	exit;
}