| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — Native Posts Window: capability gate. |
| 4 |
* |
| 5 |
* The native Posts window is gated on TWO conditions, both required: |
| 6 |
* |
| 7 |
* 1. The current user can `edit_posts` (parity with `edit.php`). |
| 8 |
* 2. The user has flipped `nativePostsEnabled` on in OS Settings → |
| 9 |
* Features. This keeps the iframe path the default — the toggle |
| 10 |
* is purely additive. |
| 11 |
* |
| 12 |
* Filterable so plugins can: |
| 13 |
* - widen the gate to a custom role (return true for, say, contributor) |
| 14 |
* - close it on a per-user basis (force the iframe back on) |
| 15 |
* - bypass the opt-in entirely on a managed install ("everyone gets the |
| 16 |
* native window") |
| 17 |
* |
| 18 |
* @package WPDesktopMode |
| 19 |
*/ |
| 20 |
|
| 21 |
defined( 'ABSPATH' ) || exit; |
| 22 |
|
| 23 |
/** |
| 24 |
* Whether the user is eligible to have the native Posts window |
| 25 |
* REGISTERED for them at boot. Cap-only check — the opt-in toggle is |
| 26 |
* a runtime, JS-side gate (handled by the URL → native-window remap |
| 27 |
* registry) so that flipping the setting takes effect immediately, |
| 28 |
* without forcing an F5. |
| 29 |
* |
| 30 |
* Why split: the registration runs once on `init` per page load. If |
| 31 |
* we gated registration on the opt-in too, a user who toggled the |
| 32 |
* setting on AFTER load would have the window unregistered for the |
| 33 |
* remainder of the session, and the dock-click remap would fall |
| 34 |
* through to the iframe path despite the setting being on. |
| 35 |
* Registering eagerly costs nothing for opted-out users (the dock |
| 36 |
* remap simply doesn't fire) and unblocks instant feedback. |
| 37 |
* |
| 38 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
function desktop_mode_posts_window_user_can_register( $user_id = null ) { |
| 42 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 43 |
|
| 44 |
$can = $user_id > 0 && user_can( $user_id, 'edit_posts' ); |
| 45 |
|
| 46 |
/** |
| 47 |
* Filter whether the current user is eligible to have the native |
| 48 |
* Posts window registered. This is the boot-time check; runtime |
| 49 |
* "should the dock click use the native window?" is the JS-side |
| 50 |
* `nativePostsEnabled` flag. |
| 51 |
* |
| 52 |
* Returning `false` skips the entire registration — no script |
| 53 |
* handle, no template, no entry in the native-window registry. |
| 54 |
* |
| 55 |
* @param bool $can Default: `edit_posts` capability. |
| 56 |
* @param int $user_id User being checked. |
| 57 |
*/ |
| 58 |
return (bool) apply_filters( |
| 59 |
'desktop_mode_posts_window_user_can_register', |
| 60 |
$can, |
| 61 |
$user_id |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Whether the user has opted into the native Posts experience. |
| 67 |
* Cap-and-opt-in check — kept for any caller that needs the combined |
| 68 |
* answer (e.g. analytics, an arrange-menu entry). Boot registration |
| 69 |
* uses {@see desktop_mode_posts_window_user_can_register()} instead; |
| 70 |
* runtime dock-click swap uses the JS-side snapshot. |
| 71 |
* |
| 72 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
function desktop_mode_posts_window_user_can_use( $user_id = null ) { |
| 76 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 77 |
|
| 78 |
$cap_ok = desktop_mode_posts_window_user_can_register( $user_id ); |
| 79 |
|
| 80 |
$opt_in = false; |
| 81 |
if ( $cap_ok && function_exists( 'desktop_mode_get_os_settings' ) ) { |
| 82 |
$settings = desktop_mode_get_os_settings( $user_id ); |
| 83 |
$opt_in = ! empty( $settings['nativePostsEnabled'] ); |
| 84 |
} |
| 85 |
|
| 86 |
$can = $cap_ok && $opt_in; |
| 87 |
|
| 88 |
/** |
| 89 |
* Filter whether the current user has opted into the native Posts |
| 90 |
* experience. |
| 91 |
* |
| 92 |
* Default: `edit_posts` AND the user has flipped the toggle in OS |
| 93 |
* Settings → Features. Returning `false` does NOT prevent |
| 94 |
* registration (toggle on/off remains live without F5); it only |
| 95 |
* affects callers that ask the combined question. |
| 96 |
* |
| 97 |
* @param bool $can Default gate result. |
| 98 |
* @param int $user_id User being checked. |
| 99 |
*/ |
| 100 |
return (bool) apply_filters( 'desktop_mode_posts_window_user_can_use', $can, $user_id ); |
| 101 |
} |
| 102 |
|