<?php 
/***
*
THEME SETUP
*
***/
function photographer_theme_setup(){
    
        // Set $content_width
        if ( ! isset( $content_width ) )
        $content_width = 575;
        $photographer_background_args = array(
        'default-color' => '',
        'default-image' => '',
        'wp-head-callback' => 'photographer_custom_background_cb',
        );
        add_theme_support( 'custom-background', $photographer_background_args );
        
        //Load Header
        $photographer_header_defaults = array(
        'default-image'          => '',
        'random-default'         => false,
        'width'                  => '1170',
        'height'                 => '370',
        'flex-height'            => false,
        'flex-width'             => false,
        'default-text-color'     => '',
        'header-text'            => false,
        'uploads'                => true,
        'wp-head-callback'       => '',
        'admin-head-callback'    => '',
        'admin-preview-callback' => '',
        );
        add_theme_support( 'custom-header', $photographer_header_defaults );
        add_theme_support( 'automatic-feed-links' );
        add_theme_support( 'post-thumbnails' );
        add_theme_support( 'title-tag' );
        add_image_size('photographer-index-image',263,263,true) ;
        register_nav_menu( 'primary', 'Main Menu' );
        load_theme_textdomain('photographer', get_template_directory() . '/languages');
    }
add_action('after_setup_theme', 'photographer_theme_setup');
    
function photographer_custom_background_cb() {
  $background = set_url_scheme( get_background_image() );
  $color = get_theme_mod( 'background_color', get_theme_support( 'custom-background', 'default-color' ) );

  if ( ! $background && ! $color )
    return;

  $style = $color ? "background-color: #$color;" : '';

  if ( $background ) {
    $image = " background-image: url('$background');";

    $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
    if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
      $repeat = 'repeat';
    $repeat = " background-repeat: $repeat;";

    $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
    if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
      $position = 'left';
    $position = " background-position: top $position;";

    $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
    if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
      $attachment = 'scroll';
    $attachment = " background-attachment: $attachment;";

    $style .= $image . $repeat . $position . $attachment;
  }
?>
<style type="text/css" id="custom-background-css">
body.custom-background { <?php echo trim( $style ); ?> }
</style>
<?php
}
/***
*
LOAD CSS AND JS STYLES
*
***/
    function photographer_load_scripts() {
   
        wp_enqueue_script('photographer-bootstrap', get_template_directory_uri().'/js/bootstrap.min.js',array('jquery'),'',true);
        wp_enqueue_script('photographer-slicknav',get_template_directory_uri().'/js/jquery.slicknav.min.js',array('jquery'),'',true);
        wp_enqueue_script('photographer-matchHeight',get_template_directory_uri().'/js/jquery.matchHeight-min.js',array('jquery'),'',true);
        wp_enqueue_script('photographer-init',get_template_directory_uri().'/js/init.js',array('jquery'),'',true);
        wp_localize_script('photographer-init', 'init_vars', array(
            'label' => __('Menu', 'photographer')
        ));

    if ( is_singular() && get_option( 'thread_comments' ) )
        wp_enqueue_script( 'comment-reply' );
    }
    add_action('wp_enqueue_scripts', 'photographer_load_scripts');
    
    function photographer_load_styles()
    { 
        wp_enqueue_style( 'photographer-bootstrap', get_template_directory_uri().'/css/bootstrap.min.css','','','all' );
        wp_enqueue_style( 'photographer-bootstrap-theme', get_template_directory_uri().'/css/bootstrap-theme.min.css','','','all' );
        wp_enqueue_style( 'photographer-slicknav',get_template_directory_uri().'/css/slicknav.css','','','all');
        wp_enqueue_style( 'photographer-fontAwesome',get_template_directory_uri().'/fonts/font-awesome/css/font-awesome.min.css','','','all');
        /** Fonts **/
        wp_enqueue_style('photographer-OpensansBold',get_template_directory_uri().'/fonts/web_fonts/opensans_bold_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansBoldItalic',get_template_directory_uri().'/fonts/web_fonts/opensans_bolditalic_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansExtraBold',get_template_directory_uri().'/fonts/web_fonts/opensans_extrabold_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansExtraBoldItalic',get_template_directory_uri().'/fonts/web_fonts/opensans_extrabolditalic_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansItalic',get_template_directory_uri().'/fonts/web_fonts/opensans_italic_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansLight',get_template_directory_uri().'/fonts/web_fonts/opensans_light_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansLightItalic',get_template_directory_uri().'/fonts/web_fonts/opensans_lightitalic_macroman/stylesheet.css','','','all');
        
        wp_enqueue_style('photographer-OpensansRegular',get_template_directory_uri().'/fonts/web_fonts/opensans_regular_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansSemiBold',get_template_directory_uri().'/fonts/web_fonts/opensans_semibold_macroman/stylesheet.css','','','all');
        wp_enqueue_style('photographer-OpensansSemiBoldItalic',get_template_directory_uri().'/fonts/web_fonts/opensans_semibolditalic_macroman/stylesheet.css','','','all');
        wp_enqueue_style( 'photographer-style', get_stylesheet_uri(),'','','all' );
    }    
    add_action('wp_enqueue_scripts', 'photographer_load_styles');
    
    function photographer_add_ie_html5_shim () {
        echo '<!--[if lt IE 9]>';
        echo '<script src="'.get_template_directory_uri().'/js/html5shiv.js"></script>';
        echo '<![endif]-->';
    }
    add_action('wp_head', 'photographer_add_ie_html5_shim');
/***
*
SIDEBARS INITIALIZATION
*
***/
function photographer_widgets_init() {
    
    register_sidebar(array(
        'name' => __('Sidebar', 'photographer' ),
        'id'   => 'sidebar',
        'description' => __('This is the widgetized sidebar.', 'photographer' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h3>',
        'after_title'   => '</h3>'
    ));
    }
add_action( 'widgets_init', 'photographer_widgets_init' );    
/***
*
THEME FUNCTIONS
*
***/  
function photographer_fallback( $args ) {
        if ( current_user_can( 'manage_options' ) ) {

            extract( $args );

            $fb_output = null;

            if ( $container ) {
                $fb_output = '<' . $container;

                if ( $container_id )
                    $fb_output .= ' id="' . $container_id . '"';

                if ( $container_class )
                    $fb_output .= ' class="' . $container_class . '"';

                $fb_output .= '>';
            }

            $fb_output .= '<ul';

            if ( $menu_id )
                $fb_output .= ' id="' . $menu_id . '"';

            if ( $menu_class )
                $fb_output .= ' class="' . $menu_class . '"';

            $fb_output .= '>';
            $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">'.__('Add a menu','photographer').'</a></li>';
            $fb_output .= '</ul>';

            if ( $container )
                $fb_output .= '</' . $container . '>';

            echo $fb_output;
        }
    }
function photographer_get_excerpt($limit, $source = null){
    global $post;
    if($source == "content" ? ($excerpt = get_the_content()) : ($excerpt = get_the_excerpt()));
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $limit);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'...';
    return $excerpt;
	}
	
function photographer_the_title_shorten($len,$rep='...') {
        $title = get_the_title();
        if($title==""):$title = get_the_date();echo  $title;
        else:
		$title = the_title('','',false);
		$shortened_title = mb_substr($title, 0, $len,'UTF-8');
		echo $shortened_title.$rep;
        endif;	
}
function photographer_pagination( $query=null ) {
 
  global $wp_query;
  $query = $query ? $query : $wp_query;
  $big = 999999999;
 
  $paginate = paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'type' => 'array',
    'total' => $query->max_num_pages,
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'prev_text' => __('&laquo;','photographer'),
    'next_text' => __('&raquo;','photographer'),
    )
  );
 
  if ($query->max_num_pages > 1) :
?>
<ul class="pagination">
  <?php
  foreach ( $paginate as $page ) {
    echo '<li>' . $page . '</li>';
  }
  ?>
</ul>
<?php
  endif;
}
function photographer_breadcrumb() {
        echo __('You are here: ','photographer');

                echo '<a href="';
                echo esc_url(home_url());
                echo '">'.__('Home','photographer');
                echo "</a> » ";
                if (is_category()):
                        single_cat_title(' ');
                elseif(is_tag()):
                        echo single_tag_title();
                elseif(is_archive()):
                    echo get_the_date(get_option('date_format'));
                else:
                echo __('Home','photographer');
                endif;
}
add_action( 'admin_menu', 'photographer_add_admin_menu' );
add_action( 'admin_init', 'photographer_settings_init' );


function photographer_add_admin_menu(  ) { 

    add_theme_page( 'Photographer', 'Photographer Settings', 'manage_options', 'photographer', 'photographer_options_page' );

}
function photographer_settings_init(  ) { 

    register_setting( 'pluginPage', 'photographer_settings', 'photographer_sanitize' );

    add_settings_section(
        'photographer_pluginPage_section', 
        __( 'General Settings For Theme.', 'photographer' ), 
        'photographer_settings_section_callback', 
        'pluginPage'
    );

    add_settings_field( 
        'photographer_add_facebook', 
        __( 'Facebook URL', 'photographer' ), 
        'photographer_add_facebook_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_twitter', 
        __( 'Twitter URL', 'photographer' ), 
        'photographer_add_twitter_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_gplus', 
        __( 'Google+ URL', 'photographer' ), 
        'photographer_add_gplus_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_rss', 
        __( 'RSS URL', 'photographer' ), 
        'photographer_add_rss_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_linkedin', 
        __( 'Linkedin URL', 'photographer' ), 
        'photographer_add_linkedin_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_instagram', 
        __( 'Instagram URL', 'photographer' ), 
        'photographer_add_instagram_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
    add_settings_field( 
        'photographer_add_pinterest', 
        __( 'Pinterest URL', 'photographer' ), 
        'photographer_add_pinterest_render', 
        'pluginPage', 
        'photographer_pluginPage_section' 
    );
    
}
function photographer_sanitize($input){
    
    $input['photographer_add_facebook'] =  esc_url_raw($input['photographer_add_facebook']);
    $input['photographer_add_twitter'] =  esc_url_raw($input['photographer_add_twitter']);
    $input['photographer_add_gplus'] =  esc_url_raw($input['photographer_add_gplus']);
    $input['photographer_add_rss'] =  esc_url_raw($input['photographer_add_rss']);
    $input['photographer_add_linkedin'] =  esc_url_raw($input['photographer_add_linkedin']);
    $input['photographer_add_instagram'] =  esc_url_raw($input['photographer_add_instagram']);
    $input['photographer_add_pinterest'] =  esc_url_raw($input['photographer_add_pinterest']);
    
    return $input;
}

function photographer_add_facebook_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_facebook']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_facebook]' value='<?php echo $value; ?>'>
    <?php
}

function photographer_add_twitter_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_twitter']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_twitter]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_add_gplus_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_gplus']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_gplus]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_add_rss_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_rss']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_rss]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_add_linkedin_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_linkedin']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_linkedin]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_add_instagram_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_instagram']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_instagram]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_add_pinterest_render() { 

    $options = get_option( 'photographer_settings' );
    $value = esc_url($options['photographer_add_pinterest']);
    ?>
    <input type='text' name='photographer_settings[photographer_add_pinterest]' value='<?php echo $value; ?>'>
    <?php
}
function photographer_settings_section_callback(  ) { 
    echo __('Premium Features', 'photographer');
    echo'<ul style="background:#ffffff; padding:10px; width:90%;">
        <li>'.__('Upload Logo & Favicon','photographer').'</li>
        <li>'.__('Full Width Slider','photogrpaher').'</li>
        <li>'.__('Page Specific Background Image','photographer').'</li>
        <li>'.__('Slider (enable/disable title & description)','photographer').'</li>
        <li>'.__('Testimonials','photographer').'</li>
        <li>'.__('Google Fonts','photographer').'</li>
        <li>'.__('Color Picker','photographer').'</li>
        <li>'.__('Gallery','photographer').'</li>
        <li>'.__('1-4 Columns Widgetized Footer Sidebar','photographer').'</li>
        </ul>
        <p>
        <a rel="nofollow" href="'.esc_url( 'http://www.ketchupthemes.com/photographer-theme/').'" style="background:red; margin:5px 0; padding:10px 20px; color:#ffffff; margin-top:10px; text-decoration:none;">'.__('Update to Premium','photographer').'</a></p>';
}
function photographer_options_page(  ) { 

    ?>
    <form action='options.php' method='post'>
        
        <h2><?php _e('Photographer','photographer'); ?></h2>
        <?php if( isset($_GET['settings-updated']) ) { ?>
        <div id="message" class="updated">
            <p><strong><?php _e('Settings saved.','photographer') ?></strong></p>
        </div>
        <?php } ?>
        <?php
        settings_fields( 'pluginPage' );
        do_settings_sections( 'pluginPage' );
        submit_button();
        ?>
        
    </form>
    <?php

}
/**== WordPress Title + Fallback ==**/
if ( version_compare( $GLOBALS['wp_version'], '4.1', '<' ) ) :
    function photographer_wp_title( $title, $sep ) {
        if ( is_feed() ) {
            return $title;
        }
        global $page, $paged;
       
        $title .= get_bloginfo( 'name', 'display' );
        
        $site_description = get_bloginfo( 'description', 'display' );
        if ( $site_description && ( is_home() || is_front_page() ) ) {
            $title .= " $sep $site_description";
        }
        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
            $title .= " $sep " . sprintf( __( 'Page %s', 'photographer' ), max( $paged, $page ) );
        }
        return $title;
    }
    add_filter( 'wp_title', 'photographer_wp_title', 10, 2 );
    
    function photographer_render_title() {
        ?><!--?php wp_title( '|', true, 'right' ); ?-->
        <?php
    }
    add_action( 'wp_head', 'photographer_render_title' );
endif;