Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.70% covered (danger)
0.70%
2 / 284
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Debugger
0.70% covered (danger)
0.70%
2 / 284
0.00% covered (danger)
0.00%
0 / 3
452.75
0.00% covered (danger)
0.00%
0 / 1
 disconnect_and_redirect
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
14.11
 jetpack_debug_display_handler
0.00% covered (danger)
0.00%
0 / 197
0.00% covered (danger)
0.00%
0 / 1
182
 jetpack_debug_admin_head
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jetpack Debugger functionality allowing for self-service diagnostic information via the legacy jetpack debugger.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\My_Jetpack\Initializer;
9use Automattic\Jetpack\Redirect;
10use Automattic\Jetpack\Status;
11
12/**
13 * Class Jetpack_Debugger
14 *
15 * A namespacing class for functionality related to the legacy in-plugin diagnostic tooling.
16 */
17class Jetpack_Debugger {
18    /**
19     * Disconnect Jetpack and redirect user to connection flow.
20     *
21     * Used in class.jetpack-admin.php.
22     */
23    public static function disconnect_and_redirect() {
24        if ( ! ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'jp_disconnect' ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
25            return;
26        }
27
28        if ( ! empty( $_GET['disconnect'] ) ) {
29            if ( Jetpack::is_connection_ready() ) {
30                Jetpack::disconnect();
31                wp_safe_redirect( Jetpack::admin_url() );
32                exit( 0 );
33            }
34        }
35    }
36
37    /**
38     * Handles output to the browser for the in-plugin debugger.
39     */
40    public static function jetpack_debug_display_handler() {
41        if ( ! current_user_can( 'manage_options' ) ) {
42            wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'jetpack' ) );
43        }
44
45        $support_url = Jetpack::is_development_version()
46            ? Redirect::get_url( 'jetpack-contact-support-beta-group' )
47            : Redirect::get_url( 'jetpack-contact-support' );
48
49        $cxntests = new Automattic\Jetpack\Connection\Connection_Health_Tests();
50        ?>
51        <div class="wrap">
52            <div class="jp-static-block">
53                <h2><?php esc_html_e( 'Debugging Center', 'jetpack' ); ?></h2>
54
55                <div class="jp-static-block-body">
56                    <h3><?php esc_html_e( "Testing your site's compatibility with Jetpack...", 'jetpack' ); ?></h3>
57                    <div class="jetpack-debug-test-container">
58                        <?php
59                        if ( $cxntests->pass() ) {
60                            ?>
61                            <div class="notice notice-success inline">
62                                <div class="notice-icon-wrapper">
63                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm-1.5 14.5-4-4 1.4-1.4 2.6 2.6 5.6-5.6 1.4 1.4-7 7z"></path></svg>
64                                </div>
65
66                                <div class="notice-main-content">
67                                    <div class="notice-title"><?php esc_html_e( 'Your Jetpack setup looks good.', 'jetpack' ); ?></div>
68                                </div>
69                            </div>
70                            <?php
71                        } else {
72                            $failures = $cxntests->list_fails();
73                            foreach ( $failures as $fail ) {
74                                ?>
75                                <div class="notice notice-error inline">
76                                    <div class="notice-icon-wrapper">
77                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="-2 -2 24 24" width="24" height="24" class="y_IPyP1wIAOhyNaqvXJq" aria-hidden="true" focusable="false"><path d="M10 2c4.42 0 8 3.58 8 8s-3.58 8-8 8-8-3.58-8-8 3.58-8 8-8zm1.13 9.38l.35-6.46H8.52l.35 6.46h2.26zm-.09 3.36c.24-.23.37-.55.37-.96 0-.42-.12-.74-.36-.97s-.59-.35-1.06-.35-.82.12-1.07.35-.37.55-.37.97c0 .41.13.73.38.96.26.23.61.34 1.06.34s.8-.11 1.05-.34z"></path></svg>
78                                    </div>
79
80                                    <div class="notice-main-content">
81                                        <div class="notice-title"><?php echo esc_html( $fail['short_description'] ); ?></div>
82
83                                        <div class="notice-action-bar">
84                                            <div>
85                                                <a href="<?php echo esc_attr( $fail['action'] ); ?>" aria-disabled="false" class="components-button is-primary"><span><?php echo esc_html( $fail['action_label'] ); ?></span></a>
86                                            </div>
87                                        </div>
88                                    </div>
89                                </div>
90                                <?php
91                            }
92                        }
93                        ?>
94                    </div>
95
96                    <div class="entry-content">
97                        <h4><?php esc_html_e( 'Trouble with Jetpack?', 'jetpack' ); ?></h4>
98                        <p><?php esc_html_e( 'It may be caused by one of these issues, which you can diagnose yourself:', 'jetpack' ); ?></p>
99                        <?php
100                        $link_html = array(
101                            'a' => array(
102                                'href'   => array(),
103                                'target' => array(),
104                                'rel'    => array(),
105                            ),
106                        );
107                        ?>
108                        <ol>
109                            <li>
110                                <strong><?php esc_html_e( 'A known issue.', 'jetpack' ); ?></strong>
111                                <?php
112                                printf(
113                                    wp_kses(
114                                        /* translators: URLs to Jetpack support pages. */
115                                        __( 'Some themes and plugins have <a href="%1$s" target="_blank" rel="noopener noreferrer">known conflicts</a> with Jetpack – check the list. (You can also browse the <a href="%2$s" target="_blank" rel="noopener noreferrer">Jetpack support pages</a> or <a href="%3$s" target="_blank" rel="noopener noreferrer">Jetpack support forum</a> to see if others have experienced and solved the problem.)', 'jetpack' ),
116                                        $link_html
117                                    ),
118                                    esc_url( Redirect::get_url( 'jetpack-contact-support-known-issues' ) ),
119                                    esc_url( Redirect::get_url( 'jetpack-support' ) ),
120                                    esc_url( Redirect::get_url( 'wporg-support-plugin-jetpack' ) )
121                                );
122                                ?>
123                            </li>
124                            <li>
125                                <strong><?php esc_html_e( 'A plugin conflict.', 'jetpack' ); ?></strong>
126                                <?php esc_html_e( "Test by disabling all plugins except Jetpack. If the problem persists, it's not a plugin issue. If the problem is solved, enable your plugins one by one until the problem pops up again. Contact the plugin developer to see if they can fix the issue.", 'jetpack' ); ?>
127                                <?php
128                                printf(
129                                    wp_kses(
130                                        /* translators: URL to a Jetpack support page. */
131                                        __( '<a href="%s" target="_blank" rel="noopener noreferrer">Learn how to troubleshoot plugin and theme conflicts</a>.', 'jetpack' ),
132                                        $link_html
133                                    ),
134                                    esc_url( Redirect::get_url( 'jetpack-debugger-center-plugin-conflicts' ) )
135                                );
136                                ?>
137                            </li>
138                            <li>
139                                <strong><?php esc_html_e( 'A theme conflict.', 'jetpack' ); ?></strong>
140                                <?php
141                                $default_theme = wp_get_theme( WP_DEFAULT_THEME );
142
143                                if ( $default_theme->exists() ) {
144                                    /* translators: %s is the name of a theme */
145                                    echo esc_html( sprintf( __( "If your problem isn't caused by a plugin, it may be a theme conflict. Try activating %s (the default WordPress theme).", 'jetpack' ), $default_theme->get( 'Name' ) ) );
146                                } else {
147                                    esc_html_e( "If your problem isn't caused by a plugin, try activating the default WordPress theme.", 'jetpack' );
148                                }
149                                ?>
150                                <?php esc_html_e( "If this solves the problem, something in your theme is causing the issue. Contact your theme's developer.", 'jetpack' ); ?>
151                                <?php
152                                printf(
153                                    wp_kses(
154                                        /* translators: URL to a Jetpack support page. */
155                                        __( '<a href="%s" target="_blank" rel="noopener noreferrer">Learn how to troubleshoot plugin and theme conflicts</a>.', 'jetpack' ),
156                                        $link_html
157                                    ),
158                                    esc_url( Redirect::get_url( 'jetpack-debugger-center-theme-conflicts' ) )
159                                );
160                                ?>
161                            </li>
162                            <li>
163                                <strong><?php esc_html_e( 'A problem with your XML-RPC file.', 'jetpack' ); ?></strong>
164                                <?php
165                                printf(
166                                    wp_kses(
167                                        /* translators: The URL to the site's xmlrpc.php file. */
168                                        __( 'Load your <a href="%s">XML-RPC file</a>. It should say “XML-RPC server accepts POST requests only.” on a line by itself.', 'jetpack' ),
169                                        array( 'a' => array( 'href' => array() ) )
170                                    ),
171                                    esc_attr( site_url( 'xmlrpc.php' ) )
172                                );
173                                ?>
174                                <ul>
175                                    <li><?php esc_html_e( "If that message isn't by itself, a theme or plugin is displaying extra characters. Try steps 2 and 3 above.", 'jetpack' ); ?></li>
176                                    <li><?php esc_html_e( 'If you get a 404 message, contact your web host. Their security may block the XML-RPC file.', 'jetpack' ); ?></li>
177                                    <li>
178                                        <?php
179                                        printf(
180                                            wp_kses(
181                                                /* translators: URL to a Jetpack support page. */
182                                                __( '<a href="%s" target="_blank" rel="noopener noreferrer">Learn how to fix XML-RPC errors</a>.', 'jetpack' ),
183                                                $link_html
184                                            ),
185                                            esc_url( Redirect::get_url( 'jetpack-debugger-center-xmlrpc-errors' ) )
186                                        );
187                                        ?>
188                                    </li>
189                                </ul>
190                            </li>
191                            <li>
192                                <strong><?php esc_html_e( 'A connection problem with WordPress.com.', 'jetpack' ); ?></strong>
193                                <?php esc_html_e( 'Jetpack connects to WordPress.com for many of its features.', 'jetpack' ); ?>
194                                <?php
195                                $connection_errors_url = Redirect::get_url( 'jetpack-debugger-center-connection-errors' );
196                                ?>
197                                <?php if ( current_user_can( 'jetpack_disconnect' ) && Jetpack::is_connection_ready() ) : ?>
198                                    <?php
199                                    printf(
200                                        wp_kses(
201                                            /* translators: %1$s is the URL to disconnect and reconnect Jetpack, %2$s is the URL to a Jetpack support page. */
202                                            __( 'If the connection is broken, disconnecting and reconnecting often gets things working again. <a href="%1$s">Disconnect from WordPress.com</a> or <a href="%2$s" target="_blank" rel="noopener noreferrer">learn how to troubleshoot connection errors</a>.', 'jetpack' ),
203                                            $link_html
204                                        ),
205                                        esc_attr(
206                                            wp_nonce_url(
207                                                Jetpack::admin_url(
208                                                    array(
209                                                        'page' => 'jetpack-debugger',
210                                                        'disconnect' => true,
211                                                    )
212                                                ),
213                                                'jp_disconnect',
214                                                'nonce'
215                                            )
216                                        ),
217                                        esc_url( $connection_errors_url )
218                                    );
219                                    ?>
220                                <?php else : ?>
221                                    <?php
222                                    printf(
223                                        wp_kses(
224                                            /* translators: URL to a Jetpack support page. */
225                                            __( '<a href="%s" target="_blank" rel="noopener noreferrer">Learn how to troubleshoot connection errors</a>.', 'jetpack' ),
226                                            $link_html
227                                        ),
228                                        esc_url( $connection_errors_url )
229                                    );
230                                    ?>
231                                <?php endif; ?>
232                            </li>
233                        </ol>
234
235                        <h4><?php esc_html_e( 'Still having trouble?', 'jetpack' ); ?></h4>
236                        <p>
237                            <?php
238                            printf(
239                                wp_kses(
240                                    /* translators: URL for Jetpack support. URL for WordPress's Site Health */
241                                    __( '<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' ),
242                                    array( 'a' => array( 'href' => array() ) )
243                                ),
244                                esc_url( $support_url ),
245                                esc_url( admin_url() . 'site-health.php?tab=debug' )
246                            );
247                            ?>
248                        </p>
249                    </div>
250                </div>
251            </div>
252
253            <div class="jp-static-block">
254                <h2><?php esc_html_e( 'More details about your Jetpack settings', 'jetpack' ); ?></h2>
255
256                <div class="jp-static-block-body">
257                    <?php if ( Jetpack::is_connection_ready() ) : ?>
258                        <div id="connected-user-details">
259                            <p>
260                                <?php
261                                printf(
262                                    wp_kses(
263                                        /* translators: %s is an e-mail address */
264                                        __( 'The primary connection is owned by <strong>%s</strong>\'s WordPress.com account.', 'jetpack' ),
265                                        array( 'strong' => array() )
266                                    ),
267                                    esc_html( Jetpack::get_master_user_email() )
268                                );
269                                ?>
270                            </p>
271                        </div>
272                    <?php else : ?>
273                        <div id="dev-mode-details">
274                            <p>
275                                <?php
276                                printf(
277                                    wp_kses(
278                                        /* translators: Link to a Jetpack support page. */
279                                        __( 'Would you like to use Jetpack on your local development site? You can do so thanks to <a href="%s">Jetpack\'s Offline Mode</a>.', 'jetpack' ),
280                                        array( 'a' => array( 'href' => array() ) )
281                                    ),
282                                    esc_url( Redirect::get_url( 'jetpack-support-development-mode' ) )
283                                );
284                                ?>
285                            </p>
286                        </div>
287                    <?php endif; ?>
288
289                    <?php
290                    if (
291                        current_user_can( 'jetpack_manage_modules' )
292                        && ( ( new Status() )->is_offline_mode() || Jetpack::is_connection_ready() )
293                    ) {
294                        printf(
295                            wp_kses(
296                                '<p><a href="%1$s">%2$s</a></p>',
297                                array(
298                                    'a' => array( 'href' => array() ),
299                                    'p' => array(),
300                                )
301                            ),
302                            esc_attr( method_exists( Initializer::class, 'get_modules_management_url' ) ? Initializer::get_modules_management_url() : Jetpack::admin_url( 'page=jetpack_modules' ) ),
303                            esc_html__( 'Access the full list of Jetpack modules available on your site.', 'jetpack' )
304                        );
305                    }
306                    ?>
307                </div>
308            </div>
309        </div>
310        <?php
311    }
312
313    /**
314     * Outputs html needed within the <head> for the in-plugin debugger page.
315     */
316    public static function jetpack_debug_admin_head() {
317
318        $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
319        wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons', 'jetpack-connection' ), JETPACK__VERSION . '-20121016' );
320        wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' );
321        wp_style_add_data( 'jetpack-admin', 'suffix', $min );
322
323        Jetpack_Admin_Page::load_wrapper_styles();
324        ?>
325        <style type="text/css">
326
327            .jetpack-debug-test-container {
328                margin: 8px 0;
329            }
330
331            #connected-user-details p strong {
332                word-break: break-all;
333            }
334
335            .jetpack-test-details {
336                margin: 4px 6px;
337                padding: 10px;
338                overflow: auto;
339                display: none;
340            }
341
342            .formbox {
343                margin: 0 0 25px 0;
344            }
345
346            .formbox input[type="text"], .formbox input[type="email"], .formbox input[type="url"], .formbox textarea, #debug_info_div {
347                border: 1px solid var(--wpds-color-stroke-surface-neutral-weak, #e4e4e4);
348                border-radius: var(--wpds-border-radius-md, 4px);
349                color: #646970;
350                font-size: 14px;
351                padding: 10px;
352                width: 97%;
353            }
354            #debug_info_div {
355                border-radius: var(--wpds-border-radius-lg, 8px);
356                margin-top: 16px;
357                background: var(--wpds-color-background-surface-neutral-strong, #fff);
358                padding: 16px;
359            }
360            .formbox .contact-support input[type="submit"] {
361                float: right;
362                margin: 0 !important;
363                border-radius: 20px !important;
364                cursor: pointer;
365                font-size: 13pt !important;
366                height: auto !important;
367                margin: 0 0 2em 10px !important;
368                padding: 8px 16px !important;
369                background-color: #dcdcde;
370                border: 1px solid rgba(0,0,0,0.05);
371                border-top-color: rgba(255,255,255,0.1);
372                border-bottom-color: rgba(0,0,0,0.15);
373                color: #333;
374                font-weight: 400;
375                display: inline-block;
376                text-align: center;
377                text-decoration: none;
378            }
379
380            .formbox span.errormsg {
381                margin: 0 0 10px 10px;
382                color: #d00;
383                display: none;
384            }
385
386            .formbox.error span.errormsg {
387                display: block;
388            }
389
390            #debug_info_div, #toggle_debug_info, #debug_info_div p {
391                font-size: 12px;
392            }
393
394            #category_div ul li {
395                list-style-type: none;
396            }
397
398        </style>
399        <script type="text/javascript">
400        jQuery( document ).ready( function($) {
401
402            $( '#debug_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
403            $( '#debug_form_info' ).prepend( 'jQuery version: ' + jQuery.fn.jquery + "\r\n" );
404
405            $( '.jetpack-test-error .jetpack-test-heading' ).on( 'click', function() {
406                $( this ).parents( '.jetpack-test-error' ).find( '.jetpack-test-details' ).slideToggle();
407                return false;
408            } );
409
410        } );
411        </script>
412        <?php
413    }
414}