<?php

if ( ! function_exists( 'destination_free_setup' ) ) :

function destination_free_setup(){
	
    load_theme_textdomain('destination_free-free', get_template_directory() . '/languages');
	add_theme_support( 'title-tag' );
	
	add_theme_support('menus');	

	add_theme_support( 'post-thumbnails' ); 
	
	add_theme_support( 'automatic-feed-links' );
}
endif;
add_action('after_setup_theme', 'destination_free_setup');
	
function destination_free_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'destination_free_content_width', 1000 );
}	
add_action( 'after_setup_theme', 'destination_free_content_width', 0 );

function destination_free_excerpt_more( $more ) {
	return '...';
}
add_filter('excerpt_more', 'destination_free_excerpt_more');

function destination_free_add_google_fonts() {

wp_enqueue_style( 'destination_free-google-fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:400,300,600&amp;subset=latin,cyrillic-ext', false ); 
		if ( is_singular() ) {
			wp_enqueue_script( "comment-reply" );
	}
}

add_action( 'wp_enqueue_scripts', 'destination_free_add_google_fonts' );


function destination_free_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Widget Area 1', 'destination_free' ),
		'id' => 'widget-area-1',
		'description' => __( 'Add widgets here to appear in your sidebar.', 'destination_free' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	register_sidebar( array(
		'name' => __( 'Widget Area 2', 'destination_free' ),
		'id' => 'widget-area-2',
		'description' => __( 'Add widgets here to appear in your sidebar.', 'destination_free' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
}
add_action( 'widgets_init', 'destination_free_widgets_init' );



if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'destination_free-new-size', 1280, 9999, false );
}
 
add_filter('image_size_names_choose', 'destination_free_image_sizes');

function destination_free_image_sizes($sizes) {
$addsizes = array(
'destination_free-new-size' => 'Full Width'
);
$newsizes = array_merge($sizes, $addsizes);
return $newsizes;
}

if ( ! function_exists( 'destination_free_comment' ) ) :

function destination_free_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
			<div class="comment-author vcard">
				<?php echo get_avatar( $comment, 40 ); ?>
				<cite class="commentauthor"><?php printf( __( '%s', 'destination_free' ), sprintf( '%s', get_comment_author_link() ) ); ?>, <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
				<?php
					printf( __( '%1$s at %2$s', 'destination_free' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'destination_free' ), ' ' );
				?></cite>
			</div>
			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'destination_free' ); ?></em>
				<br />
			<?php endif; ?>
			<div class="comment-body"><?php comment_text(); ?></div>

			<div class="reply">
				<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
			</div>
		</div>

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'destination_free' ); ?><?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'destination_free' ), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}
endif;

add_action('customize_register', 'destination_free_customize_register');
function destination_free_customize_register($wp_customize) {
  $wp_customize->remove_section( 'static_front_page' );
  $wp_customize->remove_section( 'colors' );
  $wp_customize->add_section(
      'intro_section',
      array(
          'title' => __( 'Blog Introduction', 'destination_free'),
          'description' => __('Blog introduction is the very first part of your site your readers will see. Use it wisely and do not forget to attach a background picture.', 'destination_free'),
          'priority' => 33,
      )
  );
  $wp_customize->add_setting(
	    'title_textbox',
	    array(
	        'default' => __('Type in your title text.', 'destination_free'),
			'sanitize_callback' => 'esc_attr',
	    )
	);
  $wp_customize->add_control(
    'title_textbox',
    array(
        'label' => __('Title', 'destination_free'),
        'section' => 'intro_section',
        'type' => 'text',
    )
);
  $wp_customize->add_setting(
	    'subtitle_textbox',
	    array(
	        'default' => __('Type in your subtitle text.', 'destination_free'),
			'sanitize_callback' => 'esc_attr',
	    )
	);
  $wp_customize->add_control(
    'subtitle_textbox',
    array(
        'label' => __('Subtitle', 'destination_free'),
        'section' => 'intro_section',
        'type' => 'text',
    )
);
$wp_customize->add_setting(
	 'intro-upload',
	 array(
		 'default' => '/wp-content/themes/destination_free-free/img/background.jpg',
	 	 'sanitize_callback' => 'destination_free_sanitize_upload',)
 );
 
$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'intro-upload',
        array(
            'label' => __('Background Picture', 'destination_free'),
            'section' => 'intro_section',
            'settings' => 'intro-upload'
        )
    )
);

$wp_customize->add_section(
    'personal_section',
    array(
        'title' => __('Personal Info', 'destination_free'),
        'description' => __('Tell your readers who you are! Your picture will show in the upper left corner and will be the fastest way to your About Me page.', 'destination_free'),
        'priority' => 33,
    )
);

$wp_customize->add_setting(
    'show_face',
	array(
		'default' => '',
		'sanitize_callback' => 'destination_free_sanitize_checkbox',
	)
);

$wp_customize->add_control(
    'show_face',
    array(
        'type' => 'checkbox',
        'label' => __('Show a picture of me.', 'destination_free'),
        'section' => 'personal_section',
    )
);

$wp_customize->add_setting(
	 'face-upload',
	 array(
		 'default' => '',
	 	 'sanitize_callback' => 'destination_free_sanitize_upload',)
 );
 
$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'face-upload',
        array(
            'label' => __('Picture Upload', 'destination_free'),
            'section' => 'personal_section',
            'settings' => 'face-upload'
        )
    )
);

$wp_customize->add_setting(
 'face-link' , array(
		 'default' => '',
	 	 'sanitize_callback' => 'destination_free_sanitize_dropdown',
	 )
 );

$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'face-link', array(
        'label'   => __( 'Page Link', 'theme-name' ),
        'section' => 'personal_section',
        'type'    => 'dropdown-pages',
        'settings' => 'face-link',
) ) );

$wp_customize->add_section(
    'index_section',
    array(
        'title' => __('Post Settings', 'destination_free' ),
        'priority' => 33,
    )
);

$wp_customize->add_setting(
    'post_list',
    array(
	'sanitize_callback' => 'destination_free_sanitize_postorder',
    )
);
 
$wp_customize->add_control(
    'post_list',
    array(
        'type' => 'radio',
        'label' => __('Post Order', 'destination_free' ),
        'section' => 'index_section',
        'description' => __('Set the post order. Would you rather begin telling your story from the beginning or do you prefer to begin from the end?', 'destination_free' ),
        'choices' => array(
            'left' => __( 'Starting Day 1', 'destination_free' ),
            'right' => __( 'Latest Post First', 'destination_free'),
        ),
    )
);

  $wp_customize->add_section(
      'copyright_section',
      array(
          'title' => __( 'Copyright' , 'destination_free' ),
          'description' => __( 'Use this area for your copyright text.' , 'destination_free' ),
          'priority' => 35,
      )
  );
  $wp_customize->add_setting(
	    'copyright_textbox',
	    array(
	        'default' => __( 'Type in your copyright text.', 'destination_free' ),
			'sanitize_callback' => 'esc_html',
			
	    )
	);
  $wp_customize->add_control(
    'copyright_textbox',
    array(
        'label' => __( 'Copyright text' , 'destination_free' ),
        'section' => 'copyright_section',
        'type' => 'text',
    )
);

$wp_customize->add_setting(
    'show_date',
	array(
		'default' => '',
		'sanitize_callback' => 'destination_free_sanitize_checkbox',
	)
);

$wp_customize->add_control(
    'show_date',
    array(
        'type' => 'checkbox',
        'label' => __('Display Post Date', 'destination_free' ),
        'section' => 'index_section',
    )
);

}

function destination_free_sanitize_dropdown( $input ) {
	if ( is_numeric( $input ) ) {
		return intval( $input );
	}
}

function destination_free_sanitize_postorder( $input ) {
    $valid = array(
        'left' => 'Starting Day 1',
        'right' => 'Latest Post First',
    );
 
    if ( array_key_exists( $input, $valid ) ) {
        return $input;
    } else {
        return '';
    }
}

function destination_free_sanitize_checkbox( $input ) {
	if ( 1 == $input ) {
			return true;
		} else {
			return false;
		}
}

function destination_free_sanitize_link( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
	
}	

function destination_free_sanitize_copyright( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
	
}	
	
function destination_free_sanitize_upload( $file ) {
		if ( preg_match( '/\.(png|jpg|jpeg)$/', $file ) ) {
			return $file;
		} else {
			return '';
		}	

}
