'gate', 'scope' => 'scope', 'pages' => 'scope', 'post_categories' => 'scope', 'never' => 'scope', 'skip_logged_in' => 'scope', 'min_age' => 'age', 'method' => 'method', 'days' => 'remember', 'checkout' => 'checkout', 'checkout_when' => 'checkout', 'checkout_method' => 'checkout', 'checkout_months' => 'checkout', 'leave_url' => 'looks', 'heading' => 'looks', 'message' => 'looks', 'under_message' => 'looks', 'logo' => 'looks', 'color_bg' => 'looks', 'color_text' => 'looks', 'color_button' => 'looks', 'background' => 'looks', 'product_note' => 'looks', ); /** * The defaults: nothing switched on, an 18+ one-tap gate for the whole site * once it is, remembered for 30 days; at checkout, the date of birth for carts * holding a restricted product, asked at every order — a customer's check is * remembered only for the months the owner chooses (B1). * * @return array */ public static function defaults() { return array( 'gate' => false, 'scope' => 'site', 'pages' => array(), 'post_categories' => array(), 'never' => array(), 'skip_logged_in' => false, 'min_age' => self::AGE_MIN, 'method' => 'confirm', 'days' => 30, 'checkout' => false, 'checkout_when' => 'restricted', 'checkout_method' => 'dob', 'checkout_months' => 0, 'leave_url' => '', 'heading' => '', 'message' => '', 'under_message' => '', 'logo' => 0, 'color_bg' => '', 'color_text' => '', 'color_button' => '', 'background' => 'solid', 'product_note' => false, ); } /** * The settings, defaults merged in, every value of an allowed shape. * * @return array */ public static function get() { $stored = get_option( self::OPTION, array() ); return self::sanitize( is_array( $stored ) ? $stored : array(), self::defaults() ); } /** * Merge a partial change onto the current settings, save the result, and write * every changed group to the history. * * @param array $changes Any of the keys of defaults(). * @param int|null $user_id Who made the change (default: the current user). * @return array The settings now stored. */ public static function update( array $changes, $user_id = null ) { $before = self::get(); $next = self::sanitize( $changes, $before ); if ( $next === $before && is_array( get_option( self::OPTION, null ) ) ) { return $next; } update_option( self::OPTION, $next, true ); // Remembering switched off: no customer stays remembered (B1). if ( $before['checkout_months'] > 0 && 0 === $next['checkout_months'] ) { Remember::forget_all(); } // One history entry per group that changed, holding only the keys that did. $groups = array(); foreach ( self::WHAT as $key => $what ) { if ( $before[ $key ] !== $next[ $key ] ) { $groups[ $what ]['before'][ $key ] = $before[ $key ]; $groups[ $what ]['after'][ $key ] = $next[ $key ]; } } foreach ( History::WHATS as $what ) { if ( isset( $groups[ $what ] ) ) { History::record( $what, $groups[ $what ]['before'], $groups[ $what ]['after'], $user_id ); } } // A page cache still holds the pages as they were: cleared when the change // reaches them — the gate was on, or is now, or a restricted product's page // names the checkout question (§S-CACHE, §S-WOO). $note = Woo::note_on( $before ) || Woo::note_on( $next ); if ( ( $before['gate'] || $next['gate'] || $note ) && Cache::reaches_pages( array_keys( $groups ), $note ) ) { Cache::purge( implode( ',', array_keys( $groups ) ) ); } return $next; } /** * Clean a partial change and lay it over a base. Unknown keys are dropped; a * value of the wrong shape keeps the base value. * * @param array $in The change. * @param array $base Complete settings to lay it over. * @return array */ public static function sanitize( array $in, array $base ) { $defaults = self::defaults(); $out = array(); foreach ( $defaults as $key => $default ) { $current = array_key_exists( $key, $base ) ? self::clean( $key, $base[ $key ], $default ) : $default; $out[ $key ] = array_key_exists( $key, $in ) ? self::clean( $key, $in[ $key ], $current ) : $current; } return $out; } /** * One value of one setting, or the fallback when it is not of an allowed shape. * * @param string $key The setting. * @param mixed $value The value offered. * @param mixed $fallback What to keep when the value is refused. * @return mixed */ private static function clean( $key, $value, $fallback ) { switch ( $key ) { case 'gate': case 'skip_logged_in': case 'checkout': case 'product_note': return self::bool( $value, (bool) $fallback ); case 'scope': return self::pick( $value, self::SCOPES, $fallback ); case 'method': case 'checkout_method': return self::pick( $value, self::METHODS, $fallback ); case 'checkout_when': return self::pick( $value, self::CHECKOUT_WHEN, $fallback ); case 'background': return self::pick( $value, self::BACKGROUNDS, $fallback ); case 'min_age': return self::is_age( $value ) ? (int) $value : (int) $fallback; case 'days': return self::int_between( $value, 1, self::MAX_DAYS ) ? (int) $value : (int) $fallback; case 'checkout_months': return self::int_between( $value, 0, Remember::MAX_MONTHS ) ? (int) $value : (int) $fallback; case 'pages': case 'never': return is_array( $value ) ? self::ids( $value, 'page' ) : (array) $fallback; case 'post_categories': return is_array( $value ) ? self::ids( $value, 'category' ) : (array) $fallback; case 'logo': return self::logo( $value, (int) $fallback ); case 'leave_url': return self::url( $value, (string) $fallback ); case 'heading': case 'message': case 'under_message': return is_string( $value ) ? mb_substr( sanitize_text_field( $value ), 0, self::MAX_TEXT ) : (string) $fallback; case 'color_bg': case 'color_text': case 'color_button': if ( is_string( $value ) && '' === trim( $value ) ) { return ''; } $color = is_string( $value ) ? sanitize_hex_color( trim( $value ) ) : null; return is_string( $color ) && '' !== $color ? strtolower( $color ) : (string) $fallback; } return $fallback; } /** * Whether a value is a minimum age the owner may set (18–25, whole years). * * @param mixed $value Value. * @return bool */ public static function is_age( $value ) { return self::int_between( $value, self::AGE_MIN, self::AGE_MAX ); } /** * Whether a value is a whole number between two bounds — an int, or a string * of digits (a form's value); never a float, a bool or "21abc". * * @param mixed $value Value. * @param int $min Lowest allowed. * @param int $max Highest allowed. * @return bool */ public static function int_between( $value, $min, $max ) { if ( is_int( $value ) ) { $n = $value; } elseif ( is_string( $value ) && 1 === preg_match( '/^[0-9]{1,6}$/', $value ) ) { $n = (int) $value; } else { return false; } return $n >= $min && $n <= $max; } /** * One of a fixed list of words, or the fallback. * * @param mixed $value Value. * @param string[] $allowed The fixed words. * @param mixed $fallback When the value is none of them. * @return string */ private static function pick( $value, array $allowed, $fallback ) { return is_string( $value ) && in_array( $value, $allowed, true ) ? $value : (string) $fallback; } /** * Page ids or category ids: whole numbers, each a page (any status but trash) * or an existing post category once WordPress has registered them; unique, * sorted, at most MAX_IDS. * * @param array $values Raw input. * @param string $kind 'page' or 'category'. * @return int[] */ private static function ids( array $values, $kind ) { $out = array(); $check = did_action( 'init' ) > 0; foreach ( $values as $value ) { if ( ! self::int_between( $value, 1, PHP_INT_MAX ) ) { continue; } $id = (int) $value; if ( in_array( $id, $out, true ) ) { continue; } if ( $check && 'page' === $kind ) { $post = get_post( $id ); if ( ! $post || 'page' !== $post->post_type || 'trash' === $post->post_status ) { continue; } } if ( $check && 'category' === $kind ) { $term = get_term( $id, 'category' ); if ( ! $term || is_wp_error( $term ) ) { continue; } } $out[] = $id; if ( count( $out ) >= self::MAX_IDS ) { break; } } sort( $out ); return $out; } /** * The logo: an image in the media library, or 0 for none. * * @param mixed $value Value. * @param int $fallback When the value is not an image. * @return int */ private static function logo( $value, $fallback ) { if ( 0 === $value || '0' === $value || '' === $value ) { return 0; } if ( ! self::int_between( $value, 1, PHP_INT_MAX ) ) { return $fallback; } if ( did_action( 'init' ) && ! wp_attachment_is_image( (int) $value ) ) { return $fallback; } return (int) $value; } /** * The "leave" address for a visitor under the minimum age: an http(s) address, * or '' for the browser's previous page. * * @param mixed $value Value. * @param string $fallback When the value is not an address. * @return string */ private static function url( $value, $fallback ) { if ( ! is_string( $value ) ) { return $fallback; } $value = trim( $value ); if ( '' === $value ) { return ''; } // Parsed, never resolved: checking an address must not look anything up. $url = esc_url_raw( $value, array( 'http', 'https' ) ); $host = '' !== $url ? wp_parse_url( $url, PHP_URL_HOST ) : null; return is_string( $host ) && '' !== $host && strlen( $url ) <= 2000 ? $url : $fallback; } /** * A boolean from JSON or a form: true, false, 1, 0, "1", "0", "true", "false". * * @param mixed $value Input. * @param bool $fallback When the input is none of those. * @return bool */ private static function bool( $value, $fallback ) { if ( is_bool( $value ) ) { return $value; } if ( in_array( $value, array( 1, '1', 'true', 'on', 'yes' ), true ) ) { return true; } if ( in_array( $value, array( 0, '0', 'false', 'off', 'no', '' ), true ) ) { return false; } return (bool) $fallback; } }