PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.0
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.0
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 / ajax.php

ajax.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.0, at includes/ajax.php

78 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode AJAX endpoints.
4 *
5 * @package WPDesktopMode
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Handles saving the user's desktop mode preference via AJAX.
12 *
13 * @since 0.1.0
14 */
15 function desktop_mode_ajax_save() {
16 check_ajax_referer( 'save-desktop-mode', 'nonce' );
17
18 // A valid nonce proves *this* request was authored by the current
19 // user, but WP's cap system is the authoritative gate for "is this
20 // account allowed to touch admin state at all". `read` is the
21 // minimum cap every admin-visible role carries; subscribers on sites
22 // that revoke it have no business flipping an admin-UI preference.
23 if ( ! current_user_can( 'read' ) ) {
24 wp_send_json_error( 'desktop_mode_forbidden', 403 );
25 }
26
27 /**
28 * Filters whether desktop mode is available for this user.
29 *
30 * Plugins can disable desktop mode for certain roles, capabilities, or conditions.
31 *
32 * @since 0.1.0
33 *
34 * @param bool $enabled Whether desktop mode is enabled. Default true.
35 * @param int $user_id The current user ID.
36 */
37 $allowed = apply_filters( 'desktop_mode_mode_enabled', true, get_current_user_id() );
38 if ( ! $allowed ) {
39 wp_send_json_error( 'desktop_mode_disabled' );
40 }
41
42 $enabled = ! empty( $_POST['enabled'] ) && '1' === $_POST['enabled'] ? '1' : '';
43
44 update_user_meta( get_current_user_id(), 'desktop_mode_mode', $enabled );
45
46 // Tell the client where to land.
47 //
48 // Enabling from classic admin: land directly on the Dashboard with
49 // the portal flag (`wp-admin/index.php?desktop_mode_portal=1`).
50 // Previously this redirected through `/desktop-mode/` so the
51 // portal handler could pick a landing page (saved-session focused
52 // window, `?target=`, or Dashboard fallback). That logic remains
53 // in place for users who visit `/desktop-mode/` directly — a
54 // bookmark or shared link — but the explicit "Switch to Desktop
55 // Mode" button is a deliberate user action that consistently
56 // lands on the Dashboard, so users get a predictable starting
57 // point regardless of what they did last session. The shell still
58 // honours session restore and the user's default-window pref via
59 // its own boot-time logic — the URL just provides a stable entry
60 // point rather than a portal hop.
61 //
62 // Disabling from the shell jumps to a plain admin URL — NOT the
63 // portal, which would auto-re-enable the mode via the
64 // `desktop_mode_portal_auto_enable` filter and trap the user in a
65 // loop.
66 $redirect = '1' === $enabled
67 ? admin_url( 'index.php?' . DESKTOP_MODE_PORTAL_FLAG . '=1' )
68 : admin_url();
69
70 wp_send_json_success(
71 array(
72 'enabled' => $enabled,
73 'redirect' => esc_url_raw( $redirect ),
74 )
75 );
76 }
77 add_action( 'wp_ajax_save-desktop-mode', 'desktop_mode_ajax_save' );
78