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