/**
 * Display format (gallery, video) icon
 *
 * @param $key_class
 */
function maxflat_get_format_ico($key_class)
{
    $class_name = __MAXFLAT::layout()->get_awesome_icon_class($key_class);
    $formats_array = __MAXFLAT::layout()->get_promoted_formats();
    if (in_array($key_class, $formats_array)) {
        ?>

		<span class="smartlib-format-ico"><i class="<?php 
        echo $class_name;
        ?>
"></i></span>
	<?php 
    }
}
    function widget($args, $instance)
    {
        $title = apply_filters('widget_title', $instance['title']);
        $limit = is_int($instance['gallery_limit']) ? $instance['gallery_limit'] : 4;
        extract($args);
        echo $before_widget;
        ?>

	<?php 
        if ($title) {
            echo $before_title . $title . $after_title;
        }
        $query = new WP_Query(array('posts_per_page' => $limit, 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array('post-format-gallery')))));
        if ($query->have_posts()) {
            ?>



			<ul class="small-block-grid-2">
				<?php 
            while ($query->have_posts()) {
                $query->the_post();
                ?>


							<?php 
                $featured_image = __MAXFLAT::layout()->get_featured_image('medium-square');
                if ('' != get_the_post_thumbnail()) {
                    ?>

								<li>
					<a href="<?php 
                    the_permalink();
                    ?>
" class="smartlib-thumbnail-outer"><?php 
                    maxflat_get_format_ico('gallery');
                    the_post_thumbnail('medium-square');
                    ?>
</a>
								</li>
								<?php 
                } else {
                    if (!empty($featured_image)) {
                        ?>

									<li>
						<a href="<?php 
                        the_permalink();
                        ?>
" class="smartlib-thumbnail-outer"><?php 
                        maxflat_get_format_ico('gallery');
                        echo $featured_image;
                        ?>
</a></li>
								<?php 
                    }
                }
                ?>


					<?php 
            }
            wp_reset_postdata();
            ?>

			</ul>

		<?php 
            echo $after_widget;
            ?>

		<?php 
        }
    }