*/ public static function defaults() { return array( 'enabled' => true, 'product_mode' => 'icon', 'inline_start' => '3d', 'trigger_position' => 'top-right', 'button_placement' => 'after-cta', 'button_text' => '', 'render_mode' => 'native', 'brand_color' => '', 'button_shape' => 'theme', 'inherit_font' => true, 'cards' => true, 'cards_max' => 24, 'cart_from_viewer' => true, 'match_field' => 'sku', 'attribution' => false, 'environment' => Env::PRODUCTION, ); } /** * All settings, defaults filled in. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); return array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ); } /** * One setting. * * @param string $key Setting key. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Merge and persist a partial update (values are sanitized). * * @param array $changes Partial settings. * @return array The saved settings. */ public static function update( array $changes ) { $next = self::sanitize( array_merge( self::all(), $changes ) ); update_option( self::OPTION, $next, true ); return $next; } /** * The "View in 3D" label, translated when the merchant left it empty. * * @return string */ public static function button_text() { $text = (string) self::get( 'button_text' ); return '' !== $text ? $text : __( 'View in 3D', 'thridify' ); } /** * Sanitize a full settings array. * * @param array $input Raw values. * @return array */ public static function sanitize( array $input ) { $d = self::defaults(); $out = array(); $out['enabled'] = ! empty( $input['enabled'] ); $out['product_mode'] = self::pick( $input, 'product_mode', self::MODES, $d['product_mode'] ); $out['inline_start'] = self::pick( $input, 'inline_start', self::START_TABS, $d['inline_start'] ); $out['trigger_position'] = self::pick( $input, 'trigger_position', self::POSITIONS, $d['trigger_position'] ); $out['button_placement'] = self::pick( $input, 'button_placement', self::PLACEMENTS, $d['button_placement'] ); $out['button_text'] = isset( $input['button_text'] ) ? substr( sanitize_text_field( (string) $input['button_text'] ), 0, 60 ) : ''; $out['render_mode'] = self::pick( $input, 'render_mode', self::RENDER_MODES, $d['render_mode'] ); $color = isset( $input['brand_color'] ) ? sanitize_hex_color( (string) $input['brand_color'] ) : ''; $out['brand_color'] = $color ? $color : ''; $out['button_shape'] = self::pick( $input, 'button_shape', self::SHAPES, $d['button_shape'] ); $out['inherit_font'] = ! empty( $input['inherit_font'] ); $out['cards'] = ! empty( $input['cards'] ); $out['cards_max'] = isset( $input['cards_max'] ) ? max( 1, min( 60, absint( $input['cards_max'] ) ) ) : $d['cards_max']; $out['cart_from_viewer'] = ! empty( $input['cart_from_viewer'] ); $out['match_field'] = self::pick( $input, 'match_field', self::MATCH_FIELDS, $d['match_field'] ); $out['attribution'] = ! empty( $input['attribution'] ); $out['environment'] = isset( $input['environment'] ) && Env::SANDBOX === $input['environment'] ? Env::SANDBOX : Env::PRODUCTION; return $out; } /** * An allowed value or the default. * * @param array $input Raw values. * @param string $key Key. * @param string[] $allowed Allowed values. * @param string $fallback Default. * @return string */ private static function pick( array $input, $key, array $allowed, $fallback ) { $value = isset( $input[ $key ] ) ? (string) $input[ $key ] : ''; return in_array( $value, $allowed, true ) ? $value : $fallback; } }