/**
 * Registers options in the Theme Customizer.
 *
 * Adds theme options to the Theme Customizer by looping through
 * each settings section and implementing controls for option types
 * that are allowed to be added to the Customizer.
 */
function upfw_customize_register($wp_customize)
{
    /**
     * Globalize the variable that holds
     * the Settings Page tab definitions
     *
     * @global	array	Settings Page Tab definitions
     */
    global $up_tabs;
    /**
     * Register each tab section in the Theme Customizer
     *
     * Loops through each option tab and creates a customizer section.
     */
    foreach ($up_tabs as $tab) {
        $tabname = $tab['name'];
        $tabsections = $tab['sections'];
        foreach ($tabsections as $section) {
            $sectionname = $section['name'];
            $sectiontitle = $section['title'];
            $sectiondescription = $section['description'];
            $wp_customize->add_section($sectionname, array('title' => $sectiontitle, 'description' => $sectiondescription));
        }
    }
    $upfw_options = upfw_get_options();
    $upfw_option_parameters = upfw_get_option_parameters();
    foreach ($upfw_option_parameters as $option) {
        if ($option['type'] == 'editor' || $option['type'] == 'multicheck') {
            continue;
        }
        $optionname = $option['name'];
        $theme_id = upfw_get_current_theme_id();
        $optiondb = "theme_{$theme_id}_options[{$optionname}]";
        $option_section_name = $option['section'];
        $default = '';
        if (isset($option['default'])) {
            $default = $option['default'];
        }
        $wp_customize->add_setting($optiondb, array('default' => $default, 'type' => 'option', 'capabilities' => 'edit_theme_options'));
        if ($option['type'] == 'text') {
            $wp_customize->add_control($option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb, 'type' => 'text'));
        }
        if ($option['type'] == 'textarea') {
            $wp_customize->add_control(new UpThemes_Customize_Textarea_Control($wp_customize, $option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb)));
        }
        if ($option['type'] == 'color') {
            $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb)));
        }
        if ($option['type'] == 'upload' || $option['type'] == 'image') {
            $wp_customize->add_control(new UpThemes_Customize_Image_Control($wp_customize, $option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb, 'context' => $option['name'])));
        }
        if ($option['type'] == 'radio_image') {
            $wp_customize->add_control(new UpThemes_Image_Radio_Control($wp_customize, $option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb, 'choices' => upfw_extract_valid_options_radio_image($option['valid_options']))));
        }
        if ($option['type'] == 'radio' || $option['type'] == 'select') {
            $wp_customize->add_control($option['name'], array('label' => $option['title'], 'section' => $option_section_name, 'settings' => $optiondb, 'type' => $option['type'], 'choices' => upfw_extract_valid_options($option['valid_options'])));
        }
    }
}
/**
 * Callback for get_settings_field()
 */
function upfw_setting_callback($option)
{
    global $upfw_custom_callbacks;
    $upfw_options = (array) upfw_get_options();
    $option_parameters = upfw_get_option_parameters();
    $optionname = $option['name'];
    $optiontitle = $option['title'];
    $optiondescription = $option['description'];
    $fieldtype = $option['type'];
    $fieldname = "theme_" . upfw_get_current_theme_id() . "_options[{$optionname}]";
    $attr = $option_parameters[$option['name']];
    $value = $upfw_options[$optionname];
    //Determine the type of input field
    switch ($fieldtype) {
        //Render Text Input
        case 'text':
            upfw_text($value, $attr);
            break;
            //Render textarea options
        //Render textarea options
        case 'textarea':
            upfw_textarea($value, $attr);
            break;
            //Render wordpress editor options
        //Render wordpress editor options
        case 'editor':
            upfw_editor($value, $attr);
            break;
            //Render select dropdowns
        //Render select dropdowns
        case 'select':
            upfw_select($value, $attr);
            break;
            //Render radio image dropdowns
        //Render radio image dropdowns
        case 'radio':
            upfw_radio($value, $attr);
            break;
            //Render radio image dropdowns
        //Render radio image dropdowns
        case 'radio_image':
            upfw_radio_image($value, $attr);
            break;
            //Render checkboxes
        //Render checkboxes
        case 'multicheck':
            upfw_multicheck($value, $attr);
            break;
            //Render color picker
        //Render color picker
        case 'color':
            upfw_color($value, $attr);
            break;
            //Render upload image
        //Render upload image
        case 'image':
            upfw_upload($value, $attr);
            break;
            //Render upload
        //Render upload
        case 'upload':
            upfw_upload($value, $attr);
            break;
        default:
            break;
    }
    // Check if there is a callback to envoke for custom fields
    if (isset($upfw_custom_callbacks[$fieldtype])) {
        $custom_field_name = 'theme_' . upfw_get_current_theme_id() . '_options[' . $attr['name'] . ']';
        call_user_func($upfw_custom_callbacks[$fieldtype], $value, $attr, $custom_field_name);
    }
}
Beispiel #3
0
/**
 * Separate settings by tab
 *
 * Returns an array of tabs, each of
 * which is an indexed array of settings
 * included with the specified tab.
 *
 * @uses	upfw_get_option_parameters()	defined in options.php
 *
 * @return	array	$settingsbytab	array of arrays of settings by tab
 */
function upfw_get_settings_by_tab()
{
    global $up_tabs;
    // Initialize an array to hold
    // an indexed array of tabnames
    $settingsbytab = array();
    // Loop through the array of tabs
    foreach ($up_tabs as $tab) {
        $tabname = $tab['name'];
        // Add an indexed array key
        // to the settings-by-tab
        // array for each tab name
        $tabs[] = $tabname;
    }
    // Get the array of option parameters
    $option_parameters = upfw_get_option_parameters();
    // Loop through the option parameters
    // array
    foreach ($option_parameters as $option_parameter) {
        $optiontab = $option_parameter['tab'];
        $optionname = $option_parameter['name'];
        // Add an indexed array key to the
        // settings-by-tab array for each
        // setting associated with each tab
        $settingsbytab[$optiontab][] = $optionname;
        $settingsbytab['all'][] = $optionname;
    }
    // Return the settings-by-tab
    // array
    return $settingsbytab;
}
/**
 * Validate and sanitize options on save
 *
 * This function iterates through our options, sanitizes them, and
 * saves them to the database.
 *
 * @uses has_filter()
 * @uses apply_filters()
 *
 */
function upfw_options_validate($input)
{
    global $up_tabs;
    // This is the "whitelist": current settings
    $valid_input = (array) upfw_get_options();
    // Get the array of Theme settings, by Settings Page tab
    $settingsbytab = upfw_get_settings_by_tab();
    // Get the array of option parameters
    $option_parameters = upfw_get_option_parameters();
    // Get the array of option defaults
    $option_defaults = upfw_get_option_defaults();
    // Get list of tabs
    // Determine what type of submit was input
    $submittype = 'submit';
    foreach ($up_tabs as $tab) {
        $resetname = 'reset-' . $tab['name'];
        if (!empty($input[$resetname])) {
            $submittype = 'reset';
        }
    }
    // Determine what tab was input
    $submittab = '';
    foreach ($up_tabs as $tab) {
        $submitname = 'submit-' . $tab['name'];
        $resetname = 'reset-' . $tab['name'];
        if (!empty($input[$submitname]) || !empty($input[$resetname])) {
            $submittab = $tab['name'];
        }
    }
    global $wp_customize;
    // Get settings by tab
    $tabsettings = isset($wp_customize) ? $settingsbytab['all'] : $settingsbytab[$submittab];
    // Loop through each tab setting
    foreach ($tabsettings as $setting) {
        // If no option is selected, set the default
        $valid_input[$setting] = !isset($input[$setting]) ? $option_defaults[$setting] : $input[$setting];
        // If submit, validate/sanitize $input
        if ('submit' == $submittype) {
            // Get the setting details from the defaults array
            $option = $option_parameters[$setting];
            // Get the array of valid options, if applicable
            $valid_options = isset($option['valid_options']) ? $option['valid_options'] : false;
            if (!isset($option['name'])) {
                continue;
            }
            if (!isset($option['type'])) {
                continue;
            }
            $setting = preg_replace('/[^a-zA-Z0-9._\\-]/', '', strtolower($option['name']));
            // Set checkbox to false if it wasn't sent in the $_POST
            if ('checkbox' == $option['type'] && !isset($input[$setting])) {
                $input[$setting] = false;
            }
            // Set each item in the multicheck to false if it wasn't sent in the $_POST
            if ('multicheck' == $option['type'] && !isset($input[$setting])) {
                foreach ($option['valid_options'] as $key => $value) {
                    $input[$setting][$key] = false;
                }
            }
            // For a value to be submitted to database it must pass through a sanitization filter
            if (has_filter('upfw_sanitize_' . $option['type'])) {
                $clean[$setting] = apply_filters('upfw_sanitize_' . $option['type'], $input[$setting], $option);
            }
        } elseif ('reset' == $submittype) {
            // Set $setting to the default value
            $clean[$setting] = $option_defaults[$setting];
        }
    }
    // Hook to run after validation
    do_action('upfw_after_validate', $clean);
    return $clean;
}
/**
 * Callback for get_settings_field()
 */
function upfw_setting_callback($option)
{
    $upfw_options = upfw_get_options();
    //print_r($upfw_options);
    $option_parameters = upfw_get_option_parameters();
    $optionname = $option['name'];
    $optiontitle = $option['title'];
    $optiondescription = $option['description'];
    $fieldtype = $option['type'];
    $fieldname = "theme_" . get_current_theme_id() . "_options[{$optionname}]";
    $attr = $option_parameters[$option['name']];
    $value = $upfw_options[$optionname];
    //Determine the type of input field
    switch ($fieldtype) {
        //Render Text Input
        case 'text':
            upfw_text_field($value, $attr);
            break;
            //Render Custom User Text Inputs
        //Render Custom User Text Inputs
        case 'text_list':
            upfw_text_list($value, $attr);
            break;
            //Render textarea options
        //Render textarea options
        case 'textarea':
            upfw_textarea($value, $attr);
            break;
            //Render select dropdowns
        //Render select dropdowns
        case 'select':
            upfw_select($value, $attr);
            break;
            //Render multple selects
        //Render multple selects
        case 'multiple':
            upfw_multiple($value, $attr);
            break;
            //Render checkboxes
        //Render checkboxes
        case 'checkbox':
            upfw_checkbox($value, $attr);
            break;
            //Render color picker
        //Render color picker
        case 'color':
            upfw_color($value, $attr);
            break;
            //Render upload image
        //Render upload image
        case 'image':
            upfw_image($value, $attr);
            break;
            //Render category dropdown
        //Render category dropdown
        case 'category':
            upfw_category($value, $attr);
            break;
            //Render categories multiple select
        //Render categories multiple select
        case 'categories':
            upfw_categories($value, $attr);
            break;
            //Render page dropdown
        //Render page dropdown
        case 'page':
            upfw_page($value, $attr);
            break;
            //Render pages muliple select
        //Render pages muliple select
        case 'pages':
            upfw_pages($value, $attr);
            break;
            //Render Form Button
        //Render Form Button
        case 'submit':
            upfw_submit($value, $attr);
            break;
            //Render taxonomy multiple select
        //Render taxonomy multiple select
        case 'taxonomy':
            upfw_taxonomy($value, $attr);
            break;
            //Render Typography Selector
        //Render Typography Selector
        case 'typography':
            upfw_typography($value, $attr);
            break;
            //Render Style Selector
        //Render Style Selector
        case 'styles':
            upfw_style($value, $attr);
            break;
            //Render Form Button
        //Render Form Button
        case 'button':
            upfw_button($value, $attr);
            break;
            //Render Text Input
        //Render Text Input
        case 'divider':
            upfw_divider($value, $attr);
            break;
            //Render Layouts
        //Render Layouts
        case 'layouts':
            upfw_layouts($value, $attr);
            break;
        default:
            break;
    }
}