';
}
}
}
/**
* custom function that checks if the section has been run through add_settings_section() function
* returns bool value true if section exists and false if it does not
*/
function cyberchimps_section_exists( $heading, $section ) {
global $wp_settings_sections;
if ( isset( $wp_settings_sections[$heading][$section] ) ) {
return true;
}
return false;
}
function cyberchimps_create_fields( $fields ) {
if ( empty($fields) )
return false;
// loop through and create each field
foreach ($fields as $field_args) {
$field_defaults = array(
'id' => false,
'name' => __('Default Field', 'cyberchimps'),
'callback' => 'cyberchimps_fields_callback',
'section' => 'cyberchimps_default_section',
'heading' => 'cyberchimps_default_heading',
);
$field_args = wp_parse_args( $field_args, $field_defaults );
if ( empty($field_args['id']) ) {
continue;
} elseif ( !cyberchimps_section_exists( $field_args['heading'], $field_args['section']) ){
continue;
} else {
add_settings_field(
$field_args['id'],
$field_args['name'],
$field_args['callback'],
$field_args['heading'],
$field_args['section'],
$field_args
);
}
}
}
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'];
}
// field wrapper
$output .= '
';
echo $output;
}
/**
* Validate options
*
* Validate theme options before updating to database.
*/
function cyberchimps_options_validate( $input ) {
// Theme option import functionality
if( isset( $_POST['import' ] ) ) {
if( trim( $_POST['import' ] ) ) {
$string = stripslashes( trim( $_POST['import'] ) );
$try = unserialize( $string );
if($try) {
add_settings_error( 'cyberchimps_options', 'imported_success', __( 'Options Imported', 'cyberchimps' ), 'updated fade' );
return $try;
} else {
add_settings_error( 'cyberchimps_options', 'imported_failed', __( 'Invalid Data for Import', 'cyberchimps' ), 'error fade' );
}
}
}
/*
* Restore Defaults.
*
* In the event that the user clicked the "Restore Defaults"
* button, the options defined in the theme's options.php
* file will be added to the option for the active theme.
*/
if ( isset( $_POST['reset'] ) ) {
add_settings_error( 'cyberchimps_options', 'restore_defaults', __( 'Default options restored.', 'cyberchimps' ), 'updated fade' );
return cyberchimps_get_default_values();
}
/*
* Update Settings
*
* This used to check for $_POST['update'], but has been updated
* to be compatible with the theme customizer introduced in WordPress 3.4
*/
else {
$clean = array();
$options = cyberchimps_get_fields();
foreach ( $options as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
$id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
// Set upload to false if it wasn't sent in the $_POST
if ( 'info' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set upload to false if it wasn't sent in the $_POST
if ( 'upload' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set radio to false if it wasn't sent in the $_POST
if ( 'radio' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set toggle to false if it wasn't sent in the $_POST
if ( 'toggle' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set checkbox to false if it wasn't sent in the $_POST
if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set checkbox to false if it wasn't sent in the $_POST
if ( 'images' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = false;
}
// Set each item in the multicheck to false if it wasn't sent in the $_POST
if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
foreach ( $option['options'] as $key => $value ) {
$input[$id][$key] = false;
}
}
// For a value to be submitted to database it must pass through a sanitization filter
if ( has_filter( 'cyberchimps_sanitize_' . $option['type'] ) ) {
$clean[$id] = apply_filters( 'cyberchimps_sanitize_' . $option['type'], $input[$id], $option );
}
}
add_settings_error( 'cyberchimps_options', 'save_options', __( 'Options saved.', 'cyberchimps' ), 'updated fade' );
return $clean;
}
}
/**
* Format Configuration Array.
*
* Get an array of all default values as set in
* options.php. The 'id','std' and 'type' keys need
* to be defined in the configuration array. In the
* event that these keys are not present the option
* will not be included in this function's output.
*
* @return array Rey-keyed options configuration array.
*
* @access private
*/
function cyberchimps_get_default_values() {
$output = array();
$config = cyberchimps_get_fields();
foreach ( (array) $config as $option ) {
if ( ! isset( $option['id'] ) ) {
continue;
}
if ( ! isset( $option['std'] ) ) {
continue;
}
if ( ! isset( $option['type'] ) ) {
continue;
}
if ( has_filter( 'cyberchimps_sanitize_' . $option['type'] ) ) {
$output[$option['id']] = apply_filters( 'cyberchimps_sanitize_' . $option['type'], $option['std'], $option );
}
}
return $output;
}
/**
* Add Theme Options menu item to Admin Bar.
*/
function cyberchimps_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'parent' => 'appearance',
'id' => 'cyberchimps_options_page',
'title' => __( 'Theme Options', 'cyberchimps' ),
'href' => admin_url( 'themes.php?page=cyberchimps-theme-options' )
));
}