Completed
Push — update/add-single-purpose-jetp... ( 28d890...705f7e )
by
unknown
09:19
created

Jetpack_Debugger::jetpack_debug_display_handler()   C

Complexity

Conditions 12
Paths 128

Size

Total Lines 202

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 128
nop 0
dl 0
loc 202
rs 5.3865
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
use Automattic\Jetpack\Status;
4
5
/**
6
 * Jetpack Debugger functionality allowing for self-service diagnostic information via the legacy jetpack debugger.
7
 *
8
 * @package jetpack
9
 */
10
11
/**
12
 * Class Jetpack_Debugger
13
 *
14
 * A namespacing class for functionality related to the legacy in-plugin diagnostic tooling.
15
 */
16
class Jetpack_Debugger {
17
	/**
18
	 * Returns 30 for use with a filter.
19
	 *
20
	 * To allow time for WP.com to run upstream testing, this function exists to increase the http_request_timeout value
21
	 * to 30.
22
	 *
23
	 * @deprecated 8.0.0
24
	 *
25
	 * @return int 30
26
	 */
27
	public static function jetpack_increase_timeout() {
28
		_deprecated_function( __METHOD__, 'jetpack-8.0', 'Jetpack_Cxn_Tests::increase_timeout' );
29
		return 30; // seconds.
30
	}
31
32
	/**
33
	 * Disconnect Jetpack and redirect user to connection flow.
34
	 *
35
	 * Used in class.jetpack-admin.php.
36
	 */
37
	public static function disconnect_and_redirect() {
38
		if ( ! ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'jp_disconnect' ) ) ) {
39
			return;
40
		}
41
42
		if ( isset( $_GET['disconnect'] ) && $_GET['disconnect'] ) {
43
			if ( Jetpack::is_active() ) {
44
				Jetpack::disconnect();
45
				wp_safe_redirect( Jetpack::admin_url() );
46
				exit;
47
			}
48
		}
49
	}
50
51
	/**
52
	 * Handles output to the browser for the in-plugin debugger.
53
	 */
54
	public static function jetpack_debug_display_handler() {
55
		if ( ! current_user_can( 'manage_options' ) ) {
56
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'jetpack' ) );
57
		}
58
59
		$support_url = Jetpack::is_development_version()
60
			? 'https://jetpack.com/contact-support/beta-group/'
61
			: 'https://jetpack.com/contact-support/';
62
63
		$cxntests = new Jetpack_Cxn_Tests();
64
		?>
65
		<div class="wrap">
66
			<h2><?php esc_html_e( 'Debugging Center', 'jetpack' ); ?></h2>
67
				<h3><?php esc_html_e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3>
68
				<div class="jetpack-debug-test-container">
69
					<?php
70
					if ( $cxntests->pass() ) {
71
						echo '<div class="jetpack-tests-succeed">' . esc_html__( 'Your Jetpack setup looks a-okay!', 'jetpack' ) . '</div>';
72
					} else {
73
						$failures = $cxntests->list_fails();
74
						foreach ( $failures as $fail ) {
0 ignored issues
show
Bug introduced by
The expression $failures of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
75
							echo '<div class="jetpack-test-error">';
76
							echo '<p><a class="jetpack-test-heading" href="#">' . esc_html( $fail['message'] );
77
							echo '<span class="noticon noticon-collapse"></span></a></p>';
78
							echo '<p class="jetpack-test-details">' . wp_kses(
79
								$fail['resolution'],
80
								array(
81
									'a' => array(
82
										'href'   => array(),
83
										'target' => array(),
84
										'rel'    => array(),
85
									),
86
								)
87
							) . '</p>';
88
							echo '</div>';
89
						}
90
					}
91
					?>
92
				</div>
93
			<div class="entry-content">
94
				<h3><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h3>
95
				<h4><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></h4>
96
				<ol>
97
					<li><b><em>
98
						<?php
99
						esc_html_e( 'A known issue.', 'jetpack' );
100
						?>
101
					</em></b>
102
						<?php
103
						echo sprintf(
104
							wp_kses(
105
								/* translators: URLs to Jetpack support pages. */
106
								__( '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' ),
107
								array(
108
									'a' => array(
109
										'href'   => array(),
110
										'target' => array(),
111
									),
112
								)
113
							),
114
							'https://jetpack.com/support/getting-started-with-jetpack/known-issues/',
115
							'https://jetpack.com/support/getting-started-with-jetpack/known-issues/',
116
							'https://jetpack.com/support/',
117
							'https://wordpress.org/support/plugin/jetpack'
118
						);
119
						?>
120
						</li>
121
					<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>
122
					<li>
123
						<b><em><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></em></b>
124
						<?php
125
							$default_theme = wp_get_theme( WP_DEFAULT_THEME );
126
127
						if ( $default_theme->exists() ) {
128
							/* translators: %s is the name of a theme */
129
							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' ) ) );
130
						} else {
131
							esc_html_e( "If your problem isn't known or caused by a plugin, try activating the default WordPress theme.", 'jetpack' );
132
						}
133
						?>
134
						<?php esc_html_e( "If this solves the problem, something in your theme is probably broken – let the theme's author know.", 'jetpack' ); ?>
135
					</li>
136
					<li><b><em><?php esc_html_e( 'A problem with your XMLRPC file.', 'jetpack' ); ?></em></b>
137
						<?php
138
						echo sprintf(
139
							wp_kses(
140
								/* translators: The URL to the site's xmlrpc.php file. */
141
								__( 'Load your <a href="%s">XMLRPC file</a>. It should say “XML-RPC server accepts POST requests only.” on a line by itself.', 'jetpack' ),
142
								array( 'a' => array( 'href' => array() ) )
143
							),
144
							esc_attr( site_url( 'xmlrpc.php' ) )
145
						);
146
						?>
147
						<ul>
148
							<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>
149
							<li>- <?php esc_html_e( 'If you get a 404 message, contact your web host. Their security may block XMLRPC.', 'jetpack' ); ?></li>
150
						</ul>
151
					</li>
152
					<?php if ( current_user_can( 'jetpack_disconnect' ) && Jetpack::is_active() ) : ?>
153
						<li>
154
							<strong><em><?php esc_html_e( 'A connection problem with WordPress.com.', 'jetpack' ); ?></em></strong>
155
							<?php
156
							echo sprintf(
157
								wp_kses(
158
									/* translators: URL to disconnect and reconnect Jetpack. */
159
									__( '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' ),
160
									array(
161
										'a' => array(
162
											'href'  => array(),
163
											'class' => array(),
164
										),
165
									)
166
								),
167
								esc_attr(
168
									wp_nonce_url(
169
										Jetpack::admin_url(
170
											array(
171
												'page' => 'jetpack-debugger',
172
												'disconnect' => true,
173
											)
174
										),
175
										'jp_disconnect',
176
										'nonce'
177
									)
178
								)
179
							);
180
							?>
181
						</li>
182
					<?php endif; ?>
183
				</ol>
184
				<h4><?php esc_html_e( 'Still having trouble?', 'jetpack' ); ?></h4>
185
				<p><b><em><?php esc_html_e( 'Ask us for help!', 'jetpack' ); ?></em></b>
186
				<?php
187
				/**
188
				 * Offload to new WordPress debug data.
189
				 */
190
					echo sprintf(
191
						wp_kses(
192
							/* translators: URL for Jetpack support. URL for WordPress's Site Health */
193
							__( '<a href="%1$s">Contact our Happiness team</a>. When you do, please include the <a href="%2$s">full debug information from your site</a>.', 'jetpack' ),
194
							array( 'a' => array( 'href' => array() ) )
195
						),
196
						esc_url( $support_url ),
197
						esc_url( admin_url() . 'site-health.php?tab=debug' )
198
					);
199
				?>
200
						</p>
201
				<hr />
202
				<?php if ( Jetpack::is_active() ) : ?>
203
					<div id="connected-user-details">
204
						<h3><?php esc_html_e( 'More details about your Jetpack settings', 'jetpack' ); ?></h3>
205
						<p>
206
						<?php
207
						printf(
208
							wp_kses(
209
								/* translators: %s is an e-mail address */
210
								__( 'The primary connection is owned by <strong>%s</strong>\'s WordPress.com account.', 'jetpack' ),
211
								array( 'strong' => array() )
212
							),
213
							esc_html( Jetpack::get_master_user_email() )
214
						);
215
						?>
216
							</p>
217
					</div>
218
				<?php else : ?>
219
					<div id="dev-mode-details">
220
						<p>
221
						<?php
222
						printf(
223
							wp_kses(
224
								/* translators: Link to a Jetpack support page. */
225
								__( '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' ),
226
								array( 'a' => array( 'href' => array() ) )
227
							),
228
							'https://jetpack.com/support/development-mode/'
229
						);
230
						?>
231
							</p>
232
					</div>
233
				<?php endif; ?>
234
				<?php
235
				if (
236
					current_user_can( 'jetpack_manage_modules' )
237
					&& ( ( new Status() )->is_development_mode() || Jetpack::is_active() )
238
				) {
239
					printf(
240
						wp_kses(
241
							'<p><a href="%1$s">%2$s</a></p>',
242
							array(
243
								'a' => array( 'href' => array() ),
244
								'p' => array(),
245
							)
246
						),
247
						esc_attr( Jetpack::admin_url( 'page=jetpack_modules' ) ),
248
						esc_html__( 'Access the full list of Jetpack modules available on your site.', 'jetpack' )
249
					);
250
				}
251
				?>
252
			</div>
253
		</div>
254
		<?php
255
	}
256
257
	/**
258
	 * Outputs html needed within the <head> for the in-plugin debugger page.
259
	 */
260
	public static function jetpack_debug_admin_head() {
261
262
		Jetpack_Admin_Page::load_wrapper_styles();
263
		?>
264
		<style type="text/css">
265
266
			.jetpack-debug-test-container {
267
				margin-top: 20px;
268
				margin-bottom: 30px;
269
			}
270
271
			.jetpack-tests-succeed {
272
				font-size: large;
273
				color: #8BAB3E;
274
			}
275
276
			.jetpack-test-details {
277
				margin: 4px 6px;
278
				padding: 10px;
279
				overflow: auto;
280
				display: none;
281
			}
282
283
			.jetpack-test-error {
284
				margin-bottom: 10px;
285
				background: #FFEBE8;
286
				border: solid 1px #C00;
287
				border-radius: 3px;
288
			}
289
290
			.jetpack-test-error p {
291
				margin: 0;
292
				padding: 0;
293
			}
294
295
			p.jetpack-test-details {
296
				margin: 4px 6px;
297
				padding: 10px;
298
			}
299
300
			.jetpack-test-error a.jetpack-test-heading {
301
				padding: 4px 6px;
302
				display: block;
303
				text-decoration: none;
304
				color: inherit;
305
			}
306
307
			.jetpack-test-error .noticon {
308
				float: right;
309
			}
310
311
			.formbox {
312
				margin: 0 0 25px 0;
313
			}
314
315
			.formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea, #debug_info_div {
316
				border: 1px solid #e5e5e5;
317
				border-radius: 11px;
318
				box-shadow: inset 0 1px 1px rgba(0,0,0,0.1);
319
				color: #666;
320
				font-size: 14px;
321
				padding: 10px;
322
				width: 97%;
323
			}
324
			#debug_info_div {
325
				border-radius: 0;
326
				margin-top: 16px;
327
				background: #FFF;
328
				padding: 16px;
329
			}
330
			.formbox .contact-support input[type="submit"] {
331
				float: right;
332
				margin: 0 !important;
333
				border-radius: 20px !important;
334
				cursor: pointer;
335
				font-size: 13pt !important;
336
				height: auto !important;
337
				margin: 0 0 2em 10px !important;
338
				padding: 8px 16px !important;
339
				background-color: #ddd;
340
				border: 1px solid rgba(0,0,0,0.05);
341
				border-top-color: rgba(255,255,255,0.1);
342
				border-bottom-color: rgba(0,0,0,0.15);
343
				color: #333;
344
				font-weight: 400;
345
				display: inline-block;
346
				text-align: center;
347
				text-decoration: none;
348
			}
349
350
			.formbox span.errormsg {
351
				margin: 0 0 10px 10px;
352
				color: #d00;
353
				display: none;
354
			}
355
356
			.formbox.error span.errormsg {
357
				display: block;
358
			}
359
360
			#debug_info_div, #toggle_debug_info, #debug_info_div p {
361
				font-size: 12px;
362
			}
363
364
			#category_div ul li {
365
				list-style-type: none;
366
			}
367
368
		</style>
369
		<script type="text/javascript">
370
		jQuery( document ).ready( function($) {
371
372
			$( '#debug_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
373
			$( '#debug_form_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
374
375
			$( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() {
376
				$( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle();
377
				return false;
378
			} );
379
380
		} );
381
		</script>
382
		<?php
383
	}
384
}
385