PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.6
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.6
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 / posts-window / permissions.php

permissions.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.6, at includes/posts-window/permissions.php

111 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since 0.8.0
20 */
21
22 defined( 'ABSPATH' ) || exit;
23
24 /**
25 * Whether the user is eligible to have the native Posts window
26 * REGISTERED for them at boot. Cap-only check — the opt-in toggle is
27 * a runtime, JS-side gate (handled by the URL → native-window remap
28 * registry) so that flipping the setting takes effect immediately,
29 * without forcing an F5.
30 *
31 * Why split: the registration runs once on `init` per page load. If
32 * we gated registration on the opt-in too, a user who toggled the
33 * setting on AFTER load would have the window unregistered for the
34 * remainder of the session, and the dock-click remap would fall
35 * through to the iframe path despite the setting being on.
36 * Registering eagerly costs nothing for opted-out users (the dock
37 * remap simply doesn't fire) and unblocks instant feedback.
38 *
39 * @since 0.8.0
40 *
41 * @param int|null $user_id Optional. Defaults to `get_current_user_id()`.
42 * @return bool
43 */
44 function desktop_mode_posts_window_user_can_register( $user_id = null ) {
45 $user_id = null === $user_id ? get_current_user_id() : (int) $user_id;
46
47 $can = $user_id > 0 && user_can( $user_id, 'edit_posts' );
48
49 /**
50 * Filter whether the current user is eligible to have the native
51 * Posts window registered. This is the boot-time check; runtime
52 * "should the dock click use the native window?" is the JS-side
53 * `nativePostsEnabled` flag.
54 *
55 * Returning `false` skips the entire registration — no script
56 * handle, no template, no entry in the native-window registry.
57 *
58 * @since 0.8.0
59 *
60 * @param bool $can Default: `edit_posts` capability.
61 * @param int $user_id User being checked.
62 */
63 return (bool) apply_filters(
64 'desktop_mode_posts_window_user_can_register',
65 $can,
66 $user_id
67 );
68 }
69
70 /**
71 * Whether the user has opted into the native Posts experience.
72 * Cap-and-opt-in check — kept for any caller that needs the combined
73 * answer (e.g. analytics, an arrange-menu entry). Boot registration
74 * uses {@see desktop_mode_posts_window_user_can_register()} instead;
75 * runtime dock-click swap uses the JS-side snapshot.
76 *
77 * @since 0.8.0
78 *
79 * @param int|null $user_id Optional. Defaults to `get_current_user_id()`.
80 * @return bool
81 */
82 function desktop_mode_posts_window_user_can_use( $user_id = null ) {
83 $user_id = null === $user_id ? get_current_user_id() : (int) $user_id;
84
85 $cap_ok = desktop_mode_posts_window_user_can_register( $user_id );
86
87 $opt_in = false;
88 if ( $cap_ok && function_exists( 'desktop_mode_get_os_settings' ) ) {
89 $settings = desktop_mode_get_os_settings( $user_id );
90 $opt_in = ! empty( $settings['nativePostsEnabled'] );
91 }
92
93 $can = $cap_ok && $opt_in;
94
95 /**
96 * Filter whether the current user has opted into the native Posts
97 * experience.
98 *
99 * Default: `edit_posts` AND the user has flipped the toggle in OS
100 * Settings → Features. Returning `false` does NOT prevent
101 * registration (toggle on/off remains live without F5); it only
102 * affects callers that ask the combined question.
103 *
104 * @since 0.8.0
105 *
106 * @param bool $can Default gate result.
107 * @param int $user_id User being checked.
108 */
109 return (bool) apply_filters( 'desktop_mode_posts_window_user_can_use', $can, $user_id );
110 }
111