Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Page_Handles
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 2
6.34
0.00% covered (danger)
0.00%
0 / 1
 print_handles
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
5.01
 print_one_tag_per_handle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Page_Handles class.
4 *
5 * @package automattic/jetpack-admin-ui
6 */
7
8namespace Automattic\Jetpack\Admin_UI;
9
10/**
11 * Lists the handles a page printed, since concatenated ones have no tag id for `loadMediaLibrary()` and the admin shell to skip.
12 */
13class Page_Handles {
14
15    /**
16     * Id of the JSON script tag that lists the handles.
17     */
18    const ELEMENT_ID = 'jetpack-page-handles';
19
20    /**
21     * Print the list on admin shell member pages, the only ones that load more.
22     *
23     * @return void
24     */
25    public static function print_handles() {
26        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks which page this is.
27        $page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
28        if ( '' === $page || ! isset( Admin_Shell::get_members()[ $page ] ) ) {
29            return;
30        }
31        wp_print_inline_script_tag(
32            (string) wp_json_encode(
33                array(
34                    'scripts' => array_values( wp_scripts()->done ),
35                    'styles'  => array_values( wp_styles()->done ),
36                ),
37                JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
38            ),
39            array(
40                'type' => 'application/json',
41                'id'   => self::ELEMENT_ID,
42            )
43        );
44    }
45
46    /**
47     * Print one tag per handle for the rest of the request.
48     *
49     * @return void
50     */
51    public static function print_one_tag_per_handle() {
52        $GLOBALS['concatenate_scripts'] = false; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Core's load-scripts.php.
53        // WordPress.com's stylesheet ids are positions, which also collide with the page's.
54        add_filter( 'wpcom_js_do_concat', '__return_false' );
55        add_filter( 'wpcom_css_do_concat', '__return_false' );
56    }
57}