-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallme.php
More file actions
484 lines (442 loc) · 19.6 KB
/
callme.php
File metadata and controls
484 lines (442 loc) · 19.6 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/*
Plugin Name: CallMe
Description: Twilio Powered calling to the blog owner
Version: 0.1
Author: Aaron Frank
Author URI: http://www.arfrank.com
License: GPL2
*/
$twilio_found = true;
register_activation_hook(__FILE__, 'callme_activate');
//This is to make sure it loads the twilio helper libraries, was getting alot of problems on certain platforms (dotcloud)
function load_twilio_library(){
if (file_exists(WP_PLUGIN_DIR.'/'. dirname( plugin_basename(__FILE__) ).'/php/TwilioLibrary/Services/Twilio.php')) {
require_once(WP_PLUGIN_DIR.'/'. dirname( plugin_basename(__FILE__) ).'/php/TwilioLibrary/Services/Twilio.php');
return true;
}elseif(file_exists('/'.dirname( plugin_basename(__FILE__)).'/php/TwilioLibrary/Services/Twilio.php')){
//HACK FOR DOTCLOUD
require_once('/'.dirname( plugin_basename(__FILE__) ).'/php/TwilioLibrary/Services/Twilio.php');
return true;
}elseif(file_exists('./php/TwilioLibrary/Services/Twilio.php')){
require_once('./php/TwilioLibrary/Services/Twilio.php');
return true;
}else{
return false;
}
}
function callme_activate(){
add_option('callme_settings',array(), '', 'yes');
$tw = load_twilio_library();
if (!$tw) {
throw new Exception("UNABLE TO FIND TWILIO LIBRARY", 1);
}
}
$twilio_found = load_twilio_library();
if ($twilio_found) {
$callme_settings = get_option('callme_settings',false);
$callme_settings_changed = false;
if (!$callme_settings) {
add_option('callme_settings',array(), '', 'yes');
$callme_settings = array();
}
if ($_POST['twilio_sid'] or $_POST['twilio_token']) {
$twilio_exception_caught = false;
try {
$twilio_client = new Services_Twilio($_POST['twilio_sid'], $_POST['twilio_token']);
} catch (Exception $e) {
$twilio_exception_caught = true;
}
if (!$twilio_exception_caught) {
if (!isset($callme_settings['twilio'])) {
$callme_settings['twilio'] = array();
}
if ($_POST['twilio_sid']) {
$callme_settings['twilio']['sid'] = $_POST['twilio_sid'];
}
if ($_POST['twilio_token'] && $_POST['twilio_token'] !='') {
$callme_settings['twilio']['token'] = $_POST['twilio_token'];
}
$callme_settings_changed = true;
}
}
add_filter( 'plugin_action_links', 'callme_plugin_action_links',10,2);
add_action( 'admin_menu', 'callme_config_page' );
add_action( 'wp_print_scripts', 'WPCallMe_Scripts');
add_action( 'wp_print_styles', 'WPCallMe_Styles');
add_action( 'loop_start', 'WPCallMe_HTML');
add_action('wp_dashboard_setup', 'callme_wp_dashboard_setup');
if (isset($callme_settings['twilio']['sid']) && isset($callme_settings['twilio']['token'])){
$twilio_client = new Services_Twilio($callme_settings['twilio']['sid'], $callme_settings['twilio']['token']);
if(!isset($callme_settings['twilio']['app_sid'])) {
$app = $twilio_client->account->applications->create('callme_app',
array(
'ApiVersion'=>'2010-04-01',
'VoiceUrl'=>trailingslashit( get_bloginfo('wpurl') ).PLUGINDIR.'/'.'CallMe'.'/php/app_landing.php',
'VoiceMethod'=>'GET',
'StatusCallback'=>trailingslashit( get_bloginfo('wpurl') ).PLUGINDIR.'/'.'CallMe'.'/php/callback_landing.php'
)
);
$callme_settings['twilio']['app_sid'] = $app->sid;
$callme_settings_changed = true;
}
if (!isset($callme_settings['twilio']['subaccount_sid'])) {
$sub_account = $twilio_client->accounts->create(array('FriendlyName'=>'callme_subaccount'));
$callme_settings['twilio']['subaccount_sid'] = $sub_account->sid;
$callme_settings_changed = true;
}
}
function callme_app_page(){
}
//Add the settings pages
function callme_config_page() {
if ( function_exists('add_submenu_page') )
add_submenu_page('plugins.php', __('CallMe Configuration'), __('CallMe Configuration'), 'manage_options', 'callme-config', 'callme_conf');
}
//Add the settings link to plugin row
function callme_plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( dirname(__FILE__).'/callme.php' ) ) {
$links[] = '<a href="plugins.php?page=callme-config">'.__('Settings').'</a>';
}
return $links;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//Begin option handling
function validate_number(){
}
if ($_POST['callme_type']) {
if (!isset($callme_settings['widget'])) {
$callme_settings['widget'] = array();
}
switch ($_POST['callme_type']) {
case 'callme':
if (!isset($callme_settings['callme'])) {
$callme_settings['callme'] = array();
}
$callme_settings['widget']['type'] = 'callme';
$callme_settings['callme']['widget_text'] = $_POST['widget_text'];
if ($_POST['your_number']) {
$callme_settings['callme']['your_number'] = $_POST['your_number'];
}
break;
case 'conference':
if (!isset($callme_settings['conference'])) {
$callme_settings['conference'] = array();
}
$callme_settings['widget']['type'] = 'conference';
$callme_settings['conference']['widget_text'] = $_POST['widget_text'];
$callme_settings['conference']['conference_text'] = $_POST['conference_text'];
$callme_settings['conference']['length'] = (is_numeric($_POST['conference_length']) && (int) $_POST['conference_length'] > 0) ? (int) $_POST['conference_length'] : 0;
$callme_settings['conference']['autoconnect'] = ($_POST['conference_autoconnect'] == 'yes'?true:false);
break;
case 'voicemail':
if (!isset($callme_settings['voicemail'])) {
$callme_settings['voicemail'] = array();
}
$callme_settings['widget']['type'] = 'voicemail';
$callme_settings['voicemail']['widget_text'] = $_POST['widget_text'];
$callme_settings['voicemail']['welcome'] = $_POST['voicemail_welcome'];
$callme_settings['voicemail']['length'] = $_POST['voicemail_length'];
break;
}
$callme_settings_changed = true;
}
if ($_POST['widget_location']) {
if (!isset($callme_settings['widget'])) {
$callme_settings['widget'] = array();
}
switch ($_POST['widget_location']) {
case 'topleft':
$callme_settings['widget']['location'] = 'topleft';
break;
case 'topright':
$callme_settings['widget']['location'] = 'topright';
break;
case 'bottomleft':
$callme_settings['widget']['location'] = 'bottomleft';
break;
default:
$callme_settings['widget']['location'] = 'bottomright';
break;
}
$callme_settings_changed = true;
}
if ($_POST['widget_stylesheet']) {
if (!isset($callme_settings['widget'])) {
$callme_settings['widget'] = array();
}
$callme_settings['widget']['stylesheet'] = $_POST['widget_stylesheet'];
$callme_settings_changed = true;
}
if ($_POST['twilio_number']) {
if (!isset($callme_settings['callme'])) {
$callme_settings['callme'] = array();
}
$callme_settings['callme']['twilio_number'] = $_POST['twilio_number'];
$callme_settings_changed = true;
}
if ($callme_settings_changed) {
update_option('callme_settings', $callme_settings);
}
//End option handling
/////////////////////////////////////////////////////////////////////////////////////////////////
//Utility functions
function phoneNumberFormat($number){
if( preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $number, $matches ) )
{
$result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
return $result;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//Functions below for page loading things
//Page for admin settings
function callme_conf(){
global $callme_settings, $twilio_client;
//Get a list of usable from numbers to make a phone number clal from.
$phone_numbers = array();
if (isset($twilio_client) and isset($callme_settings['twilio']['sid']) && isset($callme_settings['twilio']['token'])) {
$caller_ids = $twilio_client->account->outgoing_caller_ids->getPage();
$twilio_numbers = $twilio_client->account->incoming_phone_numbers->getPage();
foreach ($caller_ids->outgoing_caller_ids as $oci) {
$phone_numbers[] = $oci->phone_number;
}
foreach ($twilio_numbers->incoming_phone_numbers as $ipn) {
$phone_numbers[] = $ipn->phone_number;
}
}
?>
<div>
<h1>CallMe Config Page</h1>
<p>This plug allows you to easily add a call me, conference with other readers, or leave me a voicemail button to your blog.</p>
<div id="" class="">
<h2>General Settings</h2>
<h3>Twilio Credentials</h3>
<form action="#" method="post" accept-charset="utf-8">
<p><label>Twilio Account SID:<input type="text" name="twilio_sid" value="<?php echo (isset($callme_settings['twilio']['sid']) ? $callme_settings['twilio']['sid']:""); ?>"></label></p>
<p><label>Twilio Auth Token: <input type="password" name="twilio_token" value=""></label><?php echo isset($callme_settings['twilio']['token'])? " <b>Token Saved</b>":""; ?></p>
<h3>Widget Settings</h3>
<p>
<label>Location: <select name="widget_location">
<option value="bottomright" <?php echo ((isset($callme_settings['widget']['location']) && $callme_settings['widget']['location']=='bottomright' ) ? 'selected':''); ?>>Bottom Right</option>
<option value="topright" <?php echo ((isset($callme_settings['widget']['location']) && $callme_settings['widget']['location']=='topright') ?'selected':''); ?>>Top Right</option>
<option value="topleft" <?php echo ((isset($callme_settings['widget']['location']) && $callme_settings['widget']['location']=='topleft' ) ? 'selected':''); ?>>Top Left</option>
<option value="bottomleft" <?php echo ((isset($callme_settings['widget']['location']) && $callme_settings['widget']['location']=='bottomleft' ) ? 'selected':''); ?>>Bottom Left</option>
</select></label>
</p>
<p>
<label>Stylize: <textarea name="widget_stylesheet"><?php echo (isset($callme_settings['widget']['stylesheet'])? $callme_settings['widget']['stylesheet']: '' ); ?></textarea></label>
<div class="callme_caption">Please enter valid css above in order to correctly stylize the widget.</div>
</p>
<input type="submit" name="save_settings" value="Save Settings">
</form>
</div>
<div>
<?php
$callme_widget_type = (isset($callme_settings['widget']['type']) ? $callme_settings['widget']['type']:"callme")
?>
<h2>Customize CallMe Widget</h2>
<select id="callme_widget_type" name="callme_widget_type">
<option value="callme" <?php echo ($callme_widget_type=='callme'? 'selected':''); ?>>Call Me!</option>
<option value="conference" <?php echo ($callme_widget_type=='conference'? 'selected':''); ?>>Conference</option>
<option value="voicemail" <?php echo ($callme_widget_type=='voicemail'? 'selected':''); ?>>Voicemail</option>
</select>
<div id="callme" class="selected_widget_inputs" <?php echo ($callme_widget_type!='callme'? 'style="display:none"':''); ?>>
<h3>Call Me!</h3>
<p>
Visitors will be able to directly call you from your blog. In order to call you, you will need a Twilio number to call from.
</p>
<form action="#" method="post" accept-charset="utf-8">
<input type="hidden" name="callme_type" value="callme">
<p>
<label>Widget Text: <input type="text" name="widget_text" value="<?php echo (isset($callme_settings['callme']['widget_text']) ? $callme_settings['callme']['widget_text']:'Call Me!'); ?>"></label>
</p>
<p>
<label>Twilio Number:
<?php if (count($phone_numbers)) { ?>
<select name="twilio_number">
<?php foreach ($phone_numbers as $key=>$pn) {
?><option value="<?php echo $pn; ?>"><?php echo phoneNumberFormat($pn); ?></option>
<?php } ?>
</select>
<?php
}else{
?>You do not have any numbers setup with Twilio. In order to use the CallMe feature you must either purchase a number from Twilio, or validate a number.<?php
}
?>
</label>
<div class="callme_caption">
Select a number to display when receiving numbers from the widget.
</div>
</p>
<p>
<label>Number to reach you at: <input type="text" name="your_number" value="<?php echo (isset($callme_settings['callme']['your_number']) ? $callme_settings['callme']['your_number']:''); ?>"></label>
</p>
<input type="submit" value="Save">
</form>
</div>
<div id="conference" <?php echo ($callme_widget_type!='conference'? 'style="display:none"':''); ?> class="selected_widget_inputs">
<h3>Conference Call</h3>
<p>
Visitors will be able to join a conference call with other visitors to you're blog.
</p>
<form action="#" method="post" accept-charset="utf-8">
<p>
<label>Widget Text: <input type="text" name="widget_text" value="<?php echo (isset($callme_settings['conference']['widget_text']) ? $callme_settings['conference']['widget_text']:'Talk to other readers!') ; ?>"></label>
</p>
<p>
<label>Conference Call Welcome Message: <textarea name="conference_text"><?php echo (isset($callme_settings['conference']['conference_text']) ? $callme_settings['conference']['conference_text'] : 'Welcome to the group!'); ?></textarea></label>
</p>
<p>
<label>Auto-connect conference call on connection: <input type="checkbox" name="conference_autoconnect" value="yes" <?php echo ((isset($callme_settings['conference']['autoconnect']) && $callme_settings['conference']['autoconnect']) ? "checked":"" ); ?>></label>
</p>
<p>
<label>Limit call length: <input type="text" name="conference_length" value="<?php echo (isset($callme_settings['conference']['length']) && $callme_settings['conference']['length']) ? $callme_settings['conference']['length'] : '' ?>"> seconds</label>
</p>
<input type="hidden" name="callme_type" value="conference">
<input type="submit" value="Save">
</form>
</div>
<div id="voicemail" <?php echo ($callme_widget_type!='voicemail'? 'style="display:none"':''); ?> class="selected_widget_inputs">
<h3>Voicemail</h3>
<p>
When set to this, visitors will be able to leave you voicemails and we'll automatically have them forward to you're email with a recording.
</p>
<form action="#" method="post" accept-charset="utf-8">
<p>
<label>Widget Text: <input type="text" name="widget_text" value="<?php echo (isset($callme_settings['voicemail']['widget_text']) ? $callme_settings['voicemail']['widget_text']:'Leave me a voicemail!'); ?>"></label>
</p>
<p>
<label for="voicemail_welcome">Message for voicemail: <textarea name="voicemail_welcome" rows="8" cols="40"><?php echo (isset($callme_settings['voicemail']['welcome']) ? $callme_settings['voicemail']['welcome']:''); ?></textarea></label>
</p>
<p>
<label for="voicemail_lenght">Voicemail Length: <input type="text" name="voicemail_length" value="<?php echo (isset($callme_settings['voicemail']['length'])? $callme_settings['voicemail']['length'] : '60'); ?>" id="voicemail_length"> seconds</label>
</p>
<input type="hidden" name="callme_type" value="voicemail">
<input type="submit" value="Save">
</form>
</div>
</div>
</div>
<?php
}
//Styles to inject into WP main site
function WPCallMe_Styles(){
$callme_plugin_url = trailingslashit( get_bloginfo('wpurl') ).PLUGINDIR.'/CallMe';
if (!is_admin()) {
wp_enqueue_style( 'callme_public_style', $callme_plugin_url.'/css/callme.css',false,rand(),'screen');
}else{
wp_enqueue_style('callme_admin_style', $callme_plugin_url.'/css/callme.css', false,rand(),'screen');
}
}
//Scripts to inject
function WPCallMe_Scripts(){
$callme_plugin_url = trailingslashit( get_bloginfo('wpurl') ).PLUGINDIR.'/CallMe';
//Main site scripts
if (!is_admin()){
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-form');
wp_enqueue_script('callme_twilio_script','http://static.twilio.com/libs/twiliojs/1.0/twilio.min.js');
wp_enqueue_script('callme_public_script', plugins_url().'/CallMe/js/callme.js?id='.rand(), array('jquery', 'jquery-form','callme_twilio_script'));
}else{
//Admin site scripts
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-form');
wp_enqueue_script('callme_admin_script', plugins_url().'/CallMe/js/admin.js', array('jquery', 'jquery-form'),1,true);
}
}
//HTML for main site to inject
function WPCallMe_HTML(){
global $callme_settings;
if (!is_admin()) {
// put your Twilio API credentials here
if (isset($callme_settings['twilio']['sid']) && isset($callme_settings['twilio']['token']) && isset($callme_settings['widget']) && isset($callme_settings['widget']['type']) && isset($callme_settings['twilio']['app_sid'])) {
$capability = new Services_Twilio_Capability($callme_settings['twilio']['sid'], $callme_settings['twilio']['token']);
$capability->allowClientOutgoing($callme_settings['twilio']['app_sid']);
$token = $capability->generateToken();
if (isset($callme_settings['widget']['type'])) {
$callme_widget_text = $callme_settings[$callme_settings['widget']['type']]['widget_text'];
}
?>
<script>
<?php
if (isset($callme_settings['widget']['type']) and $callme_settings['widget']['type'] == 'conference') {
?>var autoconnect_conference = <?php echo(isset($callme_settings['conference']['autoconnect']) ? $callme_settings['conference']['autoconnect'] : 'false'); ?>;
<?php
}else{
?>var autoconnect_conference = false;
<?php
}
?>
var token = '<?php echo $token; ?>';
</script>
<div id="callme_widget" class="callme_<?php echo (isset($callme_settings['widget']['location']) ? $callme_settings['widget']['location']:'bottomright'); ?>" <?php if (isset($callme_settings['widget']) && isset($callme_settings['widget']['stylesheet'])) {
echo 'style="'.$callme_settings['widget']['stylesheet'].'"';
} ?>>
<?php echo $callme_widget_text; ?>
</div>
<?php
}
}
}
/* FOR ADDING DASHBBOARD WIDGET */
/**
* Content of Dashboard-Widget
*/
function callme_dashboard() {
global $callme_settings, $twilio_client;
if (isset($callme_settings['widget']['type']) && $callme_settings['widget']['type'] == 'voicemail') {
$entries = $twilio_client->account->recordings->getPage();
}else{
$entries = array();
}
echo '<h2>Recent Activity</h2>';
?>
<h3><?php
if (isset($callme_settings['widget']['type'])) {
switch ($callme_settings['widget']['type']) {
case 'voicemail':
echo "Recordings";
break;
case 'conference':
echo "Conferences";
default:
echo "Calls";
break;
}
}
?></h4>
<div>Right now we don't collect the information from a response, so we are limited in what we can show here. Currently only recordings are supported, but it is known not to be perfect. Whence Twilio supports filtering by Application we will quickly support seeing all calls, conferences and voicemails made with 100% accuracy.</div>
<table style="width:100%">
<thead>
<tr>
<th>Time</th>
<th>Length</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php foreach ($entries->recordings as $key => $value) {
$created = new DateTime($value->date_created);
?>
<td><?php echo $created->format('m-d-Y H:i:s'); ?> PST</td>
<td><?php echo $value->duration ?> Seconds</td>
<td><a href="https://api.twilio.com<?php echo $value->Uri ?>.mp3">Link</a></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
/**
* add Dashboard Widget via function wp_add_dashboard_widget()
*/
function callme_wp_dashboard_setup() {
wp_add_dashboard_widget( 'callme_dashboard', __( 'CallMe History' ), 'callme_dashboard' );
}
/**
* use hook, to integrate new widget
*/
}
?>