PluginProbe
پینوا | Pinova / trunk
پینوا | Pinova vtrunk
pinova / src / API / AdminAPI.php

AdminAPI.php in پینوا | Pinova trunk, at src/API/AdminAPI.php

76 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace Pinova\API;
5
6 use Exception;
7 use Pinova\Channels\SMS;
8 use Pinova\Objects\Identifier;
9 use Pinova\Services\OTPService;
10 use Pinova\Services\UserService;
11 use Pinova\Services\ValidationService;
12 use WP_REST_Request;
13
14 defined( 'ABSPATH' ) || exit;
15
16 class AdminAPI extends RestAPI {
17
18 public const ROUTE_PERMISSION = [
19 '/pinova/admin/test/sms' => 'manage_options',
20 ];
21
22 public function register_routes() {
23
24 register_rest_route( 'pinova/admin/test', '/sms', [
25 'methods' => 'POST',
26 'callback' => [ $this, 'test_sms' ],
27 'permission_callback' => [ $this, 'permission_callback' ],
28 'args' => [
29 'identifier' => [
30 'required' => true,
31 'validate_callback' => [ ValidationService::class, 'identifier' ],
32 ],
33 ],
34 ] );
35
36 }
37
38 /**
39 * @param WP_REST_Request $request
40 *
41 * @return void
42 */
43 public function test_sms( WP_REST_Request $request ): void {
44
45 /** @var Identifier $identifier */
46 $identifier = $request->get_param( 'identifier' );
47
48 $code = OTPService::generate_code();
49
50 try {
51
52 SMS::send_code( $identifier->get_value(), $code );
53
54 $message = sprintf( "کد تایید «%d» با �
55 وفقیت پیا�
56 ک شد.", $code );
57 $success = true;
58
59 } catch ( Exception $e ) {
60 $message = $e->getMessage();
61 $success = false;
62 }
63
64 self::response( $success, $message );
65 }
66
67 /**
68 * @param WP_REST_Request $request
69 *
70 * @return bool
71 */
72 public function permission_callback( WP_REST_Request $request ): bool {
73 return current_user_can( self::ROUTE_PERMISSION[ $request->get_route() ] ?? 'manage_options' );
74 }
75 }
76