public function add_settings($args) { if (isset($args['settings']) && is_array($args['settings'])) { $settings = Lessekirki_Field_Sanitize::sanitize_settings($args); $defaults = isset($args['default']) ? $args['default'] : array(); $sanitize_callback = Lessekirki_Field_Sanitize::sanitize_callback($args); foreach ($settings as $setting_key => $setting_value) { $default = isset($defaults[$setting_key]) ? $defaults[$setting_key] : ''; $type = Lessekirki_Field_Sanitize::sanitize_type($args); $capability = Lessekirki_Field_Sanitize::sanitize_capability($args); $transport = isset($args['transport']) ? $args['transport'] : 'refresh'; if (isset($args['sanitize_callback']) && is_array($args['sanitize_callback'])) { if (isset($args['sanitize_callback'][$setting_key])) { $sanitize_callback = Lessekirki_Field_Sanitize::sanitize_callback(array('sanitize_callback' => $args['sanitize_callback'][$setting_key])); } } $this->wp_customize->add_setting($setting_value, array('default' => $default, 'type' => $type, 'capability' => $capability, 'sanitize_callback' => $sanitize_callback, 'transport' => $transport)); } } if (isset($args['type']) && array_key_exists($args['type'], Lessekirki_Control::$setting_types)) { // We must instantiate a custom class for the setting $setting_classname = Lessekirki_Control::$setting_types[$args['type']]; $this->wp_customize->add_setting(new $setting_classname($this->wp_customize, Lessekirki_Field_Sanitize::sanitize_settings($args), array('default' => isset($args['default']) ? $args['default'] : '', 'type' => Lessekirki_Field_Sanitize::sanitize_type($args), 'capability' => Lessekirki_Field_Sanitize::sanitize_capability($args), 'transport' => isset($args['transport']) ? $args['transport'] : 'refresh', 'sanitize_callback' => Lessekirki_Field_Sanitize::sanitize_callback($args)))); } else { $this->wp_customize->add_setting(Lessekirki_Field_Sanitize::sanitize_settings($args), array('default' => isset($args['default']) ? $args['default'] : '', 'type' => Lessekirki_Field_Sanitize::sanitize_type($args), 'capability' => Lessekirki_Field_Sanitize::sanitize_capability($args), 'transport' => isset($args['transport']) ? $args['transport'] : 'refresh', 'sanitize_callback' => Lessekirki_Field_Sanitize::sanitize_callback($args))); } }
/** * 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_sanitized = Lessekirki_Field_Sanitize::sanitize_field($field); /** * Set class vars */ self::$settings = $field_sanitized['settings']; self::$callback = $field_sanitized['sanitize_callback']; self::$field_type = $field_sanitized['type']; self::$output = $field_sanitized['output']; if (!is_array(self::$output)) { self::$output = array(array('element' => self::$output, 'sanitize_callback' => null)); } /** * Get the value of this field */ self::$value = Lessekirki_Values::get_sanitized_field_value($field_sanitized); /** * Returns the styles */ return self::styles(); }
/** * Generates the scripts needed for postMessage. * This works on a per-field basis. * Once created, the script is added to the $postmessage_script property. * * @param array the field definition * @return void */ public static function generate_script($args = array()) { $script = ''; /** * Make sure "transport" is defined */ $args['transport'] = isset($args['transport']) ? $args['transport'] : 'refresh'; /** * Make sure that we need to proceed */ if (isset($args['js_vars']) && 'postMessage' == $args['transport']) { /** * Make sure that "js_vars" is an array. * If not, then early exit with return. */ if (!is_array($args['js_vars']) || empty($args['js_vars'])) { return; } /** * Start looping through all the "js_vars" items in the array. * Documentation on how to use the "js_vars" argument and its syntax * can be found on https://github.com/aristath/lessekirki/wiki/js_vars */ foreach ($args['js_vars'] as $js_vars) { /** * Sanitize the arguments */ $js_vars = array('element' => isset($js_vars['element']) ? sanitize_text_field($js_vars['element']) : '', 'function' => isset($js_vars['function']) ? esc_js($js_vars['function']) : '', 'property' => isset($js_vars['property']) ? esc_js($js_vars['property']) : '', 'units' => isset($js_vars['units']) ? esc_js($js_vars['units']) : '', 'prefix' => isset($js_vars['prefix']) ? esc_js($js_vars['prefix']) : '', 'suffix' => isset($js_vars['suffix']) ? esc_js($js_vars['suffix']) : ''); $settings = Lessekirki_Field_Sanitize::sanitize_settings($args); $units = !empty($js_vars['units']) ? " + '" . $js_vars['units'] . "'" : ''; $prefix = !empty($js_vars['prefix']) ? "'" . $js_vars['prefix'] . "' + " : ''; $suffix = !empty($js_vars['suffix']) ? " + '" . $js_vars['suffix'] . "'" : ''; $script .= 'wp.customize( \'' . $settings . '\', function( value ) {'; $script .= 'value.bind( function( newval ) {'; if ('html' == $js_vars['function']) { $script .= '$(\'' . $js_vars['element'] . '\').html( newval );'; } else { if ('style' == $js_vars['function']) { $styleID = uniqid('lessekirki-style-'); $script .= 'if( !$(\'#' . $styleID . '\').size() ) {'; $script .= '$(\'head\').append(\'<style id="' . $styleID . '"></style>\');'; $script .= '}'; $script .= 'if( newval !== \'\') {'; $script .= '$(\'#' . $styleID . '\').text(\'' . $js_vars['element'] . '{ ' . $js_vars['property'] . ':' . $prefix . '\' + newval + \'' . $units . $suffix . ';}\');'; $script .= '}else{'; $script .= '$(\'#' . $styleID . '\').text(\'\');'; $script .= "}"; } else { $script .= '$(\'' . $js_vars['element'] . '\').' . $js_vars['function'] . '(\'' . $js_vars['property'] . '\', ' . $prefix . 'newval' . $units . $suffix . ' );'; } } $script .= '}); });'; } } self::$postmessage_script .= $script; }
/** * Generates the scripts needed for tooltips. * This works on a per-field basis. * Once created, the script is added to the $tooltip_script property. * * @param array the field definition * @return void */ public static function generate_script($args = array()) { /** * The following control types already have the "help" argument in them * and they don't need an extra implementation in order to be rendered. * We're going to ignore these control-types and only process the rest. */ $ready_controls = array('checkbox', 'code', 'color-alpha', 'custom', 'dimension', 'editor', 'multicheck', 'number', 'palette', 'radio-buttonset', 'radio-image', 'radio', 'lessekirki-radio', 'repeater', 'select', 'lessekirki-select', 'select2', 'select2-multiple', 'slider', 'sortable', 'spacing', 'switch', 'textarea', 'toggle', 'typography'); /** * Make sure the field-type has been defined. * If it has not been defined the we don't know what to do with it and should exit. * No error is displayed, we just won't do anything. */ if (isset($args['type']) && in_array($args['type'], $ready_controls)) { return; } $args['settings'] = Lessekirki_Field_Sanitize::sanitize_settings($args); $script = ''; if (isset($args['help']) && !empty($args['help'])) { $content = "<a href='#' class='tooltip hint--left' data-hint='" . wp_strip_all_tags($args['help']) . "'><span class='dashicons dashicons-info'></span></a>"; $script = '$( "' . $content . '" ).prependTo( "#customize-control-' . $args['settings'] . '" );'; } self::$tooltip_script .= $script; }
/** * Add the control. * * @param $arg array * @return void */ public function add_control($args) { $control_class_name = self::control_class_name($args); $this->wp_customize->add_control(new $control_class_name($this->wp_customize, Lessekirki_Field_Sanitize::sanitize_id($args), Lessekirki_Field_Sanitize::sanitize_field($args))); }
/** * Build the variables. * * @return array ('variable-name' => value) */ public function get_variables() { $variables = array(); /** * Loop through all fields */ foreach (Lessekirki::$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'], Lessekirki::get_option(Lessekirki_Field_Sanitize::sanitize_settings($field))); } else { $variables[$variable_name] = Lessekirki::get_option($field['settings']); } } } } } /** * Pass the variables through a filter ('lessekirki/variable') * and return the array of variables */ return apply_filters('lessekirki/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 = Lessekirki_Toolkit::i18n(); $choices = self::background_choices(); // Early exit if this is not a background field. if ('background' != $field['type']) { return; } // Sanitize field $field = Lessekirki_Field_Sanitize::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'; $sanitize_callback = 'esc_attr'; 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 = Lessekirki_Color::get_rgba($value, $field['default']['opacity']); } $sanitize_callback = array('Lessekirki_Sanitize_Values', 'color'); break; case 'image': $type = 'image'; $sanitize_callback = 'esc_url_raw'; 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' => Lessekirki_Field_Sanitize::sanitize_id(array('settings' => Lessekirki_Field_Sanitize::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' => isset($sanitize_callback) ? $sanitize_callback : Lessekirki_Field_Sanitize::fallback_callback($type))); $i++; } return $fields; }
public function google_link() { /** * Get the array of fields from the Lessekirki object. */ $fields = Lessekirki::$fields; /** * Early exit if no fields are found. */ if (empty($fields)) { return; } $fonts = array(); /** * Run a loop for our fields */ foreach ($fields as $field) { /** * Sanitize the field */ $field = Lessekirki_Field_Sanitize::sanitize_field($field); /** * No reason to proceed any further if no 'output' has been defined * or if it's not defined as an array. */ if (!isset($field['output']) || !is_array($field['output'])) { continue; } /** * Run through each of our "output" items in the array separately. */ foreach ($field['output'] as $output) { $valid = false; /** * If the field-type exists and is set to "typography" * then we need some extra checks to figure out if we need to proceed. */ if (isset($field['type']) && 'typography' == $field['type']) { if (isset($field['choices']) && isset($field['choices']['font-family']) && $field['choices']['font-family']) { $valid = true; } } /** * Check if the "property" of this item is related to typography. */ if (isset($output['property']) && in_array($output['property'], array('font-family', 'font-weight', 'font-subset'))) { $valid = true; } /** * If the $valid var is not true, then we don't need to proceed. * Continue to the next item in the array. */ if (!$valid) { continue; } /** * Get the value of this field */ $value = Lessekirki_Values::get_sanitized_field_value($field); /** * Typography fields arew a bit more complex than usual fields. * We need to get the sub-items of the array * and then base our calculations on these. */ if ('typography' == $field['type']) { /** * Add the font-family to the array */ if (isset($value['font-family'])) { $fonts[]['font-family'] = $value['font-family']; } /** * Add the font-weight to the array */ if (isset($value['font-weight'])) { $fonts[]['font-weight'] = $value['font-weight']; } } else { 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; } } } } } } /** * Start going through all the items in the $fonts array. */ 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']; /** * Determine if we need to create a google-fonts link or not. */ if (!isset($has_google_font)) { if (Lessekirki_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 some sane defaults. */ $font_families = !isset($font_families) || empty($font_families) ? false : $font_families; $font_weights = !isset($font_weights) || empty($font_weights) ? array('400') : $font_weights; $font_subsets = !isset($font_subsets) || empty($font_subsets) ? array('all') : $font_subsets; /** * Get rid of duplicate values */ if (is_array($font_families) && !empty($font_families)) { $font_families = array_unique($font_families); } if (is_array($font_weights) && !empty($font_weights)) { $font_weights = array_unique($font_weights); } if (is_array($font_subsets) && !empty($font_subsets)) { $font_subsets = array_unique($font_subsets); } if (!isset($has_google_font) || !$has_google_font) { $font_families = false; } // Return the font URL. return $font_families ? Lessekirki_Toolkit::fonts()->get_google_font_uri($font_families, $font_weights, $font_subsets) : false; }
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(Lessekirki::$config[$config_id])) { $config_id = 'global'; } $config = Lessekirki::$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 */ Lessekirki::$fields[Lessekirki_Field_Sanitize::sanitize_settings($args)] = $args; if ('background' == $args['type']) { /** * Build the background fields */ Lessekirki::$fields = Lessekirki_Explode_Background_Field::process_fields(Lessekirki::$fields); } }