| 1 |
<?php |
| 2 |
/** |
| 3 |
* Posts app — the capability gates. |
| 4 |
* |
| 5 |
* The Posts window answers two questions, each filterable: |
| 6 |
* |
| 7 |
* 1. May the app be registered for this user? Cap-only (`edit_posts`, |
| 8 |
* parity with `edit.php`) — the app's `can()` gate. |
| 9 |
* 2. Has the user opted into the native window? Cap AND the |
| 10 |
* `nativePostsEnabled` toggle in OS Settings → Features — the |
| 11 |
* combined answer for any caller that wants it; the dock-click |
| 12 |
* remap reads the toggle from the JS-side settings snapshot. |
| 13 |
* |
| 14 |
* Filterable so plugins can widen the gate to a custom role, close it |
| 15 |
* per user (force the iframe back on), or bypass the opt-in on a |
| 16 |
* managed install ("everyone gets the native window"). |
| 17 |
* |
| 18 |
* @package OpenStation |
| 19 |
*/ |
| 20 |
|
| 21 |
defined( 'ABSPATH' ) || exit; |
| 22 |
|
| 23 |
/** |
| 24 |
* Whether the user is eligible to have the Posts app registered. |
| 25 |
* Cap-only: the opt-in toggle is a runtime, JS-side gate (the URL → |
| 26 |
* app remap registry), so flipping the setting takes effect at once, |
| 27 |
* without an F5. |
| 28 |
* |
| 29 |
* Why split: registration runs once on `init` per page load. Gating |
| 30 |
* it on the opt-in too would leave a user who toggles the setting on |
| 31 |
* AFTER load with no app for the rest of the session, and the |
| 32 |
* dock-click remap would fall through to the iframe path despite the |
| 33 |
* setting being on. Registering eagerly costs nothing for opted-out |
| 34 |
* users (the remap simply doesn't fire). |
| 35 |
* |
| 36 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
function openstation_posts_window_user_can_register( $user_id = null ) { |
| 40 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 41 |
|
| 42 |
$can = $user_id > 0 && user_can( $user_id, 'edit_posts' ); |
| 43 |
|
| 44 |
/** |
| 45 |
* Filter whether the current user is eligible to have the Posts |
| 46 |
* app registered. This is the boot-time check; runtime "should the |
| 47 |
* dock click use the native window?" is the JS-side |
| 48 |
* `nativePostsEnabled` flag. |
| 49 |
* |
| 50 |
* Returning `false` skips the whole registration — no manifest, no |
| 51 |
* entry in the apps registry. |
| 52 |
* |
| 53 |
* @param bool $can Default: `edit_posts` capability. |
| 54 |
* @param int $user_id User being checked. |
| 55 |
*/ |
| 56 |
return (bool) apply_filters( |
| 57 |
'openstation_posts_window_user_can_register', |
| 58 |
$can, |
| 59 |
$user_id |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Whether the user has opted into the native Posts experience: the |
| 65 |
* cap AND the toggle. The combined answer for any caller that needs |
| 66 |
* it (analytics, an arrange-menu entry). Registration uses |
| 67 |
* {@see openstation_posts_window_user_can_register()}; the dock-click |
| 68 |
* remap reads the JS-side settings snapshot. |
| 69 |
* |
| 70 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
function openstation_posts_window_user_can_use( $user_id = null ) { |
| 74 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 75 |
|
| 76 |
$cap_ok = openstation_posts_window_user_can_register( $user_id ); |
| 77 |
|
| 78 |
$opt_in = false; |
| 79 |
if ( $cap_ok && function_exists( 'openstation_get_os_settings' ) ) { |
| 80 |
$settings = openstation_get_os_settings( $user_id ); |
| 81 |
$opt_in = ! empty( $settings['nativePostsEnabled'] ); |
| 82 |
} |
| 83 |
|
| 84 |
$can = $cap_ok && $opt_in; |
| 85 |
|
| 86 |
/** |
| 87 |
* Filter whether the current user has opted into the native Posts |
| 88 |
* experience. |
| 89 |
* |
| 90 |
* Default: `edit_posts` AND the user has flipped the toggle in OS |
| 91 |
* Settings → Features. Returning `false` does NOT prevent |
| 92 |
* registration (toggle on/off remains live without F5); it only |
| 93 |
* affects callers that ask the combined question. |
| 94 |
* |
| 95 |
* @param bool $can Default gate result. |
| 96 |
* @param int $user_id User being checked. |
| 97 |
*/ |
| 98 |
return (bool) apply_filters( 'openstation_posts_window_user_can_use', $can, $user_id ); |
| 99 |
} |
| 100 |
|