PluginProbe
Getwid – Gutenberg Blocks / 2.1.3
Getwid – Gutenberg Blocks v2.1.3
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / libraries / mailchimp-api / src / Webhook.php

Webhook.php in Getwid – Gutenberg Blocks 2.1.3, at includes/libraries/mailchimp-api/src/Webhook.php

94 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace DrewM\MailChimp;
4
5 /**
6 * A MailChimp Webhook request.
7 * How to Set Up Webhooks: http://eepurl.com/bs-j_T
8 *
9 * @author Drew McLellan <drew.mclellan@gmail.com>
10 */
11 class Webhook
12 {
13 private static $eventSubscriptions = array();
14 private static $receivedWebhook = null;
15
16 /**
17 * Subscribe to an incoming webhook request. The callback will be invoked when a matching webhook is received.
18 *
19 * @param string $event Name of the webhook event, e.g. subscribe, unsubscribe, campaign
20 * @param callable $callback A callable function to invoke with the data from the received webhook
21 *
22 * @return void
23 */
24 public static function subscribe($event, callable $callback)
25 {
26 if (!isset(self::$eventSubscriptions[$event])) self::$eventSubscriptions[$event] = array();
27 self::$eventSubscriptions[$event][] = $callback;
28
29 self::receive();
30 }
31
32 /**
33 * Retrieve the incoming webhook request as sent.
34 *
35 * @param string $input An optional raw POST body to use instead of php://input - mainly for unit testing.
36 *
37 * @return array|false An associative array containing the details of the received webhook
38 */
39 public static function receive($input = null)
40 {
41 if (is_null($input)) {
42 if (self::$receivedWebhook !== null) {
43 $input = self::$receivedWebhook;
44 } else {
45 $input = file_get_contents("php://input");
46 }
47 }
48
49 if (!is_null($input) && $input != '') {
50 return self::processWebhook($input);
51 }
52
53 return false;
54 }
55
56 /**
57 * Process the raw request into a PHP array and dispatch any matching subscription callbacks
58 *
59 * @param string $input The raw HTTP POST request
60 *
61 * @return array|false An associative array containing the details of the received webhook
62 */
63 private static function processWebhook($input)
64 {
65 self::$receivedWebhook = $input;
66 parse_str($input, $result);
67 if ($result && isset($result['type'])) {
68 self::dispatchWebhookEvent($result['type'], $result['data']);
69 return $result;
70 }
71
72 return false;
73 }
74
75 /**
76 * Call any subscribed callbacks for this event
77 *
78 * @param string $event The name of the callback event
79 * @param array $data An associative array of the webhook data
80 *
81 * @return void
82 */
83 private static function dispatchWebhookEvent($event, $data)
84 {
85 if (isset(self::$eventSubscriptions[$event])) {
86 foreach (self::$eventSubscriptions[$event] as $callback) {
87 $callback($data);
88 }
89 // reset subscriptions
90 self::$eventSubscriptions[$event] = array();
91 }
92 }
93 }
94