| 1 |
<?php |
| 2 |
/** |
| 3 |
* Desktop Mode — Native User Edit Window: capability gates. |
| 4 |
* |
| 5 |
* The window is registered for ANY logged-in user (everyone has a |
| 6 |
* profile they can edit). Per-target capability is re-checked at |
| 7 |
* REST time — saving uses core's `/wp/v2/users/<id>` PUT, which |
| 8 |
* already enforces `edit_user, $id`; the insights endpoint here |
| 9 |
* applies the same check before returning data. |
| 10 |
* |
| 11 |
* @package WPDesktopMode |
| 12 |
* @since 0.8.1 |
| 13 |
*/ |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* Whether the user is eligible to have the User Edit window registered. |
| 19 |
* |
| 20 |
* Defaults to `true` for any logged-in user — everyone has at least |
| 21 |
* their own profile to edit. Returning `false` from the filter |
| 22 |
* disables registration entirely, which falls back to the classic |
| 23 |
* `user-edit.php` / `profile.php` iframe path. |
| 24 |
* |
| 25 |
* @since 0.8.1 |
| 26 |
* |
| 27 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
function desktop_mode_user_edit_window_user_can_register( $user_id = null ) { |
| 31 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 32 |
$can = $user_id > 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* Filter whether the current user can have the User Edit window |
| 36 |
* registered. |
| 37 |
* |
| 38 |
* @since 0.8.1 |
| 39 |
* |
| 40 |
* @param bool $can Default: any logged-in user. |
| 41 |
* @param int $user_id User being checked. |
| 42 |
*/ |
| 43 |
return (bool) apply_filters( |
| 44 |
'desktop_mode_user_edit_window_user_can_register', |
| 45 |
$can, |
| 46 |
$user_id |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Whether `$viewer_id` may edit `$target_id`'s profile. Server-side |
| 52 |
* canonical check used by the insights endpoint and any plugin code |
| 53 |
* that wants to mirror the gating. |
| 54 |
* |
| 55 |
* @since 0.8.1 |
| 56 |
* |
| 57 |
* @param int $viewer_id |
| 58 |
* @param int $target_id |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
function desktop_mode_user_edit_window_can_edit( $viewer_id, $target_id ) { |
| 62 |
$viewer_id = (int) $viewer_id; |
| 63 |
$target_id = (int) $target_id; |
| 64 |
if ( $viewer_id <= 0 || $target_id <= 0 ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
return (bool) user_can( $viewer_id, 'edit_user', $target_id ); |
| 68 |
} |
| 69 |
|