*/ public static function defaults() { return array( 'enabled' => true, 'placement' => 'auto', 'archives' => false, 'start_date' => false, 'no_history' => 'hide', 'struck' => 'lowest', 'badge' => 'text', 'progressive' => 'country', 'sal' => 'manual', 'text_lowest' => '', 'text_since' => '', 'remove_data' => false, ); } /** * Every setting, defaults filled in. * * @return array */ public static function all() { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { $saved = array(); } return array_merge( self::defaults(), array_intersect_key( $saved, self::defaults() ) ); } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * Sanitize callback for register_setting(). * * @param mixed $input Raw form values. * @return array */ public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $clean = self::defaults(); foreach ( array( 'enabled', 'archives', 'start_date', 'remove_data' ) as $flag ) { $clean[ $flag ] = ! empty( $input[ $flag ] ); } $clean['placement'] = ( isset( $input['placement'] ) && 'manual' === $input['placement'] ) ? 'manual' : 'auto'; $clean['no_history'] = ( isset( $input['no_history'] ) && 'regular' === $input['no_history'] ) ? 'regular' : 'hide'; $clean['struck'] = ( isset( $input['struck'] ) && 'regular' === $input['struck'] ) ? 'regular' : 'lowest'; $clean['badge'] = ( isset( $input['badge'] ) && 'percent' === $input['badge'] ) ? 'percent' : 'text'; $clean['progressive'] = ( isset( $input['progressive'] ) && in_array( $input['progressive'], array( 'first', 'each' ), true ) ) ? $input['progressive'] : 'country'; $clean['sal'] = ( isset( $input['sal'] ) && 'auto' === $input['sal'] ) ? 'auto' : 'manual'; $clean['text_lowest'] = self::clean_text( isset( $input['text_lowest'] ) ? $input['text_lowest'] : '', '{price}' ); $clean['text_since'] = self::clean_text( isset( $input['text_since'] ) ? $input['text_since'] : '', '{date}' ); return $clean; } /** * Member states that did not take article 6a(5) of Directive 98/6/EC, or * took it for a limited time: country code => the $chain of * PriorPrice::compute(), 0 for each reduction or the most days the first * start counts. Every other country keeps the first start. Sources: * Spain, Ley 7/1996 art. 20; Poland, the price information act art. 4; * the Netherlands, Besluit prijsaanduiding producten art. 5a(3) (three * calendar months: never less than 88 days and 23 hours, across a * change to summer time, so 88 days); Belgium, Code of Economic Law art. * VI.18 (30 days); Finland, the competition authority's guidance (60 * days); Commission report COM(2024) 258, section 7. */ const PROGRESSIVE = array( 'ES' => 0, 'PL' => 0, 'NL' => 88, 'BE' => 30, 'FI' => 60, ); /** * The shop's base country code. * * @return string */ public static function country() { $base = (string) get_option( 'woocommerce_default_country', '' ); return strtoupper( (string) strtok( $base, ':' ) ); } /** * The rule for a sale lowered again while it runs, as the $chain of * PriorPrice::compute(): null keeps the first start, 0 starts again at * each reduction, a number of days limits the first start. "first" is * the country's rule where the country limits it. * * @return int|null */ public static function chain() { if ( 'each' === self::get( 'progressive' ) ) { return 0; } // The first start never lasts longer than the shop's country allows, // whichever rule is picked. $country = self::country(); return isset( self::PROGRESSIVE[ $country ] ) ? self::PROGRESSIVE[ $country ] : null; } /** * Plain text wording that must keep its placeholder; anything else falls * back to the default wording. * * @param mixed $value Raw value. * @param string $placeholder Required placeholder. * @return string */ private static function clean_text( $value, $placeholder ) { $value = is_string( $value ) ? sanitize_text_field( wp_unslash( $value ) ) : ''; if ( '' === $value || false === strpos( $value, $placeholder ) ) { return ''; } if ( function_exists( 'mb_substr' ) ) { return mb_substr( $value, 0, self::MAX_TEXT ); } return substr( $value, 0, self::MAX_TEXT ); } }