Completed
Push — enhance/failed-auth-survey ( 62d2b0 )
by
unknown
09:57
created

Jetpack_Debugger::jetpack_debug_display_handler()   F

Complexity

Conditions 23
Paths 13824

Size

Total Lines 250
Code Lines 169

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 23
eloc 169
nc 13824
nop 0
dl 0
loc 250
rs 2

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
class Jetpack_Debugger {
4
5
	private static function is_jetpack_support_open() {
6
		try {
7
			$response = wp_remote_request( "http://jetpack.me/is-support-open" );
8
			$body = wp_remote_retrieve_body( $response );
9
			$json = json_decode( $body );
10
			return ( ( bool ) $json->is_support_open );
11
		}
12
		catch ( Exception $e ) {
13
			return true;
14
		}
15
	}
16
17
	public static function jetpack_increase_timeout() {
18
		return 30; // seconds
19
	}
20
21
	public static function jetpack_debug_display_handler() {
22
		if ( ! current_user_can( 'manage_options' ) )
23
			wp_die( esc_html__('You do not have sufficient permissions to access this page.', 'jetpack' ) );
24
25
		$current_user = wp_get_current_user();
26
27
		$user_id = get_current_user_id();
28
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
29
		if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) {
30
			$user_token = $user_tokens[$user_id];
31
		} else {
32
			$user_token = '[this user has no token]';
33
		}
34
		unset( $user_tokens );
35
36
		$debug_info = "\r\n";
37
		foreach ( array(
38
			'CLIENT_ID'   => 'id',
39
			'BLOG_TOKEN'  => 'blog_token',
40
			'MASTER_USER' => 'master_user',
41
			'CERT'        => 'fallback_no_verify_ssl_certs',
42
			'TIME_DIFF'   => 'time_diff',
43
			'VERSION'     => 'version',
44
			'OLD_VERSION' => 'old_version',
45
			'PUBLIC'      => 'public',
46
		) as $label => $option_name ) {
47
			$debug_info .= "\r\n" . esc_html( $label . ": " . Jetpack_Options::get_option( $option_name ) );
48
		}
49
50
		$debug_info .= "\r\n" . esc_html( "USER_ID: " . $user_id );
51
		$debug_info .= "\r\n" . esc_html( "USER_TOKEN: " . $user_token );
52
		$debug_info .= "\r\n" . esc_html( "PHP_VERSION: " . PHP_VERSION );
53
		$debug_info .= "\r\n" . esc_html( "WORDPRESS_VERSION: " . $GLOBALS['wp_version'] );
54
		$debug_info .= "\r\n" . esc_html( "JETPACK__VERSION: " . JETPACK__VERSION );
55
		$debug_info .= "\r\n" . esc_html( "JETPACK__PLUGIN_DIR: " . JETPACK__PLUGIN_DIR );
56
		$debug_info .= "\r\n" . esc_html( "SITE_URL: " . site_url() );
57
		$debug_info .= "\r\n" . esc_html( "HOME_URL: " . home_url() );
58
		$debug_info .= "\r\n" . esc_html( "SERVER_PORT: " . $_SERVER['SERVER_PORT'] );
59
60
61
		foreach ( array (
62
					  'GD_PHP_HANDLER',
63
					  'HTTP_AKAMAI_ORIGIN_HOP',
64
					  'HTTP_CF_CONNECTING_IP',
65
					  'HTTP_CLIENT_IP',
66
					  'HTTP_FASTLY_CLIENT_IP',
67
					  'HTTP_FORWARDED',
68
					  'HTTP_FORWARDED_FOR',
69
					  'HTTP_INCAP_CLIENT_IP',
70
					  'HTTP_TRUE_CLIENT_IP',
71
					  'HTTP_X_CLIENTIP',
72
					  'HTTP_X_CLUSTER_CLIENT_IP',
73
					  'HTTP_X_FORWARDED',
74
					  'HTTP_X_FORWARDED_FOR',
75
					  'HTTP_X_IP_TRAIL',
76
					  'HTTP_X_REAL_IP',
77
					  'HTTP_X_VARNISH',
78
					  'REMOTE_ADDR'
79
				  ) as $header ) {
80
			if( isset( $_SERVER[$header] ) ) {
81
				$debug_info .= "\r\n" . esc_html( 'IP HEADER: '.$header . ": " . $_SERVER[$header] );
82
			} else {
83
				$debug_info .= "\r\n" . esc_html( 'IP HEADER: '.$header . ": Not Set" );
84
			}
85
		}
86
87
88
		$debug_info .= "\r\n" . esc_html( "PROTECT_TRUSTED_HEADER: " . json_encode(get_site_option( 'trusted_ip_header' )));
89
90
		$debug_info .= "\r\n\r\nTEST RESULTS:\r\n\r\n";
91
		$debug_raw_info = '';
92
93
94
		$tests = array();
95
96
		$tests['HTTP']['result'] = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
97
		$tests['HTTP']['fail_message'] = esc_html__( 'Your site isn’t reaching the Jetpack servers.', 'jetpack' );
98
99
		$tests['HTTPS']['result'] = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
100
		$tests['HTTPS']['fail_message'] = esc_html__( 'Your site isn’t securely reaching the Jetpack servers.', 'jetpack' );
101
102
		$identity_crisis_message = '';
103
		if ( $identity_crisis = Jetpack::check_identity_crisis( true ) ) {
104
			foreach( $identity_crisis as $key => $value ) {
105
				$identity_crisis_message .= sprintf( __( 'Your `%1$s` option is set up as `%2$s`, but your WordPress.com connection lists it as `%3$s`!', 'jetpack' ), $key, (string) get_option( $key ), $value ) . "\r\n";
106
			}
107
			$identity_crisis = new WP_Error( 'identity-crisis', $identity_crisis_message, $identity_crisis );
108
		} else {
109
			$identity_crisis = 'PASS';
110
		}
111
		$tests['IDENTITY_CRISIS']['result'] = $identity_crisis;
112
		$tests['IDENTITY_CRISIS']['fail_message'] = esc_html__( 'Something has gotten mixed up in your Jetpack Connection!', 'jetpack' );
113
114
		$self_xml_rpc_url = home_url( 'xmlrpc.php' );
115
116
		$testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
117
118
		add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
119
120
		$tests['SELF']['result'] = wp_remote_get( $testsite_url . $self_xml_rpc_url );
121
		$tests['SELF']['fail_message'] = esc_html__( 'It looks like your site can not communicate properly with Jetpack.', 'jetpack' );
122
123
		remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
124
125
		?>
126
		<div class="wrap">
127
			<h2><?php esc_html_e( 'Jetpack Debugging Center', 'jetpack' ); ?></h2>
128
			<h3><?php _e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3>
129
			<div class="jetpack-debug-test-container">
130
			<?php
131
			ob_start();
132
			foreach ( $tests as $test_name => $test_info ) :
133
				if ( 'PASS' !== $test_info['result'] && ( is_wp_error( $test_info['result'] ) ||
134
					false == ( $response_code = wp_remote_retrieve_response_code( $test_info['result'] ) )  ||
135
					'200' != $response_code ) ) {
0 ignored issues
show
Bug introduced by
The variable $response_code does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
136
					$debug_info .= $test_name . ": FAIL\r\n";
137
					?>
138
					<div class="jetpack-test-error">
139
						<p>
140
							<a class="jetpack-test-heading" href="#"><?php echo $test_info['fail_message']; ?>
141
							<span class="noticon noticon-collapse"></span>
142
							</a>
143
						</p>
144
						<pre class="jetpack-test-details"><?php echo esc_html( $test_name ); ?>:
145
	<?php echo esc_html( is_wp_error( $test_info['result'] ) ? $test_info['result']->get_error_message() : print_r( $test_info['result'], 1 ) ); ?></pre>
146
					</div><?php
147
				} else {
148
					$debug_info .= $test_name . ": PASS\r\n";
149
				}
150
				$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 ) );
151
				?>
152
			<?php endforeach;
153
			$html = ob_get_clean();
154
155
			if ( '' == trim( $html ) ) {
156
				echo '<div class="jetpack-tests-succed">' . esc_html__( 'Your Jetpack setup looks a-okay!', 'jetpack' ) . '</div>';
157
			}
158
			else {
159
				echo '<h3>' . esc_html__( 'There seems to be a problem with your site’s ability to communicate with Jetpack!', 'jetpack' ) . '</h3>';
160
				echo $html;
161
			}
162
			$debug_info .= "\r\n\r\nRAW TEST RESULTS:" . $debug_raw_info ."\r\n";
163
			?>
164
			</div>
165
			<div class="entry-content">
166
				<h3><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h3>
167
				<h4><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></h4>
168
				<ol>
169
					<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">known conflicts</a> with Jetpack – check the <a href="%2$s">list</a>. (You can also browse the <a href="%3$s">Jetpack support pages</a> or <a href="%4$s">Jetpack support forum</a> to see if others have experienced and solved the problem.)', 'jetpack' ), 'http://jetpack.me/support/getting-started-with-jetpack/known-issues/', 'http://jetpack.me/support/getting-started-with-jetpack/known-issues/', 'http://jetpack.me/support/', 'http://wordpress.org/support/plugin/jetpack' ); ?></li>
170
					<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>
171
					<li>
172
						<b><em><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></em></b>
173
						<?php
174
							$default_theme = wp_get_theme( WP_DEFAULT_THEME );
175
176
							if ( $default_theme->exists() ) {
177
								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' ) ) );
178
							} else {
179
								esc_html_e( "If your problem isn't known or caused by a plugin, try activating the default WordPress theme.", 'jetpack' );
180
							}
181
						?>
182
						<?php esc_html_e( "If this solves the problem, something in your theme is probably broken – let the theme's author know.", 'jetpack' ); ?>
183
					</li>
184
					<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' ) ); ?>
185
						<ul>
186
							<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>
187
							<li>- <?php esc_html_e( "If you get a 404 message, contact your web host. Their security may block XMLRPC.", 'jetpack' ); ?></li>
188
						</ul>
189
					</li>
190
				</ol>
191
				<?php if ( self::is_jetpack_support_open() ): ?>
192
				<p class="jetpack-show-contact-form"><?php echo sprintf( __( 'If none of these help you find a solution, <a href="%s">click here to contact Jetpack support</a>. Tell us as much as you can about the issue and what steps you\'ve tried to resolve it, and one of our Happiness Engineers will be in touch to help.', 'jetpack' ), Jetpack::admin_url( array( 'page' => 'jetpack-debugger', 'contact' => true ) ) ); ?>
193
				</p>
194
				<?php endif; ?>
195
				<?php if ( Jetpack::is_active() ) : ?>
196
					<hr />
197
					<div id="connected-user-details">
198
						<p><?php printf( __( 'The primary connection is owned by <strong>%s</strong>\'s WordPress.com account.', 'jetpack' ), esc_html( Jetpack::get_master_user_email() ) ); ?></p>
199
					</div>
200
					<hr />
201
					<div id="sync-related-posts">
202
						<p><?php echo esc_html__( 'Some features of Jetpack uses the WordPress.com infrastructure and requires that your public content be mirrored there. If you see intermittent issues only affecting certain posts, please try requesting a reindex of your posts.', 'jetpack' ); ?></p>
203
						<?php echo Jetpack::init()->sync->reindex_ui() ?>
204
					</div>
205
				<?php endif; ?>
206
			</div>
207
			<div id="contact-message" <?php if( ! isset( $_GET['contact'] ) ) {?>  style="display:none" <?php } ?>>
208
			<?php if ( self::is_jetpack_support_open() ): ?>
209
				<form id="contactme" method="post" action="http://jetpack.me/contact-support/">
210
					<input type="hidden" name="action" value="submit">
211
					<input type="hidden" name="jetpack" value="needs-service">
212
213
					<input type="hidden" name="contact_form" id="contact_form" value="1">
214
					<input type="hidden" name="blog_url" id="blog_url" value="<?php echo esc_attr( site_url() ); ?>">
215
					<?php
216
						$subject_line = sprintf(
217
							/* translators: %s is the URL of the site */
218
							_x( 'from: %s Jetpack contact form', 'Support request email subject line', 'jetpack' ),
219
							esc_attr( site_url() )
220
						);
221
222
						if ( Jetpack::is_development_version() ) {
223
							$subject_line = 'BETA ' . $subject_line;
224
						}
225
226
						$subject_line_input = printf(
0 ignored issues
show
Unused Code introduced by
$subject_line_input 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...
227
							'<input type="hidden" name="subject" id="subject" value="%s"">',
228
							$subject_line
229
						);
230
					?>
231
					<div class="formbox">
232
						<label for="message" class="h"><?php esc_html_e( 'Please describe the problem you are having.', 'jetpack' ); ?></label>
233
						<textarea name="message" cols="40" rows="7" id="did"></textarea>
234
					</div>
235
236
					<div id="name_div" class="formbox">
237
						<label class="h" for="your_name"><?php esc_html_e( 'Name', 'jetpack' ); ?></label>
238
			  			<span class="errormsg"><?php esc_html_e( 'Let us know your name.', 'jetpack' ); ?></span>
239
						<input name="your_name" type="text" id="your_name" value="<?php esc_html_e( $current_user->display_name, 'jetpack'); ?>" size="40">
240
					</div>
241
242
					<div id="email_div" class="formbox">
243
						<label class="h" for="your_email"><?php esc_html_e( 'E-mail', 'jetpack' ); ?></label>
244
			  			<span class="errormsg"><?php esc_html_e( 'Use a valid email address.', 'jetpack' ); ?></span>
245
						<input name="your_email" type="text" id="your_email" value="<?php esc_html_e( $current_user->user_email, 'jetpack'); ?>" size="40">
246
					</div>
247
248
					<div id="toggle_debug_info" class="formbox">
249
						<p><?php _e( 'The test results and some other useful debug information will be sent to the support team. Please feel free to <a href="#">review/modify</a> this information.', 'jetpack' ); ?></p>
250
					</div>
251
252
					<div id="debug_info_div" class="formbox" style="display:none">
253
						<label class="h" for="debug_info"><?php esc_html_e( 'Debug Info', 'jetpack' ); ?></label>
254
			  			<textarea name="debug_info" cols="40" rows="7" id="debug_info"><?php echo esc_attr( $debug_info ); ?></textarea>
255
					</div>
256
257
					<div style="clear: both;"></div>
258
259
					<div id="blog_div" class="formbox">
260
						<div id="submit_div" class="contact-support">
261
						<input type="submit" name="submit" value="<?php esc_html_e( 'Submit &#187;', 'jetpack' ); ?>">
262
						</div>
263
					</div>
264
					<div style="clear: both;"></div>
265
				</form>
266
			<?php endif; ?>
267
			</div>
268
		</div>
269
	<?php
270
	}
271
272
	public static function jetpack_debug_admin_head() {
273
		?>
274
		<style type="text/css">
275
276
			.jetpack-debug-test-container {
277
				margin-top: 20px;
278
				margin-bottom: 30px;
279
			}
280
281
			.jetpack-tests-succed {
282
				font-size: large;
283
				color: #8BAB3E;
284
			}
285
286
			.jetpack-test-details {
287
				margin: 4px 6px;
288
				padding: 10px;
289
				overflow: auto;
290
				display: none;
291
			}
292
293
			.jetpack-test-error {
294
				margin-bottom: 10px;
295
				background: #FFEBE8;
296
				border: solid 1px #C00;
297
				border-radius: 3px;
298
			}
299
300
			.jetpack-test-error p {
301
				margin: 0;
302
				padding: 0;
303
			}
304
305
			.jetpack-test-error a.jetpack-test-heading {
306
				padding: 4px 6px;
307
				display: block;
308
				text-decoration: none;
309
				color: inherit;
310
			}
311
312
			.jetpack-test-error .noticon {
313
				float: right;
314
			}
315
316
			form#contactme {
317
				border: 1px solid #dfdfdf;
318
				background: #eaf3fa;
319
				padding: 20px;
320
				margin: 10px;
321
				background-color: #eaf3fa;
322
				border-radius: 5px;
323
				font-size: 15px;
324
				font-family: "Open Sans", "Helvetica Neue", sans-serif;
325
			}
326
327
			form#contactme label.h {
328
				color: #444;
329
				display: block;
330
				font-weight: bold;
331
				margin: 0 0 7px 10px;
332
				text-shadow: 1px 1px 0 #fff;
333
			}
334
335
			.formbox {
336
				margin: 0 0 25px 0;
337
			}
338
339
			.formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea {
340
				border: 1px solid #e5e5e5;
341
				border-radius: 11px;
342
				box-shadow: inset 0 1px 1px rgba(0,0,0,0.1);
343
				color: #666;
344
				font-size: 14px;
345
				padding: 10px;
346
				width: 97%;
347
			}
348
			.formbox .contact-support input[type="submit"] {
349
				float: right;
350
				margin: 0 !important;
351
				border-radius: 20px !important;
352
				cursor: pointer;
353
				font-size: 13pt !important;
354
				height: auto !important;
355
				margin: 0 0 2em 10px !important;
356
				padding: 8px 16px !important;
357
				background-color: #ddd;
358
				border: 1px solid rgba(0,0,0,0.05);
359
				border-top-color: rgba(255,255,255,0.1);
360
				border-bottom-color: rgba(0,0,0,0.15);
361
				color: #333;
362
				font-weight: 400;
363
				display: inline-block;
364
				text-align: center;
365
				text-decoration: none;
366
			}
367
368
			.formbox span.errormsg {
369
				margin: 0 0 10px 10px;
370
				color: #d00;
371
				display: none;
372
			}
373
374
			.formbox.error span.errormsg {
375
				display: block;
376
			}
377
378
			#contact-message ul {
379
				margin: 0 0 20px 10px;
380
			}
381
382
			#contact-message li {
383
				margin: 0 0 10px 10px;
384
				list-style: disc;
385
				display: list-item;
386
			}
387
388
		</style>
389
		<script type="text/javascript">
390
		jQuery( document ).ready( function($) {
391
392
			$('#debug_info').prepend('jQuery version: ' + jQuery.fn.jquery + "\r\n");
393
394
			$( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() {
395
				$( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle();
396
				return false;
397
			} );
398
399
			$( '.jetpack-show-contact-form a' ).on( 'click', function() {
400
				$('#contact-message').slideToggle();
401
				return false;
402
			} );
403
404
			$( '#toggle_debug_info a' ).on( 'click', function() {
405
				$('#debug_info_div').slideToggle();
406
				return false;
407
			} );
408
409
			$('form#contactme').on("submit", function(e){
410
				var form = $(this);
411
				var message = form.find('#did');
412
				var name = form.find('#your_name');
413
				var email = form.find('#your_email')
414
				var validation_error = false;
415
				if( !name.val() ) {
416
					name.parents('.formbox').addClass('error');
417
					validation_error = true;
418
				}
419
				if( !email.val() ) {
420
					email.parents('.formbox').addClass('error');
421
					validation_error = true;
422
				}
423
				if ( validation_error ) {
424
					return false;
425
				}
426
				message.val(message.val() + "\r\n\r\n----------------------------------------------\r\n\r\nDEBUG INFO:\r\n" + $('#debug_info').val()  );
427
				return true;
428
	    	});
429
430
		} );
431
		</script>
432
		<?php
433
	}
434
}
435