Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Adds support to Awesome Enterprise for sending emails, SMS and push notification

### Changelog

##### 1.0.5
* Added sms provider sms77

##### 1.0.4
* Fixed: Added option to specify the template for msg91 sms.
Expand All @@ -28,4 +30,4 @@ Adds support to Awesome Enterprise for sending emails, SMS and push notification

<p align="center">
<a href="https://www.wpoets.com/careers/"><img src="https://www.wpoets.com/wp-content/uploads/2020/11/work-with-us_1776x312.png" alt="Join us at WPoets, We specialize in designing, building and maintaining complex enterprise websites and portals in WordPress."></a>
</p>
</p>
140 changes: 96 additions & 44 deletions notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ function wpmail($atts,$content=null,$shortcode){
extract(\aw2_library::shortcode_atts( array(
'email' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_type' => null,
'notification_object_id' => null,
'tracking_set' => null
), $atts, 'aw2_wpmail' ) );

// if email is null, return
if(is_null($email)) return;

Expand All @@ -22,20 +22,20 @@ function wpmail($atts,$content=null,$shortcode){
if(!isset($email['message']))$email['message']='';
if(!isset($email['headers']))$email['headers']='';
if(!isset($email['attachment']))$email['attachment']='';

$tracking = array();
if(!empty($tracking_set))$tracking['tracking_set']=$tracking_set;

// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('mail', 'wpmail', $email, $log, $notification_object_type, $notification_object_id,$tracking);

wp_mail(
$email['to']['email_id'],
$email['subject'],
$email['message'],
$email['headers'],
$email['attachments']
wp_mail(
$email['to']['email_id'],
$email['subject'],
$email['message'],
$email['headers'],
$email['attachments']
);

$return_value = "success";
Expand All @@ -49,26 +49,26 @@ function sendgrid($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;

//including SENDGRID library

//require_once AWESOME_PATH.'/vendor/autoload.php';

extract(\aw2_library::shortcode_atts( array(
'email' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_type' => null,
'notification_object_id' => null,
'tracking_set' => null
), $atts, 'aw2_sendgrid' ) );

// if email is null, return
if(is_null($email)) return;

// Checking for values and setting them if not present.
if(!isset($email['from']['email_id']))$email['from']['email_id']='';
if(!isset($email['to']['email_id']))$email['to']['email_id']='';
if(!isset($email['message']))$email['message']='';
if(!isset($email['subject']))$email['subject']='';

//provider.apiKey or settings.sendgrid_apiKey
$apiKey = $email['provider']['key'];

Expand All @@ -91,14 +91,14 @@ function sendgrid($atts,$content=null,$shortcode){
}


// Content
// Content
$sendgrid_email->addContent(
"text/html", $email['message']
);

// Works on only when the attachments are present
if(isset($email['attachments']['file'])){

//storing file array in variable
$file = $email['attachments']['file'];
//looping through the file content
Expand All @@ -110,7 +110,7 @@ function sendgrid($atts,$content=null,$shortcode){
$sendgrid_email->addAttachment($file_encoded, null, $name, "attachment", null);
}
}

}

//$email['cc']['email_id']
Expand All @@ -130,7 +130,7 @@ function sendgrid($atts,$content=null,$shortcode){
}

//$email['reply_to']['email_id']
if(isset($email['reply_to']['email_id'])){
if(isset($email['reply_to']['email_id'])){
$reply_to_emails = explode(",",$email['reply_to']['email_id']);
foreach($reply_to_emails as $val){
$sendgrid_email->setReplyTo($val, null);
Expand All @@ -141,27 +141,27 @@ function sendgrid($atts,$content=null,$shortcode){

try {
$response = $sendgrid->send($sendgrid_email);

//get headers from the response->headers();
$header = $response->headers();
$header = $response->headers();

foreach ($header as $val) {
$val_array = explode(':', $val);

if($val_array[0] == 'X-Message-Id'){
//getting the message id from the header response
$messageId = $val_array[1];
$messageId = $val_array[1];

//setting up tracking array
$tracking['tracking_id'] = trim($messageId) ;
$tracking['tracking_status'] = 'sent_to_provider';
$tracking['tracking_stage'] = 'sent_to_provider';
if(!empty($tracking_set))$tracking['tracking_set']=$tracking_set;

// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('email', 'sendgrid', $email, $log, $notification_object_type, $notification_object_id, $tracking);

break;
}
}
Expand All @@ -172,12 +172,12 @@ function sendgrid($atts,$content=null,$shortcode){
$return_value = "success";
}


} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
$return_value = "error";
}

$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
Expand All @@ -190,7 +190,7 @@ function kookoo($atts,$content=null,$shortcode){
extract(\aw2_library::shortcode_atts( array(
'sms' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_type' => null,
'notification_object_id' => null
), $atts, 'aw2_kookoo' ) );

Expand All @@ -209,8 +209,8 @@ function kookoo($atts,$content=null,$shortcode){
$url = 'http://www.kookoo.in/outbound/outbound_sms.php';

$apiKey = $sms['provider']['key'];


if(empty($apiKey) || strlen($apiKey) === 0){
$return_value=\aw2_library::post_actions('all','No api key is not provided, check you settings for default api key!',$atts);
return $return_value;
Expand All @@ -219,7 +219,7 @@ function kookoo($atts,$content=null,$shortcode){
// parameter to send in sms
$param = array(
'api_key' => $apiKey,
'phone_no' => '0'.$sms['to']['mobile_number'],
'phone_no' => '0'.$sms['to']['mobile_number'],
'message' => $sms['message']
);

Expand All @@ -232,12 +232,12 @@ function kookoo($atts,$content=null,$shortcode){
$result = curl_exec($ch);
curl_close($ch);
$result = simplexml_load_string($result);

$return_value = 'error';
if(isset($result->status)){
$return_value= $result->status;
}

$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
Expand All @@ -251,55 +251,107 @@ function msg91($atts,$content=null,$shortcode){
extract(\aw2_library::shortcode_atts( array(
'sms' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_type' => null,
'notification_object_id' => null
), $atts, 'aw2_kookoo' ) );

// if $sms is not present
if(is_null($sms)){
return \aw2_library::post_actions('all','Sms array is required!',$atts);
}

// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('sms', 'msg91', $sms, $log, $notification_object_type, $notification_object_id);

// check if values are present or not
if(!isset($sms['to']['mobile_number']))$sms['to']['mobile_number']='';
if(!isset($sms['message']))$sms['message']='';
if(!isset($sms['provider']['key']))$sms['provider']['key']='';

// api base url
$url = 'http://api.msg91.com/api/v2/sendsms';
$apiKey = $sms['provider']['key'];

// if api key is not present
if(empty($apiKey) || strlen($apiKey) === 0){
return $return_value=\aw2_library::post_actions('all','No api key is not provided, check you settings for default api key!',$atts);
}

// create sms payload Array
$payloadArr = array();
$payloadArr = array();
$payloadArr['sender'] = $sms['provider']['sender'];
$payloadArr['DLT_TE_ID'] = $sms['dlt_template_id'];
$payloadArr['route'] = $sms['provider']['route'];
$payloadArr['country'] = $sms['provider']['country'];
$payloadArr['sms'][0]['message'] = $sms['message'];
$payloadArr['sms'][0]['to'][0] = $sms['to']['mobile_number'];

$payload = json_encode($payloadArr);

// use curl to send data
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("authkey:$apiKey","Content-Type:application/json"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = json_decode(curl_exec($ch));
curl_close($ch);

$return_value= $result->type == 'success' ? 'success' : 'error';

$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;


}

\aw2_library::add_service('notify.sms77','Send Sms77 SMS',['namespace'=>__NAMESPACE__]);

function sms77($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;

extract(\aw2_library::shortcode_atts( array(
'sms' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_id' => null
), $atts, 'aw2_kookoo' ) );

// if sms is null, return
if(is_null($sms)) return;

if(!isset($sms['to']['mobile_number']))$sms['to']['mobile_number']='';
if(!isset($sms['message']))$sms['message']='';
if(!isset($sms['provider']['key']))$sms['provider']['key']='';

// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('sms', 'sms77', $sms, $log, $notification_object_type, $notification_object_id);

$url = 'https://gateway.sms77.io/api/sms';
$apiKey = $sms['provider']['key'];

if(empty($apiKey) || strlen($apiKey) === 0){
$return_value=\aw2_library::post_actions('all','No api key is not provided, check you settings for default api key!',$atts);
return $return_value;
}

// parameter to send in sms
$payload = array(
'from' => $sms['provider']['sender'],
'to' => '0'.$sms['to']['mobile_number'],
'text' => $sms['message']
);

$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'SentWith: WPoets.CommunicationHandler', "X-Api-Key: $apiKey"));
$result = (int)curl_exec($ch);
curl_close($ch);

$return_value=100 === $result || 101 === $result ? 'success' : 'error';

$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}