Passed
Push — master ( 8403c0...3ede7f )
by Chris
08:42
created

MonsterInsights_Measurement_Protocol_V4::collect()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.6111
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
class MonsterInsights_Measurement_Protocol_V4 {
8
	private static $instance;
9
10
	public static function get_instance() {
11
		if ( empty( self::$instance ) ) {
12
			self::$instance = new self();
13
		}
14
15
		return self::$instance;
16
	}
17
18
	private $is_debug;
19
20
	private $measurement_id;
21
22
	private function __construct() {
23
		$this->is_debug       = monsterinsights_is_debug_mode();
24
		$this->measurement_id = monsterinsights_get_v4_id_to_output();
25
	}
26
27
	private function get_base_url() {
28
		return 'https://www.google-analytics.com/mp/collect';
29
	}
30
31
	private function get_url() {
32
		$api_secret = is_multisite() && is_network_admin()
33
			? MonsterInsights()->auth->get_network_measurement_protocol_secret()
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
34
			: MonsterInsights()->auth->get_measurement_protocol_secret();
35
36
		return add_query_arg(
37
			array(
38
				'api_secret'     => $api_secret,
39
				'measurement_id' => $this->measurement_id,
40
			),
41
			$this->get_base_url()
42
		);
43
	}
44
45
	private function get_client_id( $args ) {
46
		if ( ! empty( $args['client_id'] ) ) {
47
			return $args['client_id'];
48
		}
49
50
		$payment_id = 0;
51
		if ( ! empty( $args['payment_id'] ) ) {
52
			$payment_id = $args['payment_id'];
53
		}
54
55
		return monsterinsights_get_client_id( $payment_id );
56
	}
57
58
	private function validate_args( $args, $defaults ) {
59
		$out = array();
60
61
		$valid_keys = array( 'client_id', 'user_id', 'events' );
62
63
		foreach ( $defaults as $key => $default ) {
64
			if ( ! in_array( $key, $valid_keys, true ) ) {
65
				continue;
66
			}
67
68
			if ( array_key_exists( $key, $args ) ) {
69
				$out[ $key ] = $args[ $key ];
70
			} else {
71
				$out[ $key ] = $default;
72
			}
73
		}
74
75
		return $out;
76
	}
77
78
	private function request( $args ) {
79
		if ( empty( $this->measurement_id ) ) {
80
			return;
81
		}
82
83
		$body = $this->validate_args( $args, array(
84
			'client_id' => $this->get_client_id( $args ),
85
			'events'    => array(),
86
		) );
87
88
		if ( $this->is_debug ) {
89
			foreach ( $body['events'] as $index => $event ) {
90
				$body['events'][ $index ]['params']['debug_mode'] = true;
91
			}
92
		}
93
94
		$body = apply_filters( 'monsterinsights_mp_v4_api_call', $body );
95
96
		return wp_remote_post(
97
			$this->get_url(),
98
			array(
99
				'method'   => 'POST',
100
				'timeout'  => 5,
101
				'blocking' => $this->is_debug,
102
				'body'     => wp_json_encode( $body ),
103
			)
104
		);
105
	}
106
107
	public function collect( $args ) {
108
		// Detect if browser request is a prefetch
109
		if ( ( isset( $_SERVER["HTTP_X_PURPOSE"] ) && ( 'prefetch' === strtolower( $_SERVER["HTTP_X_PURPOSE"] ) ) ) ||
110
		     ( isset( $_SERVER["HTTP_X_MOZ"] ) && ( 'prefetch' === strtolower( $_SERVER["HTTP_X_MOZ"] ) ) ) ) {
111
			return;
112
		}
113
114
		return $this->request( $args );
115
	}
116
}
117
118
function monsterinsights_mp_collect_v4( $args ) {
119
	return MonsterInsights_Measurement_Protocol_V4::get_instance()->collect( $args );
120
}
121