TitanFramework::addCustomizerSaveFilter PHP Method

addCustomizerSaveFilter() public method

This uses the pre_update_option filter to check all the options being saved if it's a theme_mod option. It further checks whether these are Titan customizer options, then attaches the new hook into those.
See also: pre_update_option filter
Since: 1.8
public addCustomizerSaveFilter ( mixed $value, string $optionName, mixed $oldValue ) : mixed
$value mixed The value to be saved in the options.
$optionName string The option name.
$oldValue mixed The previously stored value.
return mixed The modified value to save
    public function addCustomizerSaveFilter($value, $optionName, $oldValue)
    {
        $theme = get_option('stylesheet');
        // Intercept theme mods only.
        if (strpos($optionName, 'theme_mods_' . $theme) !== 0) {
            return $value;
        }
        // We expect theme mods to be an array.
        if (!is_array($value)) {
            return $value;
        }
        // Checks whether a Titan customizer is in place.
        $customizerUsed = false;
        // Go through all our customizer options and filter them for saving.
        $optionIDs = array();
        if (!empty($this->mainContainers['customizer'])) {
            foreach ($this->mainContainers['customizer'] as $customizer) {
                foreach ($customizer->options as $option) {
                    if (!empty($option->settings['id'])) {
                        $optionID = $option->settings['id'];
                        $themeModName = $this->optionNamespace . '_' . $option->settings['id'];
                        if (!array_key_exists($themeModName, $value)) {
                            continue;
                        }
                        $customizerUsed = true;
                        // Try and unserialize if possible.
                        $tempValue = $value[$themeModName];
                        if (is_serialized($tempValue)) {
                            $tempValue = unserialize($tempValue);
                        }
                        // Hook 'tf_save_option_{namespace}'.
                        $newValue = apply_filters('tf_save_option_' . $this->optionNamespace, $tempValue, $option->settings['id']);
                        // Hook 'tf_save_option_{namespace}_{optionID}'.
                        $newValue = apply_filters('tf_save_option_' . $themeModName, $tempValue);
                        // We mainly check for equality here so that we won't have to serialize IF the value wasn't touched anyway.
                        if ($newValue != $tempValue) {
                            if (is_array($newValue)) {
                                $newValue = serialize($newValue);
                            }
                            $value[$themeModName] = $newValue;
                        }
                    }
                }
            }
        }
        if ($customizerUsed) {
            /** This action is documented in class-admin-page.php */
            $namespace = $this->optionNamespace;
            do_action("tf_pre_save_options_{$namespace}", $this->mainContainers['customizer']);
        }
        return $value;
    }