PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.7
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / render / classic-link-interceptor.php

classic-link-interceptor.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.8.7, at includes/render/classic-link-interceptor.php

118 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — Classic-tab link interceptor.
4 *
5 * Runs only inside detached "classic override" tabs (the ones
6 * opened by the Detach window-chrome action with
7 * `?desktop_mode_classic=1`). Re-stamps the flag onto every
8 * same-origin `/wp-admin/` `<a href>` and `<form action>` so
9 * navigations within the tab stay classic — server-side
10 * redirects are handled by
11 * `desktop_mode_classic_preserve_redirect()` in routing.php.
12 *
13 * Extracted from `render.php` during the architecture-0.8.1 PHP
14 * slicing (phase 6).
15 *
16 * @package Desktop_Mode
17 * @since 0.8.1
18 */
19
20 defined( 'ABSPATH' ) || exit;
21
22 /**
23 * Outputs a same-origin admin link/form rewriter for detached ("classic
24 * override") tabs.
25 *
26 * Without this, the first navigation after a detach drops the
27 * `desktop_mode_classic=1` flag and the next page falls back to the
28 * desktop shell — because the user meta is still `'1'` and the
29 * `admin_init` portal redirect kicks in. The JS here re-stamps the flag
30 * on every same-origin `/wp-admin/` `<a href>` and `<form action>` so
31 * navigations within the tab stay classic. Server-side redirects are
32 * covered by {@see desktop_mode_classic_preserve_redirect}.
33 *
34 * Narrowly scoped: only runs when the current request itself carries
35 * the classic flag. Skips modifier-clicks (cmd/ctrl/shift/alt), targets
36 * other than `_self`, downloads, anchors, and non-http schemes so we
37 * don't break "open in new tab" or mailto links.
38 *
39 * @since 0.4.0
40 */
41 function desktop_mode_classic_link_interceptor() {
42 if ( ! desktop_mode_is_classic_request() ) {
43 return;
44 }
45
46 $flag_literal = wp_json_encode( DESKTOP_MODE_CLASSIC_FLAG );
47
48 $js = "
49 ( function () {
50 var FLAG = {$flag_literal};
51
52 function rewriteAdminUrl( href, base ) {
53 if ( ! href || href.charAt( 0 ) === '#' ) {
54 return null;
55 }
56 if ( /^(mailto:|tel:|javascript:|data:)/i.test( href ) ) {
57 return null;
58 }
59 var url;
60 try {
61 url = new URL( href, base );
62 } catch ( err ) {
63 return null;
64 }
65 if ( url.origin !== window.location.origin ) {
66 return null;
67 }
68 if ( url.pathname.indexOf( '/wp-admin/' ) === -1 ) {
69 return null;
70 }
71 if ( url.searchParams.has( FLAG ) ) {
72 return null;
73 }
74 url.searchParams.set( FLAG, '1' );
75 return url.toString();
76 }
77
78 document.addEventListener( 'click', function ( e ) {
79 if ( e.defaultPrevented ) {
80 return;
81 }
82 if ( e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey ) {
83 return;
84 }
85 var link = e.target && e.target.closest ? e.target.closest( 'a[href]' ) : null;
86 if ( ! link ) {
87 return;
88 }
89 if ( link.target && link.target !== '' && link.target !== '_self' ) {
90 return;
91 }
92 if ( link.hasAttribute( 'download' ) ) {
93 return;
94 }
95 var rewritten = rewriteAdminUrl( link.getAttribute( 'href' ), window.location.href );
96 if ( rewritten ) {
97 link.setAttribute( 'href', rewritten );
98 }
99 }, true );
100
101 document.addEventListener( 'submit', function ( e ) {
102 var form = e.target;
103 if ( ! form || form.tagName !== 'FORM' ) {
104 return;
105 }
106 var action = form.getAttribute( 'action' );
107 var rewritten = rewriteAdminUrl( action || window.location.href, window.location.href );
108 if ( rewritten ) {
109 form.setAttribute( 'action', rewritten );
110 }
111 }, true );
112 } )();
113 ";
114
115 wp_print_inline_script_tag( $js );
116 }
117 add_action( 'admin_footer', 'desktop_mode_classic_link_interceptor' );
118