| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — Native Pages Window: capability gate. |
| 4 |
* |
| 5 |
* The native Pages window is gated on TWO conditions, both required: |
| 6 |
* |
| 7 |
* 1. The current user can `edit_pages` (parity with `edit.php?post_type=page`). |
| 8 |
* 2. The user has `nativePagesEnabled` on in OS Settings → Features. |
| 9 |
* The toggle is purely additive — flipped off, the iframe path is |
| 10 |
* restored without F5. |
| 11 |
* |
| 12 |
* Filterable so plugins can: |
| 13 |
* - widen the gate to a custom role |
| 14 |
* - close it on a per-user basis (force the iframe back on) |
| 15 |
* - bypass the opt-in entirely on a managed install |
| 16 |
* |
| 17 |
* @package OpenStation |
| 18 |
*/ |
| 19 |
|
| 20 |
defined( 'ABSPATH' ) || exit; |
| 21 |
|
| 22 |
/** |
| 23 |
* Whether the user is eligible to have the native Pages window |
| 24 |
* REGISTERED for them at boot. Cap-only check — the opt-in toggle is |
| 25 |
* a runtime, JS-side gate (handled by the URL → native-window remap |
| 26 |
* registry) so flipping the setting takes effect immediately. |
| 27 |
* |
| 28 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
function openstation_pages_window_user_can_register( $user_id = null ) { |
| 32 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 33 |
|
| 34 |
$can = $user_id > 0 && user_can( $user_id, 'edit_pages' ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Filter whether the current user is eligible to have the native |
| 38 |
* Pages window registered. This is the boot-time check; runtime |
| 39 |
* "should the dock click use the native window?" is the JS-side |
| 40 |
* `nativePagesEnabled` flag. |
| 41 |
* |
| 42 |
* @param bool $can Default: `edit_pages` capability. |
| 43 |
* @param int $user_id User being checked. |
| 44 |
*/ |
| 45 |
return (bool) apply_filters( |
| 46 |
'openstation_pages_window_user_can_register', |
| 47 |
$can, |
| 48 |
$user_id |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Combined cap-and-opt-in check. Boot registration uses |
| 54 |
* {@see openstation_pages_window_user_can_register()}; the JS-side |
| 55 |
* remap reads the OS-settings snapshot directly. This helper is for |
| 56 |
* any caller that wants the combined answer. |
| 57 |
* |
| 58 |
* @param int|null $user_id Optional. |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
function openstation_pages_window_user_can_use( $user_id = null ) { |
| 62 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 63 |
|
| 64 |
$cap_ok = openstation_pages_window_user_can_register( $user_id ); |
| 65 |
|
| 66 |
$opt_in = false; |
| 67 |
if ( $cap_ok && function_exists( 'openstation_get_os_settings' ) ) { |
| 68 |
$settings = openstation_get_os_settings( $user_id ); |
| 69 |
$opt_in = ! empty( $settings['nativePagesEnabled'] ); |
| 70 |
} |
| 71 |
|
| 72 |
$can = $cap_ok && $opt_in; |
| 73 |
|
| 74 |
/** |
| 75 |
* Filter whether the current user has opted into the native Pages |
| 76 |
* experience. |
| 77 |
* |
| 78 |
* @param bool $can Default gate result. |
| 79 |
* @param int $user_id User being checked. |
| 80 |
*/ |
| 81 |
return (bool) apply_filters( 'openstation_pages_window_user_can_use', $can, $user_id ); |
| 82 |
} |
| 83 |
|