<?php

/*-----------------------------------------------------------------------------------*/
/* Get menus posts
/*-----------------------------------------------------------------------------------*/
	function reg_menu() {
		register_nav_menus(
			array(
			    'top-nav' => "مگامنو",
				'primary' => "منوی اصلی",
				'sid-nav' => "منوی ریسپانسیو",
				'footer-top' => "منوی فوتر بالا",
				'footer-bottom' => "منوی فوتر پایین"
			)
		);
	}
	add_action( 'init', 'reg_menu' );
	
/*-----------------------------------------------------------------------------------*/
/* Get today_post posts
/*-----------------------------------------------------------------------------------*/



function wpp_post_count_by_date($date = NULL , $post_type = 'post' , $post_status = 'publish'){
	global $wpdb;
	
	if($date != NULL)
			$current_date = $date;
	else{
			date_default_timezone_set(get_option('timezone_string'));
			$current_date = date("Y-m-d" , time());
	}
	$sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = '$post_status' AND post_type = '$post_type' AND post_date BETWEEN '$current_date 00:00:00' AND '$current_date 23:59:59'";
	
	$today_post_count = $wpdb->get_var( $sql );
	return $today_post_count;
}


/*-----------------------------------------------------------------------------------*/
/* Get link_manager
/*-----------------------------------------------------------------------------------*/



add_filter( 'pre_option_link_manager_enabled', '__return_true' ); 

function limit_title($title, $n){
if ( strlen ($title) > $n )
{
echo mb_substr(the_title($before = '', $after = '', FALSE), 0, $n) . '...';
}
else { the_title(); }
}


/*-----------------------------------------------------------------------------------*/
/* Get custom_search posts
/*-----------------------------------------------------------------------------------*/




function custom_search_form($custom_search){
 
$default=array(
                'cat_show'=>false,
 'tag_show'=>false,
 'author_show'=>false,
 'archive_show'=>false,
 'field_show'=>true,
 'label_show'=>false,
 'button_show'=>true,
 'rememberd'=>true,
 'echo'=>false
);//default array
foreach((array)$custom_search as $key=>$value){
 $default[$key]=$value;
}


global $WP_Query,$query,$wp_user;
$cat_option='';$tag_option='';$author_option='';$archive_option='';
$advance_option='';$close_option='';$field_option='';$button_option='';
$cat_selected=get_query_var('cat');
$tag_selected=get_query_var('tag');
$author_selected=get_query_var('author');


if($default['cat_show']==1):
$categories = get_categories(); 
$cat_option='<div><ul>';
if($default['label_show']):
$cat_option .='<div class="cat-label">'.__('دسته بندی','qaleb').'</div>';
endif; 
$cat_option .='<div class="search-cat"><select name="cat" id="searchcat" >
<option value="">'.__('همه','qaleb').'</option>';
foreach ($categories as $category) {
$cat_option .= '<option value="'.$category->cat_ID.'"';
if(($cat_selected == $category->cat_ID)&&($default['rememberd']==1)):
$cat_option .=' selected';
endif;
$cat_option .= '>'.$category->cat_name;
$cat_option .= '</option>';
}
$cat_option .= '</select></div></ul></div>';
 endif;
 

if($default['tag_show']==1):
$tags = get_categories('taxonomy=post_tag'); 
$tag_option='<div><ul>';
if($default['label_show']):
$tag_option .='<div class="tag-label">'.__('برچسب','qaleb').'</div>';
endif;
$tag_option .='<div class="search-tag" ><select name="tag" id="searchtag" >
<option value="">'.__('همه','qaleb').'</option>';
 foreach ($tags as $tag) {
$tag_option .= '<option value="'.$tag->slug.'"';
if(($tag_selected == $tag->slug)&&($default['rememberd']==1)):
$tag_option .=' selected';
endif;
$tag_option .= '>'.$tag->cat_name;
$tag_option .= '</option>';
}
$tag_option .= '</select></div></ul></div>';
 endif;

if($default['author_show']==1):
$authors=get_users(array('who'=>'authors','fields'=>array('id','display_name')));
$author_option='<div><ul>';
if($default['label_show']):
$author_option .='<div class="author-label">'.__('نویسنده ','qaleb').'</div>';
endif;
$author_option .='<div class="search-author" >
<select name="tag" id="searchauthor" ><option value="">'.__('همه','qaleb').'</option>';
 foreach ($authors as $author) {
$author_option .= '<option value="'.$author->id.'"';
if(($author_selected == $author->id)&&($default['rememberd']==1)):
$author_option .=' selected';
endif;
$author_option .= '>'.$author->display_name;
$author_option .= '</option>';
}
$author_option .= '</select></div></ul></div>';
endif;



if($default['field_show']==1):
$field_option='<div><ul>';
if($default['label_show']):
$field_option .='<div class="search-label">'.__('جستجو برای : ','qaleb').'</div>';
endif;
$field_option .='<div class="search-field">
<input type="text" value="' . get_search_query() . '"
 name="s"  placeholder="'.__('عبارت مورد نظر','qaleb').'" />
</div>
</ul>
</div>';
endif;

if($default['button_show']==1):
$button_option='<div>
<input type="submit" class="search-submit" value="'. esc_attr__( 'جستجو','qaleb' ) .'" />
</div>';
endif;

$form = '<form role="search" method="get" class="searchform" action="' . home_url( '/' ) . '" ><ul>
 '.$cat_option.$tag_option.$author_option.$archive_option.$field_option.$button_option.'
 </ul></form>';
 
 if($default['echo']==1):
 echo $form;
 else:
 return $form;
 endif;
 
}
add_filter( 'get_search_form', 'custom_search_form',10,1 );




/*-----------------------------------------------------------------------------------*/
/* Get time_ago posts
/*-----------------------------------------------------------------------------------*/

	


function wt_time_ago() {
 
	global $post;
 
	$date = get_post_time('G', true, $post);
  
	$chunks = array(
		array( 60 * 60 * 24 * 365 , __( 'سال', 'qaleb' ), __( 'سال', 'qaleb' ) ),
		array( 60 * 60 * 24 * 30 , __( 'ماه', 'qaleb' ), __( 'ماه', 'qaleb' ) ),
		array( 60 * 60 * 24 * 7, __( 'هفته', 'qaleb' ), __( 'هفته', 'qaleb' ) ),
		array( 60 * 60 * 24 , __( 'روز', 'qaleb' ), __( 'روز', 'qaleb' ) ),
		array( 60 * 60 , __( 'ساعت', 'qaleb' ), __( 'ساعت', 'qaleb' ) ),
		array( 60 , __( 'دقیقه', 'qaleb' ), __( 'دقیقه', 'qaleb' ) ),
		array( 1, __( 'ثانیه', 'qaleb' ), __( 'ثانیه', 'wp-qaleb' ) )
	);
 
	if ( !is_numeric( $date ) ) {
		$time_chunks = explode( ':', str_replace( ' ', ':', $date ) );
		$date_chunks = explode( '-', str_replace( ' ', '-', $date ) );
		$date = gmmktime( (int)$time_chunks[1], (int)$time_chunks[2], (int)$time_chunks[3], (int)$date_chunks[1], (int)$date_chunks[2], (int)$date_chunks[0] );
	}
 
	$current_time = current_time( 'mysql', $gmt = 0 );
	$newer_date = strtotime( $current_time );
 

	$since = $newer_date - $date;
 

	if ( 0 > $since )
		return __( 'داغ', 'qaleb' );

	for ( $i = 0, $j = count($chunks); $i < $j; $i++) {
		$seconds = $chunks[$i][0];
 

		if ( ( $count = floor($since / $seconds) ) != 0 )
			break;
	}
 
	$output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
  
	if ( !(int)trim($output) ){
		$output = '0 ' . __( 'ثانیه', 'qaleb' );
	}
 
	$output .= __(' ', 'qaleb');
 
	return $output;
}



/*-----------------------------------------------------------------------------------*/
/* Get comment_posts posts
/*-----------------------------------------------------------------------------------*/


function tie_most_commented($comment_posts = 3 , $avatar_size = 80){
$comments = get_comments('status=approve&number='.$comment_posts);
foreach ($comments as $comment) { ?>
			<div class="item">			
            <div class="comment-image"><?php echo get_avatar( $comment, $avatar_size ); ?></div> 
            <div class="coauthor"><a href="<?php echo get_permalink($comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo strip_tags($comment->comment_author); ?> </a></div>
			<div class="coexcerpt"><a href="<?php echo get_permalink($comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo wp_html_excerpt( $comment->comment_content, 110 ); ?></a> </div> 
			</div> 	
<?php } 
}

/*-----------------------------------------------------------------------------------*/
/* Get thumbnails posts
/*-----------------------------------------------------------------------------------*/



if ( function_exists('add_theme_support') ) {
add_theme_support('post-thumbnails');
add_image_size('slider', 1100, 700, true, array( 'center', 'center' ) );
add_image_size('loop', 280, 180, true, array( 'center', 'center' ) );
add_image_size('loop-bar', 450, 320, true, array( 'center', 'center' ) );
add_image_size('post', 185, 125, true, array( 'center', 'center' ) );
add_image_size('woo', 280, 300, true, array( 'center', 'center' ) );
}
	

/*-----------------------------------------------------------------------------------*/
/* Get views posts
/*-----------------------------------------------------------------------------------*/

function getPostViews($postID){
    $count_key = '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, '0');
        return "بدون بازدید";
    }
    return $count.' بازدید';
}
 
// function to count views.
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
}
}


/*-----------------------------------------------------------------------------------*/
/* Get pagination posts
/*-----------------------------------------------------------------------------------*/


function numeric_posts_nav()
	{
	if (is_singular()) return;
	global $wp_query;
	/** Stop execution if there's only 1 page */
	if ($wp_query->max_num_pages <= 1) return;
	$paged = get_query_var('paged') ? absint(get_query_var('paged')) : 1;
	$max = intval($wp_query->max_num_pages);
	/** Add current page to the array */
	if ($paged >= 1) $links[] = $paged;
	/** Add the pages around the current page to the array */
	if ($paged >= 3)
		{
		$links[] = $paged - 1;
		$links[] = $paged - 2;
		}

	if (($paged + 2) <= $max)
		{
		$links[] = $paged + 2;
		$links[] = $paged + 1;
		}

	echo '<div class="pagination"><ul>' . "\n";
	/** Previous Post Link */
	if (get_previous_posts_link()) printf('<li>%s</li>' . "\n", get_previous_posts_link());
	/** Link to first page, plus ellipses if necessary */
	if (!in_array(1, $links))
		{
		$class = 1 == $paged ? ' class="active"' : '';
		printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link(1)) , '1');
		if (!in_array(2, $links)) echo '<li>…</li>';
		}

	/** Link to current page, plus 2 pages in either direction if necessary */
	sort($links);
	foreach((array)$links as $link)
		{
		$class = $paged == $link ? ' class="active"' : '';
		printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($link)) , $link);
		}

	/** Link to last page, plus ellipses if necessary */
	if (!in_array($max, $links))
		{
		if (!in_array($max - 1, $links)) echo '<li>…</li>' . "\n";
		$class = $paged == $max ? ' class="active"' : '';
		printf('<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url(get_pagenum_link($max)) , $max);
		}

	/** Next Post Link */
	if (get_next_posts_link()) printf('<li>%s</li>' . "\n", get_next_posts_link());
	echo '</ul></div>' . "\n";
	}


/*-----------------------------------------------------------------------------------*/
/* Get email posts
/*-----------------------------------------------------------------------------------*/




function direct_email($text="ارسال به دوستان"){
global $post;
$title = htmlspecialchars($post->post_title);
$subject = 'Sur '.htmlspecialchars(get_bloginfo('name')).' : '.$title;
$body = 'I recommend this page : '.$title.'. You can read it on : '.get_permalink($post->ID);
$link = '<a rel="nofollow" class="send_file left" href="mailto:?subject='.rawurlencode($subject).'&amp;body='.rawurlencode($body).'" title="'.$text.' : '.$title.'">'.$text.'</a>';
return $link;
}



/*-----------------------------------------------------------------------------------*/
/* Get comment posts
/*-----------------------------------------------------------------------------------*/




	function qaleb_comment($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li>
		<div class="comment-body">
		<div class="vcard">
		<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, '90' ); ?>
		</div>
		<div <?php comment_class('jhihaat_border'); ?>>
		<div class="commentmetadata"><?php printf('<cite class="fn">%s</cite>', get_comment_author_link()) ?> در <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s', 'qaleb'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link('(ویرایش)','  ','') ?></div>
		<div class="comment_content">
		<?php comment_text() ?>
		<?php if ($comment->comment_approved == '0') : ?><span><?php printf('%s عزیز ، ', get_comment_author_link()) ?><?php echo 'دیدگاه شما در حال بررسی است'; ?></span><?php endif; ?>
		</div></div>
		<div class="reply">
		<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
		</div>
		</div>
	<?php }
	/**
	* add page pagination
	*/
	function wp_corenavi() {
		global $wp_query, $wp_rewrite;
		$pages = '';
		$max = $wp_query->max_num_pages;
		if (!$current = get_query_var('paged')) $current = 1;
		$a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
		$a['total'] = $max;
		$a['current'] = $current;

		$total = 1; //1 - display the text "Page N of N", 0 - not display
		$a['mid_size'] = 5; //how many links to show on the left and right of the current
		$a['end_size'] = 1; //how many links to show in the beginning and end
		$a['prev_text'] = 'جدیدتر ها'; //text of the "Previous page" link
		$a['next_text'] = 'قدیمی تر ها'; //text of the "Next page" link

		if ($max > 1) echo '<div class="pagenavi">';
		if ($total == 1 && $max > 1) $pages = '<span class="pages">صفحه ' . $current . ' از ' . $max . '</span>'."\r\n";
		echo $pages . paginate_links($a);
		if ($max > 1) echo '</div>';
	}

	
/*-----------------------------------------------------------------------------------*/
/* Get sidebar posts
/*-----------------------------------------------------------------------------------*/
	
	


function unregister_default_wp_widgets() {
unregister_widget('WP_Widget_Search'); // ابزارک جست  و جو
}
add_action('widgets_init', 'unregister_default_wp_widgets', 1);
		register_sidebar( array(
		'name'          => 'صفحه نخست',
		'id'            => 'index',
		'description'   => 'جهت نمایش ابزارک ها',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-header"><h4>',
		'after_title'   => '</h4><b></b></div><div class="sidebar-content"><div class="post-wrap">',
    ));
	register_sidebar( array(
		'name'          => 'تبلیغات',
		'id'            => 'ads',
		'description'   => 'نمایش بنر تبلیغات',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-header"><h4>',
		'after_title'   => '</h4><b></b></div><div class="sidebar-content"><div class="post-wrap">',
    ));
		register_sidebar( array(
		'name'          => 'برگه نوشته',
		'id'            => 'post',
		'description'   => 'جهت نمایش ابزارک ها',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-header"><h4>',
		'after_title'   => '</h4><b></b></div><div class="sidebar-content"><div class="post-wrap">',
    ));	
	register_sidebar( array(
		'name'          => 'برگه فروشگاه',
		'id'            => 'shop',
		'description'   => 'جهت نمایش ابزارک ها',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-header"><h4>',
		'after_title'   => '</h4><b></b></div><div class="sidebar-content"><div class="post-wrap">',
    ));
	register_sidebar( array(
		'name'          => 'ابر برچسب',
		'id'            => 'tags',
		'description'   => 'نمایش برچسب ها در فوتر',
		'before_widget' => '',
		'after_widget'  => '',
		'before_title'  => '<div class="tags-header">',
		'after_title'   => '</div>',
    ));
	
?>