| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — Native Comments Window: capability gates. |
| 4 |
* |
| 5 |
* Multi-tier gating, parallel to WordPress core's |
| 6 |
* `edit-comments.php` flow: |
| 7 |
* |
| 8 |
* - `edit_posts` → REGISTER the window. The cheapest classic-admin |
| 9 |
* cap that gates "can read the comments screen" |
| 10 |
* for any author (their own comments). The REST |
| 11 |
* controller does per-row checks. |
| 12 |
* - `moderate_comments` |
| 13 |
* → bulk approve / unapprove / spam / trash actions |
| 14 |
* and the global Pending tab badge. |
| 15 |
* - `edit_comment` → edit a comment's text (per-target, server-checked). |
| 16 |
* |
| 17 |
* UI-side gating is purely UX polish — the REST routes in `rest.php` |
| 18 |
* re-validate every cap and every per-target permission before |
| 19 |
* mutating anything. |
| 20 |
* |
| 21 |
* @package WPDesktopMode |
| 22 |
* @since 0.19.0 |
| 23 |
*/ |
| 24 |
|
| 25 |
defined( 'ABSPATH' ) || exit; |
| 26 |
|
| 27 |
/** |
| 28 |
* Whether the user is eligible to have the Comments window registered. |
| 29 |
* |
| 30 |
* @since 0.19.0 |
| 31 |
* |
| 32 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
function desktop_mode_comments_window_user_can_register( $user_id = null ) { |
| 36 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 37 |
$can = $user_id > 0 && user_can( $user_id, 'edit_posts' ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Filter whether the current user can have the Comments window registered. |
| 41 |
* |
| 42 |
* @since 0.19.0 |
| 43 |
* |
| 44 |
* @param bool $can Default: `edit_posts` capability. |
| 45 |
* @param int $user_id User being checked. |
| 46 |
*/ |
| 47 |
return (bool) apply_filters( |
| 48 |
'desktop_mode_comments_window_user_can_register', |
| 49 |
$can, |
| 50 |
$user_id |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Combined cap-and-opt-in check. |
| 56 |
* |
| 57 |
* @since 0.19.0 |
| 58 |
* |
| 59 |
* @param int|null $user_id Optional. |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
function desktop_mode_comments_window_user_can_use( $user_id = null ) { |
| 63 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 64 |
|
| 65 |
$cap_ok = desktop_mode_comments_window_user_can_register( $user_id ); |
| 66 |
|
| 67 |
$opt_in = false; |
| 68 |
if ( $cap_ok && function_exists( 'desktop_mode_get_os_settings' ) ) { |
| 69 |
$settings = desktop_mode_get_os_settings( $user_id ); |
| 70 |
$opt_in = ! empty( $settings['nativeCommentsEnabled'] ); |
| 71 |
} |
| 72 |
|
| 73 |
$can = $cap_ok && $opt_in; |
| 74 |
|
| 75 |
/** |
| 76 |
* Filter whether the current user has opted into the native Comments experience. |
| 77 |
* |
| 78 |
* @since 0.19.0 |
| 79 |
* |
| 80 |
* @param bool $can Default gate result. |
| 81 |
* @param int $user_id User being checked. |
| 82 |
*/ |
| 83 |
return (bool) apply_filters( |
| 84 |
'desktop_mode_comments_window_user_can_use', |
| 85 |
$can, |
| 86 |
$user_id |
| 87 |
); |
| 88 |
} |
| 89 |
|