| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Edit app — 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 routes in `rest.php` apply |
| 9 |
* the same check before returning data. |
| 10 |
* |
| 11 |
* @package OpenStation |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Whether the user is eligible to have the User Edit window registered. |
| 18 |
* |
| 19 |
* Defaults to `true` for any logged-in user. Returning `false` from |
| 20 |
* the filter disables registration entirely, which falls back to the |
| 21 |
* classic `user-edit.php` / `profile.php` iframe path. |
| 22 |
* |
| 23 |
* @param int|null $user_id Optional. Defaults to `get_current_user_id()`. |
| 24 |
* @return bool |
| 25 |
*/ |
| 26 |
function openstation_user_edit_window_user_can_register( $user_id = null ) { |
| 27 |
$user_id = null === $user_id ? get_current_user_id() : (int) $user_id; |
| 28 |
$can = $user_id > 0; |
| 29 |
|
| 30 |
/** |
| 31 |
* Filter whether the current user can have the User Edit window |
| 32 |
* registered. |
| 33 |
* |
| 34 |
* @param bool $can Default: any logged-in user. |
| 35 |
* @param int $user_id User being checked. |
| 36 |
*/ |
| 37 |
return (bool) apply_filters( 'openstation_user_edit_window_user_can_register', $can, $user_id ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Whether `$viewer_id` may edit `$target_id`'s profile. Server-side |
| 42 |
* canonical check used by the routes and any plugin code that wants |
| 43 |
* to mirror the gating. |
| 44 |
* |
| 45 |
* @param int $viewer_id Viewer. |
| 46 |
* @param int $target_id Target. |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
function openstation_user_edit_window_can_edit( $viewer_id, $target_id ) { |
| 50 |
$viewer_id = (int) $viewer_id; |
| 51 |
$target_id = (int) $target_id; |
| 52 |
if ( $viewer_id <= 0 || $target_id <= 0 ) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
return (bool) user_can( $viewer_id, 'edit_user', $target_id ); |
| 56 |
} |
| 57 |
|