public function test_build_background_fields() { $this->assertEquals($this->final_fields(), Kirki_Explode_Background_Field::process_fields($this->initial_fields())); }
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); } }
/** * 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 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); } }
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); } }