<?php


/************************ Theme Initial setup ***********************************/

add_action ('after_setup_theme','softlights_theme_setup');


function softlights_theme_setup () {

add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 150, true);
add_theme_support( 'automatic-feed-links' );

/****** Setup Theme background ***************/

$softlights_bg_defaults = array(
	'default-color'          => '',
	'default-image'          => '',
	'default-repeat'         => '',
	'default-position-x'     => '',
	'wp-head-callback'       => '_custom_background_cb',
	'admin-head-callback'    => '',
	'admin-preview-callback' => ''
);
add_theme_support( 'custom-background', $softlights_bg_defaults );

/************** Setup Custom Header to upload Logo *****************/

$softlights_header_defaults = array(
	'default-image'          => '',
	'default-color'		=> '#fff',
	'random-default'         => false,
	'width'                  => 1600,
	'height'                 => 130,
	'flex-height'            => true,
	'flex-width'             => true,
	'default-text-color'     => '',
	'header-text'            => false,
	'uploads'                => true,
	'wp-head-callback'       => '',
	'admin-head-callback'    => '',
	'admin-preview-callback' => '',
);
add_theme_support( 'custom-header', $softlights_header_defaults );

if ( ! isset( $content_width ) )
	$content_width = 672;
	
$softlights_settings = get_option('softlights-options');

if($softlights_settings === false) {
	$softlights_settings = softlights_defaults();
}
	
update_option('softlights-options', $softlights_settings);

	
}	

/****************** Initialize Theme Default Values **************************************/

function softlights_defaults() {

$defaults = array (
				'logo' => '',
				'favicon' => '',
				'footer' => '',
				
				'showcase-image' => '',
				'showcase-title' => '',
				'showcase-text' => '',
				'showcase-link' => '',
				'menu-color' => '#caeffc',
				'header-color' => 'transparent',
				'menu-font-color' => '#333',
				'widgets-menus-color' => '',
				'footer-color' => '#b7d3e5',
				'link-color' => '#000',
				'content-link-color' => '#cc0000',
				
				'showcase-color' => '#f5fefe',
				'showcase-font-color' => '#777',
				'showcase' => '0',
				'comments' => '1',
				'reset' => '0'
				);
				
return $defaults;					
	
}	
	

/********************** Adjust content width for full page templates *******************************/

function softlights_content_width() {
	
	global $content_width;
	
	
	if(is_page_template( 'full-width.php' ) ) 
		$content_width = 1150;
			
	
}

add_action( 'template_redirect', 'softlights_content_width' ); 

/********************************* Excerpts fix *******************************/

add_filter('excerpt_more', 'softlights_new_excerpt_more');

function softlights_new_excerpt_more( $more ) {
	return '';
}

function softlights_excerpt() {

 if( strpos( get_the_content(), 'more-link' ) === false ) {
                    the_excerpt();
                }
                else {
                    the_content('Continue Reading &raquo;');
                }	
	
}


/***************************** Admin Theme Options Scripts *************************/

add_action( 'appearance_page_softlights-theme-options', 'softlights_admin_scripts' );

function softlights_admin_scripts($hook) {
	


    wp_enqueue_script('jquery');
    wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_script( 'sl-script-handle', get_template_directory_uri().'/js/myscript.js', array( 'wp-color-picker','jquery' ), false, true );
    wp_enqueue_style ('admin-css', get_template_directory_uri() . '/admin-style.css');
     
   	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_enqueue_style('thickbox');
	wp_enqueue_script('uploader', get_template_directory_uri().'/js/uploader.js');
}



/************************* Theme Custom Colors ************************************/

function softlights_custom_styles() {
	
	
	$softlights_options = get_option('softlights-options');
	
	echo '<style>nav ul li a { color:'. esc_html($softlights_options['menu-font-color']) .' !important }</style>';
	echo '<style>nav ul li ul li a, nav {background:'. esc_html($softlights_options['menu-color']) .'}
</style>';
	echo '<style>#header {background:'. esc_html($softlights_options['header-color']) .'}</style>';
	echo '<style>.widget { background:'. esc_html($softlights_options['widgets-menus-color']) .'}</style>';
	echo '<style>#footer {background:'. esc_html($softlights_options['footer-color']) .'}</style>';
	echo '<style> a {color:'. esc_html($softlights_options['link-color']) .' }</style>';
	echo '<style>.thecontent a {color:'. esc_html($softlights_options['content-link-color']) .'!important }</style>';
	echo '<style>body {background:'. esc_html($softlights_options['header-color']) .'}</style>';
	echo '<style>.showcasebox,.frontheader { background:'. esc_html($softlights_options['showcase-color']) .'}</style>';
	echo '<style>.showcasebox, .showcasebox h2 { color:'. $softlights_options['showcase-font-color'] . '}</style>';
	
	
}

add_action('wp_head','softlights_custom_styles');


/********************************** Theme Setup scripts, Fonts and styles ************************************/

function softlights_scripts_styles()
{
	

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );
	
	wp_register_style('softlights-fonts','//fonts.googleapis.com/css?family=PT+Sans|Oswald|Roboto+Condensed');
	
	wp_enqueue_style( 'softlights-css', get_stylesheet_uri() ); // default stylesheet
	wp_enqueue_style( 'softlights-fonts');
	

}

add_action ('wp_enqueue_scripts','softlights_scripts_styles');	





/******************************** Register Navigation menus ***************************/
 
add_action( 'init', 'softlights_nav_menu' );

 function softlights_nav_menu() {
    register_nav_menu( 'primary-menu',  'Primary Menu');
    register_nav_menu( 'top-menu', 'Top Menu' );
    register_nav_menu( 'footer-menu', 'Footer Menu' );
}


/************************** Theme Pagination *************************************/

function softlights_pagination() {
		global $wp_query;

		$big = 999999999; // need an unlikely integer
		
		echo paginate_links( array(
			'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
			'format' => '?paged=%#%',
			'current' => max( 1, get_query_var('paged') ),
			'total' => $wp_query->max_num_pages
		) );
 }
 
/************ Pagination for Next ******************************/
	
function softlights_wp_link_pages() {

return wp_link_pages(array('before' => '<div id="page-links"> <span>Page Sections:</span> ','after' => '</div>','next_or_number'=>'number', 'previouspagelink' => '&laquo;  Previous Page', 'nextpagelink'=>'Next Page &raquo;'));	
	
}
	

	
/********************************* Theme Widgets Setup **********************************/

add_action( 'widgets_init', 'softlights_widgets_init' );

function softlights_widgets_init() {
	
/* Right and Left Sidebars */	
	register_sidebar(array(
  'name' =>  'Right Hand Sidebar',
  'id' => 'right-sidebar',
  'description' => 'Widgets in this area will be shown on the right-hand side.',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));

register_sidebar(array(
  'name' =>  'Left Hand Sidebar',
  'id' => 'left-sidebar',
  'description' => 'Widgets in this area will be shown on the right-hand side.',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));


/* Top Widget */

register_sidebar(array(
  'name' =>  'Top Widget',
  'id' => 'top-widget',
  'description' => 'Appears below navigation menu.',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));

	
	/* Footer widgets */
	
	register_sidebar( array(
		'name' =>  'Footer Widget',
		'id' => 'footer-widget',
		'description' => 'Appears above footer. Max 5 boxes of equal size',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	) );
	
		



}	



/******************************** Filter the Title *******************************/

function softlights_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( 'Page %s', max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'softlights_wp_title', 10, 2 );


/********************************* Theme Admin section *******************************************/

add_action ('admin_menu', 'softlights_admin_menu');


function softlights_admin_menu() {
	
	add_theme_page( 'Theme Option','Softlights Options', 'edit_theme_options', 'softlights-theme-options', 'softlights_options_page'); 
	add_action( 'admin_init','softlights_register_mysettings' );

}

function softlights_register_mysettings() { // whitelist options
  register_setting( 'softlights-option-group', 'softlights-options' ,'softlights_options_validate');
}

/************************ Validation of admin input data ***********************/

function softlights_options_validate ($input) {

$input['logo'] = esc_url_raw($input['logo']);
$input['favicon'] = esc_url_raw($input['favicon']);



$input['showcase-image'] = esc_url_raw($input['showcase-image']);
$input['showcase-title'] = sanitize_text_field($input['showcase-title']);
$input['showcase-text'] = sanitize_text_field($input['showcase-text']);
$input['showcase-link'] = esc_url_raw($input['showcase-link']);

$input['link-color'] = sanitize_text_field($input['link-color']);
$input['content-link-color'] = sanitize_text_field($input['content-link-color']);

$input['menu-color'] = sanitize_text_field($input['menu-color']);
$input['menu-font-color'] = sanitize_text_field($input['menu-font-color']);
$input['header-color'] = sanitize_text_field($input['header-color']);
$input['footer-color'] = sanitize_text_field($input['footer-color']);
$input['widgets-menus-color'] = sanitize_text_field($input['widgets-menus-color']);
$input['showcase-color'] = sanitize_text_field($input['showcase-color']);

return $input;

}

/****************************** Theme Options Page *********************************/

function softlights_options_page() {
?>	
	<div class="wrap">
<img src="<?php echo get_template_directory_uri().'/images/softlights.png' ?>">

<form method="post" action="options.php"> 
<?php 
settings_fields( 'softlights-option-group' );
do_settings_sections( 'softlights-option-group' );	
				

// Reset default values
if(isset($_POST['reset'])) {
	update_option('softlights-options', softlights_defaults() );
	echo '<p style="color:red">Theme settings have been reset and default values loaded</p>';
}

$softlights_options = get_option('softlights-options');


?>
<div class="tabs">
<label for="one">General</label>
<label for="two">Showcasebox</label>
<label for="three">Theme Colors</label>
<label for="four">Link Colors</label>
<label for="five">Navigation Menu Colors</label>
<label for="six">Sidebar/Widgets Colors </label>

</div>
<input type="radio" name="tab" id="one" <?php checked(1,1); ?>>
<div id="tab1" class="tab">

           <p>Please upload the logo and favicon to media library and paste the URL below.</p> 
<strong>Logo:</strong> Please use <a href="<?php echo admin_url('themes.php?page=custom-header'); ?>">header settings</a> to upload the logo or header background image. see on the left<br><br>
Favicon <br><input size="50" id="favicon_image" type="text" name="softlights-options[favicon]" value="<?php echo esc_url($softlights_options['favicon']); ?>"><input id="favicon_button" type="button" value="Upload Image" /> <br>
Footer Copyright Text <br><input type="text" size="50" name="softlights-options[footer]" value="<?php echo esc_attr($softlights_options['footer']); ?>"> (optional)<br><br>

<h2>Comment Settings</h2>
<p>You can chose to completely disable comments and comment form in this theme. If you receive spam and hate comments, disable it. Enabled by default.</p>
<p><input type="checkbox" name="softlights-options[comments]" value="1" <?php if(isset($softlights_options['comments'])) { checked(absint($softlights_options['comments']),1); }?>> Enable/Disable Comments in Posts/Pages <p>

</div>				

<input type="radio" name="tab" id="two">
<div id="tab2" class="tab">
           
					<h2>Showcase Box Settings </h2>
<p><input type="checkbox" name="softlights-options[showcase]" value="1" <?php if(isset($softlights_options['showcase'])) { checked(absint($softlights_options['showcase']),1); }?>> Enable/Disable Showcasebox Box <p>

Showcase Image (optional):<br><input id="showcase_image" type="text" size="50" name="softlights-options[showcase-image]" value="<?php echo esc_url($softlights_options['showcase-image']); ?>"><input id="showcase_button" type="button" value="Upload Image" /> (works any size- try big sizes like 300x300)<br><br>
Showcase Title (optional): <br><input type="text" size="50" name="softlights-options[showcase-title]" value="<?php echo esc_attr($softlights_options['showcase-title']); ?>"><br><br>
Showcase Text (optional) <br><textarea rows="4" cols="50" name="softlights-options[showcase-text]"><?php echo esc_textarea($softlights_options['showcase-text']); ?></textarea><br><br>
Link URL (optional): <br><input type="text" size="50" name="softlights-options[showcase-link]" value="<?php echo esc_url($softlights_options['showcase-link']); ?>"><br><br>

<h2>Showcasebox Background Color</h2>
<p>Makes changes to background color to showcasebox.
<input type="text" name="softlights-options[showcase-color]" class='color-picker' id="color-picker-8"
style="background:<?php echo $softlights_options['showcase-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['showcase-color']);  ?>" /></p>

<h2>Showcasebox Font Color</h2>
<p>Makes changes to font color to showcasebox.
<input type="text" name="softlights-options[showcase-font-color]" class='color-picker' id="color-picker-8"
style="background:<?php echo $softlights_options['showcase-font-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['showcase-font-color']);  ?>" /></p>

</div>
							
<input type="radio" name="tab" id="three">
			
<div id="tab3" class="tab" >

<h2>Theme Color Settings</h2>
<p>Here you can set the header and footer background color.</p>


<h2>Theme Background Color or Image</h2>
<p>Set this in <a href="<?php echo admin_url('themes.php?page=custom-background'); ?>">background settings</a> under softlights theme. To set a solid background color, you can do this in <a href="<?php echo admin_url('customize.php');?>">theme customizer</a> page</p>


<h2>Header Background Color</h2>
<p>Changes the background of the header: 
<input type="text" name="softlights-options[header-color]" class='color-picker' id="color-picker-2"
style="background:<?php echo $softlights_options['header-color']; ?>" 
value="<?php  echo esc_attr($softlights_options['header-color']);  ?>" /></p>

<h2>Footer Background Color</h2>
<p>Changes the background color of the footer:
<input type="text" name="softlights-options[footer-color]" class='color-picker' id="color-picker-4"
style="background:<?php echo $softlights_options['footer-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['footer-color']); ?>" /></p>




<p>Note: Type transparent for transparency instead of a fixed color. To reset to default color values, simple delete all the color values and hit submit.</p>

</div>

<input type="radio" name="tab" id="four">			
<div id="tab4" class="tab" >
<p> Please set the link colors to your theme</p>
<h2>1. All Sitewide Links</h2>
Makes changes to links to entire site
<input type="text" name="softlights-options[link-color]" class='color-picker' id="color-picker-6"
style="background:<?php echo $softlights_options['link-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['link-color']);  ?>" /></p>

<h2>2. Single Post/Page Link color </h2>
This is specific link color, occurs only inside page or post content.
<input type="text" name="softlights-options[content-link-color]" class='color-picker' id="color-picker-7"
style="background:<?php echo $softlights_options['content-link-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['content-link-color']);  ?>" />


</div>

<input type="radio" name="tab" id="five">			
<div id="tab5" class="tab" >

<h2>Navigation Menu Background Color:</h2>
<p>Changes the background of navigation menu (primary menu) 
<input type="text" name="softlights-options[menu-color]" 
style="background:<?php echo $softlights_options['menu-color']; ?>" class='color-picker' id="color-picker-1"
value="<?php echo esc_attr($softlights_options['menu-color']); ?>" /></p>

<h2>Navigation Menu - Font Color</h2>
<p>This changes only the font color to align with above nav background color  
<input type="text" name="softlights-options[menu-font-color]" 
style="background:<?php  echo $softlights_options['menu-font-color']; ?>" class='color-picker' id="color-picker-3"
value="<?php echo esc_attr($softlights_options['menu-font-color']); ?>" /></p>
</div>

<input type="radio" name="tab" id="six">			
<div id="tab6" class="tab" >
<h2>Sidebar/Widgets Background Color</h2>
<p>Makes changes to background color of all widgets.
<input type="text" name="softlights-options[widgets-menus-color]" class='color-picker' id="color-picker-5"
style="background:<?php echo $softlights_options['widgets-menus-color'];  ?>" 
value="<?php echo esc_attr($softlights_options['widgets-menus-color']);  ?>" /></p>

</div>


<?php submit_button(); ?> 
</form>
<h2>Reset Defaults</h2>
<form method="post" action="">
	<p class="submit">
	Load theme default settings: <input name="reset" class="button button-secondary"  type="submit" value="Reset to theme default settings" >
		<input type="hidden" name="action" value="reset" />
	</p>
</form>
</div>

<?php
}
?>