1 | <?php |
||
41 | class Tracks_Event { |
||
42 | const EVENT_NAME_REGEX = '/^(([a-z0-9]+)_){2}([a-z0-9_]+)$/'; |
||
43 | const PROP_NAME_REGEX = '/^[a-z_][a-z0-9_]*$/'; |
||
44 | public $error; |
||
45 | |||
46 | function __construct( $event ) { |
||
47 | $_event = self::validate_and_sanitize( $event ); |
||
48 | if ( is_wp_error( $_event ) ) { |
||
49 | $this->error = $_event; |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | foreach( $_event as $key => $value ) { |
||
54 | $this->{$key} = $value; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | function record() { |
||
61 | |||
62 | /** |
||
63 | * Annotate the event with all relevant info. |
||
64 | * @param mixed $event Object or (flat) array |
||
65 | * @return mixed The transformed event array or WP_Error on failure. |
||
66 | */ |
||
67 | static function validate_and_sanitize( $event ) { |
||
68 | $event = (object) $event; |
||
69 | |||
70 | // Required |
||
71 | if ( ! $event->_en ) { |
||
72 | return new WP_Error( 'invalid_event', 'A valid event must be specified via `_en`', 400 ); |
||
73 | } |
||
74 | |||
75 | // delete non-routable addresses otherwise geoip will discard the record entirely |
||
76 | if ( property_exists( $event, '_via_ip' ) && preg_match( '/^192\.168|^10\./', $event->_via_ip ) ) { |
||
77 | unset($event->_via_ip); |
||
78 | } |
||
79 | |||
80 | $validated = array( |
||
81 | 'browser_type' => Tracks_Client::BROWSER_TYPE, |
||
82 | '_aua' => Tracks_Client::get_user_agent(), |
||
83 | ); |
||
84 | |||
85 | $_event = (object) array_merge( (array) $event, $validated ); |
||
86 | |||
87 | // If you want to blacklist property names, do it here. |
||
88 | |||
89 | // Make sure we have an event timestamp. |
||
90 | if ( ! isset( $_event->_ts ) ) { |
||
91 | $_event->_ts = Tracks_Client::build_timestamp(); |
||
92 | } |
||
93 | |||
94 | return $_event; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Build a pixel URL that will send a Tracks event when fired. |
||
99 | * On error, returns an empty string (''). |
||
100 | * |
||
101 | * @return string A pixel URL or empty string ('') if there were invalid args. |
||
102 | */ |
||
103 | function build_pixel_url() { |
||
104 | if ( $this->error ) { |
||
105 | return ''; |
||
106 | } |
||
107 | |||
108 | $args = get_object_vars( $this ); |
||
109 | |||
110 | // Request Timestamp and URL Terminator must be added just before the HTTP request or not at all. |
||
111 | unset( $args['_rt'] ); |
||
112 | unset( $args['_'] ); |
||
113 | |||
114 | $validated = self::validate_and_sanitize( $args ); |
||
115 | |||
116 | if ( is_wp_error( $validated ) ) |
||
117 | return ''; |
||
118 | |||
119 | return Tracks_Client::PIXEL . '?' . http_build_query( $validated ); |
||
120 | } |
||
121 | |||
122 | static function event_name_is_valid( $name ) { |
||
125 | |||
126 | static function prop_name_is_valid( $name ) { |
||
129 | |||
130 | static function scrutinize_event_names( $event ) { |
||
131 | if ( ! Tracks_Event::event_name_is_valid( $event->_en ) ) { |
||
132 | return; |
||
149 | } |
||
150 |