Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Last active May 11, 2017 20:01
Show Gist options
  • Save macbookandrew/7f41ff7d4b28300f19bd to your computer and use it in GitHub Desktop.
Save macbookandrew/7f41ff7d4b28300f19bd to your computer and use it in GitHub Desktop.
Add Slack notifications for comments posted via the Epoch comment system
<?php
/*
Plugin Name: Slack Epoch Comments
Plugin URI: https://gist.github.com/macbookandrew/7f41ff7d4b28300f19bd
Description: Adds Slack notifications for comments posted via the Epoch comment system
Version: 1.1
Author: AndrewRMinion Design
Author URI: https://andrewrminion.com
*/
// add Epoch comment system to Slack notifications
function wp_slack_epoch( $events ) {
$events['comment_post'] = array(
'action' => 'comment_post',
'description' => __( 'When a comment is posted via Epoch comment system', 'slack' ),
'message' => function( $comment_id ) {
$comment = get_comment( absint( $comment_id ) );
$post_id = $comment->comment_post_ID;
$notified_post_types = apply_filters( 'slack_event_wp_insert_comment_post_types', array(
'post',
'course_unit',
) );
if ( ! in_array( get_post_type( $post_id ), $notified_post_types ) ) {
return false;
}
$post_title = get_the_title( $post_id );
$comment_status = wp_get_comment_status( $comment_id );
// get user’s nicename if available
$user = get_user_by( 'email', $comment->comment_author_email );
if ( $user->data->display_name ) {
$user_name = $user->data->display_name;
} else {
$user_name = $comment->comment_author;
}
// ignore spam
if ( 'spam' === $comment_status ) {
return false;
}
return sprintf(
'<%4$s#comment-%1$s|New comment> by *%3$s* on *<%4$s|%5$s>* (_%2$s_)' . "\n" . '>%7$s' . "\n" . '<%6$sapprove|Approve it> | <%6$strash|Trash it> | <%6$sspam|Spam it> | <%4$s#comment-%1$s|See in context> | <%4$s?replytocom=%1$s#respond|Reply>',
$comment_id,
$comment_status,
$user_name,
get_permalink( $post_id ),
$post_title,
admin_url( "comment.php?c=$comment_id&action=" ),
preg_replace( "/\n/", "\n>", get_comment_text( $comment_id ) )
);
}
);
return $events;
}
add_filter( 'slack_get_events', 'wp_slack_epoch' );
@macbookandrew
Copy link
Author

v1.0: initial plugin

@macbookandrew
Copy link
Author

v1.1: add support for user nice names, if available

@macbookandrew
Copy link
Author

v1.1.1: don’t show approve link if comment is already approved

@macbookandrew
Copy link
Author

v1.1.2: fix message if comment needs approval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment