/** * The class constructor. * * @var string the setting ID. * @var string theme_mod / option * @var array an array of arrays of the output arguments. * @var mixed a callable function. */ public static function css($field) { /** * Make sure the field is sanitized before proceeding any further. */ $field = Kirki_Field::sanitize_field($field); /** * Get the config ID used in the Kirki class. */ $config_id = Kirki::get_config_id($field); /** * Set class vars */ self::$settings = $field['settings']; self::$output = $field['output']; self::$callback = $field['sanitize_callback']; /** * Get the value of this field */ if ('option' == Kirki::$config[$config_id]['option_type'] && '' != Kirki::$config[$config_id]['option_name']) { self::$value = Kirki::get_option($config_id, str_replace(array(']', Kirki::$config[$config_id]['option_name'] . '['), '', $field['settings'])); } else { self::$value = Kirki::get_option($config_id, $field['settings']); } /** * Returns the styles */ if (!is_array(self::$value)) { return self::styles(); } }
/** * Build the background fields. * Takes a single field with type = background and explodes it to multiple controls. * * @param array * @return null|array<Array> */ public static function explode($field) { $i18n = Kirki_Toolkit::i18n(); $choices = self::background_choices(); // Early exit if this is not a background field. if ('background' != $field['type']) { return; } // Sanitize field $field = Kirki_Field::sanitize_field($field); // No need to proceed any further if no defaults have been set. // We build the array of fields based on what default values have been defined. if (!isset($field['default']) || !is_array($field['default'])) { return; } $fields = array(); $i = 0; foreach ($field['default'] as $key => $value) { // No need to process the opacity, it is factored in the color control. if ('opacity' == $key) { continue; } $key = esc_attr($key); $setting = $key; $help = $field['help']; $description = isset($i18n['background-' . $key]) ? $i18n['background-' . $key] : ''; $output_property = 'background-' . $key; $label = 0 === $i ? $field['label'] : ''; $type = 'select'; switch ($key) { case 'color': $type = false !== strpos($field['default']['color'], 'rgba') ? 'color-alpha' : 'color'; $type = isset($field['default']['opacity']) ? 'color-alpha' : $type; break; case 'image': $type = 'image'; break; case 'attach': $output_property = 'background-attachment'; $description = $i18n['background-attachment']; break; default: $help = ''; break; } $fields[$field['settings'] . '_' . $setting] = array_merge($field, array('type' => $type, 'label' => $label, 'settings' => $field['settings'] . '_' . $setting, 'help' => $help, 'section' => $field['section'], 'priority' => $field['priority'], 'required' => $field['required'], 'description' => $description, 'default' => $value, 'id' => Kirki_Field::sanitize_id(array('settings' => Kirki_Field::sanitize_settings(array('settings' => $field['settings'] . '_' . $setting)))), 'choices' => isset($choices[$key]) ? $choices[$key] : array(), 'output' => '' != $field['output'] ? array(array('element' => $field['output'], 'property' => $output_property)) : '', 'sanitize_callback' => Kirki_Field::fallback_callback($type))); $i++; } return $fields; }
/** * 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']) && !empty($field['output']) && 'background' != $field['type']) { $css = array_merge_recursive($css, Kirki_Output::css(Kirki_Field::sanitize_field($field))); } } if (is_array($css)) { return Kirki_Output::styles_parse(Kirki_Output::add_prefixes($css)); } return; }
public function test_sanitize_field() { $this->assertEquals(array('settings' => 'foo', 'section' => 'foo', 'type' => 'text', 'default' => '', 'label' => '', 'help' => '', 'description' => '', 'required' => null, 'transport' => 'refresh', 'option_type' => 'theme_mod', 'priority' => 10, 'choices' => array(), 'output' => null, 'sanitize_callback' => 'esc_textarea', 'js_vars' => null, 'id' => 'foo', 'capability' => 'edit_theme_options', 'variables' => null, 'active_callback' => '__return_true', 'option_name' => ''), Kirki_Field::sanitize_field(array('settings' => 'foo', 'section' => 'foo', 'type' => 'text'))); }
/** * Create the settings and controls from the $fields array and register them. * @var object The WordPress Customizer object */ public function add_fields($wp_customize) { $control_types = apply_filters('kirki/control_types', array('color' => 'WP_Customize_Color_Control', 'color-alpha' => 'Kirki_Controls_Color_Alpha_Control', 'image' => 'WP_Customize_Image_Control', 'upload' => 'WP_Customize_Upload_Control', 'switch' => 'Kirki_Controls_Switch_Control', 'toggle' => 'Kirki_Controls_Toggle_Control', 'radio-buttonset' => 'Kirki_Controls_Radio_ButtonSet_Control', 'radio-image' => 'Kirki_Controls_Radio_Image_Control', 'sortable' => 'Kirki_Controls_Sortable_Control', 'slider' => 'Kirki_Controls_Slider_Control', 'number' => 'Kirki_Controls_Number_Control', 'multicheck' => 'Kirki_Controls_MultiCheck_Control', 'palette' => 'Kirki_Controls_Palette_Control', 'custom' => 'Kirki_Controls_Custom_Control', 'editor' => 'Kirki_Controls_Editor_Control', 'select2' => 'Kirki_Controls_Select2_Control', 'select2-multiple' => 'Kirki_Controls_Select2_Multiple_Control')); foreach (self::$fields as $field) { if ('background' == $field['type']) { continue; } $wp_customize->add_setting(Kirki_Field::sanitize_settings($field), array('default' => Kirki_Field::sanitize_default($field), 'type' => Kirki_Field::sanitize_type($field), 'capability' => Kirki_Field::sanitize_capability($field), 'transport' => Kirki_Field::sanitize_transport($field), 'sanitize_callback' => Kirki_Field::sanitize_callback($field))); if (array_key_exists($field['type'], $control_types)) { $class_name = $control_types[$field['type']]; $wp_customize->add_control(new $class_name($wp_customize, Kirki_Field::sanitize_id($field), Kirki_Field::sanitize_field($field))); } else { $wp_customize->add_control(new WP_Customize_Control($wp_customize, Kirki_Field::sanitize_id($field), Kirki_Field::sanitize_field($field))); } } }
/** * Create the settings and controls from the $fields array and register them. * @var object The WordPress Customizer object */ public function add_fields($wp_customize) { $control_types = self::$control_types; $setting_types = self::$setting_types; foreach (self::$fields as $field) { if ('background' == $field['type']) { continue; } if (isset($field['settings']) && is_array($field['settings'])) { $settings = Kirki_Field::sanitize_settings($field); $defaults = Kirki_Field::sanitize_default($field); foreach ($settings as $setting_key => $setting_value) { $args = array('default' => isset($defaults[$setting_key]) ? $defaults[$setting_key] : '', 'type' => Kirki_Field::sanitize_type($field), 'capability' => Kirki_Field::sanitize_capability($field), 'transport' => Kirki_Field::sanitize_transport($field)); if (isset($field['sanitize_callback']) && is_array($field['sanitize_callback'])) { if (isset($field['sanitize_callback'][$setting_key])) { $args['sanitize_callback'] = Kirki_Field::sanitize_callback(array('sanitize_callback' => $field['sanitize_callback'][$setting_key])); } else { $args['sanitize_callback'] = Kirki_Field::sanitize_callback($field); } } $wp_customize->add_setting($setting_value, $args); } } $setting_args = array('default' => Kirki_Field::sanitize_default($field), 'type' => Kirki_Field::sanitize_type($field), 'capability' => Kirki_Field::sanitize_capability($field), 'transport' => Kirki_Field::sanitize_transport($field), 'sanitize_callback' => Kirki_Field::sanitize_callback($field)); if (isset($field['type']) && array_key_exists($field['type'], $setting_types)) { // We must instantiate a custom class for the setting $setting_classname = $setting_types[$field['type']]; $setting = new $setting_classname($wp_customize, Kirki_Field::sanitize_settings($field), $setting_args); $wp_customize->add_setting($setting); } else { $wp_customize->add_setting(Kirki_Field::sanitize_settings($field), $setting_args); } $class_name = 'WP_Customize_Control'; if (array_key_exists($field['type'], $control_types)) { $class_name = $control_types[$field['type']]; } $wp_customize->add_control(new $class_name($wp_customize, Kirki_Field::sanitize_id($field), Kirki_Field::sanitize_field($field))); } }
/** * Build the background fields. * Takes a single field with type = background and explodes it to multiple controls. * * @param array * @return null|array */ public static function explode($field) { $i18n = Kirki_Toolkit::i18n(); $choices = self::background_choices(); // Early exit if this is not a background field. if ('background' != $field['type']) { return; } // Sanitize field $field = Kirki_Field::sanitize_field($field); // No need to proceed any further if no defaults have been set. // We build the array of fields based on what default values have been defined. if (!isset($field['default']) || !is_array($field['default'])) { return; } $fields = array(); $i = 0; foreach ($field['default'] as $key => $value) { // No need to process the opacity, it is factored in the color control. if ('opacity' == $key) { continue; } $key = esc_attr($key); $setting = $key; $help = $field['help']; $description = isset($i18n['background-' . $key]) ? $i18n['background-' . $key] : ''; $output_property = 'background-' . $key; $label = 0 === $i ? $field['label'] : ''; $type = 'select'; switch ($key) { case 'color': /** * Use 'color-alpha' instead of 'color' if default is an rgba value * or if 'opacity' is set. */ $type = false !== strpos($field['default']['color'], 'rgba') ? 'color-alpha' : 'color'; $type = isset($field['default']['opacity']) ? 'color-alpha' : $type; if (isset($field['default']['opacity']) && false === strpos($value, 'rgb')) { $value = Kirki_Color::get_rgba($value, $field['default']['opacity']); } break; case 'image': $type = 'image'; break; case 'attach': /** * Small hack so that background attachments properly work. */ $output_property = 'background-attachment'; $description = $i18n['background-attachment']; break; default: $help = ''; break; } /** * If we're using options & option_name is set, then we need to modify the setting. */ if (isset($field['option_type']) && 'option' == $field['option_type'] && isset($field['option_name']) && !empty($field['option_name'])) { $property_setting = str_replace(']', '', str_replace($field['option_name'] . '[', '', $field['settings'])); $property_setting = esc_attr($field['option_name']) . '[' . esc_attr($property_setting) . '_' . $setting . ']'; } else { $property_setting = esc_attr($field['settings']) . '_' . $setting; } /** * Build the field. * We're merging with the original field here, so any extra properties are inherited. */ $fields[$property_setting] = array_merge($field, array('type' => $type, 'label' => $label, 'settings' => $property_setting, 'help' => $help, 'section' => $field['section'], 'priority' => $field['priority'], 'required' => $field['required'], 'description' => $description, 'default' => $value, 'id' => Kirki_Field::sanitize_id(array('settings' => Kirki_Field::sanitize_settings(array('settings' => $field['settings'] . '_' . $setting)))), 'choices' => isset($choices[$key]) ? $choices[$key] : array(), 'output' => '' != $field['output'] ? array(array('element' => $field['output'], 'property' => $output_property)) : '', 'sanitize_callback' => Kirki_Field::fallback_callback($type))); $i++; } return $fields; }
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 */ $field = Kirki_Field::sanitize_field($field); if (!is_array($field['output'])) { continue; } foreach ($field['output'] as $output) { if (in_array($output['property'], array('font-family', 'font-weight', 'font-subset'))) { /** * Get the value of the field */ $config_id = Kirki::get_config_id($field); $settings = $field['settings']; if ('option' == Kirki::$config[$config_id]['option_type'] && '' != Kirki::$config[$config_id]['option_name']) { $settings = str_replace(array(']', Kirki::$config[$config_id]['option_name'] . '['), '', $field['settings']); } $value = Kirki::get_option($config_id, $settings); 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; }
public static function generate_css_by_fields($fields, $placeholder = false) { // Early exit if no fields are found. if (empty($fields)) { return; } $css = array(); foreach ($fields as $field) { // Only continue if $field['output'] is set if (isset($field['output']) && !empty($field['output']) && 'background' != $field['type']) { $css = array_merge_recursive($css, self::css(Kirki_Field::sanitize_field($field))); } } // Replace all values with placeholder if ($placeholder) { foreach ($css as $media_query => $styles) { foreach ($styles as $style => $style_array) { foreach ($style_array as $property => $value) { $css[$media_query][$style][$property] = '{value}'; } } } } if (is_array($css)) { return Kirki_Output::styles_parse(Kirki_Output::add_prefixes($css)); } return; }
/** * Create the settings and controls from the $fields array and register them. * @var object The WordPress Customizer object */ public function add_fields($wp_customize) { $control_types = apply_filters('kirki/control_types', array('code' => 'Kirki_Controls_Code_Control', 'color' => 'WP_Customize_Color_Control', 'color-alpha' => 'Kirki_Controls_Color_Alpha_Control', 'image' => 'WP_Customize_Image_Control', 'upload' => 'WP_Customize_Upload_Control', 'switch' => 'Kirki_Controls_Switch_Control', 'toggle' => 'Kirki_Controls_Toggle_Control', 'radio-buttonset' => 'Kirki_Controls_Radio_ButtonSet_Control', 'radio-image' => 'Kirki_Controls_Radio_Image_Control', 'sortable' => 'Kirki_Controls_Sortable_Control', 'slider' => 'Kirki_Controls_Slider_Control', 'number' => 'Kirki_Controls_Number_Control', 'multicheck' => 'Kirki_Controls_MultiCheck_Control', 'palette' => 'Kirki_Controls_Palette_Control', 'custom' => 'Kirki_Controls_Custom_Control', 'editor' => 'Kirki_Controls_Editor_Control', 'select2' => 'Kirki_Controls_Select2_Control', 'select2-multiple' => 'Kirki_Controls_Select2_Multiple_Control', 'dimension' => 'Kirki_Controls_Dimension_Control', 'repeater' => 'Kirki_Controls_Repeater_Control')); $setting_types = apply_filters('kirki/setting_types', array('repeater' => 'Kirki_Settings_Repeater_Setting')); foreach (self::$fields as $field) { if ('background' == $field['type']) { continue; } if (isset($field['settings']) && is_array($field['settings'])) { $settings = Kirki_Field::sanitize_settings($field); $defaults = Kirki_Field::sanitize_default($field); foreach ($settings as $setting_key => $setting_value) { $args = array('default' => isset($defaults[$setting_key]) ? $defaults[$setting_key] : '', 'type' => Kirki_Field::sanitize_type($field), 'capability' => Kirki_Field::sanitize_capability($field), 'transport' => Kirki_Field::sanitize_transport($field)); if (isset($field['sanitize_callback']) && is_array($field['sanitize_callback'])) { if (isset($field['sanitize_callback'][$setting_key])) { $args['sanitize_callback'] = Kirki_Field::sanitize_callback(array('sanitize_callback' => $field['sanitize_callback'][$setting_key])); } else { $args['sanitize_callback'] = Kirki_Field::sanitize_callback($field); } } $wp_customize->add_setting($setting_value, $args); } } $setting_args = array('default' => Kirki_Field::sanitize_default($field), 'type' => Kirki_Field::sanitize_type($field), 'capability' => Kirki_Field::sanitize_capability($field), 'transport' => Kirki_Field::sanitize_transport($field), 'sanitize_callback' => Kirki_Field::sanitize_callback($field)); if (isset($field['type']) && array_key_exists($field['type'], $setting_types)) { // We must instantiate a custom class for the setting $setting_classname = $setting_types[$field['type']]; $setting = new $setting_classname($wp_customize, Kirki_Field::sanitize_settings($field), $setting_args); $wp_customize->add_setting($setting); } else { $wp_customize->add_setting(Kirki_Field::sanitize_settings($field), $setting_args); } $class_name = 'WP_Customize_Control'; if (array_key_exists($field['type'], $control_types)) { $class_name = $control_types[$field['type']]; } $wp_customize->add_control(new $class_name($wp_customize, Kirki_Field::sanitize_id($field), Kirki_Field::sanitize_field($field))); } }