public function generate_script() { global $wp_customize; // Early exit if we're not in the customizer if (!isset($wp_customize)) { return; } // Get an array of all the fields $fields = Kirki::$fields; $script = ''; // Parse the fields and create the script. foreach ($fields as $field) { $field['transport'] = Kirki_Field::sanitize_transport($field); $field['js_vars'] = Kirki_Field::sanitize_js_vars($field); if (!is_null($field['js_vars']) && 'postMessage' == $field['transport']) { foreach ($field['js_vars'] as $js_vars) { $units = !empty($js_vars['units']) ? " + '" . $js_vars['units'] . "'" : ''; $prefix = !empty($js_vars['prefix']) ? "'" . $js_vars['prefix'] . "' + " : ''; $script .= 'wp.customize( \'' . Kirki_Field::sanitize_settings($field) . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; if ('html' == $js_vars['function']) { $script .= '$(\'' . $js_vars['element'] . '\').html( newval );'; } else { $script .= '$(\'' . $js_vars['element'] . '\').' . $js_vars['function'] . '(\'' . $js_vars['property'] . '\', ' . $prefix . 'newval' . $units . ' );'; } $script .= '}); });'; } } } return $script; }
/** * 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; }
public function generate_script() { global $wp_customize; // Early exit if we're not in the customizer if (!isset($wp_customize)) { return; } // Get an array of all the fields $fields = Kirki::$fields; $script = 'jQuery( "#kirki-styles-inline-css" ).remove();'; $styles = array(); // Parse the fields and create the script. foreach ($fields as $field) { $field['transport'] = Kirki_Field::sanitize_transport($field); $field['js_vars'] = Kirki_Field::sanitize_js_vars($field); if (!empty($field['setting'])) { $inline_id = 'kirki-' . $field['setting']; $styles[] = '<style id="' . esc_attr($inline_id) . '">' . Kirki_Output::generate_css_by_fields(array($field)) . '</style>'; } if ('postMessage' == $field['transport']) { $script .= 'wp.customize( \'' . Kirki_Field::sanitize_settings($field) . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; if (!is_null($field['js_vars'])) { foreach ($field['js_vars'] as $js_vars) { if ('html' == $js_vars['function']) { $script .= '$(\'' . esc_js($js_vars['element']) . '\').html( newval );'; } elseif ('css' == $js_vars['function']) { $script .= '$(\'' . esc_js($js_vars['element']) . '\').css(\'' . esc_js($js_vars['property']) . '\', newval' . (!empty($js_vars['units']) ? ' + \'' . $js_vars['units'] . "'" : '') . ' );'; } } } else { $output_unit = ''; if (!empty($field['output']) && !empty($field['output']['units'])) { $output_unit = $field['output']['units']; } $inline_id = 'kirki-' . $field['setting']; $placeholder_inline_css = Kirki_Output::generate_css_by_fields(array($field), true); $script .= 'jQuery( "#' . $inline_id . '" )[0].innerHTML = "' . str_replace(array('{value}', '>'), array('" + newval + "' . $output_unit, '>'), esc_js($placeholder_inline_css)) . '";'; } $script .= '}); });'; } } return array($script, $styles); }
/** * Add the help bubble */ public function generate_script() { $fields = Kirki::$fields; $scripts = array(); foreach ($fields as $field) { $field['help'] = Kirki_Field::sanitize_help($field); $field['settings'] = Kirki_Field::sanitize_settings($field); if (!empty($field['help'])) { $content = "<a href='#' class='tooltip hint--left' data-hint='" . strip_tags(esc_html($field['help'])) . "'><span class='dashicons dashicons-info'></span></a>"; $scripts[] = '$( "' . $content . '" ).prependTo( "#customize-control-' . $field['settings'] . '" );'; } } // No need to echo anything if the script is empty if (empty($scripts)) { return; } // Make sure we don't add any duplicates $scripts = array_unique($scripts); // Convert array to string $script = implode('', $scripts); return $script; }
public function test_sanitize_settings() { $this->assertEquals('foo', Kirki_Field::sanitize_settings(array('settings' => 'foo'))); $this->assertEquals('foo[bar]', Kirki_Field::sanitize_settings(array('settings' => 'bar', 'option_type' => 'option', 'option_name' => 'foo'))); $this->assertEquals('foo[bar]', Kirki_Field::sanitize_settings(array('settings' => 'foo[bar]'))); }
/** * Build the variables. * * @return array ('variable-name' => value) */ public function get_variables() { $variables = array(); /** * Loop through all fields */ foreach (self::$fields as $field) { /** * Check if we have variables for this field */ if (isset($field['variables']) && false != $field['variables'] && !empty($field['variables'])) { /** * Loop through the array of variables */ foreach ($field['variables'] as $field_variable) { /** * Is the variable ['name'] defined? * If yes, then we can proceed. */ if (isset($field_variable['name'])) { /** * Sanitize the variable name */ $variable_name = esc_attr($field_variable['name']); /** * Do we have a callback function defined? * If not then set $variable_callback to false. */ $variable_callback = isset($field_variable['callback']) && is_callable($field_variable['callback']) ? $field_variable['callback'] : false; /** * If we have a variable_callback defined then get the value of the option * and run it through the callback function. * If no callback is defined (false) then just get the value. */ if ($variable_callback) { $variables[$variable_name] = call_user_func($field_variable['callback'], self::get_option(Kirki_Field::sanitize_settings($field))); } else { $variables[$variable_name] = self::get_option($field['settings']); } } } } } /** * Pass the variables through a filter ('kirki/variable') * and return the array of variables */ return apply_filters('kirki/variable', $variables); }
/** * 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; }
/** * Build the variables. * * @return array ('variable-name' => value) */ public function get_variables() { $variables = array(); foreach (self::$fields as $field) { if (isset($field['variables']) && false != $field['variables']) { foreach ($field['variables'] as $field_variable) { if (isset($field_variable['name'])) { $variable_name = esc_attr($field_variable['name']); $variable_callback = isset($field_variable['callback']) && is_callable($field_variable['callback']) ? $field_variable['callback'] : false; if ($variable_callback) { $variables[$variable_name] = call_user_func($field_variable['callback'], self::get_option(Kirki_Field::sanitize_settings($field))); } else { $variables[$variable_name] = self::get_option($field['settings']); } } } } } return apply_filters('kirki/variable', $variables); }
/** * 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); }