api = $api; $this->logger = $logger; add_action( 'rest_api_init', array( $this, 'routes' ) ); } public function routes() : void { $permission = static fn() => current_user_can( 'manage_options' ); register_rest_route( 'authkey/v1', '/connection', array( 'methods' => 'POST', 'callback' => fn() => $this->respond( $this->api->profile() ), 'permission_callback' => $permission, ) ); register_rest_route( 'authkey/v1', '/templates/refresh', array( 'methods' => 'POST', 'callback' => fn() => $this->respond( $this->api->templates( true ) ), 'permission_callback' => $permission, ) ); register_rest_route( 'authkey/v1', '/logs', array( 'methods' => 'GET', 'callback' => fn() => rest_ensure_response( $this->logger->recent( 100 ) ), 'permission_callback' => $permission, ) ); register_rest_route( 'authkey/v1', '/test-message', array( 'methods' => 'POST', 'callback' => array( $this, 'test_message' ), 'permission_callback' => $permission, 'args' => array( 'mobile' => array( 'required' => true, 'sanitize_callback' => static function( $value ) : string { return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : ''; }, ), 'wid' => array( 'required' => true, 'sanitize_callback' => 'absint', ), 'values' => array( 'sanitize_callback' => static function( $value ) : array { return is_array( $value ) ? $value : array(); }, ), ), ) ); } public function test_message( \WP_REST_Request $request ) { $values = $request->get_param( 'values' ); $values = is_array( $values ) ? $values : array(); return $this->respond( $this->api->send( (string) $request->get_param( 'mobile' ), absint( $request->get_param( 'wid' ) ), $values ) ); } private function respond( array|\WP_Error $result ) { return is_wp_error( $result ) ? $result : rest_ensure_response( $result ); } }