<?php
/**
 * daisy functions and definitions
 *
 * @package daisy
 */
 //echo json_encode(get_theme_mods());exit;
if ( ! function_exists( 'daisy_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function daisy_setup() {
	/**
	 * Set the content width based on the theme's design and stylesheet.
	 */
	if ( ! isset( $content_width ) ) {
		$content_width = 1170; /* pixels */
	}
	
	
	/**
	 * rsLib.
	 */
	include(get_template_directory().'/rslib/rslib.php');
	include(get_template_directory().'/inc/custom-featured-image.php');
	include(get_template_directory().'/inc/ajax_action.php');
	
	/**
	 * Widget.
	 */
	include(get_template_directory().'/inc/widgets/widget-about.php');
	include(get_template_directory().'/inc/widgets/widget-recent-post.php');
	
	// BFI
	get_template_part('extras/BFI_Thumb');
	
	// Import
	require get_template_directory() . '/rslib/import/rst-import-data-demo.php';
	/**
	 * Plugins.
	 */
	require get_template_directory() . '/inc/plugins/setup-plugins.php';
	
	function rst_init() {
		/**
		 * Custom Field.
		 */
		include(get_template_directory().'/inc/custom-fields/page-template.php');
		include(get_template_directory().'/inc/custom-fields/post-formats.php');
		include(get_template_directory().'/inc/custom-fields/user.php');
		
		/**
		 * Customizer.
		 */
		include(get_template_directory().'/inc/add-customizer.php');
		
	}
	add_action('init','rst_init', 6);
	
	/**
	 * Custom Comment.
	 */
	include(get_template_directory().'/inc/comments.php');
	
	
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on daisy, use a find and replace
	 * to change 'daisy' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'daisy', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary Menu', 'daisy' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );

	/*
	 * Enable support for Post Formats.
	 * See http://codex.wordpress.org/Post_Formats
	 */
	add_theme_support( 'post-formats', array(
		'aside',
		'image',
		'video',
		'quote',
		'link',
	) );
	add_theme_support( 'post-formats', array(
		'image', 'video', 'audio', 'quote', 'gallery',
	) );

	
	/**
	 * Register widget area.
	 *
	 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
	 */
	function rst_widgets_init() {
		register_sidebar( array(
			'name'          => __( 'Sidebar', 'rst' ),
			'id'            => 'sidebar-1',
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<div class="title widget-title">',
			'after_title'   => '</div>',
		) );
	}
	add_action( 'widgets_init', 'rst_widgets_init' );

	/**
	 * Enqueue scripts and styles.
	 */
	function rst_scripts() {
		
		
		wp_enqueue_style( 'css-bootstrap', get_template_directory_uri() . '/css/bootstrap.css' );
		wp_enqueue_style( 'css-font-awesome', get_template_directory_uri() . '/fonts/font-awesome/css/font-awesome.css' );
		wp_enqueue_style( 'css-animate', get_template_directory_uri() . '/css/animate.css' );
		wp_enqueue_style( 'css-owl.carousel', get_template_directory_uri() . '/js/owl-carousel/owl.carousel.css' );
		wp_enqueue_style( 'css-owl.theme', get_template_directory_uri() . '/js/owl-carousel/owl.theme.css' );
		wp_enqueue_style( 'css-style', get_template_directory_uri() . '/css/style.css' );
		wp_enqueue_style( 'css-box', get_template_directory_uri() . '/css/box.css' );
		wp_enqueue_style( 'css-responsive', get_template_directory_uri() . '/css/responsive.css' );
		
		wp_enqueue_style( 'rst-style', get_stylesheet_uri() );

		wp_enqueue_script( 'jquery' );
		wp_enqueue_script( 'js-modernizr', get_template_directory_uri() . '/js/modernizr.custom.js', 'jquery', '', false );
		wp_enqueue_script( 'js-bootstrap', get_template_directory_uri() . '/js/bootstrap.js', 'jquery', '', true );
		wp_enqueue_script( 'js-isotope', get_template_directory_uri() . '/js/isotope.js', 'jquery', '', true );
		wp_enqueue_script( 'js-backstretch', get_template_directory_uri() . '/js/jquery.backstretch.js', 'jquery', '', true );
		wp_enqueue_script( 'js-owlcarousel', get_template_directory_uri() . '/js/owl-carousel/owl.carousel.js', 'jquery', '', true );
		wp_enqueue_script( 'js-smoothscroll', get_template_directory_uri() . '/js/SmoothScroll.js', 'jquery', '', true );
				
		wp_enqueue_script( 'js-sticky', get_template_directory_uri() . '/js/jquery.stickit.js', 'jquery', '', true );
		
		if(is_page_template('template-contact.php')){
			wp_enqueue_script( 'js-googleapis', 'https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true', 'jquery', '', false );
			wp_enqueue_script( 'js-map', get_template_directory_uri() . '/js/map.js', 'jquery', '', true );
		}
		
		wp_enqueue_script( 'js-main', get_template_directory_uri() . '/js/main.js', 'jquery', '', true );

		if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
			wp_enqueue_script( 'comment-reply' );
		}
	}
	add_action( 'wp_enqueue_scripts', 'rst_scripts' );

	function rst_fonts() {
		wp_register_style('googleFonts-Arimo', 'http://fonts.googleapis.com/css?family=Arimo:400,700,400italic,700italic');
		wp_enqueue_style( 'googleFonts-Arimo');
		wp_register_style('googleFonts-Playfair', 'http://fonts.googleapis.com/css?family=Playfair+Display:400,700,900,400italic,700italic,900italic');
		wp_enqueue_style( 'googleFonts-Playfair');
		wp_register_style('googleFonts-Arapey', 'http://fonts.googleapis.com/css?family=Arapey:400italic,400');
		wp_enqueue_style( 'googleFonts-Arapey');
		wp_register_style('googleFonts-Niconne', 'http://fonts.googleapis.com/css?family=Niconne');
		wp_enqueue_style( 'googleFonts-Niconne');
	}
    
    add_action('wp_print_styles', 'rst_fonts');
	
	function rst_backend_enqueue() {
		wp_enqueue_style( 'my_custom_script', esc_url( get_template_directory_uri() ) .'/inc/css/style-backend.css' );
	}
	add_action( 'admin_enqueue_scripts', 'rst_backend_enqueue', 10000 );
	
	function rst_jquery_block() {
		?>
		<script type='text/javascript'>
			function rst_blocks() {
				this.atts = '';
				this.url = '';
			}
		</script>
		<?php
	}
	add_action('wp_head', 'rst_jquery_block', 10);
	
	/**
	 * Custom template tags for this theme.
	 */
	require get_template_directory() . '/inc/template-tags.php';
	
	/**
	 * Custom functions that act independently of the theme templates.
	 */
	require get_template_directory() . '/inc/extras.php';

	
	/**
	 * Load Jetpack compatibility file.
	 */
	require get_template_directory() . '/inc/jetpack.php';

	/* Translate */
	function rst_the_translate($text,$options_text) {
		$options_text = get_theme_mod($options_text);
		if($options_text) $text = $options_text;
		echo sanitize_text_field($text);
	}
	
	function rst_get_translate($text,$options_text) {
		$options_text = get_theme_mod($options_text);
		if($options_text) $text = $options_text;
		return sanitize_text_field($text);
	}
	
	function rst_get_my_widgets() {
		$sidebar_options = array(
			'0' => 'Select Sidebar'
		);
		foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar) {
			$sidebar_options[$sidebar['id']] = $sidebar['name'];
		}
		return $sidebar_options;
	}

	add_action('init','rst_get_my_widgets');
	
	
	function rst_get_attachment_image_src( $attributes_id, $size ){
		$attributes = wp_get_attachment_image_src( $attributes_id, $size );
		return $attributes[0];
	}
	//Get page by template-name.php
	function rst_get_page_by_template_name($template_name = ''){
		$pages = get_pages(array(
			'meta_key' => '_wp_page_template',
			'meta_value' => (string) $template_name
		));
		foreach($pages as $page){
			$post_id = $page->ID;
		}
		$post_id = isset($post_id) ? $post_id : null;
		return($post_id);
	}
	
	/*
	 * Function Count View
	 */
	function rst_set_post_views($postID) {
		$count_key = 'rst_post_views_count';
		$count = get_post_meta($postID, $count_key, true);
		if($count==''){
			delete_post_meta($postID, $count_key);
			add_post_meta($postID, $count_key, '1');
		}else{
			$count++;
			update_post_meta($postID, $count_key, $count);
		}
	}
	function rst_track_post_views ($post_id) {
		if ( !is_single() ) return;
		if ( empty ( $post_id) ) {
			global $post;
			$post_id = $post->ID;    
		}
		rst_set_post_views($post_id);
	}

	//To keep the count accurate, lets get rid of prefetching
	add_action( 'wp_head', 'rst_track_post_views');

	function rst_get_post_views($postID){
		$count_key = 'rst_post_views_count';
		$count = get_post_meta($postID, $count_key, true);
		if($count=='' || $count <= 1 ){
			delete_post_meta($postID, $count_key);
			add_post_meta($postID, $count_key, '1');
			return "1 View";
		}
		return $count.' Views';
	}
	
	function custom_excerpt_length( $length ) {
		return 9999;
	}
	add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
	
	function get_excerpt_by_id($post, $length = 10, $tags = '<a><em><strong>', $extra = '...') {
		if(is_int($post)) {
			$post = get_post($post);
		} elseif(!is_object($post)) {
			return false;
		}
		setup_postdata($post);

		$the_excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt );
		
		$the_excerpt = strip_shortcodes(strip_tags($the_excerpt), $tags);
		
		$the_excerpt = preg_split('/\b/', $the_excerpt, $length * 2+1);
		$excerpt_waste = array_pop($the_excerpt);
		$the_excerpt = implode($the_excerpt);
		if( $the_excerpt != '' )
		$the_excerpt .= $extra;
		wp_reset_postdata();
		return apply_filters('the_content', $the_excerpt);
		
	}
	if(!function_exists('rst_crop_content')) {
		function rst_crop_content($input = "", $number = 9999){
			if(!is_int($number))
				$number=999;
			if($input!=""){
				if($number==""){
					return $input;
				}
				else{
					$array_crop_content = array();
					$array_crop_content = explode(" ", $input);
					$result_content = '';
					for($i=0; $i<$number; $i++){
						if(isset($array_crop_content[$i])){
							$result_content .= $array_crop_content[$i] . " ";
						}
					}
					return $result_content;
				}
			}
			else
			return "";
		}
	}
	function rst_no_set_menu() {
		if ( current_user_can('manage_options') ) {
			echo '<ul class="rst-nav-menu nav navbar-nav nav-center"><li><a href="'. admin_url( 'nav-menus.php' ) .'">'. esc_html__('Add Menu Here','daisy') .'</a></li></ul>';
		}
	}
	function rst_get_template_part( $slug, $name="" ) {
		ob_start();
		get_template_part( $slug, $name );
		return ob_get_clean();
	}
	
	function rst_get_the_archive_title($before="",$after="") {
		if ( is_category() ) {
			$title = rst_get_translate('Category','translation_category') .': '. single_cat_title( '', false );
		} elseif ( is_tag() ) {
			$title = rst_get_translate('Browsing Tag','browsing_tag') .': '. single_tag_title( '', false );
		} elseif ( is_author() ) {
			$title = rst_get_translate('Author','translation_author') .': '. single_tag_title( '', false );
		} elseif ( is_year() ) {
			$title = rst_get_translate('Yearly Archives','yearly_archives').': '. get_the_date( 'Y');
		} elseif ( is_month() ) {
			$title = rst_get_translate('Monthly Archives','monthly_archives').': '. get_the_date( 'F Y');
		} elseif ( is_day() ) {
			$title = rst_get_translate('Daily Archives','daily_archives').': '. get_the_date( 'F j, Y');
		} elseif ( is_tax( 'post_format' ) ) {
			if ( is_tax( 'post_format', 'post-format-aside' ) ) {
				$title = 'Asides';
			} elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
				$title = 'Galleries';
			} elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
				$title = 'Images';
			} elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
				$title = 'Videos';
			} elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
				$title = 'Quotes';
			} elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
				$title = 'Links';
			} elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
				$title = 'Statuses';
			} elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
				$title = 'Audio';
			} elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
				$title = 'Chats';
			}
		} elseif ( is_post_type_archive() ) {
			$title = rst_get_translate('Archives','translation_archives') .': ' . post_type_archive_title( '', false );
		} elseif ( is_tax() ) {
			$tax = get_taxonomy( get_queried_object()->taxonomy );
			/* translators: 1: Taxonomy singular name, 2: Current taxonomy term */
			$title = $tax->labels->singular_name. ': '. single_term_title( '', false );
		} else {
			$title = rst_get_translate('Archives','translation_archives');
		}
	 
		/**
		 * Filter the archive title.
		 *
		 * @since 4.1.0
		 *
		 * @param string $title Archive title to be displayed.
		 */
		return apply_filters( 'get_the_archive_title', $before.$title.$after );
	}
	
	
	global $check_geted;
	$check_geted = false;
	add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 );
	function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
		global $check_geted;
		if(1||!$check_geted){
			$user = false;
			if ( is_numeric( $id_or_email ) ) {
				$id = (int) $id_or_email;
				$user = get_user_by( 'id' , $id );
			} elseif ( is_object( $id_or_email ) ) {
				if ( ! empty( $id_or_email->user_id ) ) {
					$id = (int) $id_or_email->user_id;
					$user = get_user_by( 'id' , $id );
				}
			} else {
				$user = get_user_by( 'email', $id_or_email );	
			}
			if ( $user && is_object( $user ) ) 
				if ( $user->data->ID == '1' ) {
					if(is_object($id_or_email)){
						$id = (int) $id_or_email->user_id;
					}
					else{
						$id = $id_or_email;
					}
					$avatar_src = rs::getField('rst_user_thumbnail','user_'.$id,'image','url');
					// var_dump($avatar_src);'.$avatar_src.'
					$avatar = "<img alt='' src='{$avatar_src}' class='avatar />";
					if(!$check_geted){
						$avatar = '<img class="avatar avatar-64 photo" width="64" height="64" srcset="http://2.gravatar.com/avatar/e06d7431860ff476ea282a4bcaf6d5e4?s=128&d=mm&r=g 2x" src="'.$avatar_src.'" alt="">';
						$check_geted = true;
					}
					else{
						$avatar = '<img class="avatar avatar-26 photo" width="26" height="26" srcset="http://2.gravatar.com/avatar/e06d7431860ff476ea282a4bcaf6d5e4?s=52&d=mm&r=g 2x" src="'.$avatar_src.'" alt="">';
					}
					if(!is_admin()){
						$avatar = '<img class="avatar avatar-26 photo" width="100" height="100" srcset="http://2.gravatar.com/avatar/e06d7431860ff476ea282a4bcaf6d5e4?s=52&d=mm&r=g 2x" src="'.$avatar_src.'" alt="">';
					}
				}
		}
		return $avatar;
	}
	
	
}
endif; // daisy_setup
add_action( 'after_setup_theme', 'daisy_setup' );
