Completed
Push — fix/12470 ( 0cef1a )
by
unknown
06:42
created

Jetpack_Debugger::jetpack_debug_admin_head()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 124

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 124
rs 8
c 0
b 0
f 0

How to fix   Long Method   

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