forked from mohsinoffline/wp-twilio-core
/
helpers.php
166 lines (141 loc) · 4.8 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Sends the actual SMS
* @param array $args Array of arguments described here: https://github.com/mohsinoffline/wp-twilio-core#twl_send_sms-args-
* @return array Response object from Twilio PHP library on success or WP_Error object on failure
*/
function twl_send_sms( $args ) {
$options = twl_get_options();
$options['number_to'] = $options['message'] = '';
$args = wp_parse_args( $args, $options );
$log = twl_validate_sms_args( $args );
if( !$log ) {
extract( $args );
$message = apply_filters( 'twl_sms_message', $message, $args );
$client = new Services_Twilio( $account_sid, $auth_token );
try {
$response = $client->account->messages->SendMessage( $number_from, $number_to, $message );
$log = twl_log_entry_format( sprintf( __( 'Success! Message SID: %s', TWL_TD ), $response->sid ), $args );
$return = $response;
} catch( Services_Twilio_RestException $e ) {
$log = twl_log_entry_format( sprintf( __( '****** API Error: %s ******', TWL_TD ), $e->getMessage() ), $args );
$return = new WP_Error( 'api-error', $e->getMessage(), $e );
}
} else {
$return = new WP_Error( 'missing-details', __( 'Some details are missing. Please make sure you have added all details in the settings tab.', TWL_TD ) );
}
twl_update_logs( $log, $args['logging'] );
return $return;
}
/**
* Update logs primarily from twl_send_sms() function
* @param string $log String of new-line separated log entries to be added
* @param int/boolean $enabled Whether to update logs or skip
* @return void
*/
function twl_update_logs( $log, $enabled = 1 ) {
$options = twl_get_options();
if ( $enabled == 1 ) {
$current_logs = get_option( TWL_LOGS_OPTION );
$new_logs = $log . $current_logs;
$logs_array = explode( "\n", $new_logs );
if ( count( $logs_array ) > 100 ) {
$logs_array = array_slice( $logs_array, 0, 100 );
$new_logs = implode( "\n", $logs_array );
}
update_option( TWL_LOGS_OPTION, $new_logs );
}
}
/**
* Get saved options
* @return array of saved options
*/
function twl_get_options() {
return apply_filters( 'twl_options', get_option( TWL_CORE_OPTION, array() ) );
}
/**
* Sanitizes option array before it gets saved
* @param $array array of options to be saved
* @return array of sanitized options
*/
function twl_sanitize_option( $option ) {
$keys = array_keys( twl_get_defaults() );
foreach( $keys as $key ) {
if( !isset( $option[$key] ) ) {
$option[$key] = '';
}
}
return $option;
}
/**
* Get default option array
* @return array of default options
*/
function twl_get_defaults() {
$twl_defaults = array(
'number_from' => '',
'account_sid' => '',
'auth_token' => '',
'logging' => '',
'mobile_field' => '',
'url_shorten' => '',
'url_shorten_api_key' => '',
);
return apply_filters( 'twl_defaults', $twl_defaults );
}
/**
* Format log message with more information
* @param string $message Message to be formatted
* @param array $args Send message arguments
* @return string Formatted message entry
*/
function twl_log_entry_format( $message = '', $args ) {
if ( $message == '' )
return $message;
return date( 'Y-m-d H:i:s' ) . ' -- ' . __( 'From: ', TWL_TD ) . $args['number_from'] . ' -- ' . __( 'To: ', TWL_TD ) . $args['number_to'] . ' -- ' . $message . "\n";
}
/**
* Validates args before sending message
* @param array $args Send message arguments
* @return string Log entries for invalid arguments
*/
function twl_validate_sms_args( $args ) {
// Check that we have the required elements
$log = '';
if( !$args['number_from'] ) {
$log .= twl_log_entry_format( __( '****** Missing Twilio Number ******', TWL_TD ), $args );
}
if( !$args['number_to'] ) {
$log .= twl_log_entry_format( __( '****** Missing Recipient Number ******', TWL_TD ), $args );
}
if( !$args['message'] ) {
$log .= twl_log_entry_format( __( '****** Missing Message ******', TWL_TD ), $args );
}
if( !$args['account_sid'] ) {
$log .= twl_log_entry_format( __( '****** Missing Account SID ******', TWL_TD ), $args );
}
if( !$args['auth_token'] ) {
$log .= twl_log_entry_format( __( '****** Missing Auth Token ******', TWL_TD ), $args );
}
return $log;
}
/**
* Saves the User Profile Settings
* @param int $user_id The User ID being saved
* @return void Saves to Usermeta
*/
function twl_save_profile_settings( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
$user_key = sanitize_text_field( $_POST['mobile_number'] );
update_user_meta( $user_id, 'mobile_number', $_POST['mobile_number'] );
}
/**
* Add the Mobile Number field to the Profile page
* @param array $contact_methods List of contact methods
* @return array The list of contact methods with the mobile field added
*/
function twl_add_contact_item( $contact_methods ) {
$contact_methods['mobile_number'] = __( 'Mobile Number', TWL_TD );
return $contact_methods;
}