forked from bamadesigner/rock-the-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock-the-slackbot.php
424 lines (358 loc) · 11 KB
/
rock-the-slackbot.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
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
<?php
/**
* Plugin Name: Rock The Slackbot
* Plugin URI: https://wordpress.org/plugins/rock-the-slackbot/
* Description: Rock The Slackbot helps you stay on top of changes by sending notifications straight to you and your team inside your Slack account.
* Version: 1.0.0
* Author: Rachel Carden
* Author URI: http://bamadesigner.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: rock-the-slackbot
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// If you define them, will they be used?
define( 'ROCK_THE_SLACKBOT_VERSION', '1.0.0' );
define( 'ROCK_THE_SLACKBOT_PLUGIN_URL', 'https://wordpress.org/plugins/rock-the-slackbot/' );
// Load the files
require_once plugin_dir_path( __FILE__ ) . 'includes/hooks.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/notifications.php';
// We only need you in the admin
if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/admin.php';
}
class Rock_The_Slackbot {
/**
* Holds the class instance.
*
* @since 1.0.0
* @access private
* @var Rock_The_Slackbot
*/
private static $instance;
/**
* Returns the instance of this class.
*
* @access public
* @since 1.0.0
* @return Rock_The_Slackbot
*/
public static function instance() {
if ( ! isset( static::$instance ) ) {
$className = __CLASS__;
static::$instance = new $className;
}
return static::$instance;
}
/**
* Warming up the Slack mobile.
*
* @access public
* @since 1.0.0
*/
protected function __construct() {
// Load our textdomain
add_action( 'init', array( $this, 'textdomain' ) );
// Runs on install
register_activation_hook( __FILE__, array( $this, 'install' ) );
// Runs when the plugin is upgraded
add_action( 'upgrader_process_complete', array( $this, 'upgrader_process_complete' ), 1, 2 );
}
/**
* Method to keep our instance from being cloned.
*
* @since 1.0.0
* @access private
* @return void
*/
private function __clone() {}
/**
* Method to keep our instance from being unserialized.
*
* @since 1.0.0
* @access private
* @return void
*/
private function __wakeup() {}
/**
* Runs when the plugin is installed.
*
* @TODO Set it up so it will store what post types are registered
* when the settings are first saved and so then it can recognize
* when new post types are added and ask you in the admin if you
* want to exclude them to your notifications?
*
* @access public
* @since 1.0.0
*/
public function install() {}
/**
* Runs when the plugin is upgraded.
*
* @access public
* @since 1.0.0
*/
public function upgrader_process_complete() {}
/**
* Internationalization FTW.
* Load our textdomain.
*
* @TODO Add language files
*
* @access public
* @since 1.0.0
*/
public function textdomain() {
load_plugin_textdomain( 'rock-the-slackbot', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* In order to send links in messages to Slack,
* you have to wrap them with <>, e.g. <http://wordpress.org>.
*
* This function will remove the <> around links.
*
* @access public
* @since 1.0.0
* @param string - $text - the text that has links
* @return string - the formatted text
*/
public function unformat_slack_links( $text ) {
return preg_replace( '/\<(http([^\>])+)\>/i', '${1}', $text );
}
/**
* Returns all of our webhook events.
*
* @access public
* @since 1.0.0
* @return array - array of names of notification events
*/
public function get_webhook_events() {
return array(
'content' => array(
'label' => __( 'Content', 'rock-the-slackbot' ),
'events' => array(
'post_published' => array(
'label' => __( 'When a post is published', 'rock-the-slackbot' ),
'default' => 1,
),
'post_unpublished' => array(
'label' => __( 'When a post is unpublished', 'rock-the-slackbot' ),
'default' => 1,
),
'post_updated' => array(
'label' => __( 'When a post is updated', 'rock-the-slackbot' ),
'default' => 1,
),
'post_deleted' => array(
'label' => __( 'When a post is deleted', 'rock-the-slackbot' ),
),
'post_trashed' => array(
'label' => __( 'When a post is trashed', 'rock-the-slackbot' ),
),
'is_404' => array(
'label' => __( 'When a 404 error is thrown', 'rock-the-slackbot' ),
),
),
),
'menus' => array(
'label' => __( 'Menus', 'rock-the-slackbot' ),
'events' => array(
'menu_item_deleted' => array(
'label' => __( 'When a menu item is deleted', 'rock-the-slackbot' ),
),
),
),
'media' => array(
'label' => __( 'Media', 'rock-the-slackbot' ),
'events' => array(
'add_attachment' => array(
'label' => __( 'When media is added', 'rock-the-slackbot' ),
),
'edit_attachment' => array(
'label' => __( 'When media is edited', 'rock-the-slackbot' ),
),
'delete_attachment' => array(
'label' => __( 'When media is deleted', 'rock-the-slackbot' ),
),
),
),
'users' => array(
'label' => __( 'Users', 'rock-the-slackbot' ),
'events' => array(
'user_added' => array(
'label' => __( 'When a user is added', 'rock-the-slackbot' ),
),
'user_deleted' => array(
'label' => __( 'When a user is deleted', 'rock-the-slackbot' ),
),
)
),
'updates' => array(
'label' => __( 'Updates', 'rock-the-slackbot' ),
'events' => array(
'core_updated' => array(
'label' => __( 'When WordPress core is updated', 'rock-the-slackbot' ),
'default' => 1,
),
'plugin_updated' => array(
'label' => __( 'When a plugin is updated', 'rock-the-slackbot' ),
),
'theme_updated' => array(
'label' => __( 'When a theme is updated', 'rock-the-slackbot' ),
),
),
),
);
}
/**
* Returns all of our outgoing webhooks, no matter their status.
*
* @access public
* @since 1.0.0
* @return array|false - array of webhook or false if none exist
*/
public function get_all_outgoing_webhooks() {
return get_option( 'rock_the_slackbot_outgoing_webhooks', array() );
}
/**
* Returns all of our active outgoing webhooks.
*
* @access public
* @since 1.0.0
* @return array|false - array of webhooks or false if none exist
*/
public function get_active_outgoing_webhooks() {
// Get all outgoing webhooks
if ( ! ( $outgoing_webhooks = $this->get_all_outgoing_webhooks() ) ) {
return false;
}
// Go through and pick out active outgoing webhooks
$active_outgoing_webhooks = array();
// Check for not deactivated hooks
foreach( $outgoing_webhooks as $hook ) {
if ( ! ( isset( $hook[ 'deactivate' ] ) && $hook[ 'deactivate' ] > 0 ) ) {
$active_outgoing_webhooks[] = $hook;
}
}
return ! empty( $active_outgoing_webhooks ) ? $active_outgoing_webhooks : false;
}
/**
* Returns active outgoing webhooks, allows you to filter by event.
*
* @access public
* @since 1.0.0
* @param string|array - $events - if provided, only return webhooks with these events
* @param array - $event_data - allows hooks to pass event specific data to test with webhooks
* @return array|false - array of webhooks or false if none exist
*/
public function get_outgoing_webhooks( $events = null, $event_data = array() ) {
// Get active outgoing webhooks
if ( ! ( $outgoing_webhooks = $this->get_active_outgoing_webhooks() ) ) {
return false;
}
// If we're not filtering by event, then get out of here
if ( empty( $events ) ) {
return $outgoing_webhooks;
} else {
// Make sure events is an array
if ( ! is_array( $events ) ) {
$events = explode( ',', str_replace( ' ', '', $events ) );
}
// Filter by event
$filtered_webhooks = array();
// Did we pass data? Make sure its ready to go
if ( ! empty( $event_data ) ) {
// Did we pass a post type?
if ( isset( $event_data[ 'post_type' ] ) ) {
// Make sure its an array
if ( ! is_array( $event_data[ 'post_type' ] ) ) {
$event_data[ 'post_type' ] = explode( ',', $event_data[ 'post_type' ] );
}
}
}
// Go through and check for the event
foreach( $outgoing_webhooks as $hook ) {
// If we have excluded post types and a post type was sent, then skip this webhook
if ( isset( $event_data[ 'post_type' ] ) && isset( $hook[ 'exclude_post_types' ] ) ) {
// Make sure its an array
if ( ! is_array( $hook[ 'exclude_post_types' ] ) ) {
$hook[ 'exclude_post_types' ] = explode( ',', $hook[ 'exclude_post_types' ] );
}
// Check to see if the post type sent is supposed to be excluded
if ( array_intersect( $event_data[ 'post_type' ], $hook[ 'exclude_post_types' ] ) ) {
continue;
}
}
// Check the events
if ( ! empty( $hook[ 'events' ] ) && is_array( $hook[ 'events' ] ) ) {
foreach( $events as $event ) {
// This webhook has the event we're looking for
if ( array_key_exists( $event, $hook[ 'events' ] ) ) {
// Get the event settings
$event_settings = $hook[ 'events' ][ $event ];
// Don't include if not active
if ( ! ( isset( $event_settings[ 'active' ] ) && $event_settings[ 'active' ] == 1 ) ) {
continue;
}
// If this event has excluded post types and a post type was sent, then skip this webhook
if ( isset( $event_data[ 'post_type' ] ) && isset( $event_settings[ 'exclude_post_types' ] ) ) {
// Make sure its an array
if ( ! is_array( $event_settings[ 'exclude_post_types' ] ) ) {
$event_settings[ 'exclude_post_types' ] = explode( ',', $event_settings[ 'exclude_post_types' ] );
}
// Check to see if the post type sent is supposed to be excluded
if ( array_intersect( $event_data[ 'post_type' ], $event_settings[ 'exclude_post_types' ] ) ) {
continue;
}
}
// Add the webhook
$filtered_webhooks[] = $hook;
break;
}
}
}
}
return ! empty( $filtered_webhooks ) ? $filtered_webhooks : false;
}
}
/**
* Returns a specific outgoing webhook.
*
* @access public
* @since 1.0.0
* @param string - $hook_id - the hook ID
* @return array|false - the webhook or false if it doesn't exist
*/
public function get_outgoing_webhook( $hook_id ) {
// Get all outgoing webhooks
if ( ! ( $outgoing_webhooks = $this->get_all_outgoing_webhooks() ) ) {
return false;
}
// Go through and find one with ID
foreach( $outgoing_webhooks as $hook ) {
if ( isset( $hook[ 'ID' ] ) && $hook[ 'ID' ] == $hook_id ) {
return $hook;
}
}
return false;
}
}
/**
* Returns the instance of our main Rock_The_Slackbot class.
*
* Will come in handy when we need to access the
* class to retrieve data throughout the plugin.
*
* @since 1.0.0
* @access public
* @return Rock_The_Slackbot
*/
function rock_the_slackbot() {
return Rock_The_Slackbot::instance();
}
// Let's get this show on the road
rock_the_slackbot();