-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr-emergency-button.php
291 lines (240 loc) · 10.1 KB
/
r-emergency-button.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
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
<?php
/*
Plugin Name: R Emergency Button
Plugin URI:
Description: R Emergency Button is a plugin which allows to create a custom button and upon clicking depending of number of clicks, it will email the user geolocation and data to provided email address
Version: 1.0.0
Author URI:
*/
define( 'EEB_VERSION', '1.0.0' );
define( 'EEB_PATH', plugin_dir_path( __FILE__ ) );
define( 'EEB_BASEPATH', plugin_basename(dirname(__FILE__)) );
require_once( ABSPATH . '/wp-includes/pluggable.php' );
class r_emergency_button
{
public function __construct()
{
register_activation_hook( __FILE__, array( __class__, 'eeb_page_install' ) );
add_action('admin_menu', array(__class__, 'eeb_option_page'));
add_shortcode( 'eeb_button', array(&$this, 'eeb_display' ) );
add_action( 'wp_head', array(__class__, 'eeb_head'));
}
/*
* Activation Hook
*/
public function eeb_page_install()
{
$defaultSettings = array(
'eeb_text' => 'Text',
'eeb_no_of_clicks' => 3,
'eeb_email_to' => '',
'eeb_detect_location' => true,
'eeb_send_interval' => 5,
'eeb_text' => '',
'eeb_no_of_clicks' =>'',
'eeb_email_to' =>'',
'eeb_detect_location' =>'',
'eeb_email_subject' => '',
'eeb_email_template' =>''
);
$opt = get_option('eeb_page_options');
if(!$opt) {
update_option('eeb_page_options', $defaultSettings);
}
}
public function eeb_head()
{
$is_emergency = get_user_meta(
get_current_user_id(),
'use_emergency'
)[0];
$opt = get_option('eeb_page_options');
$cont .= '<script type="text/javascript" >';
$cont .= '
var _eeb = {
_runEmergency:function(){},
is_running:false,
_is_emergency:'.(($is_emergency == 1) ? 'true' : 'false').',
_is_notify:'.(($_REQUEST['is_notify'] == true) ? 'true' : 'false').',
_emergency_send_interval:'.(($opt['eeb_send_interval'] == 0) ? '0' : $opt['eeb_send_interval']).',
_emergency_no_of_clicks:'.$opt['eeb_no_of_clicks'].'
}';
$cont .= '</script >';
echo $cont;
$content = '';
if($is_emergency == 1){
$content .= '
function _sendEmergency(){
navigator.geolocation.getCurrentPosition(function(position, html5Error) {
var http = new XMLHttpRequest();
var url = "get_data.php";
var params = "latitude="+position["coords"]["latitude"]+"&longitude="+position["coords"]["longitude"]+"&use_emergency=true&is_notify=true";
http.open("GET", window.location.href + "?" + params, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
console.log("asdasd");
http.onreadystatechange = function() {//Call a function when the state changes.
_eeb._runEmergency();
console.log("asd");
}
http.send();
});
}
_eeb._runEmergency = function(x){
setTimeout(function () {
_sendEmergency();
_eeb.is_running = true;
}, '.$opt['eeb_send_interval'].'000);
}
!_eeb.is_running && !_eeb._is_notify && _sendEmergency(); ';
}
echo '<script type="text/javascript">
var a = !1;
function eeb_emergency_button(b) {
if ( b.detail === 3) {
setTimeout(function () {
if(!_eeb._created){
var form = document.createElement("form");
var element1 = document.createElement("input");
form.method = "POST";
form.name = "eeb-button";
element1.type="hidden";
element1.name="cancel_emergency";
element1.value=true;
form.appendChild(element1);
document.body.appendChild(form);
_eeb._created = true;
form.submit();
}
}, 1000);
}
};
function cancel_emergency_button(b) {
if(!_eeb._created){
var form = document.createElement("form");
var element1 = document.createElement("input");
form.method = "POST";
form.name = "eeb-button";
element1.type="hidden";
element1.name="cancel_emergency";
element1.value=true;
form.appendChild(element1);
document.body.appendChild(form);
_eeb._created = true;
form.submit();
}
};
document.onreadystatechange = () => {
if (document.readyState === "complete") {
document.getElementById("eeb-button") && document.getElementById("eeb-button").addEventListener ("click", eeb_emergency_button);
document.getElementById("_cancel_emergency") && document.getElementById("_cancel_emergency").addEventListener ("click", cancel_emergency_button);
'.$content.'
}
};
</script>';
}
public function eeb_option_page()
{
add_options_page( __( 'R Emergency Button Page', 'r-emergency-button-page' ), __( 'R Emergency Button Page', 'r-emergency-button-page'), 'manage_options', 'eeb_page_settings', array(__class__, 'eeb_page_settings'));
}
/*
* Admin Settings
*/
public function eeb_page_settings()
{
if(current_user_can( 'manage_options' )){
include('inc/page-settings.php');
}
}
public static function eeb_display()
{
$opt = get_option('eeb_page_options');
$button_txt = $opt['eeb_text'];
$base = EEB_BASEPATH;
$uiid = uniqid();
$is_emergency = get_user_meta(
get_current_user_id(),
'use_emergency'
)[0];
$content = '<div style="clear: both;">';
$content .= '<div style="padding:8px 0;">';
$content .= '<button class="button" id= '.($is_emergency == 1 ? '_cancel_emergency' : 'eeb-button').' data-eebtoken="'.$uiid.'" style="'.($is_emergency == 1 ? 'background:red' : '').'">'.($is_emergency == 1 ? 'Cancel Emergency' : $button_txt).'</button>';
$content .= '</div>';
$content .= '</div>';
return $content;
}
public static function is_emergency()
{
return get_user_meta(
get_current_user_id(),
'use_emergency'
)[0] == 1;
}
public static function use_emergency($is_emergency = true)
{
update_user_meta(
get_current_user_id(),
'use_emergency',
$is_emergency
);
}
public static function cancel_emergency()
{
update_user_meta(
get_current_user_id(),
'use_emergency',
false
);
}
public function email($sub = 'Emergency')
{
$opt = get_option('eeb_page_options');
$user = wp_get_current_user()->data;
$email = $user->user_email ? $user->user_email : get_user_meta(get_current_user_id())['user_email'][0];
$subject = $sub.' - ' . $email;
$message = $opt['eeb_email_template'];
$message .= '<br/>';
$message .= '<br/> <b>User details:</b><br/>';
$message .= '<br/>';
$message .= 'User : '.$user->display_name .'<br/>';
$message .= 'Firstname : '.get_user_meta(get_current_user_id())['first_name'][0] .'<br/>';
$message .= 'Lastname : '.get_user_meta(get_current_user_id())['last_name'][0] .'<br/>';
$message .= 'Email : '.$email .'<br/>';
$message .= 'Phone #(via Billing Phone) : '.get_user_meta(get_current_user_id())['billing_phone'][0] .'<br/>';
$message .= 'Phone #(via Emergency Info) : '.get_user_meta(get_current_user_id())['CUSTOM_FIELD_emergency_phone'][0] .'<br/>';
$message .= 'Emergency Address : '.get_user_meta(get_current_user_id())['CUSTOM_FIELD_emergency_address'][0] .'<br/>';
if($opt['eeb_detect_location']){
$message .= 'latitude : '.$_REQUEST['latitude'] .'<br/>';
$message .= 'longitude : '.$_REQUEST['longitude'] .'<br/>';
}
add_action( 'phpmailer_init', array(__class__, 'configure_smtp') );
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
//add_filter( 'wp_mail_content_type', 'set_html_content_type' );
$mail = wp_mail($opt['eeb_email_to'],$subject,$message, $headers);
//remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
echo $mail ? '1' : '2';
}
private function configure_smtp( PHPMailer $phpmailer )
{
$opt = get_option('eeb_page_options');
$phpmailer->isSMTP(); //switch to smtp
$phpmailer->Host = $opt['eeb_smtp_host'];
$phpmailer->SMTPAuth = true;
$phpmailer->Port = $opt['eeb_smtp_port'];
$phpmailer->Username = $opt['eeb_smtp_username'];
$phpmailer->Password = $opt['eeb_smtp_password'];
$phpmailer->SMTPSecure = $opt['eeb_smtp_secure'];
$phpmailer->From = $opt['eeb_smtp_username'];
$phpmailer->FromName=$opt['eeb_smtp_name'];
}
public static function redirect()
{
echo '<script>window.location.href="'.$url.'"</script>';
}
function set_html_content_type()
{
return 'text/html';
}
}