MonsterInsights_Auth::get_referral_url()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 3
b 0
f 0
nc 6
nop 0
dl 0
loc 20
rs 9.2222
1
<?php
2
/**
3
 * Auth class.
4
 *
5
 * Helper for auth.
6
 *
7
 * @since 7.0.0
8
 *
9
 * @package MonsterInsights
10
 * @subpackage Auth
11
 * @author  Chris Christoff
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
final class MonsterInsights_Auth {
20
21
	private $profile = array();
22
	private $network = array();
23
24
	/**
25
	 * Primary class constructor.
26
	 *
27
	 * @access public
28
	 * @since 7.0.0
29
	 */
30
	public function __construct() {
31
		$this->profile = $this->get_analytics_profile();
32
		$this->network = $this->get_network_analytics_profile();
33
	}
34
35
	public function is_manual() {
36
        $manual_code = $this->profile['manual'];
37
        return monsterinsights_is_valid_v4_id( $manual_code );
38
	}
39
40
	public function is_network_manual( $type = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
	public function is_network_manual( /** @scrutinizer ignore-unused */ $type = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
		$manual_code = $this->network['manual'];
42
        return monsterinsights_is_valid_v4_id( $manual_code );
43
	}
44
45
	public function is_authed() {
46
		return ! empty( $this->profile['key'] ) && ! empty( $this->profile[ 'v4' ] );
47
	}
48
49
	public function is_network_authed() {
50
        return ! empty( $this->network['key'] ) && ! empty( $this->network[ 'v4' ] );
51
	}
52
53
	public function get_analytics_profile( $force = false ) {
54
		if ( ! empty( $this->profile ) && ! $force ) {
55
			return $this->profile;
56
		} else {
57
			$profile       = get_option( 'monsterinsights_site_profile', array() );
58
			$this->profile = $profile;
59
60
			return $profile;
61
		}
62
	}
63
64
	public function get_network_analytics_profile( $force = false ) {
65
		if ( ! empty( $this->network ) && ! $force ) {
66
			return $this->network;
67
		} else {
68
			$profile       = get_site_option( 'monsterinsights_network_profile', array() );
69
			$this->network = $profile;
70
71
			return $profile;
72
		}
73
	}
74
75
	public function set_analytics_profile( $data = array() ) {
76
		if ( ! empty( $data ) ) {
77
			$data['connection_time'] = time();
78
		}
79
80
		update_option( 'monsterinsights_site_profile', $data );
81
		$this->profile = $data;
82
83
		// If this is the first time, save the date when they connected.
84
		$over_time    = get_option( 'monsterinsights_over_time', array() );
85
		$needs_update = false;
86
		if ( monsterinsights_is_pro_version() && empty( $over_time['connected_date_pro'] ) ) {
87
			$over_time['connected_date_pro'] = time();
88
			$needs_update                    = true;
89
		}
90
		if ( ! monsterinsights_is_pro_version() && empty( $over_time['connected_date_lite'] ) ) {
91
			$over_time['connected_date_lite'] = time();
92
			$needs_update                     = true;
93
		}
94
		if ( $needs_update ) {
95
			update_option( 'monsterinsights_over_time', $over_time, false );
96
		}
97
	}
98
99
	public function set_network_analytics_profile( $data = array() ) {
100
		update_site_option( 'monsterinsights_network_profile', $data );
101
		$this->network = $data;
102
	}
103
104
	public function delete_analytics_profile( $migrate = true ) {
105
		if ( $migrate ) {
106
			$newdata = array();
107
			if ( isset( $this->profile['v4'] ) ) {
108
				$newdata['manual_v4'] = $this->profile['v4'];
109
                $newdata['measurement_protocol_secret'] = $this->profile['measurement_protocol_secret'];
110
			}
111
			$this->profile = $newdata;
112
			$this->set_analytics_profile( $newdata );
113
		} else {
114
			$this->profile = array();
115
			delete_option( 'monsterinsights_site_profile' );
116
		}
117
	}
118
119
	public function delete_network_analytics_profile( $migrate = true ) {
120
		if ( $migrate ) {
121
			$newdata = array();
122
			if ( isset( $this->network['v4'] ) ) {
123
				$newdata['manual_v4'] = $this->network['v4'];
124
                $newdata['measurement_protocol_secret'] = $this->profile['measurement_protocol_secret'];
125
			}
126
			$this->network = $newdata;
127
			$this->set_network_analytics_profile( $newdata );
128
		} else {
129
			$this->network = array();
130
			delete_site_option( 'monsterinsights_network_profile' );
131
		}
132
	}
133
134
	public function set_manual_v4_id( $v4 = '' ) {
135
		if ( empty( $v4 ) ) {
136
			return;
137
		}
138
139
		if ( $this->is_authed() ) {
140
			MonsterInsights()->api_auth->delete_auth();
0 ignored issues
show
Bug introduced by
The method delete_auth() does not exist on MonsterInsights_Auth. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
			MonsterInsights()->api_auth->/** @scrutinizer ignore-call */ 
141
                                delete_auth();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
		}
142
143
		$data = array();
144
		if ( empty( $this->profile ) ) {
145
			$data['manual_v4'] = $v4;
146
		} else {
147
			$data              = $this->profile;
148
			$data['manual_v4'] = $v4;
149
		}
150
151
		do_action( 'monsterinsights_reports_delete_aggregate_data' );
152
153
		$this->profile = $data;
154
		$this->set_analytics_profile( $data );
155
	}
156
157
	public function set_network_manual_v4_id( $v4 = '' ) {
158
		if ( empty( $v4 ) ) {
159
			return;
160
		}
161
162
		if ( $this->is_network_authed() ) {
163
			MonsterInsights()->api_auth->delete_auth();
164
		}
165
166
		$data = array();
167
		if ( empty( $this->network ) ) {
168
			$data['manual_v4'] = $v4;
169
		} else {
170
			$data                      = $this->network;
171
			$data['manual_v4']         = $v4;
172
			$data['network_manual_v4'] = $v4;
173
		}
174
175
		do_action( 'monsterinsights_reports_delete_network_aggregate_data' );
176
177
		$this->network = $data;
178
		$this->set_network_analytics_profile( $data );
179
	}
180
181
	public function get_measurement_protocol_secret() {
182
		return ! empty( $this->profile['measurement_protocol_secret'] ) ? $this->profile['measurement_protocol_secret'] : '';
183
	}
184
185
	public function get_network_measurement_protocol_secret() {
186
		return ! empty( $this->network['measurement_protocol_secret'] ) ? $this->network['measurement_protocol_secret'] : '';
187
	}
188
189
	public function set_measurement_protocol_secret( $value ) {
190
		$data = array();
191
		if ( empty( $this->profile ) ) {
192
			$data['measurement_protocol_secret'] = $value;
193
		} else {
194
			$data                                = $this->profile;
195
			$data['measurement_protocol_secret'] = $value;
196
		}
197
198
		$this->profile = $data;
199
		$this->set_analytics_profile( $data );
200
	}
201
202
	public function set_network_measurement_protocol_secret( $value ) {
203
		$data = array();
204
		if ( empty( $this->network ) ) {
205
			$data['measurement_protocol_secret'] = $value;
206
		} else {
207
			$data                                = $this->network;
208
			$data['measurement_protocol_secret'] = $value;
209
		}
210
211
		$this->network = $data;
212
		$this->set_network_analytics_profile( $data );
213
	}
214
215
	public function delete_manual_v4_id() {
216
		if ( ! empty( $this->profile ) && ! empty( $this->profile['manual_v4'] ) ) {
217
			unset( $this->profile['manual_v4'] );
218
			$this->set_analytics_profile( $this->profile );
219
		}
220
	}
221
222
	public function delete_network_manual_v4_id() {
223
		if ( ! empty( $this->network ) && ! empty( $this->network['manual_v4'] ) ) {
224
			unset( $this->network['manual_v4'] );
225
			$this->set_network_analytics_profile( $this->network );
226
		}
227
	}
228
229
	public function get_manual_v4_id() {
230
		return ! empty( $this->profile['manual_v4'] ) ? monsterinsights_is_valid_v4_id( $this->profile['manual_v4'] ) : '';
231
	}
232
233
	public function get_network_manual_v4_id() {
234
		return ! empty( $this->network['manual_v4'] ) ? monsterinsights_is_valid_v4_id( $this->network['manual_v4'] ) : '';
235
	}
236
237
	public function get_v4_id() {
238
		return ! empty( $this->profile['v4'] ) ? monsterinsights_is_valid_v4_id( $this->profile['v4'] ) : '';
239
	}
240
241
	public function get_network_v4_id() {
242
		return ! empty( $this->network['v4'] ) ? monsterinsights_is_valid_v4_id( $this->network['v4'] ) : '';
243
	}
244
245
	public function get_viewname() {
246
		return ! empty( $this->profile['viewname'] ) ? $this->profile['viewname'] : '';
247
	}
248
249
	public function get_network_viewname() {
250
		return ! empty( $this->network['viewname'] ) ? $this->network['viewname'] : '';
251
	}
252
253
	public function get_accountid() {
254
		return ! empty( $this->profile['a'] ) ? $this->profile['a'] : '';
255
	}
256
257
	public function get_network_accountid() {
258
		return ! empty( $this->network['a'] ) ? $this->network['a'] : '';
259
	}
260
261
	public function get_propertyid() {
262
		return ! empty( $this->profile['w'] ) ? $this->profile['w'] : '';
263
	}
264
265
	public function get_network_propertyid() {
266
		return ! empty( $this->network['w'] ) ? $this->network['w'] : '';
267
	}
268
269
	public function get_viewid() { // also known as profileID
270
		return ! empty( $this->profile['p'] ) ? $this->profile['p'] : '';
271
	}
272
273
	public function get_network_viewid() { // also known as profileID
274
		return ! empty( $this->network['p'] ) ? $this->network['p'] : '';
275
	}
276
277
	public function get_key() {
278
		return ! empty( $this->profile['key'] ) ? $this->profile['key'] : '';
279
	}
280
281
	public function get_network_key() {
282
		return ! empty( $this->network['key'] ) ? $this->network['key'] : '';
283
	}
284
285
	public function get_token() {
286
		return ! empty( $this->profile['token'] ) ? $this->profile['token'] : '';
287
	}
288
289
	public function get_network_token() {
290
		return ! empty( $this->network['token'] ) ? $this->network['token'] : '';
291
	}
292
293
	public function get_referral_url() {
294
		$auth = MonsterInsights()->auth;
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...
295
296
		if ( $this->is_authed() ) {
297
			$acc_id      = $auth->get_accountid();
298
			$view_id     = $auth->get_viewid();
299
			$property_id = $auth->get_propertyid();
300
		} else if ( $this->is_network_authed() ) {
301
			$acc_id      = $auth->get_network_accountid();
302
			$view_id     = $auth->get_network_viewid();
303
			$property_id = $auth->get_network_propertyid();
304
		}
305
306
		if ( ! empty( $acc_id ) && ! empty( $view_id ) && ! empty( $property_id ) ) {
307
			$format = 'p%2$s';
308
309
			return sprintf( $format, $acc_id, $property_id, $view_id );
310
		}
311
312
		return '';
313
	}
314
}
315