Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Event
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 sendEvent
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Deactivation event.
4 *
5 * @package NewfoldLabs\WP\Module\Deactivation
6 */
7
8namespace NewfoldLabs\WP\Module\Deactivation\Events;
9
10use WP_REST_Request;
11
12/**
13 * Event class.
14 */
15class Event {
16    /**
17     * Key representing the event action that occurred.
18     *
19     * @var string
20     */
21    public $action;
22
23    /**
24     * Event category.
25     *
26     * @var string
27     */
28    public $category;
29
30    /**
31     * Array of extra data related to the event.
32     *
33     * @var array
34     */
35    public $data;
36
37    /**
38     * Data module endpoint to send the event to.
39     *
40     * @var string
41     */
42    public $endpoint = '/newfold-data/v1/events/';
43
44    /**
45     * WP_REST_Request instance.
46     *
47     * @var WP_REST_Request
48     */
49    public $request;
50
51    /**
52     * Constructor.
53     *
54     * @param string $action   Key representing the event action that occurred.
55     * @param string $category Event category.
56     * @param array  $data     Array of extra data related to the event.
57     */
58    public function __construct( $category = 'plugin_deactivation', $data = array() ) {
59        $this->category = $category;
60        $this->data     = $data;
61        $this->request  = new WP_REST_Request( 'POST', $this->endpoint );
62    }
63
64    /**
65     * Send the event to the data module endpoint.
66     *
67     * @return WP_REST_Response REST response
68     */
69    public function sendEvent() {
70        $event = array(
71            'action'   => $this->action,
72            'category' => $this->category,
73            'data'     => $this->data,
74            'queue'    => false,
75        );
76        
77        $this->request->set_body( wp_json_encode( $event ) );
78        return rest_do_request( $this->request );
79    }
80}