Example #1
0
function cyberchimps_fields_callback($value)
{
    global $allowedtags;
    $option_name = 'cyberchimps_options';
    $settings = get_option($option_name);
    $val = '';
    $select_value = '';
    $checked = '';
    $output = '';
    // Set default value to $val
    if (isset($value['std'])) {
        $val = $value['std'];
    }
    // If the option is already saved, ovveride $val
    if ($value['type'] != 'heading' && $value['type'] != 'info') {
        if (isset($settings[$value['id']])) {
            $val = $settings[$value['id']];
            // Striping slashes of non-array options
            if (!is_array($val)) {
                $val = stripslashes($val);
            }
        }
    }
    // If there is no class set make it blank
    if (!isset($value['class'])) {
        $value['class'] = '';
    }
    // If there is a description save it for labels
    $explain_value = '';
    if (isset($value['desc'])) {
        $explain_value = $value['desc'];
    }
    // add extra field wrapper class
    $wrapper_class = $value['class'] ? ' ' . $value['class'] . '-container' : '';
    // field wrapper
    $output .= '<div class="field-container' . $wrapper_class . '">';
    // Output field name
    if ($value['name'] && $value['type'] != 'info' && $value['type'] != 'welcome' && $value['type'] != 'toggle') {
        $output .= '<label for="' . esc_attr($value['id']) . '">' . $value['name'] . '</label>';
    }
    switch ($value['type']) {
        // Basic text input
        case 'text':
            $output .= '<input id="' . esc_attr($value['id']) . '" class="of-input ' . esc_attr($value['class']) . '" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" type="text" value="' . esc_attr($val) . '" />';
            break;
            // Same as text but allows some basic a, href, title, br, em and strong html, check the sanitization
        // Same as text but allows some basic a, href, title, br, em and strong html, check the sanitization
        case 'text_html':
            $output .= '<input id="' . esc_attr($value['id']) . '" class="of-input ' . esc_attr($value['class']) . '" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" type="text" value="' . esc_attr($val) . '" />';
            break;
            // Textarea
        // Textarea
        case 'textarea':
            $rows = '8';
            if (isset($value['settings']['rows'])) {
                $custom_rows = $value['settings']['rows'];
                if (is_numeric($custom_rows)) {
                    $rows = $custom_rows;
                }
            }
            $val = stripslashes($val);
            $output .= '<textarea id="' . esc_attr($value['id']) . '" class="of-input" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" rows="' . $rows . '">' . esc_textarea($val) . '</textarea>';
            break;
            // Unfiltered Textarea
        // Unfiltered Textarea
        case 'unfiltered_textarea':
            $rows = '8';
            if (isset($value['settings']['rows'])) {
                $custom_rows = $value['settings']['rows'];
                if (is_numeric($custom_rows)) {
                    $rows = $custom_rows;
                }
            }
            $val = stripslashes($val);
            $output .= '<textarea id="' . esc_attr($value['id']) . '" class="of-input ' . esc_attr($value['class']) . '" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" rows="' . $rows . '">' . esc_textarea($val) . '</textarea>';
            break;
            // css Textarea
        // css Textarea
        case 'csstextarea':
            $rows = '8';
            if (isset($value['settings']['rows'])) {
                $custom_rows = $value['settings']['rows'];
                if (is_numeric($custom_rows)) {
                    $rows = $custom_rows;
                }
            }
            $val = stripslashes($val);
            $output .= '<textarea id="' . esc_attr($value['id']) . '" class="of-input" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" rows="' . $rows . '">' . strip_tags($val) . '</textarea>';
            $output .= '<div id="custom-css-msg"></div>';
            break;
            // Select Box
        // Select Box
        case 'select':
            $output .= '<select class="of-input ' . esc_attr($value['class']) . '" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" id="' . esc_attr($value['id']) . '">';
            foreach ($value['options'] as $key => $option) {
                $selected = '';
                if ($val != '') {
                    $selected = selected($val, $key, false);
                }
                $output .= '<option value="' . esc_attr($key) . '" ' . $selected . '>' . esc_html($option) . '</option>';
            }
            $output .= '</select>';
            break;
            // Radio Box
        // Radio Box
        case "radio":
            $name = $option_name . '[' . $value['id'] . ']';
            $val = $val != '' ? $val : $value['std'];
            foreach ($value['options'] as $key => $option) {
                $id = $option_name . '-' . $value['id'] . '-' . $key;
                $output .= '<div class="radio-container ' . esc_attr($value['class']) . '"><input class="of-input of-radio" type="radio" name="' . esc_attr($name) . '" id="' . esc_attr($id) . '" value="' . esc_attr($key) . '" ' . checked($val, $key, false) . ' /><label for="' . esc_attr($id) . '" class="of-radio">' . esc_html($option) . '</label></div>';
            }
            break;
            // Image Selectors
        // Image Selectors
        case "images":
            $name = $option_name . '[' . $value['id'] . ']';
            $output .= '<div class="images-radio-container">';
            foreach ($value['options'] as $key => $option) {
                $selected = '';
                $checked = '';
                if ($val != '') {
                    if ($val == $key) {
                        $selected = ' of-radio-img-selected';
                        $checked = ' checked="checked"';
                    }
                }
                $output .= '<div class="images-radio-subcontainer"><input type="radio" id="' . esc_attr($value['id'] . '_' . $key) . '" class="of-radio-img-radio" value="' . esc_attr($key) . '" name="' . esc_attr($name) . '" ' . $checked . ' />';
                $output .= '<div class="of-radio-img-label">' . esc_html($key) . '</div>';
                $output .= '<img src="' . esc_url($option) . '" alt="' . $option . '" class="of-radio-img-img' . $selected . '" onclick="document.getElementById(\'' . esc_attr($value['id'] . '_' . $key) . '\').checked=true;" /></div>';
            }
            $output .= '</div>';
            break;
            // Checkbox
        // Checkbox
        case "checkbox":
            $output .= '<div class="checkbox-container"><input id="' . esc_attr($value['id']) . '" class="checkbox of-input" type="checkbox" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" ' . checked($val, 1, false) . ' />';
            $output .= '<label class="right-label" for="' . esc_attr($value['id']) . '">' . wp_kses($explain_value, $allowedtags) . '</label></div>';
            break;
            // Multicheck
        // Multicheck
        case "multicheck":
            foreach ($value['options'] as $key => $option) {
                $checked = '';
                $label = $option;
                $option = preg_replace('/[^a-zA-Z0-9._\\-]/', '', strtolower($key));
                $id = $option_name . '-' . $value['id'] . '-' . $option;
                $name = $option_name . '[' . $value['id'] . '][' . $option . ']';
                if (isset($val[$option])) {
                    $checked = checked($val[$option], 1, false);
                }
                $output .= '<div class="checkbox-container"><input id="' . esc_attr($id) . '" class="checkbox of-input" type="checkbox" name="' . esc_attr($name) . '" ' . $checked . ' /><label for="' . esc_attr($id) . '" class="right-label">' . esc_html($label) . '</label></div>';
            }
            break;
            // Toggle Switch
        // Toggle Switch
        case "toggle":
            $checked = "";
            if ($val) {
                $checked = 'checked="checked"';
            }
            $output .= '<div class="toggle-container" id="' . esc_attr($value['id']) . '_container"><label for="' . esc_attr($value['id']) . '">' . $value['name'] . '</label><input id="' . esc_attr($value['id']) . '"' . $checked . 'class="checkbox-toggle of-input" type="checkbox" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" ' . checked($val, 1, false) . ' /></div>';
            break;
            // Color picker
        // Color picker
        case "color":
            $output .= '<div class="input-prepend ' . $value['class'] . '"><div id="' . esc_attr($value['id'] . '_picker') . '" class="add-on colorSelector"><div style="' . esc_attr('background-color:' . $val) . '"></div></div>';
            $output .= '<input class="of-color" name="' . esc_attr($option_name . '[' . $value['id'] . ']') . '" id="' . esc_attr($value['id']) . '" type="text" value="' . esc_attr($val) . '" /></div>';
            break;
            // Uploader
        // Uploader
        case "upload":
            $output .= cyberchimps_medialibrary_uploader($value['class'], $value['id'], $val, null, $explain_value);
            break;
            // Typography
        // Typography
        case 'typography':
            unset($font_size, $font_style, $font_face, $font_color);
            $typography_defaults = array('size' => '', 'face' => '', 'style' => '', 'color' => '');
            $typography_stored = wp_parse_args($val, $typography_defaults);
            $typography_options = array('sizes' => cyberchimps_recognized_font_sizes(), 'faces' => cyberchimps_recognized_font_faces(), 'styles' => cyberchimps_recognized_font_styles(), 'color' => true);
            if (isset($value['options'])) {
                $typography_options = wp_parse_args($value['options'], $typography_options);
            }
            // Font Size
            if ($typography_options['sizes']) {
                $font_size = '<select class="of-typography of-typography-size" name="' . esc_attr($option_name . '[' . $value['id'] . '][size]') . '" id="' . esc_attr($value['id'] . '_size') . '">';
                $sizes = $typography_options['sizes'];
                foreach ($sizes as $i) {
                    $size = $i . 'px';
                    $font_size .= '<option value="' . esc_attr($size) . '" ' . selected($typography_stored['size'], $size, false) . '>' . esc_html($size) . '</option>';
                }
                $font_size .= '</select>';
            }
            // Font Face
            if ($typography_options['faces']) {
                $font_face = '<select class="of-typography of-typography-face" name="' . esc_attr($option_name . '[' . $value['id'] . '][face]') . '" id="' . esc_attr($value['id'] . '_face') . '">';
                $faces = $typography_options['faces'];
                foreach ($faces as $key => $face) {
                    $font_face .= '<option value="' . esc_attr($key) . '" ' . selected($typography_stored['face'], $key, false) . '>' . esc_html($face) . '</option>';
                }
                $font_face .= '</select>';
            }
            // Font Styles
            if ($typography_options['styles']) {
                $font_style = '<select class="of-typography of-typography-style" name="' . $option_name . '[' . $value['id'] . '][style]" id="' . $value['id'] . '_style">';
                $styles = $typography_options['styles'];
                foreach ($styles as $key => $style) {
                    $font_style .= '<option value="' . esc_attr($key) . '" ' . selected($typography_stored['style'], $key, false) . '>' . $style . '</option>';
                }
                $font_style .= '</select>';
            }
            // Font Color
            if ($typography_options['color']) {
                $font_color = '<div class="input-prepend of-typography"><div id="' . esc_attr($value['id']) . '_color_picker" class="add-on colorSelector"><div style="' . esc_attr('background-color:' . $typography_stored['color']) . '"></div></div>';
                $font_color .= '<input class="of-color of-typography of-typography-color" name="' . esc_attr($option_name . '[' . $value['id'] . '][color]') . '" id="' . esc_attr($value['id'] . '_color') . '" type="text" value="' . esc_attr($typography_stored['color']) . '" /></div>';
            }
            // Allow modification/injection of typography fields
            $typography_fields = compact('font_size', 'font_face', 'font_style', 'font_color');
            $typography_fields = apply_filters('cyberchimps_typography_fields', $typography_fields, $typography_stored, $option_name, $value);
            $output .= implode('', $typography_fields);
            break;
            // Background
        // Background
        case 'background':
            $background = $val;
            // Background Color
            $output .= '<div class="input-prepend"><div id="' . esc_attr($value['id']) . '_color_picker" class="add-on colorSelector"><div style="' . esc_attr('background-color:' . $background['color']) . '"></div></div>';
            $output .= '<input class="of-color of-background of-background-color" name="' . esc_attr($option_name . '[' . $value['id'] . '][color]') . '" id="' . esc_attr($value['id'] . '_color') . '" type="text" value="' . esc_attr($background['color']) . '" /></div>';
            // Background Image - New AJAX Uploader using Media Library
            if (!isset($background['image'])) {
                $background['image'] = '';
            }
            $output .= cyberchimps_medialibrary_uploader($value['class'], $value['id'], $background['image'], null, '', 0, 'image');
            $class = 'of-background-properties';
            if ('' == $background['image']) {
                $class .= ' hide';
            }
            $output .= '<div class="' . esc_attr($class) . '">';
            // Background Repeat
            $output .= '<select class="of-background of-background-repeat" name="' . esc_attr($option_name . '[' . $value['id'] . '][repeat]') . '" id="' . esc_attr($value['id'] . '_repeat') . '">';
            $repeats = cyberchimps_recognized_background_repeat();
            foreach ($repeats as $key => $repeat) {
                $output .= '<option value="' . esc_attr($key) . '" ' . selected($background['repeat'], $key, false) . '>' . esc_html($repeat) . '</option>';
            }
            $output .= '</select>';
            // Background Position
            $output .= '<select class="of-background of-background-position" name="' . esc_attr($option_name . '[' . $value['id'] . '][position]') . '" id="' . esc_attr($value['id'] . '_position') . '">';
            $positions = cyberchimps_recognized_background_position();
            foreach ($positions as $key => $position) {
                $output .= '<option value="' . esc_attr($key) . '" ' . selected($background['position'], $key, false) . '>' . esc_html($position) . '</option>';
            }
            $output .= '</select>';
            // Background Attachment
            $output .= '<select class="of-background of-background-attachment" name="' . esc_attr($option_name . '[' . $value['id'] . '][attachment]') . '" id="' . esc_attr($value['id'] . '_attachment') . '">';
            $attachments = cyberchimps_recognized_background_attachment();
            foreach ($attachments as $key => $attachment) {
                $output .= '<option value="' . esc_attr($key) . '" ' . selected($background['attachment'], $key, false) . '>' . esc_html($attachment) . '</option>';
            }
            $output .= '</select>';
            $output .= '</div>';
            break;
            // Editor
        // Editor
        case 'editor':
            $output .= '<div class="explain">' . wp_kses($explain_value, $allowedtags) . '</div>' . "\n";
            echo $output;
            $textarea_name = esc_attr($option_name . '[' . $value['id'] . ']');
            $default_editor_settings = array('textarea_name' => $textarea_name, 'media_buttons' => false, 'tinymce' => array('plugins' => 'wordpress'));
            $editor_settings = array();
            if (isset($value['settings'])) {
                $editor_settings = $value['settings'];
            }
            $editor_settings = array_merge($default_editor_settings, $editor_settings);
            wp_editor($val, $value['id'], $editor_settings);
            $output = '';
            break;
            // Info
        // Info
        case "info":
            $id = '';
            $class = 'section';
            if (isset($value['id'])) {
                $id = 'id="' . esc_attr($value['id']) . '" ';
            }
            if (isset($value['type'])) {
                $class .= ' section-' . $value['type'];
            }
            if (isset($value['class'])) {
                $class .= ' ' . $value['class'];
            }
            $output .= '<div ' . $id . 'class="' . esc_attr($class) . '">' . "\n";
            if (isset($value['name'])) {
                $output .= '<h4 class="heading">' . esc_html($value['name']) . '</h4>' . "\n";
            }
            if ($value['desc']) {
                $output .= apply_filters('cyberchimps_sanitize_info', $value['desc']) . "\n";
            }
            $output .= '</div>' . "\n";
            break;
            // Welcome
        // Welcome
        case "welcome":
            $id = '';
            $class = 'section';
            if (isset($value['id'])) {
                $id = 'id="' . esc_attr($value['id']) . '" ';
            }
            if (isset($value['type'])) {
                $class .= ' section-' . $value['type'];
            }
            if (isset($value['class'])) {
                $class .= ' ' . $value['class'];
            }
            $output .= '<div ' . $id . 'class="' . esc_attr($class) . '">' . "\n";
            if (isset($value['name'])) {
                $output .= '<h4 class="heading">' . esc_html(apply_filters('cyberchimps_help_sub_heading', $value['name'])) . '</h4>' . "\n";
            }
            $output .= apply_filters('cyberchimps_sanitize_info', apply_filters('cyberchimps_help_description', '')) . "\n";
            $output .= '</div>' . "\n";
            break;
        case "export":
            $output .= "<textarea rows='10'>" . esc_html(serialize($settings)) . "</textarea>";
            $output .= '<br/><a class="btn btn-info export-option" href="data:text/octet-stream;charset=utf-8,' . esc_html(serialize($settings)) . '" download="theme-option-backup.txt">Download</a>';
            break;
        case "import":
            $output .= "<textarea name='import' rows='10'></textarea>";
            $output .= "<br/><input type='file' id='import_file' name='import_file' />";
            break;
    }
    if ($value['type'] != "heading" && $value['type'] != "info" && $value['type'] != "welcome" && $value['type'] != "upload") {
        if ($value['type'] != "checkbox" && $value['type'] != "editor") {
            $output .= '<div class="desc">' . wp_kses($explain_value, $allowedtags) . '</div>' . "\n";
        }
    }
    // end field wrapper
    $output .= '</div>';
    echo $output;
}
function cyberchimps_sanitize_background_position($value)
{
    $recognized = cyberchimps_recognized_background_position();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('cyberchimps_default_background_position', current($recognized));
}