|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace Escalated\Admin; |
3 | 4 |
|
4 | 5 | use Escalated\Models\ApiToken; |
5 | 6 |
|
6 | | -class Admin_Api_Tokens { |
7 | | - |
8 | | - public function __construct() { |
9 | | - add_action( 'admin_init', [ $this, 'handle_actions' ] ); |
| 7 | +class Admin_Api_Tokens |
| 8 | +{ |
| 9 | + public function __construct() |
| 10 | + { |
| 11 | + add_action('admin_init', [$this, 'handle_actions']); |
10 | 12 | } |
11 | 13 |
|
12 | 14 | /** |
13 | 15 | * Render the API tokens admin page. |
14 | 16 | */ |
15 | | - public function render(): void { |
16 | | - $tokens = ApiToken::all(); |
| 17 | + public function render(): void |
| 18 | + { |
| 19 | + $tokens = ApiToken::all(); |
17 | 20 | $plain_token = null; |
18 | | - $message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : ''; |
| 21 | + $message = isset($_GET['message']) ? sanitize_text_field(wp_unslash($_GET['message'])) : ''; |
19 | 22 |
|
20 | 23 | // Check transient for newly created plain token. |
21 | | - $token_transient = get_transient( 'escalated_new_token_' . get_current_user_id() ); |
22 | | - if ( $token_transient ) { |
| 24 | + $token_transient = get_transient('escalated_new_token_'.get_current_user_id()); |
| 25 | + if ($token_transient) { |
23 | 26 | $plain_token = $token_transient; |
24 | | - delete_transient( 'escalated_new_token_' . get_current_user_id() ); |
| 27 | + delete_transient('escalated_new_token_'.get_current_user_id()); |
25 | 28 | } |
26 | 29 |
|
27 | | - include ESCALATED_PLUGIN_DIR . 'templates/admin/api-tokens.php'; |
| 30 | + include ESCALATED_PLUGIN_DIR.'templates/admin/api-tokens.php'; |
28 | 31 | } |
29 | 32 |
|
30 | 33 | /** |
31 | 34 | * Handle POST actions: create, delete. |
32 | 35 | */ |
33 | | - public function handle_actions(): void { |
34 | | - if ( ! isset( $_POST['escalated_token_action'] ) ) { |
| 36 | + public function handle_actions(): void |
| 37 | + { |
| 38 | + if (! isset($_POST['escalated_token_action'])) { |
35 | 39 | return; |
36 | 40 | } |
37 | 41 |
|
38 | | - if ( ! current_user_can( 'escalated_manage_api_tokens' ) ) { |
39 | | - wp_die( esc_html__( 'Permission denied.', 'escalated' ) ); |
| 42 | + if (! current_user_can('escalated_manage_api_tokens')) { |
| 43 | + wp_die(esc_html__('Permission denied.', 'escalated')); |
40 | 44 | } |
41 | 45 |
|
42 | | - $action = sanitize_text_field( wp_unslash( $_POST['escalated_token_action'] ) ); |
43 | | - $redirect = admin_url( 'admin.php?page=escalated-api-tokens' ); |
| 46 | + $action = sanitize_text_field(wp_unslash($_POST['escalated_token_action'])); |
| 47 | + $redirect = admin_url('admin.php?page=escalated-api-tokens'); |
44 | 48 |
|
45 | | - switch ( $action ) { |
| 49 | + switch ($action) { |
46 | 50 | case 'create': |
47 | | - if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_escalated_nonce'] ?? '' ) ), 'escalated_token_create' ) ) { |
48 | | - wp_die( esc_html__( 'Security check failed.', 'escalated' ) ); |
| 51 | + if (! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_escalated_nonce'] ?? '')), 'escalated_token_create')) { |
| 52 | + wp_die(esc_html__('Security check failed.', 'escalated')); |
49 | 53 | } |
50 | 54 |
|
51 | | - $name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ); |
52 | | - $abilities = isset( $_POST['abilities'] ) ? array_map( 'sanitize_text_field', (array) $_POST['abilities'] ) : [ '*' ]; |
| 55 | + $name = sanitize_text_field(wp_unslash($_POST['name'] ?? '')); |
| 56 | + $abilities = isset($_POST['abilities']) ? array_map('sanitize_text_field', (array) $_POST['abilities']) : ['*']; |
53 | 57 |
|
54 | 58 | $expires_at = null; |
55 | | - if ( ! empty( $_POST['expires_at'] ) ) { |
56 | | - $expires_at = sanitize_text_field( wp_unslash( $_POST['expires_at'] ) ); |
| 59 | + if (! empty($_POST['expires_at'])) { |
| 60 | + $expires_at = sanitize_text_field(wp_unslash($_POST['expires_at'])); |
57 | 61 | } |
58 | 62 |
|
59 | | - $result = ApiToken::create_token( get_current_user_id(), $name, $abilities ); |
| 63 | + $result = ApiToken::create_token(get_current_user_id(), $name, $abilities); |
60 | 64 |
|
61 | | - if ( $result ) { |
| 65 | + if ($result) { |
62 | 66 | // Store plain token temporarily so it can be displayed once. |
63 | | - set_transient( 'escalated_new_token_' . get_current_user_id(), $result['token'], 60 ); |
| 67 | + set_transient('escalated_new_token_'.get_current_user_id(), $result['token'], 60); |
64 | 68 |
|
65 | | - if ( $expires_at ) { |
66 | | - ApiToken::update( $result['record']->id, [ 'expires_at' => $expires_at ] ); |
| 69 | + if ($expires_at) { |
| 70 | + ApiToken::update($result['record']->id, ['expires_at' => $expires_at]); |
67 | 71 | } |
68 | 72 |
|
69 | | - $redirect = add_query_arg( 'message', 'created', $redirect ); |
| 73 | + $redirect = add_query_arg('message', 'created', $redirect); |
70 | 74 | } else { |
71 | | - $redirect = add_query_arg( 'message', 'error', $redirect ); |
| 75 | + $redirect = add_query_arg('message', 'error', $redirect); |
72 | 76 | } |
73 | 77 | break; |
74 | 78 |
|
75 | 79 | case 'delete': |
76 | | - $id = absint( $_POST['id'] ?? 0 ); |
77 | | - if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_escalated_nonce'] ?? '' ) ), 'escalated_token_delete_' . $id ) ) { |
78 | | - wp_die( esc_html__( 'Security check failed.', 'escalated' ) ); |
| 80 | + $id = absint($_POST['id'] ?? 0); |
| 81 | + if (! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_escalated_nonce'] ?? '')), 'escalated_token_delete_'.$id)) { |
| 82 | + wp_die(esc_html__('Security check failed.', 'escalated')); |
79 | 83 | } |
80 | 84 |
|
81 | | - ApiToken::delete( $id ); |
82 | | - $redirect = add_query_arg( 'message', 'deleted', $redirect ); |
| 85 | + ApiToken::delete($id); |
| 86 | + $redirect = add_query_arg('message', 'deleted', $redirect); |
83 | 87 | break; |
84 | 88 | } |
85 | 89 |
|
86 | | - wp_safe_redirect( $redirect ); |
| 90 | + wp_safe_redirect($redirect); |
87 | 91 | exit; |
88 | 92 | } |
89 | 93 | } |
0 commit comments