<?php

class post_img extends WP_Widget {
public function __construct(){
$widget_ops = array(
'classname' => 'post_img',
'description' => 'سایدبار',
);
parent::__construct( 'post_img', 'Pastor - نوشته با عکس ', $widget_ops );
}

function widget( $args, $instance ) {
$widget_title = apply_filters( 'widget_title', $instance['widget_title'] );
$post_num = apply_filters( 'post_num', $instance['post_num'] );
$post_cat = apply_filters( 'post_cat', $instance['post_cat'] );
$limit_title = apply_filters( 'limit_title', $instance['limit_title'] );
echo $args['before_widget'];
if( !empty($widget_title) ) {
echo $args['before_title'];
echo $widget_title;
echo $args['after_title'];
}
?>
<?php
$catquery = new WP_Query('cat=' . $post_cat . '&posts_per_page=' . $post_num .'');
if( $catquery->have_posts() ) :
while( $catquery->have_posts() ) : $catquery->the_post();
?>
<div class="post_img">
<div class="magi">
<?php if(has_post_thumbnail()){?>
<a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url('loop'); ?>" alt="<?php the_title(); ?>"></a>
<?php }else{ ?>
<img src="<?php bloginfo("template_url"); ?>/img/none.png">
<?php } ?>
<a href="<?php the_permalink(); ?>" class="overlayy"><i class="fa fa-file-text-o" aria-hidden="true"></i></a>
</div>
			<div class="ttil">
			  <?php if(get_post_meta( get_the_ID(), 'meta-text-1', true )!=null){ ?><span><?php echo get_post_meta( get_the_ID(), 'meta-text-1', true ); ?></span><?php } ?>
				<h2><a href="<?php the_permalink(); ?>">
				    <?php
                        if($limit_title == "on") :
                            qaleb_title('', '...', true, '70');
                        else :
                            the_title();
                        endif;
                        ?>
				</a></h2>
			</div>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
<?php
echo $args['after_widget'];
}

function update( $new_instance, $old_instance ) {
$instance = array();
$instance['widget_title'] = ( ! empty( $new_instance['widget_title'] ) ) ? strip_tags( $new_instance['widget_title'] ) : '';
$instance['post_num'] = ( ! empty( $new_instance['post_num'] ) ) ? strip_tags( $new_instance['post_num'] ) : '';
$instance['post_cat'] = ( ! empty( $new_instance['post_cat'] ) ) ? strip_tags( $new_instance['post_cat'] ) : '';
$instance['limit_title'] = ( ! empty( $new_instance['limit_title'] ) ) ? $new_instance['limit_title'] : '';
return $instance;
}

function form( $instance ) {
if ( isset( $instance[ 'widget_title' ] ) ) {
$widget_title = $instance[ 'widget_title' ];
} else {
$widget_title = 'جدیدترین مطالب';
}

if ( isset( $instance[ 'post_num' ] ) ) {
$post_num = $instance[ 'post_num' ];
} else {
$post_num = '3';
}

if ( isset( $instance[ 'limit_title' ] ) ) {
$limit_title = $instance[ 'limit_title' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'widget_title' ); ?>">نام:</label>
<input name="<?php echo $this->get_field_name( 'widget_title' ); ?>" type="text" value="<?php echo esc_attr( $widget_title ); ?>" id="<?php echo $this->get_field_id( 'widget_title' ); ?>" class="widefat">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_cat' ); ?>">دسته بندی:</label>
<select name="<?php echo $this->get_field_name( 'post_cat' ); ?>" id="<?php echo $this->get_field_id( 'post_cat' ); ?>" class="widefat">
<option value='0' <?php selected( $instance['post_cat'], "0" ); ?>>مقالات از دسته بندی </option>
<?php foreach(get_categories( array('hide_empty' => 0) ) as $cat) { ?>
<option <?php selected( $instance['post_cat'], $cat->cat_ID ); ?> value="<?php echo $cat->cat_ID; ?>"><?php echo $cat->cat_name; ?></option>
<?php } ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_num' ); ?>">تعداد نوشته‌هایی که نمایش داده می‌شود:</label>
<input name="<?php echo $this->get_field_name( 'post_num' ); ?>" type="number" step="1" min="1" value="<?php echo esc_attr( $post_num ); ?>" size="3" id="<?php echo $this->get_field_id( 'post_num' ); ?>" class="tiny-text">
</p>
		<p>
		    <input type="checkbox" name="<?php echo $this->get_field_name( 'limit_title' ); ?>" id="<?php echo $this->get_field_id( 'limit_title' ); ?>" class="checkbox" <?php checked( $limit_title, "on" ); ?>>
		    <label for="<?php echo $this->get_field_id( 'limit_title' ); ?>">محدود سازی عنوان جهت نمایش بهتر</label>
		</p>
<?php
}
}

function post_img_widget() {
register_widget( 'post_img' );
}

add_action( 'widgets_init', 'post_img_widget' );
?>