forked from bamadesigner/rock-the-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock-the-slackbot.php
746 lines (633 loc) · 20.2 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
<?php
/**
* Plugin Name: Rock The Slackbot
* Plugin URI: https://wordpress.org/plugins/rock-the-slackbot/
* Description: Helps you stay on top of changes by sending notifications straight to you and your team inside your Slack account.
* Version: 1.1.2
* Author: Rachel Carden
* Author URI: https://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
*
* @package Rock_The_Slackbot
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/*
* @TODO:
*
* Add setting to disable 404 notifications from search engines
* or to allow blacklist for notifications?
*
* Add some identifiable info to the "Exclude Post Types" life
* for when CPTs share the same label (from Matt).
*/
// Load the files.
require_once plugin_dir_path( __FILE__ ) . 'includes/hooks.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/outgoing-webhooks.php';
// We only need you in the admin.
if ( is_admin() ) {
require_once plugin_dir_path( __FILE__ ) . 'includes/admin.php';
}
/**
* Class the holds the root
* functionality for the plugin.
*
* @package Rock_The_Slackbot
*/
class Rock_The_Slackbot {
/**
* Whether or not this plugin is network active.
*
* @since 1.1.0
* @access public
* @var boolean
*/
public $is_network_active;
/**
* Holds the plugin version number.
*
* @since 1.1.2
* @access private
* @var string
*/
private $version = '1.1.2';
/**
* Holds the plugin's URL.
*
* @since 1.1.2
* @access private
* @var string
*/
private $plugin_url = 'https://wordpress.org/plugins/rock-the-slackbot/';
/**
* Holds the plugin's
* relative file path.
*
* @since 1.1.2
* @access private
* @var string
*/
private $plugin_file = 'rock-the-slackbot/rock-the-slackbot.php';
/**
* 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( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
/**
* Warming up the Slack mobile.
*
* @access protected
* @since 1.0.0
*/
protected function __construct() {
// Is this plugin network active?
$this->is_network_active = false;
if ( is_multisite() ) {
// Get the list of network active plugins.
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( ! empty( $plugins ) ) {
// Marks true if our plugin is network active.
$plugin_file = $this->get_plugin_file();
$this->is_network_active = $plugin_file && ! empty( $plugins[ $plugin_file ] );
}
}
// Load our text domain.
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.
*
* @access public
* @since 1.0.0
*/
public function textdomain() {
load_plugin_textdomain( 'rock-the-slackbot', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Returns the plugin version.
*
* @access public
* @since 1.1.2
* @return string
*/
public function get_version() {
return $this->version;
}
/**
* Returns the plugin's URL.
*
* @access public
* @since 1.1.2
* @return string
*/
public function get_plugin_url() {
return $this->plugin_url;
}
/**
* Returns the plugin's
* relative file path.
*
* @access public
* @since 1.1.2
* @return string
*/
public function get_plugin_file() {
return $this->plugin_file;
}
/**
* 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_draft' => array(
'label' => __( 'When a post is drafted', 'rock-the-slackbot' ),
'default' => 1,
),
'post_pending' => array(
'label' => __( 'When a post is pending review', 'rock-the-slackbot' ),
'default' => 1,
),
'post_future' => array(
'label' => __( 'When a post is scheduled', 'rock-the-slackbot' ),
'default' => 1,
),
'post_updated' => array(
'label' => __( "When a post's content 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' ),
),
),
),
'comments' => array(
'label' => __( 'Comments', 'rock-the-slackbot' ),
'events' => array(
'insert_comment' => array(
'label' => __( 'When a comment is added', 'rock-the-slackbot' ),
),
'comment_unapproved' => array(
'label' => __( 'When a comment is unapproved', 'rock-the-slackbot' ),
),
'comment_approved' => array(
'label' => __( 'When a comment is approved', 'rock-the-slackbot' ),
),
'comment_spammed' => array(
'label' => __( 'When a comment is marked as spam', 'rock-the-slackbot' ),
),
'comment_trashed' => array(
'label' => __( 'When a comment is trashed', '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' ),
),
'set_user_role' => array(
'label' => __( "When a user's role has changed", 'rock-the-slackbot' ),
),
),
),
'updates' => array(
'label' => __( 'Updates', 'rock-the-slackbot' ),
'events' => array(
'core_update_available' => array(
'label' => sprintf( __( 'When a %s core update is available', 'rock-the-slackbot' ), 'WordPress' ),
'default' => 1,
),
'core_updated' => array(
'label' => sprintf( __( 'When %s core is updated', 'rock-the-slackbot' ), 'WordPress' ),
'default' => 1,
),
'plugin_update_available' => array(
'label' => __( 'When a plugin update is available', 'rock-the-slackbot' ),
),
'plugin_updated' => array(
'label' => __( 'When a plugin is updated', 'rock-the-slackbot' ),
),
'theme_update_available' => array(
'label' => __( 'When a theme update is available', '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
* @param boolean - $network - whether or not to retrieve network webhooks.
* @return array|false - array of webhook or false if none exist
*/
public function get_all_outgoing_webhooks( $network = true ) {
// Get site webhooks.
$webhooks = get_option( 'rock_the_slackbot_outgoing_webhooks', array() );
// Make sure its an array.
if ( empty( $webhooks ) ) {
$webhooks = array();
}
// Get network webhooks.
if ( $network && $this->is_network_active ) {
if ( ( $network_webhooks = get_site_option( 'rock_the_slackbot_network_outgoing_webhooks', array() ) )
&& is_array( $network_webhooks ) ) {
$webhooks = array_merge( $network_webhooks, $webhooks );
}
}
return $webhooks;
}
/**
* 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 ( ! empty( $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'] ) || isset( $hook['network_exclude_post_types'] ) ) ) {
// Get the post types we should exclude.
$exclude_post_types = array();
// Get the regular exclude post types.
if ( ! empty( $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'] );
}
// Add to list.
$exclude_post_types = $hook['exclude_post_types'];
}
// Get the network exclude post types.
if ( ! empty( $hook['network_exclude_post_types'] ) ) {
// Make sure its an array.
if ( ! is_array( $hook['network_exclude_post_types'] ) ) {
$hook['network_exclude_post_types'] = explode( ',', $hook['network_exclude_post_types'] );
}
// Add to list.
$exclude_post_types = array_merge( $exclude_post_types, $hook['network_exclude_post_types'] );
}
// Check to see if the post type sent is supposed to be excluded.
if ( array_intersect( $event_data['post_type'], $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'] ) && 1 == $event_settings['active'] ) ) {
continue;
}
// If this event has excluded post types and a post type was sent, then skip this webhook.
if ( ! empty( $event_data['post_type'] ) && ! empty( $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.
* @param boolean - $network - whether or not this is a network webhook.
* @return array|false - the webhook or false if it doesn't exist
*/
public function get_outgoing_webhook( $hook_id, $network = false ) {
// Get all outgoing webhooks.
$outgoing_webhooks = $this->get_all_outgoing_webhooks( $network );
if ( ! $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;
}
/**
* Send a simple, custom message to Slack via webhook.
*
* @access public
* @since 1.1.1
* @param string - $webhook_id_or_url - provide the webhook URL or the ID of one stored in settings.
* @param string - $message - the message you want to send.
* @param string - $channel - the channel you want to send message to, will use default channel if webhook ID is passed.
* @return boolean|WP_Error - true if sent, WP_Error if error
*/
public function send_webhook_message( $webhook_id_or_url, $message, $channel = '' ) {
// Create the payload.
$payload = array(
'channel' => $channel,
'text' => $message,
);
// Set webhook URL if what is passed is URL.
$webhook_url = preg_match( '/^http/i', $webhook_id_or_url ) ? $webhook_id_or_url : false;
// If not URL, check for ID.
if ( ! $webhook_url ) {
// Get webhook - check the network too.
$webhook = rock_the_slackbot()->get_outgoing_webhook( $webhook_id_or_url, true );
// If webhook and has URL.
if ( ! empty( $webhook['webhook_url'] ) ) {
$webhook_url = $webhook['webhook_url'];
} else {
// Return the error.
return new WP_Error( 'slack_send_message_error', __( 'The webhook ID passed is not valid.', 'rock-the-slackbot' ) );
}
}
// Send the message.
$sent_message = rock_the_slackbot_outgoing_webhooks()->send_payload( $webhook_url, $payload );
// Was there an error?
if ( is_wp_error( $sent_message ) ) {
// Return the error.
return new WP_Error( 'slack_send_message_error', $sent_message->get_error_message() );
}
return true;
}
/**
* Send the "error" email when sending a payload fails.
*
* @since 1.1.2
* @param string - the email address.
* @param array - the email arguments.
*/
public function send_error_email( $email, $args = array() ) {
// Define the defaults.
$defaults = array(
'webhook_url' => '',
'payload' => '',
);
// Merge incoming arguments.
$args = wp_parse_args( $args, $defaults );
// Build email message.
$message = sprintf( __( 'There was an error when trying to post to %1$s from %2$s.', 'rock-the-slackbot' ), 'Slack', 'WordPress' );
// Add payload URL.
if ( ! empty( $args['webhook_url'] ) ) {
$message .= "\n\n<br /><br /><strong>" . __( 'Payload URL', 'rock-the-slackbot' ) . ':</strong> ' . $args['webhook_url'];
}
// Add payload channel.
if ( ! empty( $args['payload']['channel'] ) ) {
$message .= "\n<br /><strong>" . __( 'Channel', 'rock-the-slackbot' ) . ':</strong> ' . $args['payload']['channel'];
}
// Fix any links in the general text message.
if ( ! empty( $args['payload']['text'] ) ) {
// Replace Slack links.
$args['payload']['text'] = rock_the_slackbot()->unformat_slack_links( $args['payload']['text'] );
// Add general message.
$message .= "\n\n<br /><br /><strong>" . __( 'Message', 'rock-the-slackbot' ) . ':</strong> ' . $args['payload']['text'];
}
// Add attachment info.
if ( ! empty( $args['payload']['attachments'] ) ) {
$message .= "\n\n<br /><br /><strong>" . __( 'Attachments', 'rock-the-slackbot' ) . ':</strong>';
foreach ( $args['payload']['attachments'] as $attachment ) {
$message .= "\n<br />";
// Add fields.
if ( ! empty( $attachment['fields'] ) ) {
foreach ( $attachment['fields'] as $field ) {
$message .= "\n\t<br /> <strong>" . $field['title'] . ':</strong> ' . $field['value'];
}
}
}
}
// Filter the email pieces.
$email_pieces = apply_filters( 'rock_the_slackbot_error_email', array(
'to' => $email,
'subject' => sprintf( __( '%1$s to %2$s error', 'rock-the-slackbot' ), 'WordPress', 'Slack' ),
'message' => $message,
), $args );
// Make sure we still have email pieces.
if ( ! empty( $email_pieces['to'] ) && ! empty( $email_pieces['subject'] ) ) {
// Set email to be HTML.
add_filter( 'wp_mail_content_type', 'rock_the_slackbot_set_html_content_type' );
// Send email notification to the admin.
$send_email = wp_mail( $email_pieces['to'], $email_pieces['subject'], isset( $email_pieces['message'] ) ? $email_pieces['message'] : '' );
// Reset content-type to avoid conflicts.
remove_filter( 'wp_mail_content_type', 'rock_the_slackbot_set_html_content_type' );
}
return $send_email;
}
}
/**
* Set emails to be HTML.
*
* @since 1.1.1
* @return string - email content type
*/
function rock_the_slackbot_set_html_content_type() {
return 'text/html';
}
/**
* 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();