function google_link() { $fields = Kirki::fields()->get_all(); // Early exit if no fields are found. if (!$fields || empty($fields)) { return; } // Get an array of all the google fonts $google_fonts = Kirki::fonts()->get_google_fonts(); $fonts = array(); foreach ($fields as $field) { if (isset($field['output'])) { // Check if this is a font-family control $is_font_family = isset($field['output']['property']) && 'font-family' == $field['output']['property'] ? true : false; // Check if this is a font-weight control $is_font_weight = isset($field['output']['property']) && 'font-weight' == $field['output']['property'] ? true : false; // Check if this is a font subset control $is_font_subset = isset($field['output']['property']) && 'font-subset' == $field['output']['property'] ? true : false; if ($is_font_family || $is_font_weight || $is_font_subset) { // The value of this control $value = kirki_get_option($field['settings_raw']); if ($is_font_family) { $fonts[]['font-family'] = $value; } else { if ($is_font_weight) { $fonts[]['font-weight'] = $value; } else { if ($is_font_subset) { $fonts[]['subsets'] = $value; } } } } } } foreach ($fonts as $font) { if (isset($font['font-family'])) { $font_families = !isset($font_families) ? array() : $font_families; $font_families[] = $font['font-family']; if (Kirki::fonts()->is_google_font($font['font-family'])) { $has_google_font = true; } } if (isset($font['font-weight'])) { $font_weights = !isset($font_weights) ? array() : $font_weights; $font_weights[] = $font['font-weight']; } if (isset($font['subsets'])) { $font_subsets = !isset($font_subsets) ? array() : $font_subsets; $font_subsets[] = $font['subsets']; } } $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 $font_families ? Kirki::fonts()->get_google_font_uri($font_families, $font_weights, $font_subsets) : false; }
public function wp_footer() { 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()->get_all(); $script = ''; // Parse the fields and create the script. foreach ($fields as $field) { if (isset($field['transport']) && !is_null($field['js_vars']) && 'postMessage' == $field['transport']) { foreach ($field['js_vars'] as $js_vars) { $script .= 'wp.customize( \'' . $field['settings'] . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; 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 );'; } $script .= '}); });'; } } } if ('' != $script) { echo Kirki_Scripts_Registry::prepare($script); } }
/** * Build the customizer fields. * Parses all fields and creates the setting & control for each of them. */ public function build($wp_customize) { include_once KIRKI_PATH . '/includes/class-kirki-control.php'; include_once KIRKI_PATH . '/includes/class-kirki-controls.php'; $fields = Kirki::fields()->get_all(); // Early exit if controls are not set or if they're empty if (empty($fields)) { return; } foreach ($fields as $field) { $this->build_field($wp_customize, $field); } }
/** * Add the script to the footer */ function customize_controls_print_footer_scripts() { $fields = Kirki::fields()->get_all(); $scripts = array(); foreach ($fields as $field) { if ('number' == $field['type']) { $scripts[] = '$( "#customize-control-' . $field['settings'] . ' input[type=\'number\']").stepper();'; } } // 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); echo Kirki_Scripts_Registry::prepare($script); }
/** * Add the help bubble */ function customize_controls_print_footer_scripts() { $fields = Kirki::fields()->get_all(); $scripts = array(); $script = ''; foreach ($fields as $field) { if (!empty($field['help'])) { $bubble_content = $field['help']; $content = "<a href='#' class='tooltip hint--left' data-hint='" . strip_tags(esc_html($bubble_content)) . "'><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); echo Kirki_Scripts_Registry::prepare($script); }
/** * Get the value of a field. */ function kirki_get_option($option = '') { // Make sure the class is instanciated Kirki::get_instance(); // Get the array of all the fields. $fields = Kirki::fields()->get_all(); // Get the config. $config = Kirki::config()->get_all(); /** * If no setting has been defined then return all. */ if ('' == $option) { if ('option' == $config['options_type']) { $values = array(); foreach ($fields as $field) { $values[] = get_option($field['settings'], $field['default']); } } else { $values = get_theme_mods(); } return $values; } // If a value has been defined then we proceed. // Early exit if this option does not exist if (!isset($fields[$option])) { return; } $option_name = $fields[$option]['settings']; $default = $fields[$option]['default']; if ('option' == $config['options_type']) { $value = get_option($option_name, $default); } else { $value = get_theme_mod($option_name, $default); } return $value; }
public static function add_field($config_id, $args) { if (is_array($config_id) && empty($args)) { $args = $config_id; $config_id = 'global'; } $config_id = '' == $config_id ? 'global' : $config_id; /** * Get the configuration options */ if (!isset(Kirki::$config[$config_id])) { $config_id = 'global'; } $config = Kirki::$config[$config_id]; /** * If we've set an option in the configuration * then make sure we're using options and not theme_mods */ if ('' != $config['option_name']) { $config['option_type'] = 'option'; } /** * If no option name has been set for the field, * use the one from the configuration */ if (!isset($args['option_name'])) { $args['option_name'] = $config['option_name']; } /** * If no capability has been set for the field, * use the one from the configuration */ if (!isset($args['capability'])) { $args['capability'] = $config['capability']; } /** * Check if [settings] is set. * If not set, check for [setting]. * After this check is complete, we'll do some additional tweaking * based on whether this is an option or a theme_mod. * If an option and option_name is also defined, * then we'll have to change the setting. */ if (!isset($args['settings']) && isset($args['setting'])) { $args['settings'] = $args['setting']; } if (is_array($args['settings'])) { $settings = array(); foreach ($args['settings'] as $setting_key => $setting_value) { $settings[sanitize_key($setting_key)] = esc_attr($setting_value); if ('option' == $config['option_type'] && '' != $config['option_name'] && false === strpos($setting_key, '[')) { $settings[sanitize_key($setting_key)] = esc_attr($config['option_name']) . '[' . esc_attr($setting_value) . ']'; } } $args['settings'] = $settings; } else { if ('option' == $config['option_type'] && '' != $config['option_name'] && false === strpos($args['settings'], '[')) { $args['settings'] = esc_attr($args['option_name']) . '[' . esc_attr($args['settings']) . ']'; } else { $args['settings'] = esc_attr($args['settings']); } } /** * If no option-type has been set for the field, * use the one from the configuration */ if (!isset($args['option_type'])) { $args['option_type'] = $config['option_type']; } /** * Add the field to the static $fields variable properly indexed */ Kirki::$fields[$args['settings']] = $args; if ('background' == $args['type']) { /** * Build the background fields */ Kirki::$fields = Kirki_Explode_Background_Field::process_fields(Kirki::$fields); } }
/** * loop through all fields and create an array of style definitions */ public function loop_controls() { $fields = Kirki::fields()->get_all(); $styles = array(); // Early exit if no fields are found. if (!$fields || empty($fields)) { return; } foreach ($fields as $field) { $element = ''; $property = ''; $units = ''; $prefix = ''; $suffix = ''; $callback = ''; // Only continue if $field['output'] is set if (isset($field['output'])) { // Check if this is an array of style definitions $multiple_styles = isset($field['output'][0]['element']) ? true : false; if (!$multiple_styles) { // single style // If $field['output'] is not an array, then use the string as the target element if (is_string($field['output'])) { $element = $field['output']; } else { $element = isset($field['output']['element']) ? $field['output']['element'] : ''; $property = isset($field['output']['property']) ? $field['output']['property'] : ''; $units = isset($field['output']['units']) ? $field['output']['units'] : ''; $prefix = isset($field['output']['prefix']) ? $field['output']['prefix'] : ''; $suffix = isset($field['output']['suffix']) ? $field['output']['suffix'] : ''; $callback = isset($field['output']['callback']) ? $field['output']['callback'] : ''; } $styles = $this->setting_styles($field, $styles, $element, $property, $units, $prefix, $suffix, $callback); } else { // Multiple styles set foreach ($field['output'] as $style) { if (!array($style)) { $element = $style; } else { $element = isset($style['element']) ? $style['element'] : ''; $property = isset($style['property']) ? $style['property'] : ''; $units = isset($style['units']) ? $style['units'] : ''; $prefix = isset($style['prefix']) ? $style['prefix'] : ''; $suffix = isset($style['suffix']) ? $style['suffix'] : ''; $callback = isset($style['callback']) ? $style['callback'] : ''; } $styles = $this->setting_styles($field, $styles, $element, $property, $units, $prefix, $suffix, $callback); } } } } return $styles; }
public static function add_field($config_id, $args) { if (is_array($config_id) && empty($args)) { $args = $config_id; $config_id = 'global'; } $config_id = '' == $config_id ? 'global' : $config_id; /** * Get the configuration options */ if (!isset(Kirki::$config[$config_id])) { $config_id = 'global'; } $config = Kirki::$config[$config_id]; /** * If we've set an option in the configuration * then make sure we're using options and not theme_mods */ if ('' != $config['option_name']) { $config['option_type'] = 'option'; } /** * If no option name has been set for the field, * use the one from the configuration */ if (!isset($args['option_name'])) { $args['option_name'] = $config['option_name']; } /** * If no capability has been set for the field, * use the one from the configuration */ if (!isset($args['capability'])) { $args['capability'] = $config['capability']; } /** * Check if [settings] is set. * If not set, check for [setting] */ if (!isset($args['settings']) && isset($args['setting'])) { $args['settings'] = $args['setting']; } /** * If no option-type has been set for the field, * use the one from the configuration */ if (!isset($args['option_type'])) { $args['option_type'] = $config['option_type']; } /** * Add the field to the static $fields variable properly indexed */ Kirki::$fields[Kirki_Field_Sanitize::sanitize_settings($args)] = $args; if ('background' == $args['type']) { /** * Build the background fields */ Kirki::$fields = Kirki_Explode_Background_Field::process_fields(Kirki::$fields); } }
/** * Processes the field arguments * * @param array $whitelisted_properties Defines an array of arguments that will skip validation at this point. */ protected function set_field($whitelisted_properties = array()) { $properties = get_class_vars(__CLASS__); // Remove any whitelisted properties from above. // These will get a free pass, completely unfiltered. foreach ($whitelisted_properties as $key => $default_value) { if (isset($properties[$key])) { unset($properties[$key]); } } // Some things must run before the others. $priorities = array('option_name', 'option_type', 'settings'); foreach ($priorities as $priority) { if (method_exists($this, 'set_' . $priority)) { $method_name = 'set_' . $priority; $this->{$method_name}(); } } // Sanitize the properties, skipping the ones run from the $priorities. foreach ($properties as $property => $value) { if (in_array($property, $priorities, true)) { continue; } if (method_exists($this, 'set_' . $property)) { $method_name = 'set_' . $property; $this->{$method_name}(); } } // Get all arguments with their values. $args = get_class_vars(__CLASS__); foreach ($args as $key => $default_value) { $args[$key] = $this->{$key}; } // Add the whitelisted properties through the back door. foreach ($whitelisted_properties as $key => $default_value) { if (!isset($this->{$key})) { $this->{$key} = $default_value; } $args[$key] = $this->{$key}; } // Add the field to the static $fields variable properly indexed. Kirki::$fields[$this->settings] = $args; if ('background' === $this->type) { // Build the background fields. Kirki::$fields = Kirki_Explode_Background_Field::process_fields(Kirki::$fields); } }
public function test_get_option() { Kirki::$config = null; Kirki::$fields = null; $this->add_config(); $this->add_field(); $this->assertEquals('some-default-value', Kirki::get_option('my_config_theme_mods', 'my_setting_theme_mods')); $this->assertEquals('some-default-value', Kirki::get_option('my_config_options', 'my_setting_options')); $this->assertEquals('some-default-value', Kirki::get_option('my_config_options_serialized', 'my_option[my_setting_options_serialized]')); Kirki::$config = null; Kirki::$fields = null; $this->add_config(); $this->add_background_fields(); $this->assertEquals(array('color' => '#333333', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_theme_mods', 'my_settings_test_background_theme_mod')); $this->assertEquals(array('color' => '#333333', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_options', 'my_settings_test_background_options')); $this->assertEquals(array('color' => '#333333', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_options_serialized', 'my_option[my_settings_test_background_options_serialized]')); Kirki::$config = null; Kirki::$fields = null; $this->add_config(); $this->add_background_fields(); set_theme_mod('my_settings_test_background_theme_mod_color', '#000000'); $this->assertEquals(array('color' => '#000000', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_theme_mods', 'my_settings_test_background_theme_mod')); update_option('my_settings_test_background_options_color', '#222222'); $this->assertEquals(array('color' => '#222222', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_options', 'my_settings_test_background_options')); update_option('my_option', array('my_settings_test_background_options_serialized_color' => '#444444')); $this->assertEquals(array('color' => '#444444', 'image' => 'http://foo.com/bar.png', 'repeat' => 'no-repeat', 'size' => 'cover', 'attach' => 'scroll', 'position' => 'center-bottom', 'opacity' => '.6'), Kirki::get_option('my_config_options_serialized', 'my_option[my_settings_test_background_options_serialized]')); }
/** * Build the variables. * * @return string */ function kirki_get_variables() { // Get all fields $fields = Kirki::fields()->get_all(); $variables = array(); foreach ($fields as $field) { if (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[$field_variable['name']] = call_user_func($field_variable['callback'], kirki_get_option($field['settings'])); } else { $variables[$field_variable['name']] = kirki_get_option($field['settings']); } } } } } $variables_final = ''; foreach ($variables as $variable_name => $value) { $variables_final .= $variable_name . ': ' . $value . '; '; } return $variables_final; }
/** * Add the required script. */ function customize_controls_print_footer_scripts() { // Get an array of all our fields $fields = Kirki::fields()->get_all(); // Get the config options $config = Kirki::config()->get_all(); // Early exit if no controls are defined if (empty($fields)) { return; } $script = ''; foreach ($fields as $field) { $required = isset($field['required']) ? $field['required'] : false; if ($required) { $show = false; foreach ($required as $dependency) { // Find the type of the dependency control if ('option' == $config['options_type'] && '' != $config['option_name']) { $dependency['setting'] = $config['option_name'] . '[' . $dependency['setting'] . ']'; } $type = $fields[$dependency['setting']]['type']; // If "operator" is not set then set it to "==" if (!isset($dependency['operator'])) { $dependency['operator'] = '=='; } $dependency['operator'] = esc_js($dependency['operator']); // Set the control type $type = 'dropdown-pages' == $type ? 'select' : $type; $type = 'radio-image' == $type ? 'radio' : $type; $type = 'radio-buttonset' == $type ? 'radio' : $type; $type = 'toggle' == $type ? 'checkbox' : $type; $type = 'switch' == $type ? 'checkbox' : $type; // Set the controller used in the script $controller = '#customize-control-' . $fields[$dependency['setting']]['id'] . ' input'; $common_controller = ''; // Se the controller for select-type controls if ('select' == $type) { $controller = '#customize-control-' . $fields[$dependency['setting']]['id'] . ' select'; } elseif ('radio' == $type) { $controller = '#customize-control-' . $fields[$dependency['setting']]['id'] . ' input[value="' . $dependency['value'] . '"]'; $common_controller = '#customize-control-' . $fields[$dependency['setting']]['id'] . ' input'; } // The target element $target = '#customize-control-' . $field['id']; // if this is a background control then make sure we target all sub-controls if ('background' == $field['type']) { $target = '#customize-control-' . $control['settings'] . '_color, '; $target .= '#customize-control-' . $control['settings'] . '_image, '; $target .= '#customize-control-' . $control['settings'] . '_repeat, '; $target .= '#customize-control-' . $control['settings'] . '_size, '; $target .= '#customize-control-' . $control['settings'] . '_position, '; $target .= '#customize-control-' . $control['settings'] . '_attach'; } // If no operator has been defined, fallback to '==' if (!isset($dependency['operator'])) { $dependency['operator'] = '=='; } $action_1 = '.show()'; $action_2 = '.hide()'; // Allow checking both checked and unchecked checkboxes if ('checkbox' == $type) { if (0 == $dependency['value'] && '==' == $dependency['operator']) { $action_1 = '.hide()'; $action_2 = '.show()'; $show = true; } if (1 == $dependency['value'] && '!=' == $dependency['operator']) { $action_1 = '.hide()'; $action_2 = '.show()'; } } // Get the initial status $value = kirki_get_option($dependency['setting']); switch ($dependency['operator']) { case '==': $show = $dependency['value'] == $value ? true : $show; break; case '!=': $show = $dependency['value'] != $value ? true : $show; break; case '>=': $show = $dependency['value'] >= $value ? true : $show; break; case '<=': $show = $dependency['value'] <= $value ? true : $show; break; case '>': $show = $dependency['value'] > $value ? true : $show; break; case '<': $show = $dependency['value'] < $value ? true : $show; break; default: $show = $dependency['value'] == $value ? true : $show; } // if initial status is hidden then hide the control if (false == $show) { $script .= '$("' . $target . '").hide();'; } // Create the actual script $script .= "\$('" . ('radio' != $type ? $controller : $common_controller) . "')."; $script .= 'checkbox' == $type ? 'click' : 'change'; $script .= '(function(){'; $script .= "if (\$('" . $controller . "')."; $script .= 'select' != $type ? 'is(":checked") ) {' : 'val() ' . $dependency['operator'] . ' "' . $dependency['value'] . '") {'; $script .= "\$('" . $target . "')" . $action_1 . ';'; $script .= '} else {'; $script .= "\$('" . $target . "')" . $action_2 . ';'; $script .= '}});'; $script .= 'checkbox' != $type ? "\$('" . $controller . "')" . '.trigger("change");' : ''; } } } // If there's a script then echo it wrapped. if (!empty($script)) { echo Kirki_Scripts_Registry::prepare($script); } }
public static function add_field($config_id = 'global', $args = array()) { /** * Sanitize $config_id */ $config_id = self::sanitize_config_id($config_id, $args); $args['kirki_config'] = $config_id; /** * Get the config arguments */ $config = Kirki::$config[$config_id]; /** * Sanitize option_name */ $args['option_name'] = self::sanitize_option_name($config_id, $args); /** * If we've set an option in the configuration * then make sure we're using options and not theme_mods */ if (isset($config['option_name']) && !empty($config['option_name'])) { $args['option_type'] = 'option'; } /** * Sanitize option_type */ $args['option_type'] = self::sanitize_option_type($config_id, $args); /** * Sanitize capability */ $args['capability'] = self::sanitize_capability($config_id, $args); /** * Get the 'disable_output' argument from the config */ $args['disable_output'] = $config['disable_output']; /** * Sanitize settings */ $args['settings'] = self::sanitize_settings($config_id, $args); /** * Sanitize tooltip messages */ $args['tooltip'] = self::sanitize_tooltip($config_id, $args); /** * Sanitize active_callback */ $args['active_callback'] = self::sanitize_active_callback($config_id, $args); /** * Sanitize control type */ $args['type'] = self::sanitize_control_type($config_id, $args); /** * If no choices have been defined, use an empty array */ $args['choices'] = isset($args['choices']) ? $args['choices'] : array(); /** * Tweaks for simple controls */ if ('kirki-text' == $args['type']) { $args['type'] = 'kirki-generic'; $args['choices']['element'] = 'input'; $args['choices']['type'] = 'text'; if (!isset($args['sanitize_callback'])) { $args['sanitize_callback'] = 'wp_kses_post'; } } elseif ('kirki-textarea' == $args['type']) { $args['type'] = 'kirki-generic'; $args['choices']['element'] = 'textarea'; $args['choices']['rows'] = '5'; if (!isset($args['sanitize_callback'])) { $args['sanitize_callback'] = 'wp_kses_post'; } } if ('kirki-generic' == $args['type'] && !isset($args['choices']['element'])) { $args['choices']['element'] = 'input'; } /** * Sanitize the sanitize_callback argument. */ $args['sanitize_callback'] = self::sanitize_callback($config_id, $args); /** * set choices for color controls */ if ('kirki-color' == $args['type']) { $args['choices']['alpha'] = false; $args['type'] = 'color-alpha'; } elseif ('color-alpha' == $args['type']) { $args['choices']['alpha'] = true; } /** * If no output argument has been defined, use an empty array */ $args['output'] = isset($args['output']) ? $args['output'] : array(); /** * If "variables" has not been defined, set to null. */ $args['variables'] = isset($args['variables']) && is_array($args['variables']) ? $args['variables'] : null; /** * Sanitize the id (for internal use) */ $args['id'] = self::sanitize_id($config_id, $args); /** * Make sure the "multiple" argument is properly formatted for <select> controls */ if ('kirki-select' == $args['type']) { $args['multiple'] = isset($args['multiple']) ? intval($args['multiple']) : 1; } /** * Add the field to the static $fields variable properly indexed */ Kirki::$fields[$args['settings']] = $args; if ('background' == $args['type']) { /** * Build the background fields */ Kirki::$fields = Kirki_Explode_Background_Field::process_fields(Kirki::$fields); } }
function kirki_field_active_callback($control) { // Get all fields $fields = Kirki::fields()->get_all(); $current_field = $fields[$control->id]; if (false != $current_field['required']) { foreach ($current_field['required'] as $requirement) { $show = false; $value = $control->manager->get_setting($requirement['setting'])->value(); switch ($requirement['operator']) { case '===': $show = $requirement['value'] === $value ? true : $show; break; case '==': $show = $requirement['value'] == $value ? true : $show; break; case '!==': $show = $requirement['value'] !== $value ? true : $show; break; case '!=': $show = $requirement['value'] != $value ? true : $show; break; case '>=': $show = $requirement['value'] >= $value ? true : $show; break; case '<=': $show = $requirement['value'] <= $value ? true : $show; break; case '>': $show = $requirement['value'] > $value ? true : $show; break; case '<': $show = $requirement['value'] < $value ? true : $show; break; default: $show = $requirement['value'] == $value ? true : $show; } if (!$show) { return false; } } } else { $show = true; } return $show; }