checkboxes = array();
$this->settings = array();
/* Get the theme settings. */
$this->framework_get_settings();
/* Create the theme setting pages. */
$this->sections['general'] = sprintf( __( 'General Settings', 'wordsmith' ) );
$this->sections['styling'] = sprintf( __( 'Styling Settings', 'wordsmith' ) );
/* Add the admin setup function to the 'admin_menu' hook. */
add_action( 'admin_menu', array( &$this, 'framework_admin_setup' ) );
/* Add the register settings function to the 'admin_init' hook. */
add_action( 'admin_init', array( &$this, 'framework_register_settings' ) );
/* Intialize our theme settings. */
if ( ! get_option( $prefix . '_theme_settings' ) )
$this->framework_initialize_settings();
}
/* Sets up the adminstration functionality for the framework. */
function framework_admin_setup() {
/* Get the theme information. */
$theme = wp_get_theme();
/* Create the theme settings page. */
$framework->settings_page = add_theme_page(
sprintf( esc_html__( '%s Theme Settings', 'wordsmith' ), $theme->get( 'Name' ) ), /* Settings page name. */
esc_html__( 'Theme Settings', 'wordsmith' ), /* Menu item name. */
'edit_theme_options', /* Required capability. */
'theme-settings', /* Screen name. */
array( &$this, 'framework_settings_page' ) /* Callback function. */
);
/* Add the JavaScript and stylesheets needed for the theme settings screen. */
add_action( 'admin_print_scripts-' . $framework->settings_page, array( &$this, 'framework_settings_page_enqueue_scripts' ) );
add_action( 'admin_print_styles-' . $framework->settings_page, array( &$this, 'framework_settings_page_enqueue_styles' ) );
/* Add the stylesheet on the widgets page. */
add_action( 'load-widgets.php', array( &$this, 'framework_settings_page_enqueue_styles' ) );
}
/* Create the settings field. */
function framework_create_setting( $args = array() ) {
/* Get the theme information. */
$theme = wp_get_theme();
/* Default field settings. */
$defaults = array(
'choices' => array(),
'class' => '',
'default' => '',
'description' => '',
'id' => '',
'section' => '',
'title' => '',
'type' => ''
);
/* Merge the user-selected arguments with the defaults. */
extract( wp_parse_args( $args, $defaults ) );
/* Settings field arguments. */
$field_args = array(
'choices' => $choices,
'class' => $class,
'description' => $description,
'default' => $default,
'id' => esc_attr( framework_settings_field_id( $id ) ),
'label_for' => esc_attr( framework_settings_field_id( $id ) ),
'type' => $type
);
/* If a checkbox is being used. */
if ( $type == 'checkbox' )
$this->checkboxes[] = $id;
/* Add the settings fields with the above arguments. */
add_settings_field( $id, $title, array( $this, 'framework_display_settings' ), 'theme-settings', $section, $field_args );
}
/* Initializes all the theme settings page functionality. */
function framework_settings_page() {
/* Get the theme information. */
$theme = wp_get_theme();
/* Get the theme prefix. */
$prefix = framework_get_prefix();
echo '
';
echo screen_icon();
echo '
';
printf( __( '%s Theme Settings', 'wordsmith' ), $theme->get( 'Name' ) );
echo '
';
settings_errors();
echo '';
echo '';
}
function framework_display_section() {}
/* HTML output for various field types. */
function framework_display_settings( $args = array() ) {
extract( $args );
/* Get the theme information. */
$theme = wp_get_theme();
/* Get the theme prefix. */
$prefix = framework_get_prefix();
/* Get the settings so they can be displayed. */
$options = get_option( $prefix . '_theme_settings' );
if ( !isset( $options[$id] ) && $type != 'checkbox' )
$options[$id] = $default;
elseif ( !isset( $options[$id] ) )
$options[$id] = 0;
$field_class = '';
if ( $class != '' )
$field_class = ' ' . $class;
switch ( $type ) {
/* Categories */
case 'categories' :
echo '';
wp_dropdown_categories(
array(
'id' => $id,
'name' => esc_attr( framework_settings_field_name( $id ) ),
'selected' => $options[$id],
'show_option_none' => ( __( '— Select —', 'wordsmith' ) )
)
);
echo '
';
if ( $description != '' )
echo '' . $description . '';
break;
/* Checkbox */
case 'checkbox' :
echo '';
echo '';
break;
/* Color Picker */
case 'color' :
echo '';
echo '';
if ( $description != '' )
echo '' . $description . '';
break;
/* Editor */
case 'editor' :
wp_editor(
esc_textarea( $options[$id] ),
esc_attr( framework_settings_field_id( $id ) ),
$settings = array(
'media_buttons' => false,
'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close' ),
'textarea_name' => esc_attr( framework_settings_field_name( $id ) ),
'tinymce' => false
)
);
if ( $description != '' )
echo '' . $description . '';
break;
/* Pages */
case 'pages' :
echo '';
wp_dropdown_pages(
array(
'id' => $id,
'name' => esc_attr( framework_settings_field_name( $id ) ),
'selected' => $options[$id],
'show_option_none' => ( __( '— Select —', 'wordsmith' ) )
)
);
echo '
';
if ( $description != '' )
echo '' . $description . '';
break;
/* Radio */
case 'radio' :
$i = 0;
foreach ( $choices as $value => $label ) {
echo '';
echo '';
if ( $i < count( $options ) - 1 )
echo '
';
$i++;
}
break;
/* Select */
case 'select' :
echo '';
echo '';
echo '
';
if ( $description != '' )
echo '' . $description . '';
break;
/* Text Input */
case 'text' :
default : echo '';
if ( $description != '' )
echo '' . $description . '';
break;
/* Text Input - Number */
case 'number' :
default : echo '';
if ( $description != '' )
echo '' . $description . '';
break;
/* Textarea */
case 'textarea' :
echo '';
if ( $description != '' )
echo '' . $description . '';
break;
/* Upload */
case 'upload':
default : echo '';
echo '';
if ( $description != '' )
echo '' . $description . '';
if ( esc_attr( $options[$id] ) )
echo '';
break;
}
}
/* Get the theme settings. */
function framework_get_settings() {
/* Get the theme information. */
$theme = wp_get_theme();
/* Load the theme settings. */
require_once( trailingslashit( FRAMEWORK_ADMIN ) . 'theme-settings.php' );
}
/* Initialize settings to their default values. */
function framework_initialize_settings() {
$prefix = framework_get_prefix();
$default_settings = array();
foreach ( $this->settings as $id => $setting ) {
if ( $setting['type'] != 'heading' )
$default_settings[$id] = $setting['default'];
}
update_option( $prefix . '_theme_settings', $default_settings );
}
/* Register the theme settings. */
function framework_register_settings() {
/* Get the theme prefix. */
$prefix = framework_get_prefix();
/* Register and validate theme settings. */
register_setting( $prefix . '_theme_settings', $prefix . '_theme_settings', array ( &$this, 'framework_validate_settings' ) );
foreach ( $this->sections as $slug => $title ) {
add_settings_section( $slug, $title, array( &$this, 'framework_display_section' ), 'theme-settings' );
}
foreach ( $this->settings as $id => $setting ) {
$setting['id'] = $id;
$this->framework_create_setting( $setting );
}
}
/* Loads the required scripts for the theme settings page. */
function framework_settings_page_enqueue_scripts() {
wp_enqueue_script( 'admin', trailingslashit( FRAMEWORK_URI ) . 'admin/assets/js/admin.js' );
wp_enqueue_script( 'color-picker', trailingslashit( FRAMEWORK_URI ) . 'admin/assets/js/color-picker.js' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'thickbox' );
}
/* Loads the required stylesheets for the theme settings page. */
function framework_settings_page_enqueue_styles() {
wp_enqueue_style( 'admin', trailingslashit( FRAMEWORK_URI ) . 'admin/assets/css/admin.css' );
wp_enqueue_style( 'color-picker', trailingslashit( FRAMEWORK_URI ) . 'admin/assets/css/color-picker.css' );
wp_enqueue_style( 'thickbox' );
}
/* Validate the theme settings. */
function framework_validate_settings( $input ) {
/* Get the theme information. */
$theme = wp_get_theme();
/* Get the theme prefix. */
$prefix = framework_get_prefix();
/* Get theme settings. */
$options = get_option( $prefix . '_theme_settings' );
foreach ( $this->checkboxes as $id ) {
if ( isset( $options[$id] ) && !isset( $input[$id] ) )
unset( $options[$id] );
}
/* Add settings error. */
add_settings_error( "{$prefix}_theme_settings", 'save_options', sprintf( __( '%s settings have been successfully updated.', 'wordsmith' ), $theme->get( 'Name' ) ), 'updated options-saved' );
return $input;
}
}
/* Creates a settings field id attribute for use on the theme settings page. */
function framework_settings_field_id( $setting ) {
return sanitize_html_class( $setting );
}
/* Creates a settings field name attribute for use on the theme settings page. */
function framework_settings_field_name( $setting ) {
return framework_get_prefix() . "_theme_settings[{$setting}]";
}
?>