namespace, '/register-site', [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [ $this, 'register_site' ], 'permission_callback' => [ $this, 'check_admin_permissions' ], 'args' => [ 'name' => [ 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], 'email' => [ 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_email', ], 'site_url' => [ 'type' => 'string', 'required' => true, 'sanitize_callback' => 'esc_url_raw', ], ], ] ); } /** * Check admin permissions * * @return bool|WP_Error */ public function check_admin_permissions() { if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'aisentic_forbidden', __( 'You do not have permission to manage settings.', 'aisentic' ), [ 'status' => 403 ] ); } return true; } /** * Register site with the aisentic provider * * @param WP_REST_Request $request Request object. * @return \WP_HTTP_Response|\WP_Error */ public function register_site( WP_REST_Request $request ) { $result = Registration_Service::register( $request->get_param( 'name' ), $request->get_param( 'email' ), $request->get_param( 'site_url' ) ); if ( is_wp_error( $result ) ) { $status = $result->get_error_data(); $code = is_array( $status ) && ! empty( $status['status'] ) ? (int) $status['status'] : 500; return $this->error( $result->get_error_message(), $code ); } /* * Nothing to do on success but hand back what the service returned. It has * already stored the api key and fired `aisentic/site_registered`; the HTTP * handling that used to live here moved into it when it was extracted. */ return $this->response( [ 'api_key' => $result['api_key'], 'monthly_limit' => $result['monthly_limit'], ], $result['message'] ); } }