| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation AJAX endpoints. |
| 4 |
* |
| 5 |
* @package OpenStation |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Handles saving the user's OpenStation preference via AJAX. |
| 12 |
*/ |
| 13 |
function openstation_ajax_save() { |
| 14 |
check_ajax_referer( 'save-openstation', 'nonce' ); |
| 15 |
|
| 16 |
// A valid nonce proves *this* request was authored by the current |
| 17 |
// user, but WP's cap system is the authoritative gate for "is this |
| 18 |
// account allowed to touch admin state at all". `read` is the |
| 19 |
// minimum cap every admin-visible role carries; subscribers on sites |
| 20 |
// that revoke it have no business flipping an admin-UI preference. |
| 21 |
if ( ! current_user_can( 'read' ) ) { |
| 22 |
wp_send_json_error( 'openstation_forbidden', 403 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Filters whether OpenStation is available for this user. |
| 27 |
* |
| 28 |
* Plugins can disable OpenStation for certain roles, capabilities, or conditions. |
| 29 |
* |
| 30 |
* @param bool $enabled Whether OpenStation is enabled. Default true. |
| 31 |
* @param int $user_id The current user ID. |
| 32 |
*/ |
| 33 |
$allowed = apply_filters( 'openstation_mode_enabled', true, get_current_user_id() ); |
| 34 |
if ( ! $allowed ) { |
| 35 |
wp_send_json_error( 'openstation_disabled' ); |
| 36 |
} |
| 37 |
|
| 38 |
$enabled = ! empty( $_POST['enabled'] ) && '1' === $_POST['enabled'] ? '1' : ''; |
| 39 |
|
| 40 |
update_user_meta( get_current_user_id(), 'desktop_mode_mode', $enabled ); |
| 41 |
|
| 42 |
// Tell the client where to land. |
| 43 |
// |
| 44 |
// Enabling from classic admin: land on the shell screen with the |
| 45 |
// Dashboard as the page it opens first. The explicit "Switch to |
| 46 |
// Desktop Mode" button is a deliberate user action that |
| 47 |
// consistently lands on the Dashboard, so users get a predictable |
| 48 |
// starting point regardless of what they did last session; the |
| 49 |
// shell still honours session restore and the user's default-window |
| 50 |
// pref via its own boot-time logic. Going to the screen directly |
| 51 |
// rather than through `/openstation/` skips a hop the portal would |
| 52 |
// spend re-deciding what this URL already says. |
| 53 |
// |
| 54 |
// Disabling from the shell jumps to a plain admin URL — NOT the |
| 55 |
// portal, which would auto-re-enable the mode via the |
| 56 |
// `openstation_portal_auto_enable` filter and trap the user in a |
| 57 |
// loop. |
| 58 |
// Which admin to land in. The toggle reports its own context, since |
| 59 |
// `is_network_admin()` is false on every `admin-ajax.php` request, |
| 60 |
// and we confirm the capability before honouring it — a client |
| 61 |
// cannot talk us into a screen the user can't open. |
| 62 |
$in_network = ! empty( $_POST['network'] ) && current_user_can( 'manage_network' ); |
| 63 |
$admin = $in_network ? network_admin_url() : admin_url(); |
| 64 |
$redirect = '1' === $enabled |
| 65 |
? openstation_shell_url( $admin . 'index.php', false, $in_network ) |
| 66 |
: $admin; |
| 67 |
|
| 68 |
wp_send_json_success( |
| 69 |
array( |
| 70 |
'enabled' => $enabled, |
| 71 |
'redirect' => esc_url_raw( $redirect ), |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
add_action( 'wp_ajax_save-openstation', 'openstation_ajax_save' ); |
| 76 |
|