current_user_can( Capabilities::MANAGE_SETTINGS ); register_rest_route( Api::NAMESPACE, '/mail', array( array( 'methods' => 'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => $can, ), array( 'methods' => 'POST', 'callback' => array( $this, 'save' ), 'permission_callback' => $can, ), ) ); register_rest_route( Api::NAMESPACE, '/mail/test', array( 'methods' => 'POST', 'callback' => array( $this, 'test' ), 'permission_callback' => $can, ) ); } /** * The settings, the other mail plugins and the last test. */ public function show(): \WP_REST_Response { return new \WP_REST_Response( array( 'values' => MailConfig::view( $this->settings->all( MailConfig::SCOPE ) ), 'others' => OtherMailers::active(), 'last' => get_option( TestMail::LAST, null ), ) ); } /** * Saves the settings. * * @param \WP_REST_Request $request Request. * @phpstan-param \WP_REST_Request> $request */ public function save( \WP_REST_Request $request ): \WP_REST_Response { $params = (array) $request->get_json_params(); $values = array(); foreach ( array( 'mode', 'host', 'port', 'encryption', 'auth', 'username', 'from_email', 'from_name' ) as $key ) { if ( array_key_exists( $key, $params ) ) { $values[ $key ] = $params[ $key ]; } } $password = isset( $params['password'] ) && is_string( $params['password'] ) ? $params['password'] : ''; if ( '' !== $password ) { $values['password_locked'] = $this->vault->lock( $password ); } if ( ! empty( $params['clear_password'] ) ) { $values['password_locked'] = ''; } $this->settings->update( MailConfig::SCOPE, $values ); return new \WP_REST_Response( array( 'ok' => true, 'message' => __( 'Email settings saved.', 'forensix-digital' ), 'values' => MailConfig::view( $this->settings->all( MailConfig::SCOPE ) ), ) ); } /** * Sends a test email to the current user. */ public function test(): \WP_REST_Response { $user = wp_get_current_user(); $result = TestMail::send( $user->user_email ); return new \WP_REST_Response( $result ); } }