| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class PN_Server_Request{ |
| 6 |
public static $notificationServerUrl = 'http://pushnotifications.io/api/'; //slash necessary at last |
| 7 |
public static $notificationlanding = 'http://pushnotifications.io/'; //slash necessary at last |
| 8 |
public function __construct(){} |
| 9 |
|
| 10 |
public static function varifyUser($user_token){ |
| 11 |
$verifyUrl = 'validate/user'; |
| 12 |
if ( is_multisite() ) { |
| 13 |
$weblink = get_site_url(); |
| 14 |
} |
| 15 |
else { |
| 16 |
$weblink = home_url(); |
| 17 |
} |
| 18 |
$data = array("user_token"=>$user_token, "website"=> $weblink); |
| 19 |
$response = self::sendRequest($verifyUrl, $data, 'post'); |
| 20 |
$push_notification_auth_settings = get_option('push_notification_auth_settings'); |
| 21 |
$push_notification_auth_settings['user_token'] = sanitize_text_field($user_token); |
| 22 |
if($response['status']==200){ |
| 23 |
$response['response'] = array_map( 'sanitize_text_field', $response['response'] ); |
| 24 |
$push_notification_auth_settings['token_details'] = $response['response']; |
| 25 |
$push_notification_auth_settings['messageManager'] = $response['response']['messageManager']; |
| 26 |
} |
| 27 |
|
| 28 |
update_option('push_notification_auth_settings', $push_notification_auth_settings); |
| 29 |
return $response; |
| 30 |
} |
| 31 |
|
| 32 |
public static function registerSubscribers($token_id, $user_agent, $os, $ip_address){ |
| 33 |
$verifyUrl = 'register/audience/token'; |
| 34 |
if ( is_multisite() ) { |
| 35 |
$weblink = get_site_url(); |
| 36 |
} |
| 37 |
else { |
| 38 |
$weblink = home_url(); |
| 39 |
} |
| 40 |
$push_notification_auth_settings = get_option('push_notification_auth_settings'); |
| 41 |
$user_token = $push_notification_auth_settings['user_token']; |
| 42 |
$data = array("website"=> $weblink, |
| 43 |
'token_id' => $token_id, |
| 44 |
'user_agent'=>$user_agent, |
| 45 |
'os'=>$os, |
| 46 |
'user_token'=> $user_token, |
| 47 |
'ip_address'=> $ip_address |
| 48 |
); |
| 49 |
$response = self::sendRequest($verifyUrl, $data, 'post'); |
| 50 |
return $response; |
| 51 |
} |
| 52 |
|
| 53 |
public static function getsubscribersData($user_token){ |
| 54 |
$verifyUrl = 'audience/details'; |
| 55 |
if ( is_multisite() ) { |
| 56 |
$weblink = get_site_url(); |
| 57 |
} |
| 58 |
else { |
| 59 |
$weblink = home_url(); |
| 60 |
} |
| 61 |
$data = array("user_token"=>$user_token, "website"=> $weblink); |
| 62 |
$response = self::sendRequest($verifyUrl, $data, 'post'); |
| 63 |
$push_notification_auth_settings = get_option('push_notification_details_settings', array()); |
| 64 |
if($response['status']==200){ |
| 65 |
$push_notification_auth_settings['subscriber_count'] = sanitize_text_field($response['subscriber_count']); |
| 66 |
$push_notification_auth_settings['updated_at'] = date('Y-m-d H:i:s'); |
| 67 |
} |
| 68 |
update_option('push_notification_details_settings', $push_notification_auth_settings); |
| 69 |
return $response; |
| 70 |
} |
| 71 |
|
| 72 |
public static function sendPushNotificatioData($user_token, $title, $message, $link_url, $image_url){ |
| 73 |
$verifyUrl = 'campaign/create'; |
| 74 |
if ( is_multisite() ) { |
| 75 |
$weblink = get_site_url(); |
| 76 |
} |
| 77 |
else { |
| 78 |
$weblink = home_url(); |
| 79 |
} |
| 80 |
$data = array("user_token"=>$user_token, "website"=> $weblink, |
| 81 |
'title'=>$title, |
| 82 |
'message'=>$message, |
| 83 |
'link_url'=>$link_url, |
| 84 |
'image_url'=>$image_url); |
| 85 |
$response = self::sendRequest($verifyUrl, $data, 'post'); |
| 86 |
return $response; |
| 87 |
} |
| 88 |
|
| 89 |
protected static function sendRequest($suffixUrl, $data, $method="post"){ |
| 90 |
if($method==='post'){ |
| 91 |
$url = self::$notificationServerUrl.$suffixUrl; |
| 92 |
$postdata = array('body'=> $data); |
| 93 |
$remoteResponse = wp_remote_post($url, $postdata); |
| 94 |
} |
| 95 |
if( is_wp_error( $remoteResponse ) ){ |
| 96 |
$remoteData = array('status'=>401, "response"=>"could not connect to server"); |
| 97 |
}else{ |
| 98 |
$remoteData = wp_remote_retrieve_body($remoteResponse); |
| 99 |
$remoteData = json_decode($remoteData, true); |
| 100 |
} |
| 101 |
return $remoteData; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
} |