コード例 #1
0
 /**
  * Sends a batch of outgoing webhooks.
  *
  * @access  private
  * @since   1.0.0
  * @param   string - $notification_event - name of the notification event.
  * @param   array - $outgoing_webhooks - array of webhooks being sent.
  * @param   array - $payload - payload info for notification.
  * @param   array - $attachments - attachments info for notification.
  * @param   array - $event_args - event specific information to send to the filters.
  * @return  true|array - true if all notifications were sent, array of error(s) otherwise
  */
 private function send_outgoing_webhooks($notification_event, $outgoing_webhooks, $payload = array(), $attachments = array(), $event_args = array())
 {
     // Will hold notification errors if any.
     $notification_errors = array();
     // Loop through each webhook and send the notification.
     foreach ($outgoing_webhooks as $hook) {
         // We must have a webhook URL.
         $webhook_url = !empty($hook['webhook_url']) ? $hook['webhook_url'] : false;
         if (!$webhook_url) {
             continue;
         }
         // Prepare the payload.
         $payload = $this->prepare_payload($payload, $attachments, $hook, $notification_event);
         // Setup the pieces.
         $notification_pieces = compact(array('webhook_url', 'payload'));
         // Filter by event.
         $notification_pieces = (array) apply_filters("rock_the_slackbot_notification_{$notification_event}", $notification_pieces, $notification_event, $event_args);
         // Filter by hook ID.
         $notification_pieces = (array) apply_filters('rock_the_slackbot_notification_' . $hook['ID'], $notification_pieces, $notification_event, $event_args);
         // General filter.
         $notification_pieces = (array) apply_filters('rock_the_slackbot_notification', $notification_pieces, $notification_event, $event_args);
         // Assign the notification pieces to variables.
         $notification_webhook_url = !empty($notification_pieces['webhook_url']) ? $notification_pieces['webhook_url'] : '';
         $notification_payload = !empty($notification_pieces['payload']) ? $notification_pieces['payload'] : array();
         // Send the notification.
         $sent_notification = rock_the_slackbot_outgoing_webhooks()->send_payload($notification_webhook_url, $notification_payload);
         // Was there an error?
         if (is_wp_error($sent_notification)) {
             // Add to errors.
             $notification_errors[] = $sent_notification;
             // Should we send an error email?
             if (isset($hook['send_error_email']) && $hook['send_error_email'] > 0) {
                 // Define the error email address.
                 $error_email_address = !empty($hook['send_error_email_address']) ? $hook['send_error_email_address'] : get_bloginfo('admin_email');
                 // Send the error email.
                 rock_the_slackbot()->send_error_email($error_email_address, array('webhook_url' => $webhook_url, 'payload' => $payload));
             }
         }
     }
     // Return errors, if any, otherwise true for no errors.
     return !empty($notification_errors) ? $notification_errors : true;
 }
コード例 #2
0
 /**
  * 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;
 }