| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\SiteNavigation\Rest_Fields; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Page_User_Can { |
| 10 |
public function register_rest_field() { |
| 11 |
if ( ! isset( $_GET['_fields'] ) ) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
$fields = sanitize_text_field( wp_unslash( $_GET['_fields'] ) ); |
| 16 |
$array_fields = explode( ',', $fields ); |
| 17 |
|
| 18 |
if ( ! in_array( 'user_can', $array_fields ) ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
register_rest_field( |
| 23 |
'page', |
| 24 |
'user_can', |
| 25 |
[ |
| 26 |
'get_callback' => [ $this, 'get_callback' ], |
| 27 |
'schema' => [ |
| 28 |
'description' => __( 'Whether the current user can edit or delete this post', 'elementor' ), |
| 29 |
'type' => 'array', |
| 30 |
], |
| 31 |
] |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function get_callback( $post ) { |
| 36 |
$can_edit = current_user_can( 'edit_post', $post['id'] ); |
| 37 |
$can_delete = current_user_can( 'delete_post', $post['id'] ); |
| 38 |
|
| 39 |
return [ |
| 40 |
'edit' => $can_edit, |
| 41 |
'delete' => $can_delete, |
| 42 |
]; |
| 43 |
} |
| 44 |
} |
| 45 |
|