PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.13
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.13
1.1.13 1.1.12 1.1.11 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 All 37 releases
desktop-mode / includes / render / classic-link-interceptor.php

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

132 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — 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 * `openstation_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 OpenStation
17 */
18
19 defined( 'ABSPATH' ) || exit;
20
21 /**
22 * Outputs a same-origin admin link/form rewriter for detached ("classic
23 * override") tabs.
24 *
25 * Without this, the first navigation after a detach drops the
26 * `desktop_mode_classic=1` flag and the next page falls back to the
27 * desktop shell — because the user meta is still `'1'` and the
28 * `admin_init` portal redirect kicks in. The JS here re-stamps the flag
29 * on every same-origin `/wp-admin/` `<a href>` and `<form action>` so
30 * navigations within the tab stay classic, and keeps it in the address
31 * bar. Server-side redirects are covered by
32 * {@see openstation_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 function openstation_classic_link_interceptor() {
40 if ( ! openstation_is_classic_request() ) {
41 return;
42 }
43
44 $flag_literal = wp_json_encode( OPENSTATION_CLASSIC_FLAG );
45
46 $js = "
47 ( function () {
48 var FLAG = {$flag_literal};
49
50 function rewriteAdminUrl( href, base ) {
51 if ( ! href || href.charAt( 0 ) === '#' ) {
52 return null;
53 }
54 if ( /^(mailto:|tel:|javascript:|data:)/i.test( href ) ) {
55 return null;
56 }
57 var url;
58 try {
59 url = new URL( href, base );
60 } catch ( err ) {
61 return null;
62 }
63 if ( url.origin !== window.location.origin ) {
64 return null;
65 }
66 if ( url.pathname.indexOf( '/wp-admin/' ) === -1 ) {
67 return null;
68 }
69 if ( url.searchParams.has( FLAG ) ) {
70 return null;
71 }
72 url.searchParams.set( FLAG, '1' );
73 return url.toString();
74 }
75
76 document.addEventListener( 'click', function ( e ) {
77 if ( e.defaultPrevented ) {
78 return;
79 }
80 if ( e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey ) {
81 return;
82 }
83 var link = e.target && e.target.closest ? e.target.closest( 'a[href]' ) : null;
84 if ( ! link ) {
85 return;
86 }
87 if ( link.target && link.target !== '' && link.target !== '_self' ) {
88 return;
89 }
90 if ( link.hasAttribute( 'download' ) ) {
91 return;
92 }
93 var rewritten = rewriteAdminUrl( link.getAttribute( 'href' ), window.location.href );
94 if ( rewritten ) {
95 link.setAttribute( 'href', rewritten );
96 }
97 }, true );
98
99 document.addEventListener( 'submit', function ( e ) {
100 var form = e.target;
101 if ( ! form || form.tagName !== 'FORM' ) {
102 return;
103 }
104 var action = form.getAttribute( 'action' );
105 var rewritten = rewriteAdminUrl( action || window.location.href, window.location.href );
106 if ( rewritten ) {
107 form.setAttribute( 'action', rewritten );
108 }
109 }, true );
110
111 // The block editor rewrites the address bar on load, dropping the
112 // flag; the server reads it off the Referer of script navigations.
113 [ 'pushState', 'replaceState' ].forEach( function ( method ) {
114 var original = window.history[ method ];
115 if ( typeof original !== 'function' ) {
116 return;
117 }
118 window.history[ method ] = function ( state, title, url ) {
119 var rewritten = null;
120 if ( typeof url === 'string' || url instanceof URL ) {
121 rewritten = rewriteAdminUrl( String( url ), window.location.href );
122 }
123 return original.call( window.history, state, title, rewritten || url );
124 };
125 } );
126 } )();
127 ";
128
129 wp_print_inline_script_tag( $js );
130 }
131 add_action( 'admin_footer', 'openstation_classic_link_interceptor' );
132