Completed
Push — update/debugger ( 920eeb )
by
unknown
06:59
created

class.jetpack-debugger.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class Jetpack_Debugger {
4
5
	private static function what_jetpack_plan() {
6
		$plan = Jetpack::get_active_plan();
7
		$plan = ! empty( $plan['class'] ) ? $plan['class'] : 'undefined';
8
		return 'JetpackPlan' . $plan;
9
	}
10
11
	static function seconds_to_time( $seconds ) {
12
		$units = array(
13
			'week'   => WEEK_IN_SECONDS,
14
			'day'    => DAY_IN_SECONDS,
15
			'hour'   => HOUR_IN_SECONDS,
16
			'minute' => MINUTE_IN_SECONDS,
17
			'second' => 1,
18
		);
19
		// specifically handle zero
20
		if ( $seconds == 0 ) {
21
			return '0 seconds';
22
		}
23
		$human_readable = '';
24
		foreach ( $units as $name => $divisor ) {
25
			if ( $quot = intval( $seconds / $divisor ) ) {
26
				$human_readable .= "$quot $name";
27
				$human_readable .= ( abs( $quot ) > 1 ? 's' : '' ) . ', ';
28
				$seconds        -= $quot * $divisor;
29
			}
30
		}
31
		return substr( $human_readable, 0, -2 );
32
	}
33
34
	public static function jetpack_increase_timeout() {
35
		return 30; // seconds
36
	}
37
38
	public static function disconnect_and_redirect() {
39
		$can_disconnect = isset( $_GET['disconnect'] ) && $_GET['disconnect'] && isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'jp_disconnect' );
40
		if ( $can_disconnect ) {
41
			if ( Jetpack::is_active() ) {
42
				Jetpack::disconnect();
43
				wp_safe_redirect( Jetpack::admin_url() );
44
				exit;
45
			}
46
		}
47
	}
48
49
	public static function run_self_test() {
50
		$self_xml_rpc_url = site_url( 'xmlrpc.php' );
51
52
		$testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
53
54
		add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
55
56
		$response = wp_remote_get( $testsite_url . $self_xml_rpc_url );
57
58
		remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
59
60
		return $response;
61
62
	}
63
64
	public static function jetpack_debug_display_handler() {
65
		if ( ! current_user_can( 'manage_options' ) ) {
66
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'jetpack' ) );
67
		}
68
69
		$current_user = wp_get_current_user();
0 ignored issues
show
$current_user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
71
		$user_id     = get_current_user_id();
72
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
73
		if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) {
74
			$user_token = $user_tokens[ $user_id ];
75
		} else {
76
			$user_token = '[this user has no token]';
77
		}
78
		unset( $user_tokens );
79
80
		$debug_info = "\r\n";
81
		foreach ( array(
82
			'CLIENT_ID'   => 'id',
83
			'BLOG_TOKEN'  => 'blog_token',
84
			'MASTER_USER' => 'master_user',
85
			'CERT'        => 'fallback_no_verify_ssl_certs',
86
			'TIME_DIFF'   => 'time_diff',
87
			'VERSION'     => 'version',
88
			'OLD_VERSION' => 'old_version',
89
			'PUBLIC'      => 'public',
90
		) as $label => $option_name ) {
91
			$debug_info .= "\r\n" . esc_html( $label . ': ' . Jetpack_Options::get_option( $option_name ) );
92
		}
93
94
		$debug_info .= "\r\n" . esc_html( 'USER_ID: ' . $user_id );
95
		$debug_info .= "\r\n" . esc_html( 'USER_TOKEN: ' . $user_token );
96
		$debug_info .= "\r\n" . esc_html( 'PHP_VERSION: ' . PHP_VERSION );
97
		$debug_info .= "\r\n" . esc_html( 'WORDPRESS_VERSION: ' . $GLOBALS['wp_version'] );
98
		$debug_info .= "\r\n" . esc_html( 'JETPACK__VERSION: ' . JETPACK__VERSION );
99
		$debug_info .= "\r\n" . esc_html( 'JETPACK__PLUGIN_DIR: ' . JETPACK__PLUGIN_DIR );
100
		$debug_info .= "\r\n" . esc_html( 'SITE_URL: ' . site_url() );
101
		$debug_info .= "\r\n" . esc_html( 'HOME_URL: ' . home_url() );
102
		$debug_info .= "\r\n" . esc_html( 'PLAN: ' . self::what_jetpack_plan() );
103
104
		$debug_info .= "\r\n";
105
106
		$debug_info .= "\r\n" . '-- SYNC Status -- ';
107
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php';
108
		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' );
109
		if ( $sync_module ) {
110
			$sync_statuses              = $sync_module->get_status();
111
			$human_readable_sync_status = array();
112
			foreach ( $sync_statuses  as $sync_status => $sync_status_value ) {
113
				$human_readable_sync_status[ $sync_status ] =
114
					in_array( $sync_status, array( 'started', 'queue_finished', 'send_started', 'finished' ) )
115
						? date( 'r', $sync_status_value ) : $sync_status_value;
116
			}
117
			$debug_info .= "\r\n" . sprintf( esc_html__( 'Jetpack Sync Full Status: `%1$s`', 'jetpack' ), print_r( $human_readable_sync_status, 1 ) );
118
		}
119
120
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
121
122
		$queue = Jetpack_Sync_Sender::get_instance()->get_sync_queue();
123
124
		$debug_info .= "\r\n" . sprintf( esc_html__( 'Sync Queue size: %1$s', 'jetpack' ), $queue->size() );
125
		$debug_info .= "\r\n" . sprintf( esc_html__( 'Sync Queue lag: %1$s', 'jetpack' ), self::seconds_to_time( $queue->lag() ) );
126
127
		$full_sync_queue = Jetpack_Sync_Sender::get_instance()->get_full_sync_queue();
128
129
		$debug_info .= "\r\n" . sprintf( esc_html__( 'Full Sync Queue size: %1$s', 'jetpack' ), $full_sync_queue->size() );
130
		$debug_info .= "\r\n" . sprintf( esc_html__( 'Full Sync Queue lag: %1$s', 'jetpack' ), self::seconds_to_time( $full_sync_queue->lag() ) );
131
132
		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php';
133
		$idc_urls    = array(
134
			'home'       => Jetpack_Sync_Functions::home_url(),
135
			'siteurl'    => Jetpack_Sync_Functions::site_url(),
136
			'WP_HOME'    => Jetpack_Constants::is_defined( 'WP_HOME' ) ? Jetpack_Constants::get_constant( 'WP_HOME' ) : '',
137
			'WP_SITEURL' => Jetpack_Constants::is_defined( 'WP_SITEURL' ) ? Jetpack_Constants::get_constant( 'WP_SITEURL' ) : '',
138
		);
139
		$debug_info .= "\r\n" . esc_html( sprintf( 'Sync IDC URLs: %s', json_encode( $idc_urls ) ) );
140
		$debug_info .= "\r\n" . esc_html( sprintf( 'Sync error IDC option: %s', json_encode( Jetpack_Options::get_option( 'sync_error_idc' ) ) ) );
141
		$debug_info .= "\r\n" . esc_html( sprintf( 'Sync IDC Optin: %s', (string) Jetpack::sync_idc_optin() ) );
142
143
		$debug_info .= "\r\n";
144
145
		foreach ( array(
146
			'HTTP_HOST',
147
			'SERVER_PORT',
148
			'HTTPS',
149
			'GD_PHP_HANDLER',
150
			'HTTP_AKAMAI_ORIGIN_HOP',
151
			'HTTP_CF_CONNECTING_IP',
152
			'HTTP_CLIENT_IP',
153
			'HTTP_FASTLY_CLIENT_IP',
154
			'HTTP_FORWARDED',
155
			'HTTP_FORWARDED_FOR',
156
			'HTTP_INCAP_CLIENT_IP',
157
			'HTTP_TRUE_CLIENT_IP',
158
			'HTTP_X_CLIENTIP',
159
			'HTTP_X_CLUSTER_CLIENT_IP',
160
			'HTTP_X_FORWARDED',
161
			'HTTP_X_FORWARDED_FOR',
162
			'HTTP_X_IP_TRAIL',
163
			'HTTP_X_REAL_IP',
164
			'HTTP_X_VARNISH',
165
			'REMOTE_ADDR',
166
		) as $header ) {
167
			if ( isset( $_SERVER[ $header ] ) ) {
168
				$debug_info .= "\r\n" . esc_html( $header . ': ' . $_SERVER[ $header ] );
169
			}
170
		}
171
172
		$debug_info .= "\r\n" . esc_html( 'PROTECT_TRUSTED_HEADER: ' . json_encode( get_site_option( 'trusted_ip_header' ) ) );
173
174
		$debug_info    .= "\r\n\r\nTEST RESULTS:\r\n\r\n";
175
		$debug_raw_info = '';
176
177
		$tests = array();
178
179
		$tests['HTTP']['result']       = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
180
		$tests['HTTP']['fail_message'] = esc_html__( 'Your site isn’t reaching the Jetpack servers.', 'jetpack' );
181
182
		$tests['HTTPS']['result']       = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
183
		$tests['HTTPS']['fail_message'] = esc_html__( 'Your site isn’t securely reaching the Jetpack servers.', 'jetpack' );
184
185
		$identity_crisis_message = '';
186
		if ( $identity_crisis = Jetpack::check_identity_crisis() ) {
187
			$identity_crisis_message .= sprintf(
188
				__( 'Your url is set as `%1$s`, but your WordPress.com connection lists it as `%2$s`!', 'jetpack' ),
189
				$identity_crisis['home'],
190
				$identity_crisis['wpcom_home']
191
			);
192
			$identity_crisis          = new WP_Error( 'identity-crisis', $identity_crisis_message, $identity_crisis );
193
		} else {
194
			$identity_crisis = 'PASS';
195
		}
196
		$tests['IDENTITY_CRISIS']['result']       = $identity_crisis;
197
		$tests['IDENTITY_CRISIS']['fail_message'] = esc_html__( 'Something has gotten mixed up in your Jetpack Connection!', 'jetpack' );
198
199
		$tests['SELF']['result'] = self::run_self_test();
200
201
		if ( is_wp_error( $tests['SELF']['result'] ) && 0 == strpos( $tests['SELF']['result']->get_error_message(), 'Operation timed out' ) ) {
202
			$tests['SELF']['fail_message'] = esc_html__( 'Your site did not get a response from our debugging service in the expected timeframe. If you are not experiencing other issues, this could be due to a slow connection between your site and our server.', 'jetpack' );
203
		} else {
204
			$tests['SELF']['fail_message'] = esc_html__( 'It looks like your site can not communicate properly with Jetpack.', 'jetpack' );
205
		}
206
207
		?>
208
		<div class="wrap">
209
			<h2><?php esc_html_e( 'Debugging Center', 'jetpack' ); ?></h2>
210
			<?php if ( isset( $can_disconnect ) && $can_disconnect ) : ?>
211
				<div id="message" class="updated notice notice-success is-dismissible"><p><?php esc_html_e( 'This site was successfully disconnected.', 'jetpack' ); ?> <a href="<?php echo esc_url( Jetpack::admin_url() ); ?>"><?php esc_html_e( 'Go to connection screen.', 'jetpack' ); ?></a></p>
212
					<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'jetpack' ); ?></span></button></div>
213
			<?php else : ?>
214
				<h3><?php _e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3>
215
				<div class="jetpack-debug-test-container">
216
					<?php
217
					ob_start();
218
					foreach ( $tests as $test_name => $test_info ) :
219
						if ( 'PASS' !== $test_info['result'] && ( is_wp_error( $test_info['result'] ) ||
220
								false == ( $response_code = wp_remote_retrieve_response_code( $test_info['result'] ) ) ||
221
								'200' != $response_code ) ) {
222
							$debug_info .= $test_name . ": FAIL\r\n";
223
							?>
224
							<div class="jetpack-test-error">
225
							<p>
226
								<a class="jetpack-test-heading" href="#"><?php echo $test_info['fail_message']; ?>
227
									<span class="noticon noticon-collapse"></span>
228
								</a>
229
							</p>
230
						<pre class="jetpack-test-details"><?php echo esc_html( $test_name ); ?>:
231
							<?php echo esc_html( is_wp_error( $test_info['result'] ) ? $test_info['result']->get_error_message() : print_r( $test_info['result'], 1 ) ); ?></pre>
232
							</div>
233
							<?php
234
						} else {
235
							$debug_info .= $test_name . ": PASS\r\n";
236
						}
237
						$debug_raw_info .= "\r\n\r\n" . $test_name . "\r\n" . esc_html( is_wp_error( $test_info['result'] ) ? $test_info['result']->get_error_message() : print_r( $test_info['result'], 1 ) );
238
						?>
239
						<?php
240
					endforeach;
241
					$html = ob_get_clean();
242
243
					if ( '' == trim( $html ) ) {
244
						echo '<div class="jetpack-tests-succed">' . esc_html__( 'Your Jetpack setup looks a-okay!', 'jetpack' ) . '</div>';
245
					} else {
246
						echo '<h3>' . esc_html__( 'There seems to be a problem with your site’s ability to communicate with Jetpack!', 'jetpack' ) . '</h3>';
247
						echo $html;
248
					}
249
					$debug_info .= "\r\n\r\nRAW TEST RESULTS:" . $debug_raw_info . "\r\n";
250
					?>
251
				</div>
252
			<?php endif; ?>
253
254
			<div class="entry-content">
255
				<h3><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h3>
256
				<h4><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></h4>
257
				<ol>
258
					<li><b><em><?php esc_html_e( 'A known issue.', 'jetpack' ); ?></em></b>  <?php echo sprintf( __( 'Some themes and plugins have <a href="%1$s" target="_blank">known conflicts</a> with Jetpack – check the <a href="%2$s" target="_blank">list</a>. (You can also browse the <a href="%3$s" target="_blank">Jetpack support pages</a> or <a href="%4$s" target="_blank">Jetpack support forum</a> to see if others have experienced and solved the problem.)', 'jetpack' ), 'http://jetpack.com/support/getting-started-with-jetpack/known-issues/', 'http://jetpack.com/support/getting-started-with-jetpack/known-issues/', 'http://jetpack.com/support/', 'https://wordpress.org/support/plugin/jetpack' ); ?></li>
259
					<li><b><em><?php esc_html_e( 'An incompatible plugin.', 'jetpack' ); ?></em></b>  <?php esc_html_e( "Find out by disabling all plugins except Jetpack. If the problem persists, it's not a plugin issue. If the problem is solved, turn your plugins on one by one until the problem pops up again – there's the culprit! Let us know, and we'll try to help.", 'jetpack' ); ?></li>
260
					<li>
261
						<b><em><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></em></b>
262
						<?php
263
							$default_theme = wp_get_theme( WP_DEFAULT_THEME );
264
265
						if ( $default_theme->exists() ) {
266
							/* translators: %s is the name of a theme */
267
							echo esc_html( sprintf( __( "If your problem isn't known or caused by a plugin, try activating %s (the default WordPress theme).", 'jetpack' ), $default_theme->get( 'Name' ) ) );
268
						} else {
269
							esc_html_e( "If your problem isn't known or caused by a plugin, try activating the default WordPress theme.", 'jetpack' );
270
						}
271
						?>
272
						<?php esc_html_e( "If this solves the problem, something in your theme is probably broken – let the theme's author know.", 'jetpack' ); ?>
273
					</li>
274
					<li><b><em><?php esc_html_e( 'A problem with your XMLRPC file.', 'jetpack' ); ?></em></b>  <?php echo sprintf( __( 'Load your <a href="%s">XMLRPC file</a>. It should say “XML-RPC server accepts POST requests only.” on a line by itself.', 'jetpack' ), site_url( 'xmlrpc.php' ) ); ?>
275
						<ul>
276
							<li>- <?php esc_html_e( "If it's not by itself, a theme or plugin is displaying extra characters. Try steps 2 and 3.", 'jetpack' ); ?></li>
277
							<li>- <?php esc_html_e( 'If you get a 404 message, contact your web host. Their security may block XMLRPC.', 'jetpack' ); ?></li>
278
						</ul>
279
					</li>
280
					<?php if ( current_user_can( 'jetpack_disconnect' ) && Jetpack::is_active() ) : ?>
281
						<li>
282
							<strong><em><?php esc_html_e( 'A connection problem with WordPress.com.', 'jetpack' ); ?></em></strong>
283
							<?php
284
							echo wp_kses(
285
								sprintf(
286
									__( 'Jetpack works by connecting to WordPress.com for a lot of features. Sometimes, when the connection gets messed up, you need to disconnect and reconnect to get things working properly. <a href="%s">Disconnect from WordPress.com</a>', 'jetpack' ),
287
									wp_nonce_url(
288
										Jetpack::admin_url(
289
											array(
290
												'page' => 'jetpack-debugger',
291
												'disconnect' => true,
292
											)
293
										),
294
										'jp_disconnect',
295
										'nonce'
296
									)
297
								),
298
								array(
299
									'a' => array(
300
										'href'  => array(),
301
										'class' => array(),
302
									),
303
								)
304
							);
305
							?>
306
						</li>
307
					<?php endif; ?>
308
				</ol>
309
				<hr />
310
				<?php if ( Jetpack::is_active() ) : ?>
311
					<div id="connected-user-details">
312
						<h3><?php esc_html_e( 'More details about your Jetpack settings', 'jetpack' ); ?></h3>
313
						<p>
314
						<?php
315
						printf(
316
							/* translators: %s is an e-mail address */
317
							__( 'The primary connection is owned by <strong>%s</strong>\'s WordPress.com account.', 'jetpack' ),
318
							esc_html( Jetpack::get_master_user_email() )
319
						);
320
						?>
321
							</p>
322
					</div>
323
				<?php else : ?>
324
					<div id="dev-mode-details">
325
						<p>
326
						<?php
327
						printf(
328
							__( 'Would you like to use Jetpack on your local development site? You can do so thanks to <a href="%s">Jetpack\'s development mode</a>.', 'jetpack' ),
329
							'https://jetpack.com/support/development-mode/'
330
						);
331
						?>
332
							</p>
333
					</div>
334
				<?php endif; ?>
335
				<?php
336
				if (
337
					current_user_can( 'jetpack_manage_modules' )
338
					&& ( Jetpack::is_development_mode() || Jetpack::is_active() )
339
				) {
340
					printf(
341
						'<p><a href="%1$s">%2$s</a></p>',
342
						Jetpack::admin_url( 'page=jetpack_modules' ),
343
						esc_html__( 'Access the full list of Jetpack modules available on your site.', 'jetpack' )
344
					);
345
				}
346
				?>
347
			</div>
348
		<hr />
349
		<div id="toggle_debug_info"><?php _e( 'Advanced Debug Results', 'jetpack' ); ?></div>
350
			<div id="debug_info_div">
351
			<h4><?php esc_html_e( 'Debug Info', 'jetpack' ); ?></h4>
352
			<div id="debug_info"><pre><?php echo esc_html( $debug_info ); ?></pre></div>
353
		</div>
354
		</div>
355
		<?php
356
	}
357
358
	public static function jetpack_debug_admin_head() {
359
360
		Jetpack_Admin_Page::load_wrapper_styles();
361
		?>
362
		<style type="text/css">
363
364
			.jetpack-debug-test-container {
365
				margin-top: 20px;
366
				margin-bottom: 30px;
367
			}
368
369
			.jetpack-tests-succed {
370
				font-size: large;
371
				color: #8BAB3E;
372
			}
373
374
			.jetpack-test-details {
375
				margin: 4px 6px;
376
				padding: 10px;
377
				overflow: auto;
378
				display: none;
379
			}
380
381
			.jetpack-test-error {
382
				margin-bottom: 10px;
383
				background: #FFEBE8;
384
				border: solid 1px #C00;
385
				border-radius: 3px;
386
			}
387
388
			.jetpack-test-error p {
389
				margin: 0;
390
				padding: 0;
391
			}
392
393
			.jetpack-test-error a.jetpack-test-heading {
394
				padding: 4px 6px;
395
				display: block;
396
				text-decoration: none;
397
				color: inherit;
398
			}
399
400
			.jetpack-test-error .noticon {
401
				float: right;
402
			}
403
404
			.formbox {
405
				margin: 0 0 25px 0;
406
			}
407
408
			.formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea, #debug_info_div {
409
				border: 1px solid #e5e5e5;
410
				border-radius: 11px;
411
				box-shadow: inset 0 1px 1px rgba(0,0,0,0.1);
412
				color: #666;
413
				font-size: 14px;
414
				padding: 10px;
415
				width: 97%;
416
			}
417
			#debug_info_div {
418
				border-radius: 0;
419
				margin-top: 16px;
420
				background: #FFF;
421
				padding: 16px;
422
			}
423
			.formbox .contact-support input[type="submit"] {
424
				float: right;
425
				margin: 0 !important;
426
				border-radius: 20px !important;
427
				cursor: pointer;
428
				font-size: 13pt !important;
429
				height: auto !important;
430
				margin: 0 0 2em 10px !important;
431
				padding: 8px 16px !important;
432
				background-color: #ddd;
433
				border: 1px solid rgba(0,0,0,0.05);
434
				border-top-color: rgba(255,255,255,0.1);
435
				border-bottom-color: rgba(0,0,0,0.15);
436
				color: #333;
437
				font-weight: 400;
438
				display: inline-block;
439
				text-align: center;
440
				text-decoration: none;
441
			}
442
443
			.formbox span.errormsg {
444
				margin: 0 0 10px 10px;
445
				color: #d00;
446
				display: none;
447
			}
448
449
			.formbox.error span.errormsg {
450
				display: block;
451
			}
452
453
			#debug_info_div, #toggle_debug_info, #debug_info_div p {
454
				font-size: 12px;
455
			}
456
457
			#category_div ul li {
458
				list-style-type: none;
459
			}
460
461
		</style>
462
		<script type="text/javascript">
463
		jQuery( document ).ready( function($) {
464
465
			$( '#debug_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
466
			$( '#debug_form_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
467
468
			$( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() {
469
				$( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle();
470
				return false;
471
			} );
472
473
		} );
474
		</script>
475
		<?php
476
	}
477
}
478