Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.15% covered (warning)
50.15%
172 / 343
55.88% covered (warning)
55.88%
19 / 34
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin_UI
50.15% covered (warning)
50.15%
172 / 343
55.88% covered (warning)
55.88%
19 / 34
980.25
0.00% covered (danger)
0.00%
0 / 1
 init
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
 remove_license_activation_jitms
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 maybe_load_wp_build
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 get_dashboard_render_callback
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 enable_menu
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 enable_dashboard_menu
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 should_add_dashboard_menu
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 should_add_wp_admin_submenu
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 init_inactive_menu
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 enable_inactive_menu
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 add_wp_admin_submenu
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 get_admin_page_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_allowed_video_extensions
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 admin_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_admin_shell_member
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 shell_header_subtitle
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 load_as_admin_shell_guest
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 remove_jetpack_hooks
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_admin_scripts
46.15% covered (danger)
46.15%
6 / 13
0.00% covered (danger)
0.00%
0 / 1
8.90
 edit_video_link
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 get_logo_svg
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 attachment_details_two_column_template
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
2
 attachment_details_template
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 1
2
 attachment_info_template_part
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 load_wp_build
13.64% covered (danger)
13.64%
3 / 22
0.00% covered (danger)
0.00%
0 / 1
8.80
 load_wp_build_with_screen_alias
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
2.06
 maybe_strip_chapters_editor_routes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 strip_chapters_editor_routes
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 alias_screen_id_for_wp_build
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 restore_screen_id_after_wp_build
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 is_modernized
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_trim_cut_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_chapters_editor_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_videopress_admin_request
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * The initializer class for Admin UI elements
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use Automattic\Jetpack\Admin_UI\Admin_Menu;
11use Automattic\Jetpack\Admin_UI\Admin_Shell;
12use Automattic\Jetpack\Admin_UI\Media_Library;
13use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
15use Automattic\Jetpack\Status\Host;
16use Automattic\Jetpack\VideoPress\Status as VideoPress_Status;
17
18/**
19 * Initialized the VideoPress package
20 */
21class Admin_UI {
22
23    const JETPACK_VIDEOPRESS_PKG_NAMESPACE = 'jetpack-videopress-pkg';
24
25    const ADMIN_PAGE_SLUG = 'jetpack-videopress';
26
27    /**
28     * The name hosts use for the VideoPress sidebar item in the visibility filter.
29     *
30     * Deliberately not ADMIN_PAGE_SLUG: this is a name hosts write into their own code, so
31     * it has to survive a slug change. Both registrations below share it, since they are
32     * mutually exclusive and a host hiding VideoPress should not have to know which is live.
33     */
34    const VISIBILITY_KEY = 'jetpack-videopress';
35
36    /**
37     * The My Jetpack interstitial where VideoPress can be activated, relative to wp-admin.
38     *
39     * Used as the target of the "Jetpack > VideoPress" menu item when VideoPress
40     * is not active.
41     */
42    const MY_JETPACK_ADD_VIDEOPRESS_URI = 'admin.php?page=my-jetpack#/add-videopress';
43
44    /**
45     * Filter name that gates the wp-build–based dashboard.
46     *
47     * When this filter returns false, no "Jetpack > VideoPress" menu is registered.
48     */
49    const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_videopress';
50
51    /**
52     * Filter name that gates the chapters editor.
53     *
54     * The chapters editor is generally available, so this defaults to true and
55     * the filter serves as a kill switch: returning false withdraws it from both
56     * places it lives — the dashboard's Editor tab (and the `/video/$id/editor`
57     * route behind it) and the block editor's chapter manager modal.
58     */
59    const CHAPTERS_EDITOR_FILTER = 'jetpack_videopress_chapters_editor';
60
61    /**
62     * Route paths that belong to the chapters editor.
63     *
64     * Kept in sync with the wp-build route registry (`build/routes/registry.php`);
65     * entries with these paths are stripped from the registry when the chapters
66     * editor filter is off, so the routes never register.
67     */
68    const CHAPTERS_EDITOR_ROUTE_PATHS = array( '/video/$id/editor' );
69
70    /**
71     * The screen ID alias_screen_id_for_wp_build() replaced, until it is restored.
72     *
73     * @var string|null
74     */
75    private static $wp_build_original_screen_id = null;
76
77    /**
78     * Initializes the Admin UI of VideoPress
79     *
80     * This method is called only once by the Initializer class
81     *
82     * @return void
83     */
84    public static function init() {
85
86        // Load the wp-build dashboard assets on the VideoPress admin page, on every
87        // host. Registered before enable_menu() below — both hook admin_menu:1, and
88        // same-priority callbacks fire in registration order — so the render function
89        // and SCRIPT_HANDLE exist by the time the menu callback is selected: on
90        // standalone/Atomic by enable_menu(), on wpcom Simple by add_wp_admin_submenu()
91        // (wpcom-admin-menu.php, admin_menu:999999). maybe_load_wp_build() self-gates
92        // to the VideoPress admin request, so it's inert on every other page.
93        add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 );
94
95        if ( class_exists( Admin_Shell::class ) ) {
96            add_filter( 'jetpack_admin_shell_members', array( __CLASS__, 'add_admin_shell_member' ) );
97        }
98
99        // The thumbnail picker loads the media library from this endpoint.
100        if ( method_exists( Media_Library::class, 'init' ) ) {
101            Media_Library::init();
102        }
103
104        // On WordPress.com Simple the standalone menu system (Admin_Menu) is not used:
105        // the Jetpack parent menu is created late (admin_menu priority 999999) by
106        // wpcom-admin-menu.php, which registers the VideoPress submenu directly via
107        // Admin_UI::add_wp_admin_submenu(). So on Simple we skip enable_menu() and the
108        // Jetpack/Atomic media-library hooks below — the media hooks are Phase 3 media
109        // parity, out of scope for menu + boot, and would patch a media library wpcom
110        // owns (their VideoPress branches are inert on Simple anyway: videos keep their
111        // original mime, never video/videopress).
112        if ( ( new Host() )->is_wpcom_simple() ) {
113            return;
114        }
115
116        add_action( 'admin_menu', array( __CLASS__, 'enable_menu' ), 1 ); // Akismet uses 4, so we use 1 to ensure both menus are added when only they exist.
117
118        add_action( 'admin_footer-upload.php', array( __CLASS__, 'attachment_details_two_column_template' ) );
119        add_action( 'admin_footer-post.php', array( __CLASS__, 'attachment_details_template' ), 20 );
120
121        add_filter( 'get_edit_post_link', array( __CLASS__, 'edit_video_link' ), 10, 3 );
122
123        add_action( 'admin_init', array( __CLASS__, 'remove_jetpack_hooks' ) );
124
125        add_filter( 'jetpack_jitm_received_envelopes', array( __CLASS__, 'remove_license_activation_jitms' ), 10, 2 );
126    }
127
128    /**
129     * Drops the license activation JITMs from the VideoPress page on free-plan sites.
130     *
131     * The dashboard's free-plan notice offers the license there, so the JITM would be a second upsell.
132     *
133     * @param array  $envelopes    The received JITMs.
134     * @param string $message_path The message path the JITMs were requested for.
135     * @return array The JITMs to show.
136     */
137    public static function remove_license_activation_jitms( $envelopes, $message_path ) {
138        if (
139            ! is_array( $envelopes )
140            || ! is_string( $message_path )
141            || ! str_contains( $message_path, 'jetpack_page_jetpack-videopress' )
142            || false !== Initial_State::has_videopress_access()
143        ) {
144            return $envelopes;
145        }
146
147        $license_jitm_ids = array( 'single_licensing_activation_notice', 'multiple_licensing_activation_notice' );
148
149        return array_values(
150            array_filter(
151                $envelopes,
152                function ( $envelope ) use ( $license_jitm_ids ) {
153                    return ! is_object( $envelope ) || ! isset( $envelope->id ) || ! in_array( $envelope->id, $license_jitm_ids, true );
154                }
155            )
156        );
157    }
158
159    /**
160     * Load the wp-build dashboard assets when modernization is enabled and the current
161     * request targets the VideoPress admin page.
162     *
163     * The single wp-build load path on every host, hooked at admin_menu:1 from init().
164     * Requiring build.php here defines the render function that enable_menu() /
165     * add_wp_admin_submenu() select later in the same hook, and registers the
166     * SCRIPT_HANDLE that Initial_State::enqueue() hydrates from.
167     *
168     * @return void
169     */
170    public static function maybe_load_wp_build() {
171        if ( ! self::is_modernized() || ! self::is_videopress_admin_request() ) {
172            return;
173        }
174
175        self::load_wp_build_with_screen_alias();
176    }
177
178    /**
179     * Select the dashboard render callback, or null when the dashboard can't render.
180     *
181     * The wp-build render function is only loaded on the VideoPress page itself (see
182     * maybe_load_wp_build()), so it is only checked there; other admin pages still
183     * register the menu item.
184     *
185     * @return string|null Callback for Admin_Menu::add_menu()/add_submenu_page().
186     */
187    private static function get_dashboard_render_callback() {
188        if ( ! self::is_modernized() ) {
189            return null;
190        }
191
192        $callback = 'jetpack_videopress_jetpack_videopress_dashboard_wp_admin_render_page';
193        if ( self::is_videopress_admin_request() && ! function_exists( $callback ) ) {
194            return null;
195        }
196
197        return $callback;
198    }
199
200    /**
201     * Register the "Jetpack > VideoPress" menu item, choosing its target dynamically.
202     *
203     * Hooked on admin_menu from both init() and init_inactive_menu(), so the item
204     * reflects the module state when the menu renders rather than when the package
205     * initialized: when VideoPress is active it opens the VideoPress library
206     * (admin.php?page=jetpack-videopress); when it is not, it links to the
207     * My Jetpack interstitial where VideoPress can be activated.
208     *
209     * Registered separately to init due to translations needing to run early for
210     * the page suffix.
211     *
212     * @return void
213     */
214    public static function enable_menu() {
215        if ( VideoPress_Status::is_active() ) {
216            self::enable_dashboard_menu();
217            return;
218        }
219
220        self::enable_inactive_menu();
221    }
222
223    /**
224     * Register the VideoPress library dashboard page and its menu item.
225     *
226     * @return void
227     */
228    private static function enable_dashboard_menu() {
229        if ( ! self::should_add_dashboard_menu() ) {
230            return;
231        }
232
233        $page_suffix = Admin_Menu::add_menu(
234            // "VideoPress" is a product name, do not translate.
235            'Jetpack VideoPress',
236            'VideoPress',
237            'manage_options',
238            self::ADMIN_PAGE_SLUG,
239            self::get_dashboard_render_callback(),
240            null,
241            array(
242                'product' => 'videopress',
243                'key'     => self::VISIBILITY_KEY,
244            )
245        );
246        add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );
247    }
248
249    /**
250     * Whether enable_dashboard_menu() registers the dashboard page, and so whether the admin shell may serve it.
251     *
252     * @return bool
253     */
254    private static function should_add_dashboard_menu() {
255        return VideoPress_Status::is_active() && null !== self::get_dashboard_render_callback();
256    }
257
258    /**
259     * Whether wpcom-admin-menu.php registers the dashboard page on Simple, and so whether the admin shell may serve it there.
260     *
261     * @return bool
262     */
263    private static function should_add_wp_admin_submenu() {
264        return function_exists( 'wpcom_site_has_feature' )
265            && wpcom_site_has_feature( 'videopress' )
266            && null !== self::get_dashboard_render_callback();
267    }
268
269    /**
270     * Initializes the "Jetpack > VideoPress" menu item when VideoPress is not active.
271     *
272     * This method is called by the Initializer class instead of init() when the
273     * VideoPress module is not active. It hooks the same enable_menu() callback as
274     * init(), which picks the item's target from the module state at admin_menu
275     * time — so if the state changed since package initialization, the rendered
276     * link is still correct.
277     *
278     * @return void
279     */
280    public static function init_inactive_menu() {
281        // On WordPress.com Simple the standalone menu system (Admin_Menu) is not used;
282        // the Jetpack menu is fully managed by wpcom-admin-menu.php. See init().
283        if ( ( new Host() )->is_wpcom_simple() ) {
284            return;
285        }
286
287        add_action( 'admin_menu', array( __CLASS__, 'enable_menu' ), 1 );
288    }
289
290    /**
291     * Register the inactive-state menu item: a direct link to the My Jetpack
292     * "add VideoPress" interstitial.
293     *
294     * A slug that is not a registered page makes WordPress render the item as a
295     * plain link to that URI (same mechanism as the "Upgrade Jetpack" menu item).
296     *
297     * @return void
298     */
299    public static function enable_inactive_menu() {
300        // Activating VideoPress from My Jetpack requires a connected user, so only
301        // offer the item to users with their own Jetpack connection. Checked here,
302        // at admin_menu time, so the item starts rendering as soon as the current
303        // user connects.
304        $connection = new Connection_Manager();
305        if ( ! $connection->is_connected() || ! $connection->is_user_connected() ) {
306            return;
307        }
308
309        // The link targets My Jetpack; don't render a dead link when that page
310        // is not registered (e.g. offline mode).
311        if ( ! My_Jetpack_Initializer::should_initialize() ) {
312            return;
313        }
314
315        Admin_Menu::add_menu(
316            // "VideoPress" is a product name, do not translate.
317            'Jetpack VideoPress',
318            'VideoPress',
319            'manage_options',
320            self::MY_JETPACK_ADD_VIDEOPRESS_URI,
321            null,
322            null,
323            // Hidden while VideoPress is off, unless a host forces the shared key visible.
324            array(
325                'product' => 'videopress',
326                'key'     => self::VISIBILITY_KEY,
327            )
328        );
329    }
330
331    /**
332     * Add the VideoPress submenu directly under the Jetpack menu on wpcom Simple.
333     *
334     * Called from wpcom-admin-menu.php at a late priority (999999), once the Jetpack
335     * parent menu exists. On Simple enable_menu() is skipped (see init()), so this is
336     * the only place the submenu is registered there. The callback mirrors enable_menu():
337     * the wp-build render function (loaded by maybe_load_wp_build() at admin_menu:1,
338     * which runs first), and no menu when the dashboard can't render.
339     *
340     * @return void
341     */
342    public static function add_wp_admin_submenu() {
343        $callback = self::get_dashboard_render_callback();
344        if ( null === $callback ) {
345            return;
346        }
347
348        $page_suffix = add_submenu_page(
349            'jetpack',
350            // "VideoPress" is a product name, do not translate.
351            'Jetpack VideoPress',
352            'VideoPress',
353            'manage_options',
354            self::ADMIN_PAGE_SLUG,
355            $callback
356        );
357
358        if ( $page_suffix ) {
359            add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );
360        }
361    }
362
363    /**
364     * Gets the URL for the VideoPress admin page
365     *
366     * @return string
367     */
368    public static function get_admin_page_url() {
369        return admin_url( 'admin.php?page=' . self::ADMIN_PAGE_SLUG );
370    }
371
372    /**
373     * Gets the list of allowed video extensions
374     *
375     * @return array
376     */
377    public static function get_allowed_video_extensions() {
378        return array(
379            '3g2'  => 'video/3gpp2',
380            '3gp'  => 'video/3gpp',
381            '3gp2' => 'video/3gpp2',
382            '3gpp' => 'video/3gpp',
383            'avi'  => 'video/avi',
384            'm4v'  => 'video/mp4',
385            'mov'  => 'video/quicktime',
386            'mp4'  => 'video/mp4',
387            'mpe'  => 'video/mpeg',
388            'mpeg' => 'video/mpeg',
389            'mpg'  => 'video/mpeg',
390            'ogv'  => 'video/ogg',
391            'wmv'  => 'video/x-ms-wmv',
392        );
393    }
394
395    /**
396     * Initialize the admin resources.
397     */
398    public static function admin_init() {
399        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_admin_scripts' ) );
400    }
401
402    /**
403     * Register VideoPress with the admin shell, which switches between Jetpack dashboards without a page load.
404     *
405     * @since 0.63.0
406     *
407     * @param array $members Shell members keyed by admin page slug.
408     * @return array
409     */
410    public static function add_admin_shell_member( $members ) {
411        $registers_page = ( new Host() )->is_wpcom_simple() ? self::should_add_wp_admin_submenu() : self::should_add_dashboard_menu();
412        if ( ! $registers_page || ! file_exists( dirname( __DIR__ ) . '/build/build.php' ) ) {
413            return $members;
414        }
415
416        $members[ self::ADMIN_PAGE_SLUG ] = array(
417            'build_prefix' => 'jetpack_videopress',
418            'build_page'   => 'jetpack-videopress-dashboard',
419            'init_modules' => array( '@jetpack-videopress/init' ),
420            // "VideoPress" is a product name, do not translate.
421            'title'        => 'Jetpack VideoPress',
422            'header'       => array(
423                'title'    => 'VideoPress',
424                'subtitle' => self::shell_header_subtitle(),
425            ),
426            'capability'   => 'manage_options',
427            'handles'      => array( 'jetpack-videopress-dashboard-shell', Admin_Menu::DESIGN_TOKENS_HANDLE, 'jetpack-script-data', 'wp-jp-i18n-loader' ),
428            'data'         => array( Initial_State::class, 'get_script' ),
429            'load'         => array( __CLASS__, 'load_as_admin_shell_guest' ),
430        );
431        return $members;
432    }
433
434    /**
435     * The dashboard's subtitle without its "Learn more" link, and the full stop after it in any script.
436     *
437     * @return string
438     */
439    private static function shell_header_subtitle() {
440        $subtitle = __( 'Host, manage, customize, and track your videos — all in one place. <link>Learn more</link>.', 'jetpack-videopress-pkg' );
441        return trim( wp_strip_all_tags( (string) preg_replace( '#\s*<link>.*?</link>[.。.]?\s*#u', '', $subtitle ) ) );
442    }
443
444    /**
445     * Ready the VideoPress routes and page data while another dashboard's page is open, without the screen ID alias.
446     *
447     * @since 0.63.0
448     *
449     * @return void
450     */
451    public static function load_as_admin_shell_guest() {
452        self::load_wp_build();
453        self::admin_init();
454    }
455
456    /**
457     * Remove extra fields from Attachment details modal
458     *
459     * @return void
460     */
461    public static function remove_jetpack_hooks() {
462        if ( class_exists( '\VideoPress_Edit_Attachment' ) ) {
463            $edit_attachment = \VideoPress_Edit_Attachment::init();
464            remove_filter( 'attachment_fields_to_edit', array( $edit_attachment, 'fields_to_edit' ) );
465            remove_filter( 'attachment_fields_to_save', array( $edit_attachment, 'save_fields' ) );
466        }
467    }
468
469    /**
470     * Enqueue plugin admin scripts and styles.
471     */
472    public static function enqueue_admin_scripts() {
473        // This callback is registered via `load-{$page_suffix}` in `enable_menu()`,
474        // so it only fires on the VideoPress admin page — no need to re-check the page here.
475        Admin_Menu::enqueue_design_tokens();
476
477        // Page-level shell stylesheet: scopes the shared `jetpack-admin-page-layout`
478        // mixin to the dashboard body so every route inherits the proper
479        // scrollable chrome (fixed `#wpbody-content`, scrollable middle, pinned
480        // footer) regardless of whether it uses DashboardLayout. Without this,
481        // non-tabbed routes (e.g. Video details) would only get the layout
482        // after a sibling route's chunk happened to inject the same CSS.
483        // Pick the flipped build ourselves on RTL locales rather than leaning on
484        // `wp_style_add_data( …, 'rtl', 'replace' )`: core resolves that by
485        // rewriting `index.css` to `index-rtl.css` (hyphenated) *and* dropping
486        // the LTR tag, but webpack emits the flipped file as `index.rtl.css`
487        // (dotted, the same convention `Assets::register_script()`'s `css_path`
488        // follows). The hyphenated file never exists, so RTL sites used to load
489        // no shell stylesheet at all — the layout flex chain never formed and
490        // the dashboard rendered as a blank page.
491        $shell_dir  = dirname( __DIR__ ) . '/build/dashboard-shell/';
492        $shell_file = is_rtl() && file_exists( $shell_dir . 'index.rtl.css' ) ? 'index.rtl.css' : 'index.css';
493        if ( file_exists( $shell_dir . $shell_file ) ) {
494            wp_register_style(
495                'jetpack-videopress-dashboard-shell',
496                plugins_url( 'build/dashboard-shell/' . $shell_file, __DIR__ ),
497                array(),
498                (string) filemtime( $shell_dir . $shell_file )
499            );
500            wp_enqueue_style( 'jetpack-videopress-dashboard-shell' );
501        }
502
503        // The i18n loader is registered on every admin page by jetpack-assets but
504        // only enqueued when depended on; the esbuild bundles don't pull it in.
505        if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) {
506            wp_enqueue_script( 'wp-jp-i18n-loader' );
507        }
508
509        // Beyond the shell stylesheet, wp-build
510        // manages its own enqueue pipeline.
511    }
512
513    /**
514     * Replaces the edit link for videopress videos
515     *
516     * @param string $link - the post link.
517     * @param int    $post_id - the post ID.
518     * @param string $context - the context.
519     *
520     * @return string
521     */
522    public static function edit_video_link( $link, $post_id, $context ) {
523        $post_id = (int) $post_id;
524        if ( ! $post_id ) {
525            return $link;
526        }
527
528        $post = get_post( $post_id );
529        if ( ! $post ) {
530            return $link;
531        }
532
533        if ( 'attachment' !== $post->post_type || 'video/videopress' !== $post->post_mime_type ) {
534            return $link;
535        }
536
537        $url = add_query_arg( 'p', sprintf( '/video/%d', $post_id ), self::get_admin_page_url() );
538
539        if ( 'display' === $context ) {
540            return esc_url( $url );
541        }
542
543        return esc_url_raw( $url );
544    }
545
546    /**
547     * Gets the SVG for the sub brand Jetpack VideoPress logo
548     *
549     * @return string
550     */
551    protected static function get_logo_svg() {
552        return '<svg width="212" height="27" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M110.441 16.294c.479-1.458.995-2.93 1.547-4.414l2.545-6.87h1.687l-5.09 13.361h-1.427l-5.09-13.361h1.687l2.545 6.87a109.628 109.628 0 0 1 1.556 4.414h.04ZM117.919 18.371V8.884h1.547v9.487h-1.547Zm-.04-11.694V4.95h1.627v1.727h-1.627ZM123.331 13.728c0 .546.06 1.042.179 1.488.127.439.306.815.539 1.128.233.306.519.543.858.71.34.166.729.249 1.168.249.326 0 .622-.044.888-.13.266-.087.503-.196.709-.33a3.045 3.045 0 0 0 .898-.838V11.04a3.706 3.706 0 0 0-1.108-.779c-.419-.2-.871-.3-1.357-.3a2.83 2.83 0 0 0-.928.17 2.26 2.26 0 0 0-.898.59c-.273.286-.499.672-.679 1.158-.179.48-.269 1.095-.269 1.848Zm-1.617.02c0-.66.063-1.242.19-1.748.133-.513.306-.959.519-1.338.212-.38.462-.696.748-.949.286-.26.582-.47.888-.629.306-.16.616-.273.928-.34.32-.073.619-.11.898-.11.579 0 1.082.097 1.507.29.426.193.809.476 1.148.85h.03V3.91h1.547v14.46h-1.168l-.319-1.058h-.04c-.12.153-.26.31-.419.47-.16.152-.349.292-.569.419a2.83 2.83 0 0 1-.749.3 3.57 3.57 0 0 1-.978.119 4.63 4.63 0 0 1-1.526-.26 3.666 3.666 0 0 1-1.338-.849c-.386-.392-.698-.895-.938-1.507-.239-.62-.359-1.372-.359-2.257ZM136.626 9.963c-.352 0-.682.07-.988.21a2.57 2.57 0 0 0-.788.569c-.226.24-.409.522-.549.849-.14.32-.226.662-.259 1.028h4.79c0-.373-.05-.719-.15-1.038a2.474 2.474 0 0 0-.419-.85 1.935 1.935 0 0 0-.689-.559 2.046 2.046 0 0 0-.948-.21Zm.849 7.31c.512 0 .978-.04 1.397-.12a8.775 8.775 0 0 0 1.267-.36v1.289c-.339.166-.758.296-1.257.39-.499.1-1.035.149-1.607.149a6.871 6.871 0 0 1-1.916-.26 4.108 4.108 0 0 1-1.557-.838c-.446-.387-.798-.89-1.058-1.508-.253-.626-.379-1.379-.379-2.257 0-.866.12-1.615.359-2.247.24-.64.559-1.172.958-1.598.4-.426.859-.746 1.378-.959a4.238 4.238 0 0 1 1.626-.32c.532 0 1.028.09 1.487.27.466.173.868.45 1.208.83.339.379.605.868.798 1.467.2.6.299 1.318.299 2.157v.21c0 .053-.003.18-.009.38h-6.507c0 .605.09 1.121.269 1.547.187.42.436.763.749 1.029.319.26.692.45 1.117.57.426.119.885.179 1.378.179ZM149.623 13.638c0-.586-.074-1.105-.22-1.558a3.288 3.288 0 0 0-.599-1.159 2.421 2.421 0 0 0-.908-.718 2.674 2.674 0 0 0-1.157-.25c-.42 0-.806.083-1.158.25a2.5 2.5 0 0 0-.918.719c-.253.312-.453.699-.599 1.158-.14.453-.21.972-.21 1.558 0 .579.07 1.098.21 1.558.146.452.349.835.609 1.148.259.313.565.553.918.72.352.159.738.239 1.157.239.42 0 .802-.08 1.148-.24a2.56 2.56 0 0 0 .908-.719c.26-.313.459-.696.599-1.148.146-.46.22-.979.22-1.558Zm1.616 0a6.17 6.17 0 0 1-.319 2.037 4.617 4.617 0 0 1-.908 1.578 3.987 3.987 0 0 1-1.407 1.009c-.552.24-1.171.359-1.857.359-.705 0-1.337-.12-1.896-.36a4.09 4.09 0 0 1-1.417-1.008 4.562 4.562 0 0 1-.888-1.578 6.361 6.361 0 0 1-.309-2.037c0-.752.106-1.435.319-2.047a4.697 4.697 0 0 1 .898-1.578c.393-.44.865-.78 1.417-1.019a4.69 4.69 0 0 1 1.867-.36c.698 0 1.327.12 1.886.36.559.24 1.031.58 1.417 1.019.386.44.682.965.888 1.578.206.612.309 1.295.309 2.047ZM155.204 18.371h-1.627V5.01h3.703c.552 0 1.061.033 1.527.1.472.066.905.17 1.297.309.798.286 1.404.716 1.816 1.288.413.566.619 1.265.619 2.097 0 .68-.136 1.279-.409 1.798a3.473 3.473 0 0 1-1.168 1.288c-.512.346-1.137.61-1.876.79-.732.172-1.563.259-2.495.259-.446 0-.908-.02-1.387-.06v5.492Zm0-6.98a12.234 12.234 0 0 0 1.367.08c.765 0 1.417-.06 1.956-.18.539-.12.978-.293 1.318-.52.339-.232.585-.512.738-.838.153-.333.23-.71.23-1.129 0-.519-.127-.945-.38-1.278-.246-.333-.598-.586-1.057-.759a4.097 4.097 0 0 0-.988-.22 10.59 10.59 0 0 0-1.298-.07h-1.886v4.914ZM169.62 10.143h-.12c-.365 0-.725.033-1.077.1a4.45 4.45 0 0 0-.998.309c-.306.133-.586.3-.839.5a2.74 2.74 0 0 0-.618.708v6.611h-1.547V8.884h1.197l.3 1.508h.03c.153-.246.339-.476.559-.689.226-.213.479-.4.758-.56a3.72 3.72 0 0 1 .918-.369c.333-.093.682-.14 1.048-.14.067 0 .133.004.2.01.073 0 .136.004.189.01v1.489ZM174.673 9.963c-.353 0-.682.07-.988.21-.3.14-.562.33-.789.569-.226.24-.409.522-.549.849-.139.32-.226.662-.259 1.028h4.79c0-.373-.05-.719-.149-1.038a2.476 2.476 0 0 0-.42-.85 1.931 1.931 0 0 0-.688-.559 2.052 2.052 0 0 0-.948-.21Zm.848 7.31c.512 0 .978-.04 1.397-.12a8.824 8.824 0 0 0 1.268-.36v1.289c-.34.166-.759.296-1.258.39-.499.1-1.034.149-1.607.149a6.876 6.876 0 0 1-1.916-.26 4.108 4.108 0 0 1-1.557-.838c-.445-.387-.798-.89-1.057-1.508-.253-.626-.38-1.379-.38-2.257 0-.866.12-1.615.36-2.247.239-.64.559-1.172.958-1.598a3.9 3.9 0 0 1 1.377-.959 4.243 4.243 0 0 1 1.627-.32c.532 0 1.027.09 1.487.27.465.173.868.45 1.207.83.339.379.606.868.798 1.467.2.6.3 1.318.3 2.157v.21c0 .053-.003.18-.01.38h-6.507c0 .605.09 1.121.27 1.547.186.42.435.763.748 1.029.319.26.692.45 1.118.57.426.119.885.179 1.377.179ZM181.831 11.41c0 .26.056.473.17.64.113.166.262.303.449.41.193.1.412.182.658.25.253.066.513.126.779.179.366.08.712.17 1.038.27.326.093.625.236.898.429.279.193.502.456.668.789.173.326.26.752.26 1.278 0 .48-.093.906-.28 1.278a2.64 2.64 0 0 1-.788.929 3.66 3.66 0 0 1-1.217.56c-.473.132-.992.199-1.557.199-.346 0-.662-.02-.948-.06a5.2 5.2 0 0 1-.749-.14 4.56 4.56 0 0 1-.549-.17 3.646 3.646 0 0 1-.379-.17v-1.297c.399.16.795.286 1.188.379.392.093.825.14 1.297.14.373 0 .709-.034 1.008-.1.299-.067.555-.163.768-.29.22-.133.386-.296.499-.489.12-.2.18-.43.18-.69 0-.265-.053-.485-.16-.658a1.315 1.315 0 0 0-.429-.43 2.582 2.582 0 0 0-.629-.28 9.795 9.795 0 0 0-.738-.199c-.339-.073-.685-.16-1.038-.26-.346-.1-.662-.24-.948-.419a2.102 2.102 0 0 1-.699-.729c-.173-.313-.259-.712-.259-1.198 0-.5.09-.932.269-1.299.18-.366.423-.669.729-.908.306-.24.662-.42 1.068-.54.406-.12.835-.18 1.287-.18.526 0 1.011.05 1.457.15.453.1.862.227 1.228.38v1.318a8.85 8.85 0 0 0-1.218-.37 5.61 5.61 0 0 0-1.257-.16c-.366 0-.679.037-.938.11a1.78 1.78 0 0 0-.639.31c-.166.127-.289.28-.369.46a1.46 1.46 0 0 0-.11.579ZM189.787 11.41c0 .26.057.473.17.64.113.166.263.303.449.41.193.1.413.182.659.25.253.066.512.126.778.179.366.08.712.17 1.038.27.326.093.626.236.898.429.28.193.503.456.669.789.173.326.259.752.259 1.278 0 .48-.093.906-.279 1.278a2.64 2.64 0 0 1-.788.929c-.34.246-.746.433-1.218.56-.472.132-.991.199-1.557.199-.346 0-.662-.02-.948-.06a5.185 5.185 0 0 1-.748-.14 4.56 4.56 0 0 1-.549-.17 3.552 3.552 0 0 1-.379-.17v-1.297c.399.16.795.286 1.187.379.393.093.825.14 1.298.14.372 0 .708-.034 1.007-.1.3-.067.556-.163.769-.29.219-.133.386-.296.499-.489.12-.2.18-.43.18-.69 0-.265-.054-.485-.16-.658a1.334 1.334 0 0 0-.429-.43 2.6 2.6 0 0 0-.629-.28 9.925 9.925 0 0 0-.738-.199c-.34-.073-.686-.16-1.038-.26-.346-.1-.662-.24-.948-.419a2.102 2.102 0 0 1-.699-.729c-.173-.313-.259-.712-.259-1.198 0-.5.089-.932.269-1.299.18-.366.422-.669.729-.908a3.16 3.16 0 0 1 1.067-.54c.406-.12.835-.18 1.288-.18.525 0 1.011.05 1.457.15.452.1.861.227 1.227.38v1.318a8.837 8.837 0 0 0-1.217-.37 5.612 5.612 0 0 0-1.258-.16 3.49 3.49 0 0 0-.938.11 1.797 1.797 0 0 0-.639.31c-.166.127-.289.28-.369.46-.073.173-.11.366-.11.579Z" fill="#000"/><path d="M13.491 27c7.451 0 13.492-6.044 13.492-13.5 0-7.457-6.04-13.5-13.492-13.5C6.041 0 0 6.044 0 13.5 0 20.957 6.04 27 13.491 27Z" fill="#069E08"/><path d="M14.16 11.233v13.088l6.746-13.088H14.16ZM12.798 15.742V2.679l-6.72 13.063h6.72Z" fill="#fff"/><path d="M34.834 22.414c-.387-.593-.747-1.184-1.107-1.751 1.905-1.16 2.548-2.087 2.548-3.84V6.699h-2.24V4.767H38.8v11.542c0 2.937-.85 4.586-3.965 6.105ZM54.787 15.51c0 .979.695 1.082 1.159 1.082.464 0 1.133-.155 1.647-.31v1.804c-.72.232-1.467.412-2.497.412-1.236 0-2.678-.464-2.678-2.628v-5.308h-1.313v-1.83h1.313V6.029h2.369v2.706h2.986v1.83h-2.986v4.946ZM59.73 23.316V8.708h2.266v.876c.901-.696 1.906-1.134 3.141-1.134 2.137 0 3.837 1.494 3.837 4.715 0 3.195-1.853 5.307-4.918 5.307-.746 0-1.338-.103-1.957-.232v5.05h-2.368v.026Zm4.789-12.908c-.695 0-1.57.335-2.395 1.057v4.973a8.59 8.59 0 0 0 1.777.18c1.673 0 2.626-1.057 2.626-3.272 0-2.036-.695-2.938-2.008-2.938ZM78.294 18.266H76.08V17.21h-.052c-.772.592-1.725 1.236-3.14 1.236-1.237 0-2.576-.902-2.576-2.731 0-2.447 2.086-2.911 3.553-3.117l2.085-.283v-.283c0-1.289-.514-1.7-1.725-1.7-.593 0-1.982.18-3.115.643l-.206-1.906a11.91 11.91 0 0 1 3.63-.618c2.317 0 3.811.928 3.811 3.684v6.131h-.05Zm-2.369-4.457-1.957.31c-.592.076-1.21.437-1.21 1.313 0 .773.438 1.21 1.081 1.21.696 0 1.442-.411 2.085-.875v-1.958h.001ZM88.079 17.957c-.978.335-1.853.541-2.961.541-3.553 0-4.97-2.035-4.97-4.998 0-3.117 1.958-5.05 5.124-5.05 1.184 0 1.905.207 2.703.465v2.01c-.695-.259-1.7-.542-2.677-.542-1.442 0-2.678.773-2.678 2.989 0 2.447 1.236 3.195 2.806 3.195.747 0 1.571-.155 2.678-.593v1.983h-.025ZM92.558 12.83c.205-.231.36-.463 3.346-4.096h3.09l-3.863 4.535 4.222 5.023h-3.089l-3.681-4.535v4.535h-2.368V4.767h2.369v8.064h-.026ZM49.328 17.957c-1.236.387-2.292.541-3.528.541-3.038 0-4.917-1.52-4.917-5.076 0-2.602 1.596-4.972 4.66-4.972 3.038 0 4.094 2.112 4.094 4.122 0 .67-.052 1.03-.077 1.416h-6.128c.052 2.087 1.236 2.577 3.013 2.577.978 0 1.853-.232 2.858-.593v1.983h.025v.002Zm-2.162-5.54c0-1.159-.387-2.164-1.647-2.164-1.184 0-1.906.85-2.06 2.165h3.707Z" fill="#000"/></svg>';
553    }
554
555    // phpcs:disable WordPress.Security.EscapeOutput.UnsafePrintingFunction
556    // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
557
558    /**
559     * Overwrites the backbone template for the attachment details modal
560     *
561     * This template is originally added in WP core in wp-includes/media-templates.php
562     *
563     * We override the initialize method of the TwoColumn view class (located at core's js/media/view/attachment/detail-two-column.js)
564     * and use the custom template only for VideoPress videos.
565     *
566     * @return void
567     */
568    public static function attachment_details_two_column_template() {
569
570        ?>
571        <script type="text/html" id="tmpl-videopress_iframe_vnext">
572            <iframe class="videopress-iframe" style="display: block; max-width: 100%; max-height: 100%;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
573        </script>
574        <script type="text/html" id="tmpl-attachment-details-two-column-videopress">
575            <div class="attachment-media-view {{ data.orientation }}">
576                <h2 class="screen-reader-text"><?php _e( 'Attachment Preview', 'jetpack-videopress-pkg' ); ?></h2>
577                <div class="thumbnail thumbnail-{{ data.type }}">
578                </div>
579            </div>
580            <div class="attachment-info">
581                <h2>
582                    <?php echo self::get_logo_svg(); ?>
583                </h2>
584                <?php self::attachment_info_template_part(); ?>
585            </div>
586        </script>
587        <script>
588            jQuery(document).ready( function($) {
589                if( typeof wp.media.view.Attachment.Details.TwoColumn != 'undefined' ){
590                    var TwoColumn   = wp.media.view.Attachment.Details.TwoColumn,
591                        old_render  = TwoColumn.prototype.render,
592                        vp_template = wp.template('videopress_iframe_vnext');
593
594                    TwoColumn.prototype.initialize = function() {
595                        if ( 'video' === this.model.attributes.type && 'videopress' === this.model.attributes.subtype ) {
596                            this.template = wp.template( 'attachment-details-two-column-videopress' );
597                            this.listenTo( this.model, 'change:videopress_status', this.render );
598                        } else {
599                            this.template = wp.template( 'attachment-details-two-column' );
600                        }
601                        // From this point on, we are just copying the function from core.
602                        this.controller.on( 'content:activate:edit-details', _.bind( this.editAttachment, this ) );
603                        wp.media.view.Attachment.Details.prototype.initialize.apply( this, arguments );
604                    }
605
606                    // Add the VideoPress player
607                    TwoColumn.prototype.render = function() {
608                        // Have the original renderer run first.
609                        old_render.apply( this, arguments );
610
611                        // Now our stuff!
612                        if ( 'video' === this.model.get('type') ) {
613                            if ( 'failed' === this.model.get('videopress_status') ) {
614                                // The legacy Jetpack renderer can insert a player during old_render.
615                                this.$('.attachment-media-view .thumbnail-video').empty();
616                            } else if ( this.model.get('videopress_guid') ) {
617                                this.$('.attachment-media-view .thumbnail-video').html( vp_template( {
618                                    guid   : this.model.get('videopress_guid'),
619                                    width  : this.model.get('width') > 0 ? this.model.get('width') : '100%',
620                                    height : this.model.get('height') > 0 ? this.model.get('height') : '100%'
621                                }));
622                            }
623                        }
624                    };
625                }
626            });
627        </script>
628        <?php
629    }
630
631    /**
632     * Overwrites the backbone template for the attachment details modal
633     *
634     * This template is originally added in WP core in wp-includes/media-templates.php
635     *
636     * We override the initialize method of the TwoColumn view class (located at core's js/media/view/attachment/detail-two-column.js)
637     * and use the custom template only for VideoPress videos.
638     *
639     * @return void
640     */
641    public static function attachment_details_template() {
642        ?>
643        <style>
644            .attachment-details.videopress-attachment-details ~ .compat-item .compat-field-videopress_conversion_error {
645                display: none;
646            }
647        </style>
648        <script type="text/html" id="tmpl-videopress_iframe_vnext">
649            <iframe class="videopress-iframe" style="display: block; max-width: 100%; max-height: 180px;" width="{{ data.width }}" height="{{ data.height }}" src="https://videopress.com/embed/{{ data.guid }}?{{ data.urlargs }}" frameborder='0' allowfullscreen></iframe>
650        </script>
651        <script type="text/html" id="tmpl-attachment-details-videopress">
652            <h2>
653                <?php echo self::get_logo_svg(); ?>
654            </h2>
655            <div class="attachment-info">
656                <div class="wp-media-wrapper wp-video">
657                </div>
658                <?php self::attachment_info_template_part(); ?>
659            </div>
660        </script>
661        <script>
662            jQuery(document).ready( function($) {
663                if( typeof wp.media.view.Attachment.Details != 'undefined' ){
664                    var DetailsTemplate = wp.media.view.Attachment.Details,
665                        old_render      = DetailsTemplate.prototype.render,
666                        vp_template     = wp.template('videopress_iframe_vnext');
667
668                        DetailsTemplate.prototype.initialize = function() {
669                            if ( 'video' === this.model.attributes.type && 'videopress' === this.model.attributes.subtype ) {
670                                this.template = wp.template( 'attachment-details-videopress' );
671                                this.$el.addClass( 'videopress-attachment-details' );
672                                this.listenTo( this.model, 'change:videopress_status', this.render );
673                            } else {
674                                this.template = wp.template( 'attachment-details' );
675                            }
676                            // From this point on, we are just copying the function from core.
677                            this.options = _.defaults( this.options, {
678                                rerenderOnModelChange: false
679                            });
680
681                            // Call 'initialize' directly on the parent class.
682                            wp.media.view.Attachment.prototype.initialize.apply( this, arguments );
683
684                            this.copyAttachmentDetailsURLClipboard();
685                        }
686
687                        // Add the VideoPress player
688                        DetailsTemplate.prototype.render = function() {
689                            // Have the original renderer run first.
690                            old_render.apply( this, arguments );
691
692                            // Now our stuff!
693                            if ( 'video' === this.model.get('type') ) {
694                                if ( this.model.get('videopress_guid') && 'failed' !== this.model.get('videopress_status') ) {
695                                    this.$('.attachment-info .wp-video').html( vp_template( {
696                                        guid   : this.model.get('videopress_guid'),
697                                        // width  : this.model.get('width') > 0 ? this.model.get('width') : '100%',
698                                        // height : this.model.get('height') > 0 ? this.model.get('height') : '100%'
699                                    }));
700                                }
701                            }
702                        };
703                }
704            });
705        </script>
706        <?php
707    }
708
709    /**
710     * Echoes the piece of the custom template that is shared between the two templates above
711     *
712     * @return void
713     */
714    protected static function attachment_info_template_part() {
715        ?>
716        <# if ( 'failed' === data.videopress_status ) { #>
717            <div class="notice notice-error" role="alert">
718                <p><?php esc_html_e( 'Conversion failed. Delete this video and upload it again.', 'jetpack-videopress-pkg' ); ?></p>
719            </div>
720        <# } #>
721        <span class="setting" data-setting="title">
722            <label for="attachment-details-title" class="name"><?php _e( 'Title', 'jetpack-videopress-pkg' ); ?></label>
723            <input type="text" id="attachment-details-title" value="{{ data.title }}" readonly />
724        </span>
725        <span class="setting" data-setting="filename">
726            <label for="attachment-details-filename" class="name"><?php _e( 'File name', 'jetpack-videopress-pkg' ); ?></label>
727            <input type="text" id="attachment-details-filename" value="{{ data.filename }}" readonly />
728        </span>
729        <span class="setting" data-setting="fileurl">
730            <label for="attachment-details-copy-link" class="name"><?php _e( 'File URL:', 'jetpack-videopress-pkg' ); ?></label>
731            <input type="text" class="attachment-details-copy-link" id="attachment-details-copy-link" value="{{ data.url }}" readonly />
732            <div class="copy-to-clipboard-container">
733                <button type="button" class="button button-small copy-attachment-url" data-clipboard-target="#attachment-details-copy-link"><?php _e( 'Copy URL to clipboard', 'jetpack-videopress-pkg' ); ?></button>
734                <span class="success hidden" aria-hidden="true"><?php _e( 'Copied!', 'jetpack-videopress-pkg' ); ?></span>
735            </div>
736        </span>
737        <p><a href="{{ data.editLink }}" class="button button-medium" target="_blank"><?php _e( 'Edit video details', 'jetpack-videopress-pkg' ); ?></a></p>
738        <?php
739    }
740    // phpcs:enable WordPress.Security.EscapeOutput.UnsafePrintingFunction
741    // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
742
743    /**
744     * Load the wp-build entry file and register its polyfills.
745     *
746     * Only called on `?page=jetpack-videopress` admin requests when the
747     * modernization filter is enabled. Keeps wp-build off every other request.
748     *
749     * @return void
750     */
751    private static function load_wp_build() {
752        $build_index = dirname( __DIR__ ) . '/build/build.php';
753
754        if ( ! file_exists( $build_index ) ) {
755            return;
756        }
757
758        require_once $build_index;
759
760        // wp-build registers standalone modules (e.g. the init module) on
761        // wp_default_scripts, which has already fired by admin_menu. Register them
762        // directly so the init module makes it into the import map.
763        if ( function_exists( 'jetpack_videopress_register_script_modules' ) ) {
764            jetpack_videopress_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes.
765        }
766
767        // Defer the chapters-editor route stripping to the page init actions, at a
768        // priority ahead of the `build/routes.php` readers (default 10). Those
769        // actions fire from the page's `admin_enqueue_scripts` callback, one
770        // priority before `Initial_State` mirrors the chapters-editor filter to
771        // the client (priority 11), so both evaluations of the filter happen at
772        // the same request stage and a filter registered after this method
773        // runs (`init`, `admin_init`, …) is honored consistently by both.
774        add_action( 'jetpack-videopress-dashboard_init', array( __CLASS__, 'maybe_strip_chapters_editor_routes' ), 5 );
775        add_action( 'jetpack-videopress-dashboard-wp-admin_init', array( __CLASS__, 'maybe_strip_chapters_editor_routes' ), 5 );
776
777        \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register(
778            'jetpack-videopress',
779            array_merge(
780                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES,
781                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS
782            ),
783            null,
784            array(
785                array(
786                    'prefix' => 'jetpack_videopress',
787                    'slug'   => 'jetpack-videopress-dashboard',
788                ),
789            )
790        );
791    }
792
793    /**
794     * Load wp-build with the screen ID aliased across its generated enqueue check.
795     *
796     * @see WP_Build_Screen_Id::load_with_alias()
797     * @return void
798     */
799    private static function load_wp_build_with_screen_alias() {
800        // Fallback: an older wp-build-polyfills under the jetpack-autoloader may predate load_with_alias().
801        if ( method_exists( \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::class, 'load_with_alias' ) ) {
802            \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id::load_with_alias(
803                array( __CLASS__, 'alias_screen_id_for_wp_build' ),
804                array( __CLASS__, 'restore_screen_id_after_wp_build' ),
805                function () {
806                    self::load_wp_build();
807                }
808            );
809            return;
810        }
811
812        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
813        self::load_wp_build();
814        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id_after_wp_build' ) );
815    }
816
817    /**
818     * Remove the chapters editor routes from the generated route registry when
819     * the chapters editor filter is off.
820     *
821     * The wp-build routes loader (`build/routes.php`) stores each page's route
822     * list in `$GLOBALS['jetpack_videopress_jetpack_videopress_dashboard_routes_data']`
823     * and reads it back on the page init hooks at default priority, so
824     * `load_wp_build()` hooks this on those same actions at priority 5 —
825     * filtering the global just before it is consumed keeps the chapters editor
826     * route modules from ever being registered.
827     *
828     * @since 0.45.0
829     *
830     * @internal Only public so the wp-build page init actions can invoke it.
831     *
832     * @return void
833     */
834    public static function maybe_strip_chapters_editor_routes() {
835        if ( self::is_chapters_editor_enabled() || self::is_trim_cut_enabled() ) {
836            return;
837        }
838
839        $global_name = 'jetpack_videopress_jetpack_videopress_dashboard_routes_data';
840
841        // Guard against a build change renaming the generated global: the
842        // chapters editor route would leak, but nothing fatals.
843        if ( empty( $GLOBALS[ $global_name ] ) || ! is_array( $GLOBALS[ $global_name ] ) ) {
844            return;
845        }
846
847        $GLOBALS[ $global_name ] = self::strip_chapters_editor_routes( $GLOBALS[ $global_name ] );
848    }
849
850    /**
851     * Filter the chapters editor route entries out of a wp-build route registry array.
852     *
853     * Pure helper extracted from `maybe_strip_chapters_editor_routes()` so the
854     * filtering logic is unit-testable without the generated global.
855     *
856     * @since 0.45.0
857     *
858     * @internal For use by `maybe_strip_chapters_editor_routes()` and tests only.
859     *
860     * @param array $routes Route entries as generated by `build/routes.php`, each an array with a `path` key.
861     * @return array Route entries with the chapters editor paths removed.
862     */
863    public static function strip_chapters_editor_routes( $routes ) {
864        return array_values(
865            array_filter(
866                $routes,
867                static function ( $route ) {
868                    return ! isset( $route['path'] ) || ! in_array( $route['path'], self::CHAPTERS_EDITOR_ROUTE_PATHS, true );
869                }
870            )
871        );
872    }
873
874    /**
875     * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check.
876     *
877     * Wp-build's `<page>-wp-admin` enqueue callback enqueues only when the screen ID
878     * matches the wp-build page slug (`jetpack-videopress-dashboard`). Our WP-admin
879     * menu slug stays `jetpack-videopress`, so we mutate the screen object in place
880     * to make the check pass without changing the user-facing URL.
881     *
882     * Hooked only when modernization is on AND we're on the VideoPress admin page,
883     * so this never affects any other request.
884     *
885     * @since 0.51.1 Takes no argument; hooked on `admin_enqueue_scripts`.
886     *
887     * @return void
888     */
889    public static function alias_screen_id_for_wp_build() {
890        $screen = get_current_screen();
891        if ( ! $screen ) {
892            return;
893        }
894
895        self::$wp_build_original_screen_id = $screen->id;
896        $screen->id                        = 'jetpack-videopress-dashboard';
897    }
898
899    /**
900     * Undo alias_screen_id_for_wp_build(), so code after the generated check sees the real screen ID.
901     *
902     * @since 0.51.1
903     *
904     * @return void
905     */
906    public static function restore_screen_id_after_wp_build() {
907        $screen = get_current_screen();
908        if ( ! $screen || null === self::$wp_build_original_screen_id ) {
909            return;
910        }
911
912        $screen->id                        = self::$wp_build_original_screen_id;
913        self::$wp_build_original_screen_id = null;
914    }
915
916    /**
917     * Returns true when the wp-build modernization filter is enabled.
918     *
919     * @return bool
920     */
921    public static function is_modernized() {
922        return (bool) apply_filters( self::MODERNIZATION_FILTER, true );
923    }
924
925    /**
926     * Whether the trim and cut editor and its REST endpoints are available.
927     *
928     * @since 0.53.0
929     * @return bool
930     */
931    public static function is_trim_cut_enabled() {
932        /**
933         * Whether the trim and cut editor is available for this site.
934         *
935         * @since 0.53.0
936         * @param bool $enabled Whether trim and cut is enabled. Default true.
937         */
938        return (bool) apply_filters( 'jetpack_videopress_trim_cut', true );
939    }
940
941    /**
942     * Returns true when the chapters editor feature filter is enabled.
943     *
944     * Note the default is true: the dashboard Editor tab, the
945     * `/video/$id/editor` route, the Details-tab deep link, and the block
946     * editor's "Manage chapters" toolbar button are all available unless a site
947     * explicitly opts out via the filter.
948     *
949     * @since 0.45.0
950     *
951     * @return bool
952     */
953    public static function is_chapters_editor_enabled() {
954        /**
955         * Whether the VideoPress chapters editor UI is available.
956         *
957         * Gates UI only — the chapters REST surface stays registered either way.
958         *
959         * @since 0.45.0
960         *
961         * @param bool $enabled Whether the chapters editor UI is enabled. Default true.
962         */
963        return (bool) apply_filters( self::CHAPTERS_EDITOR_FILTER, true );
964    }
965
966    /**
967     * Returns true when the current request targets the VideoPress admin page.
968     *
969     * Used to scope wp-build loading to the one page that needs it. The
970     * `$_GET['page']` value is populated by wp-admin/admin.php before any of
971     * our hooks fire, so this check is reliable from `init()` onwards.
972     *
973     * @return bool
974     */
975    private static function is_videopress_admin_request() {
976        if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
977            return false;
978        }
979
980        return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::ADMIN_PAGE_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
981    }
982}