public function test_sanitize_settings_raw() { $this->assertEquals('my_settingsub-setting', Kirki_Field::sanitize_settings_raw(array('settings' => 'my_setting[sub-setting]'))); $this->assertEquals('my_settingsub-setting', Kirki_Field::sanitize_settings_raw(array('settings' => 'my_setting["sub-setting"]'))); $this->assertEquals('my_settingsub-setting', Kirki_Field::sanitize_settings_raw(array('settings' => 'my_setting sub-setting'))); $this->assertEquals('my_settingsub-setting', Kirki_Field::sanitize_settings_raw(array('setting' => 'my_setting sub-setting'))); }
/** * loop through all fields and create an array of style definitions */ public function loop_controls() { $fields = Kirki::$fields; $css = array(); // Early exit if no fields are found. if (empty($fields)) { return; } foreach ($fields as $field) { // Only continue if $field['output'] is set if (isset($field['output']) && 'background' != $field['type']) { $css = array_merge_recursive($css, Kirki_Output::css(Kirki_Field::sanitize_settings_raw($field), Kirki_Field::sanitize_type($field), Kirki_Field::sanitize_output($field), isset($field['output']['callback']) ? $field['output']['callback'] : '', true)); } } return Kirki_Output::styles_parse(Kirki_Output::add_prefixes($css)); }
/** * Get the value of a field. * This is a deprecated function that we in use when there was no API. * Please use the Kirki::get_option() method instead. * Documentation is available for the new method on https://github.com/aristath/kirki/wiki/Getting-the-values */ function kirki_get_option($option = '') { // Make sure the class is instanciated Kirki_Toolkit::get_instance(); $values = array(); // Get the array of all the fields. $fields = Kirki::$fields; // Get the config. $config = apply_filters('kirki/config', array()); $config['options_type'] = isset($config['options_type']) ? esc_attr($config['options_type']) : 'theme_mod'; $config['option_name'] = isset($config['option_name']) ? esc_attr($config['option_name']) : ''; // If we're using options instead of theme_mods, // then first we'll have to get the array of all options. if ('option' == $config['options_type']) { if ('' == $config['option_name']) { // No option name is defined. // Each options is saved separately in the db, so we'll manually build the array here. foreach ($fields as $field) { $values[Kirki_Field::sanitize_settings($field)] = get_option(Kirki_Field::sanitize_settings($field), Kirki_Field::sanitize_default($field)); } } else { // An option_name has been defined so our options are all saved in an array there. $values = get_option($config['option_name']); foreach ($fields as $field) { if (!isset($values[Kirki_Field::sanitize_settings_raw($field)])) { $values[Kirki_Field::sanitize_settings_raw($field)] = maybe_unserialize(Kirki_Field::sanitize_default($field)); } } } } if ('' == $option) { // No option has been defined so we'll get all options and return an array // If we're using options then we already have the $values set above. // All we need here is a fallback for theme_mods if ('option' != $config['options_type']) { // We're using theme_mods $values = get_theme_mods(); } // Early exit and return the array of all values return $values; } // If a value has been defined then we proceed. // Early exit if this option does not exist $field_id = 'option' == $config['options_type'] && '' != $config['option_name'] ? $config['option_name'] . '[' . $option . ']' : $option; if (!isset($fields[$field_id])) { return; } if ('option' == $config['options_type']) { // We're using options instead of theme_mods. // We already have the array of values set from above so we'll use that. $value = isset($values[$option]) ? $values[$option] : $fields[$option]['default']; } else { // We're using theme_mods $value = get_theme_mod($option, $fields[$option]['default']); } // Combine background options to a single array if ('background' == $fields[$field_id]['type']) { if ('option' == $config['options_type']) { $value = array('background-color' => isset($values[$option . '_color']) ? $values[$option . '_color'] : null, 'background-repeat' => isset($values[$option . '_repeat']) ? $values[$option . '_repeat'] : null, 'background-attachment' => isset($values[$option . '_attach']) ? $values[$option . '_attach'] : null, 'background-image' => isset($values[$option . '_image']) ? $values[$option . '_image'] : null, 'background-position' => isset($values[$option . '_position']) ? $values[$option . '_position'] : null, 'background-clip' => isset($values[$option . '_clip']) ? $values[$option . '_clip'] : null, 'background-size' => isset($values[$option . '_size']) ? $values[$option . '_size'] : null); } else { $value = array('background-color' => isset($fields[$field_id]['default']['color']) ? get_theme_mod($option . '_color', $fields[$field_id]['default']['color']) : null, 'background-repeat' => isset($fields[$field_id]['default']['repeat']) ? get_theme_mod($option . '_repeat', $fields[$field_id]['default']['repeat']) : null, 'background-attachment' => isset($fields[$field_id]['default']['attach']) ? get_theme_mod($option . '_attach', $fields[$field_id]['default']['attach']) : null, 'background-image' => isset($fields[$field_id]['default']['image']) ? get_theme_mod($option . '_image', $fields[$field_id]['default']['image']) : null, 'background-position' => isset($fields[$field_id]['default']['position']) ? get_theme_mod($option . '_position', $fields[$field_id]['default']['position']) : null, 'background-clip' => isset($fields[$field_id]['default']['clip']) ? get_theme_mod($option . '_clip', $fields[$field_id]['default']['clip']) : null, 'background-size' => isset($fields[$field_id]['default']['size']) ? get_theme_mod($option . '_size', $fields[$field_id]['default']['size']) : null); } } // Return the single value. // Pass it through maybe_unserialize so we're sure we get a proper value. return maybe_unserialize($value); }
public function google_link() { // Get the array of fields $fields = Kirki::$fields; // Early exit if no fields are found. if (empty($fields)) { return; } $fonts = array(); foreach ($fields as $field) { // Sanitize the field's output & settings_raw items. $field['output'] = Kirki_Field::sanitize_output($field); $field['settings_raw'] = Kirki_Field::sanitize_settings_raw($field); // Make sure output is properly formatted if (isset($field['output']) && is_array($field['output'])) { foreach ($field['output'] as $output) { if (in_array($output['property'], array('font-family', 'font-weight', 'font-subset'))) { // The value of this control $value = Kirki::get_option($field['settings_raw']); if ('font-family' == $output['property']) { // Add the font-family to the array $fonts[]['font-family'] = $value; } else { if ('font-weight' == $output['property']) { // Add font-weight to the array $fonts[]['font-weight'] = $value; } else { if ('font-subset' == $output['property']) { // add font subsets to the array $fonts[]['subsets'] = $value; } } } } } } } foreach ($fonts as $font) { // Do we have font-families? if (isset($font['font-family'])) { $font_families = !isset($font_families) ? array() : $font_families; $font_families[] = $font['font-family']; if (Kirki_Toolkit::fonts()->is_google_font($font['font-family'])) { $has_google_font = true; } } // Do we have font-weights? if (isset($font['font-weight'])) { $font_weights = !isset($font_weights) ? array() : $font_weights; $font_weights[] = $font['font-weight']; } // Do we have font-subsets? if (isset($font['subsets'])) { $font_subsets = !isset($font_subsets) ? array() : $font_subsets; $font_subsets[] = $font['subsets']; } } // Make sure there are no empty values and define defaults. $font_families = !isset($font_families) || empty($font_families) ? false : $font_families; $font_weights = !isset($font_weights) || empty($font_weights) ? '400' : $font_weights; $font_subsets = !isset($font_subsets) || empty($font_subsets) ? 'all' : $font_subsets; if (!isset($has_google_font) || !$has_google_font) { $font_families = false; } // Return the font URL. return $font_families ? Kirki_Toolkit::fonts()->get_google_font_uri($font_families, $font_weights, $font_subsets) : false; }