'auto', 'goal_amount' => '', 'text_empty' => '', 'text_progress' => '', 'text_done' => '', 'place_top' => true, 'top_empty' => true, 'place_cart' => true, 'place_checkout' => true, 'place_mini' => true, 'place_product' => false, 'style' => 'light', 'color' => '', 'delete_data' => false, ); } /** * Allowed values for the choice settings. * * @return array */ public static function choices() { return array( 'goal_mode' => array( 'auto', 'manual' ), 'style' => array( 'light', 'dark', 'color' ), ); } /** * The texts shoppers see, with the default used when a setting is empty. * * @return array Setting key => default text. */ public static function default_texts() { return array( /* translators: {goal} is replaced with the amount for free shipping. Keep it as it is. */ 'text_empty' => __( 'Free shipping on orders over {goal}', 'cartmeter' ), /* translators: {remaining} is replaced with the amount still missing. Keep it as it is. */ 'text_progress' => __( 'Add {remaining} more to get free shipping', 'cartmeter' ), 'text_done' => __( 'You get free shipping!', 'cartmeter' ), ); } /** * A shopper-facing text: the saved one, or the default. * * @param string $key text_empty, text_progress or text_done. * @return string */ public static function text( $key ) { $s = self::get(); $defaults = self::default_texts(); if ( isset( $s[ $key ] ) && '' !== $s[ $key ] ) { return $s[ $key ]; } return isset( $defaults[ $key ] ) ? $defaults[ $key ] : ''; } /** * Current settings merged over the defaults. * * @return array */ public static function get() { if ( null === self::$cache ) { $saved = get_option( self::OPTION, array() ); self::$cache = self::sanitize( is_array( $saved ) ? $saved : array() ); } return self::$cache; } /** * Cleans raw input (from the form or the database) into a full settings array. * * @param array $input Raw values. * @return array */ public static function sanitize( array $input ) { $defaults = self::defaults(); $clean = array(); foreach ( $defaults as $key => $default ) { $value = array_key_exists( $key, $input ) ? $input[ $key ] : $default; if ( is_bool( $default ) ) { $clean[ $key ] = (bool) $value && '0' !== $value; } elseif ( isset( self::choices()[ $key ] ) ) { $value = sanitize_key( (string) $value ); $clean[ $key ] = in_array( $value, self::choices()[ $key ], true ) ? $value : $default; } elseif ( 'color' === $key ) { $color = sanitize_hex_color( (string) $value ); $clean[ $key ] = $color ? $color : ''; } elseif ( 'goal_amount' === $key ) { $clean[ $key ] = self::clean_amount( $value ); } else { $clean[ $key ] = sanitize_text_field( (string) $value ); } } return $clean; } /** * An amount typed by the shop owner ("3,000", "49.90", "49,90") as a plain decimal string, or '' if not a positive number. * * @param mixed $value Raw value. * @return string */ public static function clean_amount( $value ) { $value = trim( (string) $value ); if ( '' === $value ) { return ''; } $value = (string) wc_format_decimal( $value, false, true ); if ( ! is_numeric( $value ) || (float) $value <= 0 ) { return ''; } return (string) $value; } /** * Saves settings from the form. * * @param array $input Raw form values. */ public static function save( array $input ) { $clean = self::sanitize( $input ); update_option( self::OPTION, $clean ); self::$cache = $clean; } /** * The bar color in use. * * @return string */ public static function color() { $s = self::get(); return $s['color'] ? $s['color'] : self::DEFAULT_COLOR; } /** * Black or white text, whichever reads better on a background color. * * @param string $hex Background color (#rrggbb or #rgb). * @return string */ public static function text_color_for( $hex ) { $hex = ltrim( (string) $hex, '#' ); if ( 3 === strlen( $hex ) ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } if ( 6 !== strlen( $hex ) ) { return '#ffffff'; } $channel = static function ( $part ) { $c = hexdec( $part ) / 255; return $c <= 0.03928 ? $c / 12.92 : pow( ( $c + 0.055 ) / 1.055, 2.4 ); }; $lum = 0.2126 * $channel( substr( $hex, 0, 2 ) ) + 0.7152 * $channel( substr( $hex, 2, 2 ) ) + 0.0722 * $channel( substr( $hex, 4, 2 ) ); // Contrast with white is (1.05) / (lum + 0.05); with black (lum + 0.05) / 0.05. Pick the higher one. return ( 1.05 / ( $lum + 0.05 ) ) >= ( ( $lum + 0.05 ) / 0.05 ) ? '#ffffff' : '#111111'; } }