Skip to content

Commit

Permalink
Refactor send to webhook method for versatility
Browse files Browse the repository at this point in the history
Added and additional filter to filter URL if necessary and added some contextual objects to make the filters more useful.
  • Loading branch information
hanifn committed Jun 19, 2024
1 parent 331166c commit 586e779
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions modules/notifications/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public function notification_status_change( $new_status, $old_status, $post ) {
$format = __( '*%1$s* changed the status of *%2$s #%3$s - <%4$s|%5$s>* from *%6$s* to *%7$s*', 'edit-flow' );
$text = sprintf( $format, $current_user->display_name, $post_type, $post_id, $edit_link, $post_title, $old_status_friendly_name, $new_status_friendly_name );

$this->send_to_webhook( $text );
$this->send_to_webhook( $text, 'status-change', $current_user, $post );
}
}
}
Expand Down Expand Up @@ -825,7 +825,7 @@ public function notification_comment( $comment ) {
$format .= '%6$s';
$text = sprintf( $format, $comment->comment_author, $post_type, $post_id, $edit_link, $post_title, $comment->comment_content );

$this->send_to_webhook( $text );
$this->send_to_webhook( $text, 'comment', $current_user, $comment );
}
}

Expand Down Expand Up @@ -869,25 +869,33 @@ public function send_email( $action, $post, $subject, $message, $message_headers

/**
* Send notifications to Slack
*
* @param string $message Message to be sent to webhook
* @param string $action Action being taken. Currently only `status-change` and `comment`
* @param WP_User $user User who is taking the action
* @param WP_Post|WP_Comment $post Post or comment that the action is being taken on
*/
public function send_to_webhook( $message ) {
public function send_to_webhook( $message, $action, $user, $post ) {
$webhook_url = $this->module->options->webhook_url;

// apply filters to the URL
$webhook_url = apply_filters( 'ef_notification_send_to_webhook_url', $webhook_url, $action, $user, $post );

// Bail if the webhook URL is not set
if ( empty( $webhook_url ) ) {
return;
}

// apply filters to the message
$message = apply_filters( 'ef_notification_send_to_webhook_message', $message );
$message = apply_filters( 'ef_notification_send_to_webhook_message', $message, $action, $user, $post );

// Set up the payload
$payload = array(
'text' => $message,
);

// apply filters to the payload
$payload = apply_filters( 'ef_notification_send_to_webhook_payload', $payload );
$payload = apply_filters( 'ef_notification_send_to_webhook_payload', $payload, $action, $user, $post );

// Send the notification
$response = wp_remote_post(
Expand Down

0 comments on commit 586e779

Please sign in to comment.