enable_rest_api(); $this->enable_wp_cli(); add_action('init', [$this, 'register_webhook_listeners']); add_action('wp_ajax_wu_send_test_event', [$this, 'send_test_event']); } /** * Adds the listeners to the webhook callers, extend this by adding actions to wu_register_webhook_listeners * * @todo This needs to have a switch, allowing us to turn it on and off. * @return void. */ public function register_webhook_listeners(): void { foreach (wu_get_event_types() as $key => $event) { add_action('wu_event_' . $key, [$this, 'send_webhooks']); } } /** * Sends all the webhooks that are triggered by a specific event. * * @since 2.0.0 * * @param array $args with events slug and payload. * @return void */ public function send_webhooks($args): void { $webhooks = Webhook::get_all(); foreach ($webhooks as $webhook) { if ('wu_event_' . $webhook->get_event() === current_filter()) { $blocking = wu_get_setting('webhook_calls_blocking', false); $this->send_webhook($webhook, $args, $blocking); } } } /** * Sends a specific webhook. * * @since 2.0.0 * * @param Webhook $webhook The webhook to send. * @param array $data Key-value array of data to send. * @param boolean $blocking Decides if we want to wait for a response to keep a log. * @param boolean $count If we should update the webhook event count. * @return string|null. */ public function send_webhook($webhook, $data, $blocking = true, $count = true) { if ( ! $data) { return null; } $request = wp_remote_post( $webhook->get_webhook_url(), [ 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'headers' => [ 'Content-Type' => 'application/json', ], 'cookies' => [], 'body' => wp_json_encode($data), 'blocking' => $blocking, ] ); if (is_wp_error($request)) { $error_message = $request->get_error_message(); if ($count) { $this->create_event( $webhook->get_event(), $webhook->get_id(), $webhook->get_webhook_url(), $data, $error_message, true ); } return $error_message; } $response = ''; // if blocking, we have a response if ($blocking) { $response = wp_remote_retrieve_body($request); } if ($count) { $this->create_event( $webhook->get_event(), $webhook->get_id(), $webhook->get_webhook_url(), $data, $response ); $new_count = $webhook->get_event_count() + 1; $webhook->set_event_count($new_count); $webhook->save(); } return $response; } /** * Send a test event of the webhook * * @return void */ public function send_test_event(): void { if ( ! current_user_can('manage_network')) { wp_send_json( [ 'response' => __('You do not have enough permissions to send a test event.', 'wp-multisite-waas'), 'webhooks' => Webhook::get_items_as_array(), ] ); } $event = wu_get_event_type($_POST['webhook_event']); $webhook_data = [ 'active' => true, ]; $webhook = new Webhook($webhook_data); $response = $this->send_webhook($webhook, wu_maybe_lazy_load_payload($event['payload']), true, false); wp_send_json( [ 'response' => htmlentities2($response), 'id' => wu_request('webhook_id'), ] ); } /** * Reads the log file and displays the content. * * @return void. */ public function serve_logs(): void { echo ' '; if ( ! current_user_can('manage_network')) { esc_html_e('You do not have enough permissions to read the logs of this webhook.', 'wp-multisite-waas'); exit; } $id = absint($_REQUEST['id']); $logs = array_map( function ($line): string { $line = str_replace(' - ', ' - ', $line); $matches = []; $line = str_replace('\'', '\\\'', $line); $line = preg_replace('~(\{(?:[^{}]|(?R))*\})~', '
', $line); return '' . $line . '