-
Notifications
You must be signed in to change notification settings - Fork 0
/
when-last-login-slack-notifications.php
252 lines (170 loc) · 8.21 KB
/
when-last-login-slack-notifications.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
<?php
/**
* Plugin Name: When Last Login - Slack Notifications
* Plugin URI: https://yoohooplugins.com/plugins/when-last-login-slack-notifications
* Description: Adds functionality to WordPress to show when a user last logged in. Sends a notification via Slack when this is done.
* Version: 1.1
* Author: Yoohoo Plugins
* Author URI: https://www.whenlastlogin.com
* Text Domain: when-last-login-slack-notifications
*/
class WhenLastLoginSlackNotifications{
public function __construct(){
add_action( 'wll_logged_in_action', array( $this, 'wll_sn_logged_in' ), 10, 1 );
add_action( 'admin_head', array( $this, 'wll_sn_admin_head' ) );
add_action( 'admin_menu', array ( $this, 'wll_sn_settings_submenu') );
add_action( 'user_register', array( $this, 'wll_sn_new_user_notification' ) );
add_filter( 'wll_settings_page_tabs', array( $this, 'wll_sn_settings_tab' ) );
add_filter( 'wll_settings_page_content', array( $this, 'wll_sn_settings_content' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'wll_sn_admin_scripts' ) );
}
public function wll_sn_admin_scripts(){
if ( ( isset( $_GET['page'] ) && $_GET['page'] == 'when-last-login-settings' ) && ( isset( $_GET['tab'] ) && $_GET['tab'] == 'slack-notifications' ) ){
wp_enqueue_script( 'wll-sn-admin-script', plugins_url( '/js/admin.js', __FILE__ ), array( 'jquery' ) );
}
}
public function wll_sn_logged_in( $array ){
if( isset( $array['user'] ) ){
$user = $array['user'];
$user_id = $user->ID;
$user_display_name = $user->data->display_name;
$settings = get_option( 'wll_sn_settings' );
$user_role_matches = false;
$send_notification = false;
if( $settings && $settings != "" && isset( $settings['webhook'] ) ){
if( isset( $settings['user_role'] ) ){
if( $settings['user_role'] == 'all' ){
$user_role_matches = true;
} else {
foreach( $user->roles as $role ){
if( $role == $settings['user_role'] ){
$user_role_matches = true;
}
}
}
} else {
$user_role_matches = true;
}
$wll_current_time = current_time( 'timestamp' );
if( $user_role_matches ){
$start_time_h = isset( $settings['start_time_h'] ) ? $settings['start_time_h'] : "";
$start_time_m = isset( $settings['start_time_m'] ) ? $settings['start_time_m'] : "";
$end_time_h = isset( $settings['end_time_h'] ) ? $settings['end_time_h'] : "";
$end_time_m = isset( $settings['end_time_m'] ) ? $settings['end_time_m'] : "";
$day_of_week = date( 'w' );
if( $start_time_h == "" || $start_time_m == "" || $end_time_h == "" || $end_time_m = "" ){
$send_notification = true;
} else {
$date_start = strtotime( date( 'Y-m-d' ).' '.$start_time_h.':'.$start_time_m.':00' );
$date_end = strtotime( date( 'Y-m-d' ).' '.$end_time_h.':'.$end_time_m.':00' );
if( $settings['timeslot'] == 'every_day' ){
$send_notification = true;
} else if( $settings['timeslot'] == 'weekdays' ){
if( $day_of_week !== 0 && $day_of_week !== 6 ){
//Its a week day
if( $wll_current_time > $date_start && $wll_current_time < $date_end ){
$send_notification = true;
}
}
} else if( $settings['timeslot'] == 'weekends' ){
if( $day_of_week == 0 || $day_of_week == 6 ){
//Its a weekend
if( $wll_current_time > $date_start && $wll_current_time < $date_end ){
$send_notification = true;
}
}
} else if( $settings['timeslot'] == 'both' ){
if( $wll_current_time > $date_start && $wll_current_time < $date_end ){
$send_notification = true;
}
}
}
}
if( $send_notification ){
$wll_site_url = get_option( 'siteurl' );
$text = sprintf( __('%s has logged in to %s at %s', 'when-last-login-slack-notifications' ), $user_display_name, $wll_site_url, date( 'Y-m-d H:i:s', $wll_current_time ) );
if ( 1 === $settings['show_ip'] ) {
$ip = When_Last_Login::wll_get_user_ip_address();
$text .= " " . __( 'from', 'when-last-login-slack-notifications' ) . " ";
$text .= $ip;
}
$text = apply_filters( 'wll_slack_login_text_filter', $text );
$payload = array(
'text' => $text,
'username' => __( 'When Last Login - Slack Notification', 'when-last-login-slack-notifications' )
);
$output = 'payload=' . json_encode( $payload );
$response = wp_remote_post( $settings['webhook'], array( 'body' => $output ) );
}
}
}
}
public function wll_sn_new_user_notification( $user_id ) {
$settings = get_option( 'wll_sn_settings' );
if ( 1 != $settings['new_user_notification'] ) {
return;
}
$wll_site_url = get_option( 'siteurl' );
$wll_current_time = current_time( 'timestamp' );
$user = get_user_by( 'ID', $user_id );
$text = sprintf( __( '%s has just registered on %s at %s', 'when-last-login-slack-notifications' ), $user->display_name, $wll_site_url, date( 'Y-m-d H:i:s', $wll_current_time ) );
$text = apply_filters( 'wll_slack_register_text_filter', $text, $user );
$payload = array(
'text' => $text,
'username' => __( 'When Last Login - Slack Notification', 'when-last-login-slack-notifications' )
);
$output = 'payload=' . json_encode( $payload );
$response = wp_remote_post( $settings['webhook'], array( 'body' => $output ) );
}
public function wll_sn_settings_submenu() {
add_submenu_page( 'when-last-login-settings', __( 'Slack Notifications', 'when-last-login-slack-notifications' ), __( 'Slack Notifications', 'when-last-login-slack-notifications' ), 'manage_options', '?page=when-last-login-settings&tab=slack-notifications' );
}
public function wll_sn_settings_tab( $array ){
$array['slack-notifications'] = array(
'title' => __( 'Slack Notifications', 'when-last-login-slack-notifications' ),
'icon' => ''
);
return $array;
}
public function wll_sn_settings_content( $content ){
$content['slack-notifications'] = plugin_dir_path( __FILE__ ).'/when-last-login-slack-notifications-settings.php';
return $content;
}
public function wll_sn_admin_head(){
if( isset( $_POST['wll_sn_save_settings'] ) ){
if( isset( $_POST['wll_sn_webhook_url'] ) ){
$wll_sn_settings = array(
'webhook' => isset( $_POST['wll_sn_webhook_url'] ) ? sanitize_text_field( $_POST['wll_sn_webhook_url'] ) : "",
'user_role' => isset( $_POST['wll_sn_notify_specific_user_role'] ) ? sanitize_text_field( $_POST['wll_sn_notify_specific_user_role'] ) : "",
'new_user_notification' => isset( $_POST['wll_sn_new_user_notification'] ) ? (int) $_POST['wll_sn_new_user_notification'] : "",
'show_ip' => isset( $_POST['wll_sn_include_IP'] ) ? (int) $_POST['wll_sn_include_IP'] : "",
'timeslot' => isset( $_POST['wll_sn_notify_timeslot'] ) ? sanitize_text_field( $_POST['wll_sn_notify_timeslot'] ) : "",
'start_time_h' => isset( $_POST['wll_sn_notify_start_time_hours'] ) ? (int) $_POST['wll_sn_notify_start_time_hours'] : "",
'start_time_m' => isset( $_POST['wll_sn_notify_start_time_minutes'] ) ? (int) $_POST['wll_sn_notify_start_time_minutes'] : "",
'end_time_h' => isset( $_POST['wll_sn_notify_end_time_hours'] ) ? (int) $_POST['wll_sn_notify_end_time_hours'] : "",
'end_time_m' => isset( $_POST['wll_sn_notify_end_time_minutes'] ) ? (int) $_POST['wll_sn_notify_end_time_minutes'] : ""
);
$updated = update_option( 'wll_sn_settings', $wll_sn_settings );
if( $updated ){
echo "<div class='notice notice-success is-dismissible'><p>".__('Settings successfully updated', 'when-last-login-slack-notifications')."</p></div>";
}
}
}
}
public static function wll_sn_time_array(){
$h = array();
for( $i = 0; $i <= 23; $i++){
$h[] = sprintf('%02d', $i );
}
$m = array();
for( $i = 0; $i <= 59; $i++){
$m[] = sprintf('%02d', $i );
}
$t = array(
'hours' => $h,
'minutes' => $m
);
return $t;
}
}
new WhenLastLoginSlackNotifications();