MP_Addon_SMS_Callback
Class Callback.
Source Source
File: calculator-sms-add-on/inc/MP_Addon_SMS_Callback.php
class MP_Addon_SMS_Callback {
/**
* Email settings.
*
* @var mixed|void
*
* @since 1.0.0
* @access public
*/
public $options_email;
/**
* Lead settings.
*
* @var mixed|void
*
* @since 1.0.0
* @access public
*/
public $lead_settings;
/**
* Style settings.
*
* @var mixed|void
*
* @since 1.0.0
* @access public
*/
public $options_style;
/**
* Twilio account sid.
*
* @var string
*
* @since 1.0.0
* @access private
*/
private $account_sid;
/**
* Twilio auth token.
*
* @var string
*
* @since 1.0.0
* @access private
*/
private $auth_token;
/**
* Twilio phone number.
*
* @var string
*
* @since 1.0.0
* @access private
*/
private $twilio_number;
/**
* SMS constructor.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
$this->options_email = get_option( MortgagePlatform::$prefix . 'sms' );
$this->lead_settings = get_option( MortgagePlatform::$prefix . 'leads' );
$this->options_style = get_option( MortgagePlatform::$prefix . 'style' );
$this->account_sid = get_option( MortgagePlatform::$prefix . 'sms' )['account_sid'];
$this->auth_token = get_option( MortgagePlatform::$prefix . 'sms' )['auth_token'];
$this->twilio_number = get_option( MortgagePlatform::$prefix . 'sms' )['twilio_phone'];
}
/**
* Callback function to response sms.
*
* @since 1.0.0
* @access public
*/
public function response_sms() {
$phone = MP_Settings::is_isset( $_POST['From'], null );
$body = MP_Settings::is_isset( $_POST['Body'] );
$post_id = $this->request_lead( $phone );
if ($post_id) {
var_dump($post_id);
$this->add_sms_message($post_id, $body);
MP_Logs::set_logs( $post_id, 'incoming_sms', ' from: ' . $phone );
update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'sms_status', true, '' );
$this->send_message_about_new_sms();
$this->send_thanks_message($post_id);
} else {
$this->send_thanks_message();
}
}
/**
* Add SMS message for Lead page.
*
* @access private
* @since 1.0.0
*
* @param int|string $post_id Post id.
* @param string $body sms message.
*/
private function add_sms_message($post_id, $body) {
$message = array(
'date' => current_time( 'Y-m-d H:i:s' ),
'body' => $body,
'from' => 'user',
);
add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'sms', wp_slash( $message ), '' );
}
/**
* Send SMS to admin about a new SMS from the Lead.
*
* @access private
* @since 1.0.0
*/
private function send_message_about_new_sms() {
if ( get_option( MortgagePlatform::$prefix . 'sms' )['send_sms_to_admin_from_lead'] &&
( ! empty( get_option( MortgagePlatform::$prefix . 'sms' )['number_sms_to_admin'] ) ||
! empty( get_option( MortgagePlatform::$prefix . 'sms' )['send_additional_sms'] ) ) ) {
$admin_phone = MP_Settings::check_data( get_option( MortgagePlatform::$prefix . 'sms' )['number_sms_to_admin'], get_option( MortgagePlatform::$prefix . 'sms' )['send_additional_sms'] );
return $this->send( $admin_phone, __( 'You have a new SMS from the Lead', 'CalculatorSMSAddon' ) );
}
}
/**
* Send thank you SMS to Lead.
*
* @access private
* @since 1.0.0
*
* @param int|string|null $post_id Post ID of the Lead.
*/
private function send_thanks_message($post_id = null) {
if ($post_id) {
$sms_dates = get_post_meta( $post_id, MortgagePlatform::$prefix . 'sms' );
if ($sms_dates) {
foreach ( $sms_dates as $sms_date ) {
$smsdate = $sms_date['date'];
$smsdate = time() - strtotime( $smsdate );
if ( $smsdate > 3600 ) {
$this->thanks_message();
}
}
}
} else {
$this->thanks_message();
}
}
/**
* Send thank you SMS to Lead.
*
* @access private
* @since 1.0.0
*/
private function thanks_message(){
$response = new TwiML\MessagingResponse();
$message = get_option( MortgagePlatform::$prefix . 'sms' )['send_thankyou_sms'] ? get_option( MortgagePlatform::$prefix . 'sms' )['send_thankyou_sms'] : __( 'Thank you for your message, we will reply to you shortly.', 'CalculatorSMSAddon' );
$response->message( $message );
print esc_attr( $response );
wp_die();
}
/**
* Request Lead by phone number in database.
*
* @since 1.0.0
* @access public
*
* @param string $phone phone
*
* @return mixed
*/
public function request_lead( $phone ) {
$found = false;
$phone_query = wp_cache_get('sms_addon_phone', MortgagePlatform::$domain, false, $found);
if(!$found){
$phone_query = new \WP_Query(
array(
'post_type' => array( MortgagePlatform::$lead, MortgagePlatform::$abandoned_lead ),
'posts_per_page' => - 1,
'post_status' => array( 'publish', 'pending' ),
'orderby' => array(
'ID' => 'DESC',
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => MortgagePlatform::$prefix . 'lead_phone',
'value' => $phone,
),
array(
'key' => MortgagePlatform::$prefix . 'abandoned_lead_phone',
'value' => ltrim( $phone, '+' ),
),
),
)
);
wp_cache_set('sms_addon_phone', $phone_query, MortgagePlatform::$domain, WEEK_IN_SECONDS);
}
$leads = array();
if ( $phone_query->have_posts() ) {
while ( $phone_query->have_posts() ) {
$phone_query->the_post();
$post_id = $phone_query->post->ID;
array_push( $leads, $post_id );
}
}
wp_reset_postdata();
return !empty($leads) ? $leads[0] : false;
}
/**
* Send Text Message.
*
* @since 1.0.0
* @access public
*
* @param string $phone phone
* @param string $body message
*
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
*/
public function send( $phone, $body ) {
$twilio = new Client( $this->account_sid, $this->auth_token );
$message = $twilio->messages->create(
$phone,
array(
'from' => $this->twilio_number,
'body' => $body,
)
);
return $message;
}
/**
* Send manually sms to lead from the lead page.
*
* @since 1.0.0
* @access public
*/
public function json_send_sms() {
MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );
$post_id = $_POST['leadId'];
$number = '+' . $_POST['leadPhone'];
$body = $_POST['leadSMSText'];
$smssend = $this->send( $number, $body );
$smssend_status = '' === $smssend->errorMessage ? 'Success' : $smssend->errorMessage;
MP_Logs::set_logs( $post_id, 'lead_sms_manually', $smssend_status . ': SID: ' . $smssend->sid );
$message = array(
'date' => current_time( 'Y-m-d H:i:s' ),
'body' => $body,
'from' => 'admin',
);
add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'sms', wp_slash( $message ), '' );
$response['status'] = $smssend_status;
$response['message'] = $message;
wp_send_json( $response );
}
/**
* Send SMS metabox.
*
* @since 1.0.0
* @access public
*/
public function metabox_send_sms( $post_id ) {
$site_url = get_site_url( '', '/wp-admin/post.php?post=' . $post_id . '&action=edit' );
if ( $this->options_style['bitly'] ) {
$bitly = new MP_Bitly();
$site_url = $bitly->get_short( $site_url );
}
if ( $this->options_email['send_sms'] &&
! empty( $this->options_email['account_sid'] ) &&
! empty( $this->options_email['auth_token'] ) &&
! empty( $this->options_email['twilio_phone'] )
) {
if ( $this->options_email['send_sms_to_user'] && isset( $this->lead_settings['phone'] ) ) {
$number = $this->lead_settings['phone'];
$body = $this->options_email['body_sms_to_user'] ? $this->options_email['body_sms_to_user'] : __( 'Thank you', 'CalculatorSMSAddon' );
$smssend = $this->send( $number, $body );
$smssend_status = '' === $smssend->errorMessage ? 'Success' : $smssend->errorMessage;
MP_Logs::set_logs( $post_id, 'user_sms', $smssend_status . ': SID: ' . $smssend->sid );
}
if ( $this->options_email['send_sms_to_admin'] && $this->options_email['number_sms_to_admin'] ) {
$number = $this->options_email['number_sms_to_admin'] ? $this->options_email['number_sms_to_admin'] : null;
$addition_number = $this->options_email['send_additional_sms'] ? $this->options_email['send_additional_sms'] : null;
$body = ! empty( $this->options_email['body_sms_to_admin'] ) ? $this->options_email['body_sms_to_admin'] . $site_url : __( 'New Lead: ', 'CalculatorSMSAddon' ) . $site_url;
if ( $number ) {
$smssend_admin = $this->send( $number, $body );}
if ( $addition_number ) {
$smssend_additional = $this->send( $addition_number, $body );}
$smssend_admin_status = '' === $smssend_admin->errorMessage ? 'Success' : $smssend_admin->errorMessage;
$smssend_additional_status = '' === $smssend_additional->errorMessage ? 'Success' : $smssend_additional->errorMessage;
MP_Logs::set_logs( $post_id, 'admin_sms', $smssend_admin_status . ': SID: ' . $smssend_admin->sid );
MP_Logs::set_logs( $post_id, 'adiitional_sms', $smssend_additional_status . ': SID: ' . $smssend_additional->sid );
}
}
}
/**
* Callback create API.
*
* @since 1.0.0
* @access public
*/
public function create_api() {
MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );
$chat = new MP_Addon_SMS_LiveChat();
$chat->get_api( MortgagePlatform::$plugin_name );
$chat->get_service( MortgagePlatform::$plugin_name );
wp_die();
}
/**
* Create Token for Frontend widget.
*
* @since 1.0.0
* @access public
*/
public function token() {
MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_sms-widget' );
// get user info from user ip address and from $_SERVER
$ip = $_SERVER['REMOTE_ADDR'];
$identity = MP_Settings::is_isset( $_POST['identity'], 'user_' . time() );
$email = MP_Settings::is_isset( $_POST['email'] );
$chat = new MP_Addon_SMS_LiveChat();
$service_sid = $chat->get_service( MortgagePlatform::$plugin_name );
$token = $chat->create_token( $identity );
$channel = $chat->get_channel( $identity );
if ( ! $channel ) {
$channel_sid = $chat->create_channel(
array(
'Identity' => $identity,
'UniqueName' => $identity,
'FriendlyName' => $identity . ' Chat Channel',
'CreatedBy' => 'Admin',
'Type' => 'private',
'Attributes' => '{"ip": "' . $ip . '"}',
)
);
$role_sid = $chat->get_role( $service_sid, 'channel admin' );
$admin_member = $chat->set_member( $service_sid, $channel_sid, 'Admin', array( 'roleSid' => $role_sid ) );
$user_member = $chat->set_member( $service_sid, $channel_sid, $identity );
}
wp_send_json( $token );
}
/**
* Create token for admin.
*
* @since 1.0.0
* @access public
*/
public function admin_chats() {
MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );
$identity = 'Admin';
$chat = new MP_Addon_SMS_LiveChat();
$token = $chat->create_token( $identity );
//$serviceSid = $chat->get_service(MortgagePlatform::$plugin_name);
//$chat->del_channel($serviceSid, '');
wp_send_json( $token );
}
/**
* Get User Address.
*
* @since 1.0.0
* @access public
*/
public function get_user_address() {
MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );
$ip = MP_Settings::is_isset( $_POST['ip'], null );
if ( $ip ) {
$geo = new MP_Geo();
$city = $geo->get_city( $ip );
wp_send_json( $city );
}
wp_die();
}
}
Methods Methods
- __construct — SMS constructor.
- add_sms_message — Add SMS message for Lead page.
- admin_chats — Create token for admin.
- create_api — Callback create API.
- get_user_address — Get User Address.
- json_send_sms — Send manually sms to lead from the lead page.
- metabox_send_sms — Send SMS metabox.
- request_lead — Request Lead by phone number in database.
- response_sms — Callback function to response sms.
- send — Send Text Message.
- send_message_about_new_sms — Send SMS to admin about a new SMS from the Lead.
- send_thanks_message — Send thank you SMS to Lead.
- thanks_message — Send thank you SMS to Lead.
- token — Create Token for Frontend widget.
Changelog Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |