default for the Quick view / details button. */ public static function color_defaults( $prefix = '' ) { unset( $prefix ); return array( 'bg' => '#1d2433', 'text' => '#ffffff', 'hover_bg' => '#000000', 'hover_text' => '#ffffff' ); } /** All colour settings: full key => default hex. */ public static function colors( $prefix = null ) { $out = array(); foreach ( null === $prefix ? array( '', 'd_' ) : array( $prefix ) as $p ) { foreach ( self::color_defaults( $p ) as $key => $hex ) { $out[ ( '' === $p ? 'button_' : $p ) . $key ] = $hex; } } return $out; } public static function positions() { return array( 'below' => __( 'Below the price', 'cpod-kit' ), 'image' => __( 'On the image (shown on hover)', 'cpod-kit' ), 'manual' => __( 'Nowhere (only with the [cpod_quick_view] shortcode)', 'cpod-kit' ), ); } public static function aligns() { return array( 'left' => __( 'Left', 'cpod-kit' ), 'center' => __( 'Center', 'cpod-kit' ), 'right' => __( 'Right', 'cpod-kit' ), ); } public static function styles() { return array( 'solid' => __( 'Filled', 'cpod-kit' ), 'outline' => __( 'Outline', 'cpod-kit' ), 'link' => __( 'Text link', 'cpod-kit' ), 'icon' => __( 'Icon only (round)', 'cpod-kit' ), ); } public static function weights() { return array( '400' => __( 'Normal (400)', 'cpod-kit' ), '500' => __( 'Medium (500)', 'cpod-kit' ), '600' => __( 'Semi-bold (600)', 'cpod-kit' ), '700' => __( 'Bold (700)', 'cpod-kit' ), '800' => __( 'Extra bold (800)', 'cpod-kit' ), ); } public static function icon_names() { return array( 'eye' => __( 'Eye', 'cpod-kit' ), 'search' => __( 'Magnifier', 'cpod-kit' ), 'expand' => __( 'Expand', 'cpod-kit' ), 'plus' => __( 'Plus', 'cpod-kit' ), 'bag' => __( 'Shopping bag', 'cpod-kit' ), 'arrow' => __( 'Arrow', 'cpod-kit' ), 'custom' => __( 'Custom SVG', 'cpod-kit' ), 'none' => __( 'No icon', 'cpod-kit' ), ); } /** Look settings of one button: key (without prefix) => default. */ public static function look_defaults( $prefix = '' ) { $details = 'd_' === $prefix; return array( 'style' => 'outline', 'icon' => $details ? 'arrow' : 'eye', 'icon_svg' => '', 'icon_after' => $details ? 1 : 0, 'full_width' => $details ? 1 : 0, 'uppercase' => 0, 'radius' => 4, 'font_size' => 14, 'weight' => '600', ); } public static function defaults() { $defaults = array( 'position' => 'below', 'align' => 'center', 'margin_top' => 10, 'margin_bottom' => 10, 'text' => '', 'show_gallery' => 1, 'show_rating' => 1, 'show_excerpt' => 1, 'show_meta' => 0, 'show_details' => 1, 'details_text' => '', 'added' => '', 'view_cart' => '', 'prefetch' => 1, ); foreach ( array( '', 'd_' ) as $p ) { foreach ( self::look_defaults( $p ) as $key => $value ) { $defaults[ $p . $key ] = $value; } } foreach ( self::colors() as $key => $hex ) { $defaults[ $key ] = $hex; $defaults[ $key . '_var' ] = ''; } return $defaults; } /** Texts shown to customers: key => default (used when the setting is empty). */ public static function text_defaults() { return array( 'text' => __( 'Quick view', 'cpod-kit' ), 'details_text' => __( 'View full details', 'cpod-kit' ), 'added' => __( 'Added to cart.', 'cpod-kit' ), 'view_cart' => __( 'View cart', 'cpod-kit' ), ); } public static function all() { if ( null === self::$cache ) { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); // 2.6.0 saved the icon as a checkbox. if ( isset( $saved['icon'] ) && in_array( (string) $saved['icon'], array( '0', '1' ), true ) ) { $saved['icon'] = '1' === (string) $saved['icon'] ? 'eye' : 'none'; } self::$cache = wp_parse_args( $saved, self::defaults() ); } return self::$cache; } public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** Customer text: the setting, or the translated default. */ public static function text( $key ) { $value = (string) self::get( $key ); if ( '' !== trim( $value ) ) { return $value; } $defaults = self::text_defaults(); return isset( $defaults[ $key ] ) ? $defaults[ $key ] : ''; } public static function css_color( $key ) { return Fields::css_color( self::all(), $key ); } /** CSS custom properties of one button. */ public static function css_vars( $prefix = '' ) { $s = self::all(); $cp = '' === $prefix ? 'button_' : $prefix; return '--cpod-qv-bg:' . self::css_color( $cp . 'bg' ) . ';--cpod-qv-text:' . self::css_color( $cp . 'text' ) . ';--cpod-qv-hover-bg:' . self::css_color( $cp . 'hover_bg' ) . ';--cpod-qv-hover-text:' . self::css_color( $cp . 'hover_text' ) . ';--cpod-qv-radius:' . absint( $s[ $prefix . 'radius' ] ) . 'px;--cpod-qv-size:' . absint( $s[ $prefix . 'font_size' ] ) . 'px;--cpod-qv-weight:' . absint( $s[ $prefix . 'weight' ] ) . ';'; } /** SVG tags and attributes allowed in a custom icon. */ public static function svg_kses() { $common = array( 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'fill-rule' => true, 'clip-rule' => true, 'transform' => true, 'opacity' => true ); return array( 'svg' => $common + array( 'xmlns' => true, 'viewbox' => true, 'width' => true, 'height' => true ), 'g' => $common, 'path' => $common + array( 'd' => true ), 'circle' => $common + array( 'cx' => true, 'cy' => true, 'r' => true ), 'ellipse' => $common + array( 'cx' => true, 'cy' => true, 'rx' => true, 'ry' => true ), 'rect' => $common + array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true ), 'line' => $common + array( 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true ), 'polyline' => $common + array( 'points' => true ), 'polygon' => $common + array( 'points' => true ), ); } public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $d = self::defaults(); $out = array(); foreach ( array_keys( self::text_defaults() ) as $key ) { $out[ $key ] = isset( $input[ $key ] ) ? sanitize_text_field( $input[ $key ] ) : ''; } $out['position'] = isset( $input['position'] ) && array_key_exists( $input['position'], self::positions() ) ? $input['position'] : $d['position']; $out['align'] = isset( $input['align'] ) && array_key_exists( $input['align'], self::aligns() ) ? $input['align'] : $d['align']; foreach ( array( 'margin_top', 'margin_bottom' ) as $key ) { $out[ $key ] = isset( $input[ $key ] ) && is_numeric( $input[ $key ] ) ? max( -40, min( 80, (int) $input[ $key ] ) ) : $d[ $key ]; } foreach ( array( 'show_gallery', 'show_rating', 'show_excerpt', 'show_meta', 'show_details', 'prefetch' ) as $key ) { $out[ $key ] = empty( $input[ $key ] ) ? 0 : 1; } foreach ( array( '', 'd_' ) as $p ) { $out[ $p . 'style' ] = isset( $input[ $p . 'style' ] ) && array_key_exists( $input[ $p . 'style' ], self::styles() ) ? $input[ $p . 'style' ] : $d[ $p . 'style' ]; $out[ $p . 'icon' ] = isset( $input[ $p . 'icon' ] ) && array_key_exists( $input[ $p . 'icon' ], self::icon_names() ) ? $input[ $p . 'icon' ] : $d[ $p . 'icon' ]; $out[ $p . 'weight' ] = isset( $input[ $p . 'weight' ] ) && array_key_exists( (string) $input[ $p . 'weight' ], self::weights() ) ? (string) $input[ $p . 'weight' ] : $d[ $p . 'weight' ]; foreach ( array( 'icon_after', 'full_width', 'uppercase' ) as $key ) { $out[ $p . $key ] = empty( $input[ $p . $key ] ) ? 0 : 1; } $out[ $p . 'radius' ] = isset( $input[ $p . 'radius' ] ) && '' !== $input[ $p . 'radius' ] ? min( 60, absint( $input[ $p . 'radius' ] ) ) : $d[ $p . 'radius' ]; $out[ $p . 'font_size' ] = isset( $input[ $p . 'font_size' ] ) && '' !== $input[ $p . 'font_size' ] ? min( 30, max( 10, absint( $input[ $p . 'font_size' ] ) ) ) : $d[ $p . 'font_size' ]; $out[ $p . 'icon_svg' ] = isset( $input[ $p . 'icon_svg' ] ) ? trim( wp_kses( (string) $input[ $p . 'icon_svg' ], self::svg_kses() ) ) : ''; } foreach ( self::colors() as $key => $hex ) { $color = isset( $input[ $key ] ) ? sanitize_hex_color( $input[ $key ] ) : ''; $out[ $key ] = $color ? $color : $hex; $out[ $key . '_var' ] = isset( $input[ $key . '_var' ] ) ? \CPODKit\Core\Settings::sanitize_css_var( $input[ $key . '_var' ] ) : ''; } self::$cache = null; return $out; } }