← All changes
|
includes/__js/admin/settings/ui_full_screen_mode.js
+44
-13
11.4.3
→
11.9
View file →
| @@ -1,22 +1,53 @@ | ||
| 1 | 1 | "use strict"; |
| 2 | 2 | // ===================================================================================================================== |
| 3 | 3 | // == Full Screen - support functions == |
| 4 | 4 | // ===================================================================================================================== |
| 5 | 5 | |
| 6 | -/** | |
| 7 | - * Save Full Screen preference in a short browser cookie. | |
| 8 | - * | |
| 9 | - * This makes the next admin page load deterministic even if the async user-meta | |
| 10 | - * AJAX request is interrupted by immediate navigation. | |
| 11 | - * | |
| 12 | - * @param string value 'On' or 'Off'. | |
| 13 | - */ | |
| 14 | -function wpbc_admin_ui__full_screen__set_cookie( value ) { | |
| 15 | - var max_age = 60 * 60 * 24 * 365; | |
| 16 | - | |
| 17 | - document.cookie = 'wpbc_admin_full_screen=' + encodeURIComponent( value ) + '; path=/; max-age=' + max_age + '; SameSite=Lax'; | |
| 18 | -} | |
| 6 | +/** | |
| 7 | + * Return every cookie path that can apply to the current WordPress admin URL. | |
| 8 | + * | |
| 9 | + * WordPress may run from a subdirectory. Updating the root, site, and admin | |
| 10 | + * paths prevents an older, more-specific cookie from overriding the new mode. | |
| 11 | + * | |
| 12 | + * @return {string[]} Unique absolute cookie paths. | |
| 13 | + */ | |
| 14 | +function wpbc_admin_ui__full_screen__get_cookie_paths() { | |
| 15 | + var cookie_paths = [ '/' ]; | |
| 16 | + var admin_marker = '/wp-admin/'; | |
| 17 | + var current_path = window.location && window.location.pathname ? window.location.pathname : ''; | |
| 18 | + var admin_index = current_path.indexOf( admin_marker ); | |
| 19 | + | |
| 20 | + if ( admin_index >= 0 ) { | |
| 21 | + cookie_paths.push( current_path.substring( 0, admin_index + 1 ) ); | |
| 22 | + cookie_paths.push( current_path.substring( 0, admin_index + admin_marker.length ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + return cookie_paths.filter( function ( path, index ) { | |
| 26 | + return path && cookie_paths.indexOf( path ) === index; | |
| 27 | + } ); | |
| 28 | +} | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Save Full Screen preference in a short-lived browser cookie. | |
| 32 | + * | |
| 33 | + * This makes the next admin page load deterministic even if the asynchronous | |
| 34 | + * user-meta request is interrupted. The timestamp lets PHP distinguish this | |
| 35 | + * pending value from a stale legacy cookie. | |
| 36 | + * | |
| 37 | + * @param {string} value Fullscreen mode, either `On` or `Off`. | |
| 38 | + * | |
| 39 | + * @return {void} | |
| 40 | + */ | |
| 41 | +function wpbc_admin_ui__full_screen__set_cookie( value ) { | |
| 42 | + var max_age = 5 * 60; | |
| 43 | + var issued_at = Math.floor( Date.now() / 1000 ); | |
| 44 | + var cookie_value = encodeURIComponent( value + '|' + issued_at ); | |
| 45 | + | |
| 46 | + wpbc_admin_ui__full_screen__get_cookie_paths().forEach( function ( cookie_path ) { | |
| 47 | + document.cookie = 'wpbc_admin_full_screen=' + cookie_value + '; path=' + cookie_path + '; max-age=' + max_age + '; SameSite=Lax'; | |
| 48 | + } ); | |
| 49 | +} | |
| 19 | 50 | |
| 20 | 51 | /** |
| 21 | 52 | * Apply Full Screen mode from a user click. |
| 22 | 53 | * |