*/ public static function defaults(): array { return array( 'mode' => 'wordpress', 'host' => '', 'port' => 587, 'encryption' => 'tls', 'auth' => true, 'username' => '', 'password_locked' => '', 'from_email' => '', 'from_name' => '', ); } /** * Cleans values before they are saved. * * @param array $values Values. * @return array */ public static function sanitize( array $values ): array { $values = array_merge( self::defaults(), $values ); $host = strtolower( trim( (string) $values['host'] ) ); $email = sanitize_email( (string) $values['from_email'] ); $clean = array( 'mode' => 'smtp' === $values['mode'] ? 'smtp' : 'wordpress', // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- A setting value, not text. 'host' => 1 === preg_match( '/^[a-z0-9]([a-z0-9.-]{0,251}[a-z0-9])?$/', $host ) ? $host : '', 'port' => min( 65535, max( 1, (int) $values['port'] ) ), 'encryption' => in_array( $values['encryption'], array( 'none', 'tls', 'ssl' ), true ) ? (string) $values['encryption'] : 'tls', 'auth' => (bool) $values['auth'], 'username' => substr( sanitize_text_field( (string) $values['username'] ), 0, 255 ), 'password_locked' => (bool) $values['auth'] ? preg_replace( '/[^A-Za-z0-9+\/=]/', '', (string) $values['password_locked'] ) : '', 'from_email' => is_email( $email ) ? $email : '', 'from_name' => substr( sanitize_text_field( (string) $values['from_name'] ), 0, 100 ), ); if ( '' === $clean['host'] ) { $clean['mode'] = 'wordpress'; // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- A setting value, not text. } return $clean; } /** * What the screen may see: everything except the password, plus whether one is saved. * * @param array $values Saved values. * @return array */ public static function view( array $values ): array { $has_password = '' !== (string) ( $values['password_locked'] ?? '' ); unset( $values['password_locked'] ); $values['password_set'] = $has_password; return $values; } }