Created
December 4, 2017 08:40
-
-
Save congthien/c359a40ef523dba676065b6cb4b60d29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Widget Name: Gallery Images | |
Description: A gallery image widget | |
*/ | |
class Gallery_Widget extends SiteOrigin_Widget { | |
function __construct() { | |
//Here you can do any preparation required before calling the parent constructor, such as including additional files or initializing variables. | |
$tags = hotel_luxury_get_taxonomy( 'gallery_tag' ); | |
//Call the parent constructor with the required arguments. | |
parent::__construct( | |
// The unique id for your widget. | |
'gallery-widget', | |
// The name of the widget for display purposes. | |
esc_html__('Galery Images', 'hotel-luxury'), | |
// The $widget_options array, which is passed through to WP_Widget. | |
// It has a couple of extras like the optional help URL, which should link to your sites help or support page. | |
array( | |
'description' => esc_html__('Display multiple gallery by tags.', 'hotel-luxury'), | |
), | |
//The $control_options array, which is passed through to WP_Widget | |
array( | |
), | |
//The $form_options array, which describes the form fields used to configure SiteOrigin widgets. We'll explain these in more detail later. | |
array( | |
'title' => array( | |
'type' => 'text', | |
'label' => esc_html__( 'Gallery Title', 'hotel-luxury' ), | |
'default' => esc_html__( 'Gallery', 'hotel-luxury' ) | |
), | |
'gallery_tags' => array( | |
'type' => 'select', | |
'label' => esc_html__('Select some gallery tags', 'hotel-luxury'), | |
'default' => 'all', | |
'multiple' => true, | |
'options' => $tags | |
), | |
'number_toshow' => array( | |
'type' => 'number', | |
'label' => esc_html__( 'Nubmer of galleries to show', 'hotel-luxury' ), | |
'description' => esc_html__( 'Leave empty to show all', 'hotel-luxury' ), | |
'default' => '' | |
), | |
'column_layout' => array( | |
'type' => 'select', | |
'label' => esc_html__( 'Column Layout', 'hotel-luxury' ), | |
'default' => 'four', | |
'options' => array( | |
'six' => 2, | |
'four' => 3 | |
) | |
), | |
'enable_filter' => array( | |
'type' => 'checkbox', | |
'label' => esc_html__( 'Enable Filter by tags?', 'hotel-luxury' ), | |
'default' => '1' | |
) | |
), | |
//The $base_folder path string. | |
plugin_dir_path(__FILE__) | |
); | |
} | |
public function widget( $args, $instance ) { | |
$gallery_tags = $instance['gallery_tags']; | |
$enable_filter = esc_attr( $instance['enable_filter'] ); | |
?> | |
<div class="builder-item-wrapper builder-gallery "> | |
<div class="page-title-wrapper has_filter"> | |
<h2 class="page-title"><?php echo esc_attr( $instance['title'] ) ?></h2> | |
<!-- filter tag --> | |
<?php | |
if ( $enable_filter == 1 ) { | |
$filter = ''; | |
$terms = get_terms( 'gallery_tag', array( 'include' => $gallery_tags, 'fields' => 'all' ) ); | |
$filter .= '<ul data-option-key="filter" class="cpt-filters right"> | |
<li><button type="button" data-filter="all">' . esc_html__( 'All', 'hotel-luxury' ) . '</button></li>'; | |
foreach ( $terms as $term ) { | |
$filter .= '<li><button data-filter=".' . esc_attr( $term->slug ) . '">' . esc_html( stripslashes( $term->name ) ) . '</button></li>'; | |
} | |
$filter .= '</ul>'; | |
echo $filter; | |
} | |
?> | |
</div> | |
<!-- gallery layout --> | |
<?php | |
$number_post = esc_attr( $instance['number_toshow'] ); | |
$column = esc_attr( $instance['column_layout'] ); | |
$args = array( | |
'post_type' => 'gallery' | |
); | |
if ( $number_post ) { | |
$args['posts_per_page'] = $number_post; | |
} | |
if ( $gallery_tags ) { | |
$args['tax_query'] = array( | |
array( | |
'taxonomy' => 'gallery_tag', | |
'field' => 'term_id', | |
'terms' => $gallery_tags, | |
'operator' => 'IN' | |
), | |
); | |
} | |
$query = new WP_Query( $args ); | |
if ( $query->have_posts() ) { | |
echo '<div class="gallery-items row clearfix">'; | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
$gallery_id = get_the_ID(); | |
$term_list = wp_get_post_terms( $gallery_id, 'gallery_tag', array("fields" => "all")); | |
$filter_class = array(); | |
$caption = array(); | |
foreach($term_list as $term){ | |
$filter_class[]= $term->slug; | |
} | |
$image_data = hotel_luxury_get_gallery_data($gallery_id); | |
$n = count( $image_data ); | |
if( $n == 1 ){ | |
$caption = sprintf( esc_html__('Total %s photo','hotel-luxury'), $n ); | |
}else{ | |
$caption = sprintf( esc_html__('Total %s photos', 'hotel-luxury'), $n ); | |
} | |
if ( $n > 0 ) : | |
?> | |
<div class="mix <?php echo $column . ' ' . esc_attr( join(' ', $filter_class ) ); ?> column b30"> | |
<div class="thumb-wrapper"> | |
<a class="first-gallery-thumb" href="<?php echo $image_data[0]['full'] ?>"> | |
<img src="<?php echo $image_data[0]['thumbnail'] ?>" alt="" /> | |
<div><span><i class="fa fa-picture-o" aria-hidden="true"></i></span></div> | |
</a> | |
<div style="display: none"> | |
<?php | |
unset($image_data[0]); | |
foreach ( $image_data as $image ) { | |
?> | |
<a href="<?php echo $image['full'] ?>" title="<?php the_title() ?>" > | |
<img src="<?php echo $image['thumbnail'] ?>" alt="<?php echo $image['title'] ?>" /> | |
</a> | |
<?php } ?> | |
</div> | |
</div> | |
<div class="cpt-detail"> | |
<h2 class="cpt-title"><?php the_title() ?></h2> | |
<div class="cpt-desc"><?php echo $caption; ?></div> | |
</div> | |
</div> | |
<?php | |
endif; | |
} | |
echo '</div>'; | |
} | |
wp_reset_postdata(); | |
?> | |
</div> | |
<script type="text/javascript"> | |
jQuery( function ($) { | |
var mixer = mixitup('.gallery-items', { | |
selectors: { | |
target: '.mix' | |
}, | |
animation: { | |
duration: 300 | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
siteorigin_widget_register('gallery-widget', __FILE__, 'Gallery_Widget'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment