|null */ private static $cache = null; /** * Defaults. * * @return array */ public static function defaults() { return array( 'billing_source' => 'all', 'billing_custom' => array(), 'billing_exclude' => array(), 'shipping_source' => 'all', 'shipping_custom' => array(), 'shipping_exclude' => array(), 'popular' => array(), 'mode' => 'block', ); } /** * All settings, defaults filled in. * * @return array */ public static function all() { if ( null !== self::$cache ) { return self::$cache; } $stored = get_option( self::OPTION, array() ); $all = self::sanitize( is_array( $stored ) ? $stored : array() ); if ( Countries::all_codes() ) { self::$cache = $all; } return $all; } /** * One setting. * * @param string $key Key. * @return mixed */ public static function get( $key ) { $all = self::all(); return array_key_exists( $key, $all ) ? $all[ $key ] : null; } /** * Saves settings (sanitized). * * @param array $values Values. */ public static function update( array $values ) { update_option( self::OPTION, self::sanitize( array_merge( self::all(), $values ) ), true ); self::$cache = null; } /** * Forgets the cached settings. */ public static function reset() { self::$cache = null; } /** * Keeps known keys and allowed values only. * * @param array $raw Raw values. * @return array */ public static function sanitize( array $raw ) { $out = self::defaults(); foreach ( self::GROUPS as $group ) { $source = isset( $raw[ $group . '_source' ] ) ? (string) $raw[ $group . '_source' ] : ''; if ( in_array( $source, self::SOURCES, true ) ) { $out[ $group . '_source' ] = $source; } $out[ $group . '_custom' ] = self::codes( isset( $raw[ $group . '_custom' ] ) ? $raw[ $group . '_custom' ] : array() ); $out[ $group . '_exclude' ] = self::codes( isset( $raw[ $group . '_exclude' ] ) ? $raw[ $group . '_exclude' ] : array() ); } $out['popular'] = array_slice( self::codes( isset( $raw['popular'] ) ? $raw['popular'] : array() ), 0, 20 ); $mode = isset( $raw['mode'] ) ? (string) $raw['mode'] : ''; if ( in_array( $mode, self::MODES, true ) ) { $out['mode'] = $mode; } return $out; } /** * Country codes that WooCommerce knows (any two letters before WooCommerce has loaded its countries), upper case, unique. * * @param mixed $raw Raw list. * @return array */ public static function codes( $raw ) { if ( ! is_array( $raw ) ) { return array(); } $known = Countries::all_codes(); $out = array(); foreach ( array_slice( $raw, 0, 300 ) as $code ) { $code = is_string( $code ) ? strtoupper( trim( $code ) ) : ''; $ok = $known ? isset( $known[ $code ] ) : (bool) preg_match( '/^[A-Z]{2}$/', $code ); if ( $ok && ! in_array( $code, $out, true ) ) { $out[] = $code; } } return $out; } }