<?php

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

if ( function_exists('add_theme_support') ) {
add_theme_support('post-thumbnails');
add_image_size('loop-1', 250, 150, true, array( 'center', 'center' ) );
add_image_size('loop-2', 450, 300, true, array( 'center', 'center' ) );
add_image_size('loop-3', 600, 400, true, array( 'center', 'center' ) );
}

/*-----------------------------------------------------------------------------------*/
/* Get menus posts
/*-----------------------------------------------------------------------------------*/
	function reg_menu() {
		register_nav_menus(
			array(
			    'top'     => "منوی بالا",
			    'primary' => "منوی اصلی",
				'mega'    => " منوی تمام عرض",
			    'mobile'  => "منوی موبایل",
				'footer'  => "منوی فوتر"
			)
		);
	}
	add_action( 'init', 'reg_menu' );	

	
/*-----------------------------------------------------------------------------------*/
/* 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">
			
			<figure>
            <div class="thumc"><?php echo get_avatar( $comment, $avatar_size ); ?></div> 
            </figure>
			<h3><a href="<?php echo get_permalink($comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo strip_tags($comment->comment_author); ?> </a></h3>
			<p><?php echo wp_html_excerpt( $comment->comment_content, 105 ); ?><a href="<?php echo get_permalink($comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>"> ....</a></p>
			
			</div> 	
<?php } 
}



/*-----------------------------------------------------------------------------------*/
/* 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 "0";
    }
    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);
}
}





/*------------------------------------------*/
/*	Woocommerce
/*------------------------------------------*/


add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}



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



function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
 
global $paged;
if(empty($paged)) $paged = 1;
 
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
 
if(1 != $pages)
{
echo "<div class=\"pagination\">";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>اولین صفحه</a>";
if($paged > 1 && $showitems < $pages) echo "";
 
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
 
if ($paged < $pages && $showitems < $pages) echo "";
if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>آخرین صفحه</a>";
echo "</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 mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li id="<?php comment_class(); ?>">
<div class="top_cm" id="comment-<?php comment_ID() ?>">
<div class="avatar"><?php echo get_avatar( $comment, 50 ); ?></div>
<div class="com_da">
<span><?php printf(__('%s'), get_comment_author_link()) ?>  -  <?php printf(__('تاریخ : %1$s'), get_comment_date('j - F - Y')) ?></span>
</div>
<?php
if(function_exists('like_counter_c')) { like_counter_c(''); }
?>

<?php
if(function_exists('dislike_counter_c')) { dislike_counter_c(''); }
?>
</div><!-- top_cm -->
<div class="comment_box">
<?php comment_text() ?> 
<div class="replay_cm"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?></div>
</div><!-- comment_box -->
</li>
<?php
         }
		 
/*-----------------------------------------------------------------------------------*/
/* 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 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 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 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-right',
		'description'   => 'صفحه نخست راست',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-title"><h3>',
		'after_title'   => '</h3></div><div class="sidebar-content"><div class="post-wrap">',
    ));
		register_sidebar( array(
		'name'          => 'سایدبار چپ',
		'id'            => 'index-left',
		'description'   => 'صفحه نخست چپ',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-title"><h3>',
		'after_title'   => '</h3></div><div class="sidebar-content"><div class="post-wrap">',
    ));
		register_sidebar( array(
		'name'          => 'سایدبار نوشته',
		'id'            => 'index-post',
		'description'   => 'سمت چپ برگه نوشته و آرشیو',
		'before_widget' => '<div class="sidebar-box">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div class="sidebar-title"><h3>',
		'after_title'   => '</h3></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-title"><h3>',
		'after_title'   => '</h3></div><div class="sidebar-content"><div class="post-wrap">',
    ));	
	register_sidebar( array(
		'name'          => 'ابر برچسب',
		'id'            => 'tage',
		'description'   => 'نمایش برچسب ها در فوتر',
		'before_widget' => '<div class="sidebar-foot">',
		'after_widget'  => '</div></div></div>',
		'before_title'  => '<div><div class="sidebar-title"><h3>',
		'after_title'   => '</h3></div><div class="post-wrap">',
    ));		