/** * Sanitize a value from a list of allowed values. * * @since 2.0.0 * @param mixed $value The value to sanitize. * @param mixed $setting The setting for which the sanitizing is occurring. * @return mixed The sanitized value. */ function hoot_customizer_sanitize_choices($value, $setting) { if (is_object($setting)) { $setting = $setting->id; } $choices = hoot_customizer_get_choices($setting); if (!array_key_exists($value, $choices)) { $value = hoot_customizer_get_default($setting); } return $value; }
/** * Helper function to return the theme mod value. * If no value has been saved, it returns $default. * If no $default provided, it checks the default options array. * * @since 2.0.0 * @access public * @param string $name * @param mixed $default * @return mixed */ function hoot_get_mod($name, $default = NULL) { // is_customize_preview() can be used since WordPress 4.0, else use global $wp_customize; global $wp_customize; if (function_exists('is_customize_preview') && is_customize_preview() || isset($wp_customize)) { $default = $default !== NULL ? $default : hoot_customizer_get_default($name); $mod = get_theme_mod($name, $default); // We cannot use empty as this will become false for empty values, and hence fallback to default. isset() is not an option either as $mod is always set. Hence we calculate the default here, and supply it as second argument to get_theme_mod() // if ( !empty( $mod ) ) return apply_filters('hoot_get_mod_customizer', $mod, $name, $default); } else { /*** Return value if set ***/ // Cache static $mods = NULL; // Get the values from database if (!$mods) { $mods = get_theme_mods(); $mods = apply_filters('hoot_get_mod', $mods); } if (isset($mods[$name])) { // Filter applied as in get_theme_mod() core function $mods[$name] = apply_filters("theme_mod_{$name}", $mods[$name]); // Add exceptions: If a value has been set but is empty, this gives the option to return default values in such cases. Simply return $override as (bool)true. $override = apply_filters('hoot_get_mod_override_empty_values', false, $name, $mods[$name]); if ($override !== true) { return $mods[$name]; } } /*** Return default if provided ***/ if ($default !== NULL) { return apply_filters("theme_mod_{$name}", $default); } /*** Return default theme option value ***/ $default = hoot_customizer_get_default($name); if (!empty($default)) { return apply_filters("theme_mod_{$name}", $default); } } /*** We dont have anything! ***/ return false; }
/** * Sanitize icon value to allow only allowed choices. * * @since 2.0.0 * @param string $value The unsanitized string. * @param mixed $setting The setting for which the sanitizing is occurring. * @return string The sanitized value. */ function hoot_customizer_sanitize_icon($value, $setting) { $choices = hoot_enum_icons(); if (!in_array($value, $choices)) { if (is_object($setting)) { $setting = $setting->id; } $value = hoot_customizer_get_default($setting); } return $value; }