Passed
Push — master ( 3b714f...562556 )
by Chris
03:05
created

MonsterInsights_Tracking::get_data()   F

Complexity

Conditions 11
Paths 576

Size

Total Lines 71
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 48
nc 576
nop 0
dl 0
loc 71
rs 3.6222
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Tracking functions for reporting plugin usage to the MonsterInsights site for users that have opted in
4
 *
5
 * @package     MonsterInsights
6
 * @subpackage  Admin
7
 * @copyright   Copyright (c) 2018, Chris Christoff
8
 * @since       7.0.0
9
*/
10
11
// Exit if accessed directly
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Usage tracking
18
 *
19
 * @access public
20
 * @since  7.0.0
21
 * @return void
22
 */
23
class MonsterInsights_Tracking {
24
25
	public function __construct() {
26
		add_action( 'init', array( $this, 'schedule_send' ) );
27
		add_action( 'monsterinsights_settings_save_general_end', array( $this, 'check_for_settings_optin' ) );
28
		add_action( 'admin_head', array( $this, 'check_for_optin' ) );
29
		add_action( 'admin_head', array( $this, 'check_for_optout' ) );
30
		add_filter( 'cron_schedules', array( $this, 'add_schedules' ) );
31
		add_action( 'monsterinsights_usage_tracking_cron', array( $this, 'send_checkin' ) );
32
	}
33
34
	private function get_data() {
35
		$data = array();
36
37
		// Retrieve current theme info
38
		$theme_data    = wp_get_theme();
39
		$tracking_mode = monsterinsights_get_option( 'tracking_mode', 'analytics' );
40
		$events_mode   = monsterinsights_get_option( 'events_mode', 'none' );
41
		$update_mode   = monsterinsights_get_option( 'automatic_updates', false );
42
43
		if ( $tracking_mode === false ) {
0 ignored issues
show
introduced by
The condition $tracking_mode === false can never be true.
Loading history...
44
			$tracking_mode = 'analytics';
45
		}
46
47
		if ( $events_mode === false ) {
0 ignored issues
show
introduced by
The condition $events_mode === false can never be true.
Loading history...
48
			$events_mode = 'none';
49
		}
50
51
		if ( $update_mode === false ) {
0 ignored issues
show
introduced by
The condition $update_mode === false can never be true.
Loading history...
52
			$update_mode = 'none';
53
		}
54
55
		$count_b = 1;
56
		if ( is_multisite() ) {
57
			if ( function_exists( 'get_blog_count' ) ) {
58
				$count_b = get_blog_count();
59
			} else {
60
				$count_b = 'Not Set';
61
			}
62
		}
63
64
		$data['php_version']   = phpversion();
65
		$data['mi_version']    = MONSTERINSIGHTS_VERSION;
66
		$data['wp_version']    = get_bloginfo( 'version' );
67
		$data['server']        = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
68
		$data['over_time']     = get_option( 'monsterinsights_over_time', array() );
69
		$data['multisite']     = is_multisite();
70
		$data['url']           = home_url();
71
		$data['themename']     = $theme_data->Name;
72
		$data['themeversion']  = $theme_data->Version;
73
		$data['email']         = get_bloginfo( 'admin_email' );
74
		$data['key']           = monsterinsights_get_license_key();
75
		$data['sas']           = monsterinsights_get_shareasale_id();
76
		$data['settings']      = monsterinsights_get_options();
77
		$data['tracking_mode'] = $tracking_mode;
78
		$data['events_mode']   = $events_mode;
79
		$data['autoupdate']    = $update_mode;
80
		$data['pro']           = (int) monsterinsights_is_pro_version();
81
		$data['sites']         = $count_b;
82
		$data['usagetracking'] = get_option( 'monsterinsights_usage_tracking_config', $tracking );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tracking does not exist. Did you maybe mean $tracking_mode?
Loading history...
83
		$data['usercount']     = function_exists( 'get_user_count' ) ? get_user_count() : 'Not Set';
84
85
		// Retrieve current plugin information
86
		if( ! function_exists( 'get_plugins' ) ) {
87
			include ABSPATH . '/wp-admin/includes/plugin.php';
88
		}
89
90
		$plugins        = array_keys( get_plugins() );
91
		$active_plugins = get_option( 'active_plugins', array() );
92
93
		foreach ( $plugins as $key => $plugin ) {
94
			if ( in_array( $plugin, $active_plugins ) ) {
0 ignored issues
show
Bug introduced by
It seems like $active_plugins can also be of type false; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

94
			if ( in_array( $plugin, /** @scrutinizer ignore-type */ $active_plugins ) ) {
Loading history...
95
				// Remove active plugins from list so we can show active and inactive separately
96
				unset( $plugins[ $key ] );
97
			}
98
		}
99
100
		$data['active_plugins']   = $active_plugins;
101
		$data['inactive_plugins'] = $plugins;
102
		$data['locale']           = get_locale();
103
104
		return $data;
105
	}
106
107
	public function send_checkin( $override = false, $ignore_last_checkin = false ) {
108
109
		$home_url = trailingslashit( home_url() );
110
		if ( strpos( $home_url, 'monsterinsights.com' ) !== false ) {
111
			return false;
112
		}
113
114
		if( ! $this->tracking_allowed() && ! $override ) {
115
			return false;
116
		}
117
118
		// Send a maximum of once per week
119
		$last_send = get_option( 'monsterinsights_usage_tracking_last_checkin' );
120
		if ( is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) && ! $ignore_last_checkin ) {
121
			return false;
122
		}
123
124
		$request = wp_remote_post( 'https://miusage.com/v1/checkin/', array(
0 ignored issues
show
Unused Code introduced by
The assignment to $request is dead and can be removed.
Loading history...
125
			'method'      => 'POST',
126
			'timeout'     => 5,
127
			'redirection' => 5,
128
			'httpversion' => '1.1',
129
			'blocking'    => false,
130
			'body'        => $this->get_data(),
131
			'user-agent'  => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . get_bloginfo( 'url' )
132
		) );
133
134
		// If we have completed successfully, recheck in 1 week
135
		update_option( 'monsterinsights_usage_tracking_last_checkin', time() );
136
		return true;
137
	}
138
139
	private function tracking_allowed() {
140
		return (bool) monsterinsights_get_option( 'anonymous_data', false ) || monsterinsights_is_pro_version();
141
	}
142
143
	public function schedule_send() {
144
		if ( ! wp_next_scheduled( 'monsterinsights_usage_tracking_cron' ) ) {
145
			$tracking             = array();
146
			$tracking['day']      = rand( 0, 6  );
147
			$tracking['hour']     = rand( 0, 23 );
148
			$tracking['minute']   = rand( 0, 59 );
149
			$tracking['second']   = rand( 0, 59 );
150
			$tracking['offset']   = ( $tracking['day']    * DAY_IN_SECONDS    ) +
151
									( $tracking['hour']   * HOUR_IN_SECONDS   ) +
152
									( $tracking['minute'] * MINUTE_IN_SECONDS ) +
153
									 $tracking['second'];
154
			$tracking['initsend'] = strtotime("next sunday") + $tracking['offset'];
155
156
			wp_schedule_event( $tracking['initsend'], 'weekly', 'monsterinsights_usage_tracking_cron' );
157
			update_option( 'monsterinsights_usage_tracking_config', $tracking );
158
		}
159
	}
160
161
	public function check_for_settings_optin() {
162
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
163
			return;
164
		}
165
166
		if ( monsterinsights_is_pro_version() ) {
167
			return;
168
		}
169
170
		// Send an intial check in on settings save
171
		$anonymous_data = isset( $_POST['anonymous_data'] ) ? 1 : 0;
172
		if ( $anonymous_data ) {
173
			$this->send_checkin( true, true );
174
		}
175
176
	}
177
178
	public function check_for_optin() {
179
		if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_into_tracking' === $_REQUEST['mi_action'] ) ) {
180
			return;
181
		}
182
183
		if ( monsterinsights_get_option( 'anonymous_data', false ) ) {
184
			return;
185
		}
186
187
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
188
			return;
189
		}
190
191
		if ( monsterinsights_is_pro_version() ) {
192
			return;
193
		}
194
195
		monsterinsights_update_option( 'anonymous_data', 1 );
196
		$this->send_checkin( true, true );
197
		update_option( 'monsterinsights_tracking_notice', 1 );
198
	}
199
200
	public function check_for_optout() {
201
		if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_out_of_tracking' === $_REQUEST['mi_action'] ) ) {
202
			return;
203
		}
204
205
		if ( monsterinsights_get_option( 'anonymous_data', false ) ) {
206
			return;
207
		}
208
209
		if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
210
			return;
211
		}
212
213
		if ( monsterinsights_is_pro_version() ) {
214
			return;
215
		}
216
217
		monsterinsights_update_option( 'anonymous_data', 0 );
218
		update_option( 'monsterinsights_tracking_notice', 1 );
219
	}
220
221
	public function add_schedules( $schedules = array() ) {
222
		// Adds once weekly to the existing schedules.
223
		$schedules['weekly'] = array(
224
			'interval' => 604800,
225
			'display'  => __( 'Once Weekly', 'google-analytics-for-wordpress' )
226
		);
227
		return $schedules;
228
	}
229
}
230
new MonsterInsights_Tracking();