| 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. Server-side redirects are |
| 31 |
* covered by {@see openstation_classic_preserve_redirect}. |
| 32 |
* |
| 33 |
* Narrowly scoped: only runs when the current request itself carries |
| 34 |
* the classic flag. Skips modifier-clicks (cmd/ctrl/shift/alt), targets |
| 35 |
* other than `_self`, downloads, anchors, and non-http schemes so we |
| 36 |
* don't break "open in new tab" or mailto links. |
| 37 |
*/ |
| 38 |
function openstation_classic_link_interceptor() { |
| 39 |
if ( ! openstation_is_classic_request() ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$flag_literal = wp_json_encode( OPENSTATION_CLASSIC_FLAG ); |
| 44 |
|
| 45 |
$js = " |
| 46 |
( function () { |
| 47 |
var FLAG = {$flag_literal}; |
| 48 |
|
| 49 |
function rewriteAdminUrl( href, base ) { |
| 50 |
if ( ! href || href.charAt( 0 ) === '#' ) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
if ( /^(mailto:|tel:|javascript:|data:)/i.test( href ) ) { |
| 54 |
return null; |
| 55 |
} |
| 56 |
var url; |
| 57 |
try { |
| 58 |
url = new URL( href, base ); |
| 59 |
} catch ( err ) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
if ( url.origin !== window.location.origin ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
if ( url.pathname.indexOf( '/wp-admin/' ) === -1 ) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
if ( url.searchParams.has( FLAG ) ) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
url.searchParams.set( FLAG, '1' ); |
| 72 |
return url.toString(); |
| 73 |
} |
| 74 |
|
| 75 |
document.addEventListener( 'click', function ( e ) { |
| 76 |
if ( e.defaultPrevented ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
if ( e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
var link = e.target && e.target.closest ? e.target.closest( 'a[href]' ) : null; |
| 83 |
if ( ! link ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
if ( link.target && link.target !== '' && link.target !== '_self' ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
if ( link.hasAttribute( 'download' ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
var rewritten = rewriteAdminUrl( link.getAttribute( 'href' ), window.location.href ); |
| 93 |
if ( rewritten ) { |
| 94 |
link.setAttribute( 'href', rewritten ); |
| 95 |
} |
| 96 |
}, true ); |
| 97 |
|
| 98 |
document.addEventListener( 'submit', function ( e ) { |
| 99 |
var form = e.target; |
| 100 |
if ( ! form || form.tagName !== 'FORM' ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
var action = form.getAttribute( 'action' ); |
| 104 |
var rewritten = rewriteAdminUrl( action || window.location.href, window.location.href ); |
| 105 |
if ( rewritten ) { |
| 106 |
form.setAttribute( 'action', rewritten ); |
| 107 |
} |
| 108 |
}, true ); |
| 109 |
} )(); |
| 110 |
"; |
| 111 |
|
| 112 |
wp_print_inline_script_tag( $js ); |
| 113 |
} |
| 114 |
add_action( 'admin_footer', 'openstation_classic_link_interceptor' ); |
| 115 |
|