|null */ private $values = null; /** * One setting. * * @since 0.1.0 * * @param string $key Setting name. * @param mixed $fallback Returned for an unknown key. * @return mixed */ public function get( string $key, $fallback = null ) { $values = $this->all(); return array_key_exists( $key, $values ) ? $values[ $key ] : $fallback; } /** * Every setting with defaults applied. * * @since 0.1.0 * * @return array */ public function all(): array { if ( null === $this->values ) { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); $values = array(); foreach ( Definitions::fields() as $key => $definition ) { $values[ $key ] = array_key_exists( $key, $saved ) ? $this->cast( $definition, $saved[ $key ] ) : $definition['default']; } $this->values = $values; } return $this->values; } /** * Validates and saves the given settings; settings not mentioned stay as they are. * * @since 0.1.0 * * @param array $input Raw input. * @return array{values: array, errors: array} */ public function update( array $input ): array { $fields = Definitions::fields(); $clean = array(); $errors = array(); foreach ( $input as $key => $raw ) { if ( ! is_string( $key ) || ! isset( $fields[ $key ] ) ) { continue; } $definition = $fields[ $key ]; $result = $this->sanitize_value( $definition, $raw ); if ( null !== $result['error'] ) { $errors[ $key ] = $result['error']; continue; } $clean[ $key ] = $result['value']; } if ( $errors ) { return array( 'values' => $this->all(), 'errors' => $errors, ); } $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); update_option( self::OPTION, array_merge( $saved, $clean ), true ); $this->values = null; return array( 'values' => $this->all(), 'errors' => array(), ); } /** * Settings for the admin app. * * @since 0.1.0 * * @return array */ public function export_for_app(): array { return $this->all(); } /** * Casts a stored value to the type of its definition. * * @param array $definition Definition. * @param mixed $value Stored value. * @return mixed */ private function cast( array $definition, $value ) { switch ( $definition['type'] ) { case 'bool': return (bool) $value; case 'int': case 'page': return (int) $value; default: return is_scalar( $value ) ? (string) $value : (string) $definition['default']; } } /** * Sanitizes and validates one value. * * @param array $definition Definition. * @param mixed $raw Raw value. * @return array{value:mixed,error:?string} */ private function sanitize_value( array $definition, $raw ): array { $max = isset( $definition['max'] ) ? (int) $definition['max'] : 0; switch ( $definition['type'] ) { case 'bool': return $this->ok( in_array( $raw, array( true, 1, '1', 'true', 'on', 'yes' ), true ) ); case 'int': if ( ! is_int( $raw ) && ! ( is_string( $raw ) && 1 === preg_match( '/^-?\d+$/', trim( $raw ) ) ) ) { return $this->fail( __( 'Enter a whole number.', 'interviewnea' ) ); } $value = (int) $raw; if ( isset( $definition['min'], $definition['max'] ) && ( $value < $definition['min'] || $value > $definition['max'] ) ) { /* translators: 1: minimum, 2: maximum. */ return $this->fail( sprintf( __( 'Enter a number from %1$d to %2$d.', 'interviewnea' ), $definition['min'], $definition['max'] ) ); } return $this->ok( $value ); case 'page': $value = is_scalar( $raw ) ? absint( $raw ) : 0; if ( $value && 'page' !== get_post_type( $value ) ) { return $this->fail( __( 'Choose a valid WordPress page.', 'interviewnea' ) ); } return $this->ok( $value ); case 'email': $text = trim( Helpers::scalar( $raw ) ); $value = '' === $text ? '' : sanitize_email( $text ); if ( '' !== $text && ( '' === $value || ! is_email( $value ) ) ) { return $this->fail( __( 'Enter a valid email address.', 'interviewnea' ) ); } return $this->ok( $value ); default: $value = sanitize_text_field( Helpers::scalar( $raw ) ); if ( $max && Helpers::length( $value ) > $max ) { /* translators: %d: maximum number of characters. */ return $this->fail( sprintf( __( 'Use at most %d characters.', 'interviewnea' ), $max ) ); } return $this->ok( $value ); } } /** * Success result. * * @param mixed $value Clean value. * @return array{value:mixed,error:?string} */ private function ok( $value ): array { return array( 'value' => $value, 'error' => null, ); } /** * Failure result. * * @param string $message Message. * @return array{value:mixed,error:?string} */ private function fail( string $message ): array { return array( 'value' => null, 'error' => $message, ); } }