Completed
Push — update/debugger-for-wp52-site-... ( d73c7a )
by
unknown
15:48 queued 08:59
created

Jetpack_Debug_Data::debug_data()   F

Complexity

Conditions 19
Paths 16384

Size

Total Lines 230

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 16384
nop 0
dl 0
loc 230
rs 0.2799
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
 * Jetpack Debug Data for the legacy Jetpack debugger page and the WP 5.2-era Site Health sections.
4
 *
5
 * @package jetpack
6
 */
7
8
/**
9
 * Class Jetpack_Debug_Data
10
 *
11
 * Collect and return debug data for Jetpack.
12
 *
13
 * @since 7.3.0
14
 */
15
class Jetpack_Debug_Data {
16
	/**
17
	 * Determine the active plan and normalize it for the debugger results.
18
	 *
19
	 * @since 7.3.0
20
	 *
21
	 * @return string The plan slug.
22
	 */
23
	public static function what_jetpack_plan() {
24
		$plan = Jetpack_Plan::get();
25
		return ! empty( $plan['class'] ) ? $plan['class'] : 'undefined';
26
	}
27
28
	/**
29
	 * Convert seconds to human readable time.
30
	 *
31
	 * A dedication function instead of using Core functionality to allow for output in seconds.
32
	 *
33
	 * @since 7.3.0
34
	 *
35
	 * @param int $seconds Number of seconds to convert to human time.
36
	 *
37
	 * @return string Human readable time.
38
	 */
39
	public static function seconds_to_time( $seconds ) {
40
		$seconds = intval( $seconds );
41
		$units   = array(
42
			'week'   => WEEK_IN_SECONDS,
43
			'day'    => DAY_IN_SECONDS,
44
			'hour'   => HOUR_IN_SECONDS,
45
			'minute' => MINUTE_IN_SECONDS,
46
			'second' => 1,
47
		);
48
		// specifically handle zero.
49
		if ( 0 === $seconds ) {
50
			return '0 seconds';
51
		}
52
		$human_readable = '';
53
		foreach ( $units as $name => $divisor ) {
54
			$quot = intval( $seconds / $divisor );
55
			if ( $quot ) {
56
				$human_readable .= "$quot $name";
57
				$human_readable .= ( abs( $quot ) > 1 ? 's' : '' ) . ', ';
58
				$seconds        -= $quot * $divisor;
59
			}
60
		}
61
		return substr( $human_readable, 0, -2 );
62
	}
63
64
	/**
65
	 * Return debug data in the format expected by Core's Site Health Info tab.
66
	 *
67
	 * @since 7.3.0
68
	 *
69
	 * @param array $debug {
70
	 *     The debug information already compiled by Core.
71
	 *
72
	 *     @type string  $label        The title for this section of the debug output.
73
	 *     @type string  $description  Optional. A description for your information section which may contain basic HTML
74
	 *                                 markup: `em`, `strong` and `a` for linking to documentation or putting emphasis.
75
	 *     @type boolean $show_count   Optional. If set to `true` the amount of fields will be included in the title for
76
	 *                                 this section.
77
	 *     @type boolean $private      Optional. If set to `true` the section and all associated fields will be excluded
78
	 *                                 from the copy-paste text area.
79
	 *     @type array   $fields {
80
	 *         An associative array containing the data to be displayed.
81
	 *
82
	 *         @type string  $label    The label for this piece of information.
83
	 *         @type string  $value    The output that is of interest for this field.
84
	 *         @type boolean $private  Optional. If set to `true` the field will not be included in the copy-paste text area
85
	 *                                 on top of the page, allowing you to show, for example, API keys here.
86
	 *     }
87
	 * }
88
	 *
89
	 * @return array $args Debug information in the same format as the initial argument.
90
	 */
91
	public static function core_debug_data( $debug ) {
92
		$jetpack = array(
93
			'jetpack' => array(
94
				'label'       => __( 'Jetpack', 'jetpack' ),
95
				'description' => __( 'Diagnostic information helpful to <a href="https://jetpack.com/contact-support">your Jetpack Happiness team</a>', 'jetpack' ),
96
				'fields'      => self::debug_data(),
97
			),
98
		);
99
		$debug   = array_merge( $debug, $jetpack );
100
		return $debug;
101
	}
102
103
	/**
104
	 * Compile and return array of debug information.
105
	 *
106
	 * @since 7.3.0
107
	 *
108
	 * @return array $args {
109
	 *          Associated array of arrays with the following.
110
	 *         @type string  $label    The label for this piece of information.
111
	 *         @type string  $value    The output that is of interest for this field.
112
	 *         @type boolean $private  Optional. Set to true if data is sensitive (API keys, etc).
113
	 * }
114
	 */
115
	public static function debug_data() {
116
		$debug_info = array();
117
118
		/* Add various important Jetpack options */
119
		$debug_info['site_id']        = array(
120
			'label'   => 'Jetpack Site ID',
121
			'value'   => Jetpack_Options::get_option( 'id' ),
122
			'private' => false,
123
		);
124
		$debug_info['ssl_cert']       = array(
125
			'label'   => 'Jetpack SSL Verfication Bypass',
126
			'value'   => ( Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) ? 'Yes' : 'No',
127
			'private' => false,
128
		);
129
		$debug_info['time_diff']      = array(
130
			'label'   => "Offset between Jetpack server's time and this server's time.",
131
			'value'   => Jetpack_Options::get_option( 'time_diff' ),
132
			'private' => false,
133
		);
134
		$debug_info['version_option'] = array(
135
			'label'   => 'Current Jetpack Version Option',
136
			'value'   => Jetpack_Options::get_option( 'version' ),
137
			'private' => false,
138
		);
139
		$debug_info['old_version']    = array(
140
			'label'   => 'Previous Jetpack Version',
141
			'value'   => Jetpack_Options::get_option( 'old_version' ),
142
			'private' => false,
143
		);
144
		$debug_info['public']         = array(
145
			'label'   => 'Jetpack Site Public',
146
			'value'   => ( Jetpack_Options::get_option( 'public' ) ) ? 'Public' : 'Private',
147
			'private' => false,
148
		);
149
		$debug_info['master_user']    = array(
150
			'label'   => 'Jetpack Master User',
151
			'value'   => Jetpack_Options::get_option( 'master_user' ),
152
			'private' => false,
153
		);
154
155
		/* Token information is private, but awareness if there one is set is helpful. */
156
		$user_id     = get_current_user_id();
157
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
158
		$blog_token  = Jetpack_Options::get_option( 'blog_token' );
159
		if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) {
160
			$user_token = $user_tokens[ $user_id ];
161
		} else {
162
			$user_token = null;
163
		}
164
		unset( $user_tokens );
165
166
		$tokenset = '';
167
		if ( $blog_token ) {
168
			$tokenset = 'Blog ';
169
		}
170
		if ( $user_token ) {
171
			$tokenset .= 'User';
172
		}
173
		if ( ! $tokenset ) {
174
			$tokenset = 'None';
175
		}
176
177
		$debug_info['current_user'] = array(
178
			'label'   => 'Current User',
179
			'value'   => $user_id,
180
			'private' => false,
181
		);
182
		$debug_info['tokens_set']   = array(
183
			'label' => 'Tokens defined',
184
			'value' => $tokenset,
185
			'private => false,',
186
		);
187
		$debug_info['blog_token']   = array(
188
			'label'   => 'Blog token',
189
			'value'   => ( $blog_token ) ? $blog_token : 'Not set.',
190
			'private' => true,
191
		);
192
		$debug_info['user_token']   = array(
193
			'label'   => 'User token',
194
			'value'   => ( $user_token ) ? $user_token : 'Not set.',
195
			'private' => true,
196
		);
197
198
		/** Jetpack Environmental Information */
199
		$debug_info['version']       = array(
200
			'label'   => 'Jetpack Version',
201
			'value'   => JETPACK__VERSION,
202
			'private' => false,
203
		);
204
		$debug_info['jp_plugin_dir'] = array(
205
			'label'   => 'Jetpack Directory',
206
			'value'   => JETPACK__PLUGIN_DIR,
207
			'private' => false,
208
		);
209
		$debug_info['plan']          = array(
210
			'label'   => 'Plan Type',
211
			'value'   => self::what_jetpack_plan(),
212
			'private' => false,
213
		);
214
215
		foreach ( array(
216
			'HTTP_HOST',
217
			'SERVER_PORT',
218
			'HTTPS',
219
			'GD_PHP_HANDLER',
220
			'HTTP_AKAMAI_ORIGIN_HOP',
221
			'HTTP_CF_CONNECTING_IP',
222
			'HTTP_CLIENT_IP',
223
			'HTTP_FASTLY_CLIENT_IP',
224
			'HTTP_FORWARDED',
225
			'HTTP_FORWARDED_FOR',
226
			'HTTP_INCAP_CLIENT_IP',
227
			'HTTP_TRUE_CLIENT_IP',
228
			'HTTP_X_CLIENTIP',
229
			'HTTP_X_CLUSTER_CLIENT_IP',
230
			'HTTP_X_FORWARDED',
231
			'HTTP_X_FORWARDED_FOR',
232
			'HTTP_X_IP_TRAIL',
233
			'HTTP_X_REAL_IP',
234
			'HTTP_X_VARNISH',
235
			'REMOTE_ADDR',
236
		) as $header ) {
237
			if ( isset( $_SERVER[ $header ] ) ) {
238
				$debug_info[ $header ] = array(
239
					'label'   => 'Server Variable ' . $header,
240
					'value'   => ( $_SERVER[ $header ] ) ? $_SERVER[ $header ] : 'false',
241
					'private' => false,
242
				);
243
			}
244
		}
245
246
		$debug_info['protect_header'] = array(
247
			'label'   => 'Trusted IP',
248
			'value'   => wp_json_encode( get_site_option( 'trusted_ip_header' ) ),
249
			'private' => false,
250
		);
251
252
		/** Sync Debug Information */
253
		/** Load Sync modules */
254
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
255
		/** Load Sync sender */
256
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
257
		/** Load Sync functions */
258
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php';
259
260
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
261
		if ( $sync_module ) {
262
			$sync_statuses              = $sync_module->get_status();
263
			$human_readable_sync_status = array();
264
			foreach ( $sync_statuses as $sync_status => $sync_status_value ) {
265
				$human_readable_sync_status[ $sync_status ] =
266
					in_array( $sync_status, array( 'started', 'queue_finished', 'send_started', 'finished' ), true )
267
						? date( 'r', $sync_status_value ) : $sync_status_value;
268
			}
269
			$debug_info['full_sync'] = array(
270
				'label'   => 'Full Sync Status',
271
				'value'   => wp_json_encode( $human_readable_sync_status ),
272
				'private' => false,
273
			);
274
		}
275
276
		$queue = Jetpack_Sync_Sender::get_instance()->get_sync_queue();
277
278
		$debug_info['sync_size'] = array(
279
			'label'   => 'Sync Queue Size',
280
			'value'   => $queue->size(),
281
			'private' => false,
282
		);
283
		$debug_info['sync_lag']  = array(
284
			'label'   => 'Sync Queue Lag',
285
			'value'   => self::seconds_to_time( $queue->lag() ),
286
			'private' => false,
287
		);
288
289
		$full_sync_queue = Jetpack_Sync_Sender::get_instance()->get_full_sync_queue();
290
291
		$debug_info['full_sync_size'] = array(
292
			'label'   => 'Full Sync Queue Size',
293
			'value'   => $full_sync_queue->size(),
294
			'private' => false,
295
		);
296
		$debug_info['full_sync_lag']  = array(
297
			'label'   => 'Full Sync Queue Lag',
298
			'value'   => self::seconds_to_time( $full_sync_queue->lag() ),
299
			'private' => false,
300
		);
301
302
		/**
303
		 * IDC Information
304
		 *
305
		 * Must follow sync debug since it depends on sync functionality.
306
		 */
307
		$idc_urls = array(
308
			'home'       => Jetpack_Sync_Functions::home_url(),
309
			'siteurl'    => Jetpack_Sync_Functions::site_url(),
310
			'WP_HOME'    => Jetpack_Constants::is_defined( 'WP_HOME' ) ? Jetpack_Constants::get_constant( 'WP_HOME' ) : '',
311
			'WP_SITEURL' => Jetpack_Constants::is_defined( 'WP_SITEURL' ) ? Jetpack_Constants::get_constant( 'WP_SITEURL' ) : '',
312
		);
313
314
		$debug_info['idc_urls']         = array(
315
			'label'   => 'IDC URLs',
316
			'value'   => wp_json_encode( $idc_urls ),
317
			'private' => false,
318
		);
319
		$debug_info['idc_error_option'] = array(
320
			'label'   => 'IDC Error Option',
321
			'value'   => wp_json_encode( Jetpack_Options::get_option( 'sync_error_idc' ) ),
322
			'private' => false,
323
		);
324
		$debug_info['idc_optin']        = array(
325
			'label'   => 'IDC Opt-in',
326
			'value'   => Jetpack::sync_idc_optin(),
327
			'private' => false,
328
		);
329
330
		// @todo -- Add testing results?
331
		$cxn_tests               = new Jetpack_Cxn_Tests();
332
		$debug_info['cxn_tests'] = array(
333
			'label'   => 'Connection Tests',
334
			'value'   => '',
335
			'private' => false,
336
		);
337
		if ( $cxn_tests->pass() ) {
338
			$debug_info['cxn_tests']['value'] = 'All Pass.';
339
		} else {
340
			$debug_info['cxn_tests']['value'] = wp_json_encode( $cxn_tests->list_fails() );
341
		}
342
343
		return $debug_info;
344
	}
345
}
346