Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (warning)
86.96%
20 / 23
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Event
86.96% covered (warning)
86.96%
20 / 23
0.00% covered (danger)
0.00%
0 / 1
7.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
86.96% covered (warning)
86.96%
20 / 23
0.00% covered (danger)
0.00%
0 / 1
7.11
1<?php
2
3namespace NewfoldLabs\WP\Module\Data;
4
5/**
6 * Event data object
7 */
8class Event {
9
10    /**
11     * Event category
12     *
13     * @var string
14     */
15    public $category;
16
17    /**
18     * Key representing the event action that occurred
19     *
20     * @var string
21     */
22    public $key;
23
24    /**
25     * Array of extra data related to the event
26     *
27     * @var array
28     */
29    public $data;
30
31    /**
32     * Array of data about the request that triggered the event
33     *
34     * @var array
35     */
36    public $request;
37
38    /**
39     * Array of data about the user triggering the event
40     *
41     * @var array
42     */
43    public $user;
44
45    /**
46     * DateTime when the event occurred
47     *
48     * @var string
49     */
50    public $created_at;
51
52    /**
53     * Construct
54     *
55     * @param string $category General category of the event. Should match to a Listener class
56     * @param string $key      Key representing the action that occurred
57     * @param array  $data     Additional data specific to the event that occurred
58     */
59    public function __construct( $category = 'Admin', $key = '', $data = array() ) {
60        global $title;
61
62        // Event details
63        $this->created_at  = date( 'Y-m-d H:i:s.u' );
64        $this->category = strtolower( $category );
65        $this->key      = $key;
66        $this->data     = $data;
67
68        // Try to grab user request IP and account for any proxies
69        if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
70            // In case there are multiple proxies, just use the first one
71            $ip_list = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] );
72            $ip      = $ip_list[0];
73        } else {
74            $ip = $_SERVER['REMOTE_ADDR'];
75        }
76
77        // Request information
78        $this->request = array(
79            'url'        => ( isset( $this->data['page'] ) ) ? $this->data['page'] : get_site_url( null, $_SERVER['REQUEST_URI'] ),
80            'page_title' => ( isset( $this->data['page_title'] ) ) ? $this->data['page_title'] : $title,
81            'user_agent' => ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? $_SERVER['HTTP_USER_AGENT'] : '',
82            'ip'         => $ip,
83        );
84
85        // User information
86        $user       = get_user_by( 'id', get_current_user_id() );
87        $this->user = array(
88            'id'     => get_current_user_id(),
89            'login'  => ( ! empty( $user->user_nicename ) ) ? $user->user_nicename : '',
90            'role'   => ( ! empty( $user->roles[0] ) ) ? $user->roles[0] : '',
91            'locale' => get_user_locale(),
92        );
93    }
94}