Completed
Push — add/xml-sitemap ( f1e7a4...063fac )
by Jeremy
110:17 queued 98:29
created

class.jetpack-debugger.php (1 issue)

Labels
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 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
		global $current_user;
26
		get_currentuserinfo();
27
28
		$user_id = get_current_user_id();
29
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
30
		if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) {
31
			$user_token = $user_tokens[$user_id];
32
		} else {
33
			$user_token = '[this user has no token]';
34
		}
35
		unset( $user_tokens );
36
37
		$debug_info = "\r\n";
38
		foreach ( array(
39
			'CLIENT_ID'   => 'id',
40
			'BLOG_TOKEN'  => 'blog_token',
41
			'MASTER_USER' => 'master_user',
42
			'CERT'        => 'fallback_no_verify_ssl_certs',
43
			'TIME_DIFF'   => 'time_diff',
44
			'VERSION'     => 'version',
45
			'OLD_VERSION' => 'old_version',
46
			'PUBLIC'      => 'public',
47
		) as $label => $option_name ) {
48
			$debug_info .= "\r\n" . esc_html( $label . ": " . Jetpack_Options::get_option( $option_name ) );
49
		}
50
51
		$debug_info .= "\r\n" . esc_html( "USER_ID: " . $user_id );
52
		$debug_info .= "\r\n" . esc_html( "USER_TOKEN: " . $user_token );
53
		$debug_info .= "\r\n" . esc_html( "PHP_VERSION: " . PHP_VERSION );
54
		$debug_info .= "\r\n" . esc_html( "WORDPRESS_VERSION: " . $GLOBALS['wp_version'] );
55
		$debug_info .= "\r\n" . esc_html( "JETPACK__VERSION: " . JETPACK__VERSION );
56
		$debug_info .= "\r\n" . esc_html( "JETPACK__PLUGIN_DIR: " . JETPACK__PLUGIN_DIR );
57
		$debug_info .= "\r\n" . esc_html( "SITE_URL: " . site_url() );
58
		$debug_info .= "\r\n" . esc_html( "HOME_URL: " . home_url() );
59
		$debug_info .= "\r\n" . esc_html( "SERVER_PORT: " . $_SERVER['SERVER_PORT'] );
60
61
62
		foreach ( array (
63
					  'GD_PHP_HANDLER',
64
					  'HTTP_AKAMAI_ORIGIN_HOP',
65
					  'HTTP_CF_CONNECTING_IP',
66
					  'HTTP_CLIENT_IP',
67
					  'HTTP_FASTLY_CLIENT_IP',
68
					  'HTTP_FORWARDED',
69
					  'HTTP_FORWARDED_FOR',
70
					  'HTTP_INCAP_CLIENT_IP',
71
					  'HTTP_TRUE_CLIENT_IP',
72
					  'HTTP_X_CLIENTIP',
73
					  'HTTP_X_CLUSTER_CLIENT_IP',
74
					  'HTTP_X_FORWARDED',
75
					  'HTTP_X_FORWARDED_FOR',
76
					  'HTTP_X_IP_TRAIL',
77
					  'HTTP_X_REAL_IP',
78
					  'HTTP_X_VARNISH',
79
					  'REMOTE_ADDR'
80
				  ) as $header ) {
81
			if( isset( $_SERVER[$header] ) ) {
82
				$debug_info .= "\r\n" . esc_html( 'IP HEADER: '.$header . ": " . $_SERVER[$header] );
83
			} else {
84
				$debug_info .= "\r\n" . esc_html( 'IP HEADER: '.$header . ": Not Set" );
85
			}
86
		}
87
88
89
		$debug_info .= "\r\n" . esc_html( "PROTECT_TRUSTED_HEADER: " . json_encode(get_site_option( 'trusted_ip_header' )));
90
91
		$debug_info .= "\r\n\r\nTEST RESULTS:\r\n\r\n";
92
		$debug_raw_info = '';
93
94
95
		$tests = array();
96
97
		$tests['HTTP']['result'] = wp_remote_get( preg_replace( '/^https:/', 'http:', JETPACK__API_BASE ) . 'test/1/' );
98
		$tests['HTTP']['fail_message'] = esc_html__( 'Your site isn’t reaching the Jetpack servers.', 'jetpack' );
99
100
		$tests['HTTPS']['result'] = wp_remote_get( preg_replace( '/^http:/', 'https:', JETPACK__API_BASE ) . 'test/1/' );
101
		$tests['HTTPS']['fail_message'] = esc_html__( 'Your site isn’t securely reaching the Jetpack servers.', 'jetpack' );
102
103
		$identity_crisis_message = '';
104
		if ( $identity_crisis = Jetpack::check_identity_crisis( true ) ) {
105
			foreach( $identity_crisis as $key => $value ) {
106
				$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";
107
			}
108
			$identity_crisis = new WP_Error( 'identity-crisis', $identity_crisis_message, $identity_crisis );
109
		} else {
110
			$identity_crisis = 'PASS';
111
		}
112
		$tests['IDENTITY_CRISIS']['result'] = $identity_crisis;
113
		$tests['IDENTITY_CRISIS']['fail_message'] = esc_html__( 'Something has gotten mixed up in your Jetpack Connection!', 'jetpack' );
114
115
		$self_xml_rpc_url = home_url( 'xmlrpc.php' );
116
117
		$testsite_url = Jetpack::fix_url_for_bad_hosts( JETPACK__API_BASE . 'testsite/1/?url=' );
118
119
		add_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
120
121
		$tests['SELF']['result'] = wp_remote_get( $testsite_url . $self_xml_rpc_url );
122
		$tests['SELF']['fail_message'] = esc_html__( 'It looks like your site can not communicate properly with Jetpack.', 'jetpack' );
123
124
		remove_filter( 'http_request_timeout', array( 'Jetpack_Debugger', 'jetpack_increase_timeout' ) );
125
126
		?>
127
		<div class="wrap">
128
			<h2><?php esc_html_e( 'Jetpack Debugging Center', 'jetpack' ); ?></h2>
129
			<h3><?php _e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3>
130
			<div class="jetpack-debug-test-container">
131
			<?php
132
			ob_start();
133
			foreach ( $tests as $test_name => $test_info ) :
134
				if ( 'PASS' !== $test_info['result'] && ( is_wp_error( $test_info['result'] ) ||
135
					false == ( $response_code = wp_remote_retrieve_response_code( $test_info['result'] ) )  ||
136
					'200' != $response_code ) ) {
0 ignored issues
show
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...
137
					$debug_info .= $test_name . ": FAIL\r\n";
138
					?>
139
					<div class="jetpack-test-error">
140
						<p>
141
							<a class="jetpack-test-heading" href="#"><?php echo $test_info['fail_message']; ?>
142
							<span class="noticon noticon-collapse"></span>
143
							</a>
144
						</p>
145
						<pre class="jetpack-test-details"><?php echo esc_html( $test_name ); ?>:
146
	<?php echo esc_html( is_wp_error( $test_info['result'] ) ? $test_info['result']->get_error_message() : print_r( $test_info['result'], 1 ) ); ?></pre>
147
					</div><?php
148
				} else {
149
					$debug_info .= $test_name . ": PASS\r\n";
150
				}
151
				$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 ) );
152
				?>
153
			<?php endforeach;
154
			$html = ob_get_clean();
155
156
			if ( '' == trim( $html ) ) {
157
				echo '<div class="jetpack-tests-succed">' . esc_html__( 'Your Jetpack setup looks a-okay!', 'jetpack' ) . '</div>';
158
			}
159
			else {
160
				echo '<h3>' . esc_html__( 'There seems to be a problem with your site’s ability to communicate with Jetpack!', 'jetpack' ) . '</h3>';
161
				echo $html;
162
			}
163
			$debug_info .= "\r\n\r\nRAW TEST RESULTS:" . $debug_raw_info ."\r\n";
164
			?>
165
			</div>
166
			<div class="entry-content">
167
				<h3><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h3>
168
				<h4><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></h4>
169
				<ol>
170
					<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>
171
					<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>
172
					<li><b><em><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></em></b>  <?php esc_html_e( "If your problem isn't known or caused by a plugin, try activating Twenty Fifteen (the default WordPress theme). If this solves the problem, something in your theme is probably broken – let the theme's author know.", 'jetpack' ); ?></li>
173
					<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' ) ); ?>
174
						<ul>
175
							<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>
176
							<li>- <?php esc_html_e( "If you get a 404 message, contact your web host. Their security may block XMLRPC.", 'jetpack' ); ?></li>
177
						</ul>
178
					</li>
179
				</ol>
180
				<?php if ( self::is_jetpack_support_open() ): ?>
181
				<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 ) ) ); ?>
182
				</p>
183
				<?php endif; ?>
184
				<?php if ( Jetpack::is_active() ) : ?>
185
					<hr />
186
					<div id="connected-user-details">
187
						<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>
188
					</div>
189
					<hr />
190
					<div id="sync-related-posts">
191
						<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>
192
						<?php echo Jetpack::init()->sync->reindex_ui() ?>
193
					</div>
194
				<?php endif; ?>
195
			</div>
196
			<div id="contact-message" <?php if( ! isset( $_GET['contact'] ) ) {?>  style="display:none" <?php } ?>>
197
			<?php if ( self::is_jetpack_support_open() ): ?>
198
				<form id="contactme" method="post" action="http://jetpack.me/contact-support/">
199
					<input type="hidden" name="action" value="submit">
200
					<input type="hidden" name="jetpack" value="needs-service">
201
202
					<input type="hidden" name="contact_form" id="contact_form" value="1">
203
					<input type="hidden" name="blog_url" id="blog_url" value="<?php echo esc_attr( site_url() ); ?>">
204
					<input type="hidden" name="subject" id="subject" value="from: <?php echo esc_attr( site_url() ); ?> Jetpack contact form">
205
					<div class="formbox">
206
						<label for="message" class="h"><?php esc_html_e( 'Please describe the problem you are having.', 'jetpack' ); ?></label>
207
						<textarea name="message" cols="40" rows="7" id="did"></textarea>
208
					</div>
209
210
					<div id="name_div" class="formbox">
211
						<label class="h" for="your_name"><?php esc_html_e( 'Name', 'jetpack' ); ?></label>
212
			  			<span class="errormsg"><?php esc_html_e( 'Let us know your name.', 'jetpack' ); ?></span>
213
						<input name="your_name" type="text" id="your_name" value="<?php esc_html_e( $current_user->display_name, 'jetpack'); ?>" size="40">
214
					</div>
215
216
					<div id="email_div" class="formbox">
217
						<label class="h" for="your_email"><?php esc_html_e( 'E-mail', 'jetpack' ); ?></label>
218
			  			<span class="errormsg"><?php esc_html_e( 'Use a valid email address.', 'jetpack' ); ?></span>
219
						<input name="your_email" type="text" id="your_email" value="<?php esc_html_e( $current_user->user_email, 'jetpack'); ?>" size="40">
220
					</div>
221
222
					<div id="toggle_debug_info" class="formbox">
223
						<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>
224
					</div>
225
226
					<div id="debug_info_div" class="formbox" style="display:none">
227
						<label class="h" for="debug_info"><?php esc_html_e( 'Debug Info', 'jetpack' ); ?></label>
228
			  			<textarea name="debug_info" cols="40" rows="7" id="debug_info"><?php echo esc_attr( $debug_info ); ?></textarea>
229
					</div>
230
231
					<div style="clear: both;"></div>
232
233
					<div id="blog_div" class="formbox">
234
						<div id="submit_div" class="contact-support">
235
						<input type="submit" name="submit" value="<?php esc_html_e( 'Submit &#187;', 'jetpack' ); ?>">
236
						</div>
237
					</div>
238
					<div style="clear: both;"></div>
239
				</form>
240
			<?php endif; ?>
241
			</div>
242
		</div>
243
	<?php
244
	}
245
246
	public static function jetpack_debug_admin_head() {
247
		?>
248
		<style type="text/css">
249
250
			.jetpack-debug-test-container {
251
				margin-top: 20px;
252
				margin-bottom: 30px;
253
			}
254
255
			.jetpack-tests-succed {
256
				font-size: large;
257
				color: #8BAB3E;
258
			}
259
260
			.jetpack-test-details {
261
				margin: 4px 6px;
262
				padding: 10px;
263
				overflow: auto;
264
				display: none;
265
			}
266
267
			.jetpack-test-error {
268
				margin-bottom: 10px;
269
				background: #FFEBE8;
270
				border: solid 1px #C00;
271
				border-radius: 3px;
272
			}
273
274
			.jetpack-test-error p {
275
				margin: 0;
276
				padding: 0;
277
			}
278
279
			.jetpack-test-error a.jetpack-test-heading {
280
				padding: 4px 6px;
281
				display: block;
282
				text-decoration: none;
283
				color: inherit;
284
			}
285
286
			.jetpack-test-error .noticon {
287
				float: right;
288
			}
289
290
			form#contactme {
291
				border: 1px solid #dfdfdf;
292
				background: #eaf3fa;
293
				padding: 20px;
294
				margin: 10px;
295
				background-color: #eaf3fa;
296
				border-radius: 5px;
297
				font-size: 15px;
298
				font-family: "Open Sans", "Helvetica Neue", sans-serif;
299
			}
300
301
			form#contactme label.h {
302
				color: #444;
303
				display: block;
304
				font-weight: bold;
305
				margin: 0 0 7px 10px;
306
				text-shadow: 1px 1px 0 #fff;
307
			}
308
309
			.formbox {
310
				margin: 0 0 25px 0;
311
			}
312
313
			.formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea {
314
				border: 1px solid #e5e5e5;
315
				border-radius: 11px;
316
				box-shadow: inset 0 1px 1px rgba(0,0,0,0.1);
317
				color: #666;
318
				font-size: 14px;
319
				padding: 10px;
320
				width: 97%;
321
			}
322
			.formbox .contact-support input[type="submit"] {
323
				float: right;
324
				margin: 0 !important;
325
				border-radius: 20px !important;
326
				cursor: pointer;
327
				font-size: 13pt !important;
328
				height: auto !important;
329
				margin: 0 0 2em 10px !important;
330
				padding: 8px 16px !important;
331
				background-color: #ddd;
332
				border: 1px solid rgba(0,0,0,0.05);
333
				border-top-color: rgba(255,255,255,0.1);
334
				border-bottom-color: rgba(0,0,0,0.15);
335
				color: #333;
336
				font-weight: 400;
337
				display: inline-block;
338
				text-align: center;
339
				text-decoration: none;
340
			}
341
342
			.formbox span.errormsg {
343
				margin: 0 0 10px 10px;
344
				color: #d00;
345
				display: none;
346
			}
347
348
			.formbox.error span.errormsg {
349
				display: block;
350
			}
351
352
			#contact-message ul {
353
				margin: 0 0 20px 10px;
354
			}
355
356
			#contact-message li {
357
				margin: 0 0 10px 10px;
358
				list-style: disc;
359
				display: list-item;
360
			}
361
362
		</style>
363
		<script type="text/javascript">
364
		jQuery( document ).ready( function($) {
365
366
			$('#debug_info').prepend('jQuery version: ' + jQuery.fn.jquery + "\r\n");
367
368
			$( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() {
369
				$( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle();
370
				return false;
371
			} );
372
373
			$( '.jetpack-show-contact-form a' ).on( 'click', function() {
374
				$('#contact-message').slideToggle();
375
				return false;
376
			} );
377
378
			$( '#toggle_debug_info a' ).on( 'click', function() {
379
				$('#debug_info_div').slideToggle();
380
				return false;
381
			} );
382
383
			$('form#contactme').on("submit", function(e){
384
				var form = $(this);
385
				var message = form.find('#did');
386
				var name = form.find('#your_name');
387
				var email = form.find('#your_email')
388
				var validation_error = false;
389
				if( !name.val() ) {
390
					name.parents('.formbox').addClass('error');
391
					validation_error = true;
392
				}
393
				if( !email.val() ) {
394
					email.parents('.formbox').addClass('error');
395
					validation_error = true;
396
				}
397
				if ( validation_error ) {
398
					return false;
399
				}
400
				message.val(message.val() + "\r\n\r\n----------------------------------------------\r\n\r\nDEBUG INFO:\r\n" + $('#debug_info').val()  );
401
				return true;
402
	    	});
403
404
		} );
405
		</script>
406
		<?php
407
	}
408
}
409