Passed
Push — master ( 5624f1...cf9d65 )
by Chris
03:54
created

MonsterInsights_Tracking::setup_data()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 38
nc 96
nop 0
dl 0
loc 58
rs 7.1977
c 0
b 0
f 0

How to fix   Long Method   

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
		$data['php_version']   = phpversion();
56
		$data['mi_version']    = MONSTERINSIGHTS_VERSION;
57
		$data['wp_version']    = get_bloginfo( 'version' );
58
		$data['server']        = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
59
		$data['over_time']     = get_option( 'monsterinsights_over_time', array() );
60
		$data['multisite']     = is_multisite();
61
		$data['url']           = home_url();
62
		$data['themename']     = $theme_data->Name;
63
		$data['themeversion']  = $theme_data->Version;
64
		$data['email']         = get_bloginfo( 'admin_email' );
65
		$data['key']           = monsterinsights_get_license_key();
66
		$data['sas']           = monsterinsights_get_shareasale_id();
67
		$data['settings']      = monsterinsights_get_options();
68
		$data['tracking_mode'] = $tracking_mode;
69
		$data['events_mode']   = $events_mode;
70
		$data['autoupdate']    = $update_mode;
71
		$data['pro']           = (int) monsterinsights_is_pro_version();
72
		$data['sites']         = is_multisite() ? (int) get_blog_count() : 1;
73
		$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...
74
		$data['usercount']     = get_user_count();
75
76
		// Retrieve current plugin information
77
		if( ! function_exists( 'get_plugins' ) ) {
78
			include ABSPATH . '/wp-admin/includes/plugin.php';
79
		}
80
81
		$plugins        = array_keys( get_plugins() );
82
		$active_plugins = get_option( 'active_plugins', array() );
83
84
		foreach ( $plugins as $key => $plugin ) {
85
			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

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