<?php
/**
 * A unique identifier is defined to store the options in the database and reference them from the theme.
 * By default it uses the theme name, in lowercase and without spaces, but this can be changed if needed.
 * If the identifier changes, it'll appear as if the options have been reset.
 * 
 */

function optionsframework_option_name() {

	// This gets the theme name from the stylesheet (lowercase and without spaces)
	$theme = wp_get_theme();
	$themename = $theme->Name;
	$themename = preg_replace("/\W/", "", strtolower($themename) );
	
	$optionsframework_settings = get_option('optionsframework');
	$optionsframework_settings['id'] = $themename;
	update_option('optionsframework', $optionsframework_settings);
}

/*
 * Allow the Script tag in the theme options panel
 */
add_action('admin_init','optionscheck_change_santiziation', 100);

function optionscheck_change_santiziation() {
    remove_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
    add_filter( 'of_sanitize_textarea', 'custom_sanitize_textarea' );
}
 
function custom_sanitize_textarea($input) {
    global $allowedposttags;
    
      $custom_allowedtags["script"] = array();
 
      $custom_allowedtags = array_merge($custom_allowedtags, $allowedposttags);
      $output = wp_kses( $input, $custom_allowedtags);
    return $output;
}

/**
 * Defines an array of options that will be used to generate the settings page and be saved in the database.
 * When creating the "id" fields, make sure to use all lowercase and no spaces.
 *  
 */

function optionsframework_options() {
	
	// Post Featured Image Size
	$post_featured_image_size = array("large" => "Large", "small" => "Small");
	
	// Slideshow Transition Effect
	$slideshow_effect = array("slide" => "Slide", "fade" => "Fade");
			
	// If using image radio buttons, define a directory path
	$imagepath =  get_bloginfo('stylesheet_directory') . '/images/';
		
	$options = array();
		
	$options[] = array( "name" => __('General','morgan'),
						"type" => "heading");	
	
	$options['logo'] = array( "name" => __('Logo','morgan'),
						"desc" => __('Upload a custom logo.','morgan'),
						"id" => "logo",
						"type" => "upload");
						
	$options['morgan_favicon'] = array( "name" => __('Favicon','morgan'),
						"desc" => __('Upload a custom favicon.','morgan'),
						"id" => "morgan_favicon",
						"type" => "upload");					

	$options['morgan_home_message'] = array( "name" => __('Home Message','morgan'),
						"desc" => __('Enter a short message to be displayed on the home page.','morgan'),
						"id" => "morgan_home_message",
						"std" => "LOVE GOD. SERVE OTHERS.",
						"type" => "textarea");
	
	$options[] = array( "name" => __('Number of Home Features to Show','morgan'),
						"desc" => __('Enter the number of features to show on the home page.','morgan'),
						"id" => "morgan_features_count",
						"std" => "3",
						"type" => "text");	
						
	$options['morgan_custom_css'] = array( "name" => __('Custom CSS','morgan'),
						"desc" => __('Enter custom CSS here.','morgan'),
						"id" => "morgan_custom_css",
						"std" => "",
						"type" => "textarea");								
							
	$options['morgan_analytics'] = array( "name" => __('Analytics','morgan'),
						"desc" => __('Enter your custom analytics code. (e.g. Google Analytics).','morgan'),
						"id" => "morgan_analytics",
						"std" => "",
						"type" => "textarea",
						"validate" => "none");
						
	$options[] = array( "name" => __('Appearance','morgan'),
						"type" => "heading");
							
	$options['morgan_color_scheme'] = array( "name" => __('Header Color','morgan'),
						"desc" => __('Select a color for the site header.','morgan'),
						"id" => "morgan_color_scheme",
						"std" => "#332a2b",
						"type" => "color");
							
	$options['morgan_accent_color'] = array( "name" => __('Accent Color','morgan'),
						"desc" => __('Select a color for the site accents.','morgan'),
						"id" => "morgan_accent_color",
						"std" => "#b43c24",
						"type" => "color");
										
	$options['morgan_color_link'] = array( "name" => __('Link Color','morgan'),
						"desc" => __('Select a color for your links.','morgan'),
						"id" => "morgan_color_link",
						"std" => "#1a5971",
						"type" => "color");

	$options['morgan_color_link_hover'] = array( "name" => __('Link Hover Color','morgan'),
						"desc" => __('Select a hover color for your links.','morgan'),
						"id" => "morgan_color_link_hover",
						"std" => "#b43c24",
						"type" => "color");
						
	$options['morgan_heading_font'] = array( "name" => __('Font for Headings','morgan'),
						"desc" => __('Enter the name of the <a href="http://www.google.com/webfonts" target="_blank">Google Web Font</a> you want to use for headings.','morgan'),
						"id" => "morgan_heading_font",
						"std" => "Kaushan Script",
						"type" => "text");
						
	$options['morgan_body_font'] = array( "name" => __('Font for Body Text','morgan'),
						"desc" => __('Enter the name of the <a href="http://www.google.com/webfonts" target="_blank">Google Web Font</a> you want to use for the body text.','morgan'),
						"id" => "morgan_body_font",
						"std" => "Lato",
						"type" => "text");
						
	$options['morgan_home_message_font'] = array( "name" => __('Font for Home Message','morgan'),
						"desc" => __('Enter the name of the <a href="http://www.google.com/webfonts" target="_blank">Google Web Font</a> you want to use for the home message text.','morgan'),
						"id" => "morgan_home_message_font",
						"std" => "Lato",
						"type" => "text");
/* Slideshow */																							
	$options[] = array( "name" => __('Slideshow','morgan'),
						"type" => "heading");
						
	$options['morgan_slideshow_enabled'] = array( "name" => __('Enable Slideshow','morgan'),
						"desc" => __('Check this box to enable the home page slideshow.','morgan'),
						"id" => "morgan_slideshow_enabled",
						"std" => "1",
						"type" => "checkbox");				

	$options['morgan_slideshow_delay'] = array( "name" => __('Slideshow Delay','morgan'),
						"desc" => __('Enter the delay in seconds between slides. Enter 0 to disable auto-playing.','morgan'),
						"id" => "morgan_slideshow_delay",
						"std" => "6",
						"type" => "text");

	$options['morgan_slideshow_effect'] = array( "name" => __('Slideshow Effect','morgan'),
						"desc" => __('Select the type of transition effect for the slideshow.','morgan'),
						"id" => "morgan_slideshow_effect",
						"std" => "fade",
						"type" => "select",
						"options" => $slideshow_effect);	
	/* Contact Links */
	$options[] = array( "name" => __('Contacts','morgan'),
						"type" => "heading");

	$options['morgan_street'] = array( 'name' => __('Street Address', 'morgan'),
                'desc' => __('Enter the street: e.g., 4700 NW 10th St', 'morgan'),
                'id' => 'morgan_street',
                'std' => '',
                'type' => 'text');
				
	$options['morgan_address'] = array( 'name' => __('City, State, and ZIP', 'morgan'),
                'desc' => __('Enter the city, state, and zip on one line: e.g., Oklahoma City, OK 73127', 'morgan'),
                'id' => 'morgan_address',
                'std' => '',
                'type' => 'text');

	$options['morgan_phone'] = array( 'name' => __('Phone', 'morgan'),
                'desc' => __('Enter the phone number.', 'morgan'),
                'id' => 'morgan_phone',
                'std' => '',
                'type' => 'text');
				
	$options['social_fb'] = array( 'name' => __('Facebook', 'morgan'),
                'desc' => __('Enter the full url to your page', 'morgan'),
                'id' => 'social_fb',
                'std' => '',
                'type' => 'text');

	$options['social_twitter'] = array( 'name' => __('Twitter', 'morgan'),
                'desc' => __('Enter the full url to your page', 'morgan'),
                'id' => 'social_twitter',
                'std' => '',
                'type' => 'text');

	$options['social_gplus'] = array( 'name' => __('Google+', 'morgan'),
                'desc' => __('Enter the full url to your page', 'morgan'),
                'id' => 'social_gplus',
                'std' => '',
                'type' => 'text');

	$options['social_email'] = array( 'name' => __('Email', 'morgan'),
                'desc' => __('Enter your email address', 'morgan'),
                'id' => 'social_email',
                'std' => '',
                'type' => 'text');
																										
	$options[] = array( "name" => __('Footer','morgan'),
						"type" => "heading");
						
	$options['morgan_footer_left'] = array( "name" => __('Left Footer Text','morgan'),
						"desc" => __('This will appear on the left side of the footer.','morgan'),
						"id" => "morgan_footer_left",
						"std" => "",
						"type" => "textarea");

	$options['morgan_footer_right'] = array( "name" => __('Right Footer Text','morgan'),
						"desc" => __('This will appear on the right side of the footer.','morgan'),
						"id" => "morgan_footer_right",
						"std" => "",
						"type" => "textarea");
							
	return $options;
}

/**
 * Front End Customizer
 *
 * WordPress 3.4 Required
 */

add_action( 'customize_register', 'morgan_customizer_register' );

function morgan_customizer_register($wp_customize) {
	
	// This gets the theme name from the stylesheet (lowercase and without spaces)
	$theme = wp_get_theme();
	$themename = $theme->Name;
	$themename = preg_replace("/\W/", "", strtolower($themename) );
	
	/**
	 * This is optional, but if you want to reuse some of the defaults
	 * or values you already have built in the options panel, you
	 * can load them into $options for easy reference
	 */
	 
	$options = optionsframework_options();

	$wp_customize->add_section( 'morgan_general', array(
		'title' => __( 'General', 'morgan' ),
		'priority' => 25
	) );

	$wp_customize->add_setting( $themename.'[logo]', array(
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'logo', array(
		'label' => $options['logo']['name'],
		'section' => 'morgan_general',
		'settings' => $themename.'[logo]'
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_favicon]', array(
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'morgan_favicon', array(
		'label' => $options['morgan_favicon']['name'],
		'section' => 'morgan_general',
		'settings' => $themename.'[morgan_favicon]'
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_home_message]', array(
		'default' => $options['morgan_home_message']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_customizer_home_message', array(
		'label' => $options['morgan_home_message']['name'],
		'section' => 'morgan_general',
		'settings' => $themename.'[morgan_home_message]',
		'type' => 'text'
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_custom_css]', array(
		'default' => $options['morgan_custom_css']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Textarea_Control( $wp_customize, 'morgan_customizer_custom_css', array(
		'label' => $options['morgan_custom_css']['name'],
		'section' => 'morgan_general',
		'settings' => $themename.'[morgan_custom_css]',
		'type' => $options['morgan_custom_css']['type']
	) ) );
	
	/* Appearance */
	$wp_customize->add_section( 'morgan_appearance', array(
		'title' => __( 'Appearance', 'morgan' ),
		'priority' => 26
	) );

	$wp_customize->add_setting( $themename.'[morgan_color_scheme]', array(
		'default' => $options['morgan_color_scheme']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'morgan_color_scheme', array(
		'label'   => $options['morgan_color_scheme']['name'],
		'section' => 'morgan_appearance',
		'settings'   => $themename.'[morgan_color_scheme]'
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_accent_color]', array(
		'default' => $options['morgan_accent_color']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'morgan_accent_color', array(
		'label'   => $options['morgan_accent_color']['name'],
		'section' => 'morgan_appearance',
		'settings'   => $themename.'[morgan_accent_color]'
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_color_link]', array(
		'default' => $options['morgan_color_link']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'morgan_color_link', array(
		'label'   => $options['morgan_color_link']['name'],
		'section' => 'morgan_appearance',
		'settings'   => $themename.'[morgan_color_link]'
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_color_link_hover]', array(
		'default' => $options['morgan_color_link_hover']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'morgan_color_link_hover', array(
		'label'   => $options['morgan_color_link_hover']['name'],
		'section' => 'morgan_appearance',
		'settings'   => $themename.'[morgan_color_link_hover]'
	) ) );
	
	/* Typography */
	$wp_customize->add_section( 'morgan_typography', array(
		'title' => __( 'Typography', 'morgan' ),
		'priority' => 27
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_heading_font]', array(
		'default' => $options['morgan_heading_font']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_customizer_heading_font', array(
		'label' => $options['morgan_heading_font']['name'],
		'section' => 'morgan_typography',
		'settings' => $themename.'[morgan_heading_font]',
		'type' => $options['morgan_heading_font']['type']
	) );

	$wp_customize->add_setting( $themename.'[morgan_body_font]', array(
		'default' => $options['morgan_body_font']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_customizer_body_font', array(
		'label' => $options['morgan_body_font']['name'],
		'section' => 'morgan_typography',
		'settings' => $themename.'[morgan_body_font]',
		'type' => $options['morgan_body_font']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_home_message_font]', array(
		'default' => $options['morgan_home_message_font']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_customizer_home_message_font', array(
		'label' => $options['morgan_home_message_font']['name'],
		'section' => 'morgan_typography',
		'settings' => $themename.'[morgan_home_message_font]',
		'type' => $options['morgan_home_message_font']['type']
	) );

	/* Slideshow */
	$wp_customize->add_section( 'morgan_slideshow', array(
		'title' => __( 'Slideshow', 'morgan' ),
		'priority' => 28
	) );

	$wp_customize->add_setting( $themename.'[morgan_slideshow_enabled]', array(
		'default' => $options['morgan_slideshow_enabled']['std'],
		'type' => 'option'
	) );

	$wp_customize->add_control( 'morgan_slideshow_enabled', array(
		'label' => $options['morgan_slideshow_enabled']['name'],
		'section' => 'morgan_slideshow',
		'settings' => $themename.'[morgan_slideshow_enabled]',
		'type' => $options['morgan_slideshow_enabled']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_slideshow_delay]', array(
		'default' => $options['morgan_slideshow_delay']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_customizer_slideshow_delay', array(
		'label' => $options['morgan_slideshow_delay']['name'],
		'section' => 'morgan_slideshow',
		'settings' => $themename.'[morgan_slideshow_delay]',
		'type' => $options['morgan_slideshow_delay']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_slideshow_effect]', array(
		'default' => $options['morgan_slideshow_effect']['std'],
		'type' => 'option'
	) );

	$wp_customize->add_control( 'morgan_customizer_slideshow_effect', array(
		'label' => $options['morgan_slideshow_effect']['name'],
		'section' => 'morgan_slideshow',
		'settings' => $themename.'[morgan_slideshow_effect]',
		'type' => $options['morgan_slideshow_effect']['type'],
		'choices' => $options['morgan_slideshow_effect']['options']
	) );
	
	/* Social Links */
	$wp_customize->add_section( 'morgan_social', array(
		'title' => __( 'Contacts', 'morgan' ),
		'priority' => 29
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_address]', array(
		'default' => $options['morgan_address']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_address', array(
		'label' => $options['morgan_address']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[morgan_address]',
		'type' => $options['morgan_address']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_street]', array(
		'default' => $options['morgan_street']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_street', array(
		'label' => $options['morgan_street']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[morgan_street]',
		'type' => $options['morgan_street']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_phone]', array(
		'default' => $options['morgan_phone']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'morgan_phone', array(
		'label' => $options['morgan_phone']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[morgan_phone]',
		'type' => $options['morgan_phone']['type']
	) );

	
	$wp_customize->add_setting( $themename.'[social_fb]', array(
		'default' => $options['social_fb']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'social_fb', array(
		'label' => $options['social_fb']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[social_fb]',
		'type' => $options['social_fb']['type']
	) );

	$wp_customize->add_setting( $themename.'[social_twitter]', array(
		'default' => $options['social_twitter']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'social_twitter', array(
		'label' => $options['social_twitter']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[social_twitter]',
		'type' => $options['social_twitter']['type']
	) );
	
	$wp_customize->add_setting( $themename.'[social_gplus]', array(
		'default' => $options['social_gplus']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'social_gplus', array(
		'label' => $options['social_gplus']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[social_gplus]',
		'type' => $options['social_gplus']['type']
	) );
	

	$wp_customize->add_setting( $themename.'[social_email]', array(
		'default' => $options['social_email']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( 'social_email', array(
		'label' => $options['social_email']['name'],
		'section' => 'morgan_social',
		'settings' => $themename.'[social_email]',
		'type' => $options['social_email']['type']
	) );
	
	/* Footer */
	$wp_customize->add_section( 'morgan_footer', array(
		'title' => __( 'Footer', 'morgan' ),
		'priority' => 31
	) );
	
	$wp_customize->add_setting( $themename.'[morgan_footer_left]', array(
		'default' => $options['morgan_footer_left']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Textarea_Control( $wp_customize, 'morgan_customizer_footer_left', array(
		'label' => $options['morgan_footer_left']['name'],
		'section' => 'morgan_footer',
		'settings' => $themename.'[morgan_footer_left]',
		'type' => $options['morgan_footer_left']['type']
	) ) );
	
	$wp_customize->add_setting( $themename.'[morgan_footer_right]', array(
		'default' => $options['morgan_footer_right']['std'],
		'type' => 'option'
	) );
	
	$wp_customize->add_control( new WP_Customize_Textarea_Control( $wp_customize, 'morgan_customizer_footer_right', array(
		'label' => $options['morgan_footer_right']['name'],
		'section' => 'morgan_footer',
		'settings' => $themename.'[morgan_footer_right]',
		'type' => $options['morgan_footer_right']['type']
	) ) );
	
}