Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Media_Library
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 ajax_media_library
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 get_html
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Media library on demand: the scripts and templates of wp_enqueue_media(), for a dashboard to load when it opens the library.
4 *
5 * @package automattic/jetpack-admin-ui
6 */
7
8namespace Automattic\Jetpack\Admin_UI;
9
10/**
11 * Serves the media library to `loadMediaLibrary()` in `@automattic/jetpack-components/tools/load-media-library`.
12 */
13class Media_Library {
14
15    /**
16     * The admin-ajax action that returns the media library.
17     */
18    const AJAX_ACTION = 'jetpack_media_library';
19
20    /**
21     * The script and style handles wp_enqueue_media() enqueues. A fixed list: the function runs once per request, so a queue diff can come back empty.
22     */
23    const HANDLES = array( 'media-editor', 'media-audiovideo', 'mce-view', 'image-edit', 'media-views', 'imgareaselect' );
24
25    /**
26     * Whether init() has run.
27     *
28     * @var bool
29     */
30    private static $initialized = false;
31
32    /**
33     * Hook up the endpoint. admin-ajax never builds the admin menu, so callers outside it call this too.
34     *
35     * @return void
36     */
37    public static function init() {
38        if ( self::$initialized ) {
39            return;
40        }
41        self::$initialized = true;
42        add_action( 'wp_ajax_' . self::AJAX_ACTION, array( __CLASS__, 'ajax_media_library' ) );
43        // After core prints the late scripts and styles.
44        add_action( 'admin_print_footer_scripts', array( Page_Handles::class, 'print_handles' ), 100 );
45    }
46
47    /**
48     * Send the media library's styles, scripts and templates.
49     *
50     * @return void
51     */
52    public static function ajax_media_library() {
53        if ( ! current_user_can( 'upload_files' ) ) {
54            wp_send_json_error( null, 403, JSON_UNESCAPED_SLASHES );
55        }
56        // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only: they only leave out what the page already has.
57        $done = array(
58            'scripts' => isset( $_REQUEST['scripts'] ) && is_string( $_REQUEST['scripts'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['scripts'] ) ) : '',
59            'styles'  => isset( $_REQUEST['styles'] ) && is_string( $_REQUEST['styles'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['styles'] ) ) : '',
60        );
61        // phpcs:enable WordPress.Security.NonceVerification.Recommended
62        $html = self::get_html( array_filter( explode( ',', $done['scripts'] ) ), array_filter( explode( ',', $done['styles'] ) ) );
63        wp_send_json_success( array( 'html' => $html ), 200, JSON_UNESCAPED_SLASHES );
64    }
65
66    /**
67     * The media library's styles and scripts, with their dependencies, then its templates.
68     *
69     * @param string[] $done_scripts Script handles the page already has. Concatenated ones carry no id the client could skip.
70     * @param string[] $done_styles  Style handles the page already has.
71     * @return string
72     */
73    public static function get_html( array $done_scripts = array(), array $done_styles = array() ) {
74        // Notices from other plugins' wp_enqueue_media callbacks would break the JSON.
75        ob_start();
76        wp_enqueue_media();
77        ob_end_clean();
78        wp_scripts()->done = array_merge( wp_scripts()->done, $done_scripts );
79        wp_styles()->done  = array_merge( wp_styles()->done, $done_styles );
80        Page_Handles::print_one_tag_per_handle();
81        ob_start();
82        wp_print_styles( self::HANDLES );
83        wp_print_scripts( self::HANDLES );
84        // Core prints these on admin_footer. They come after the scripts, since a print_media_templates callback can read wp.media.
85        wp_print_media_templates();
86        return (string) ob_get_clean();
87    }
88}