';
echo $output;
}
/**
* Validate options
*
* Validate theme options before updating to database.
*/
function cyberchimps_options_validate( $input ) {
global $wp_filesystem;
/*
* Import functionality
*
* Both the copy/paste and file upload options are active. First it checks for file, if any file is uploaded then
* it processes that. Otherwise it checks if anything is sent with the textarea for the import.
*/
// Check if any file is uplaoded
if( isset( $_FILES['import_file'] ) && $_FILES['import_file']['name'] ) {
// Initialise WP filesystem.
WP_Filesystem( request_filesystem_credentials( 'options.php', '', false, false, null ) );
// Get the text of the uploaded file and trim it to remove space from either end.
$import_file_text = trim( $wp_filesystem->get_contents( $_FILES['import_file']['tmp_name'] ) );
if( $import_file_text ) {
$string = stripslashes( $import_file_text );
// check string is serialized and unserialize it
if( is_serialized( $string ) ) {
$try = unserialize( ( $string ) );
}
// make sure $try is set with the unserialized data
if( $try ) {
add_settings_error( 'cyberchimps_options', 'imported_success', __( 'Options Imported', 'cyberchimps_core' ), 'updated fade' );
return $try;
}
else {
add_settings_error( 'cyberchimps_options', 'imported_failed', __( 'Invalid Data for Import', 'cyberchimps_core' ), 'error fade' );
}
}
}
// If no file is uploaded then check for the texarea field for improt option.
else {
if( isset( $_POST['import'] ) ) {
if( trim( $_POST['import'] ) ) {
$string = stripslashes( trim( $_POST['import'] ) );
// check string is serialized and unserialize it
if( is_serialized( $string ) ) {
$try = unserialize( ( $string ) );
}
// make sure $try is set with the unserialized data
if( $try ) {
add_settings_error( 'cyberchimps_options', 'imported_success', __( 'Options Imported', 'cyberchimps_core' ), 'updated fade' );
return $try;
}
else {
add_settings_error( 'cyberchimps_options', 'imported_failed', __( 'Invalid Data for Import', 'cyberchimps_core' ), '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_core' ), '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;
}
}
// Catch any other id's that have not been set and set them to false. For some themes we remove options and we don't want them causing a problem
if( !isset( $input[$id] ) ) {
$input[$id] = 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 );
}
}
do_action( 'cyberchimps_options_before_save', $input );
add_settings_error( 'cyberchimps_options', 'save_options', __( 'Options saved.', 'cyberchimps_core' ), '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_core' ),
'href' => admin_url( 'themes.php?page=cyberchimps-theme-options' )
) );
}