Add player gallery widget

This commit is contained in:
Brian Miyaji
2014-03-17 22:55:14 +11:00
parent a8587bd811
commit da10162988
3 changed files with 121 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ if ( !function_exists( 'sportspress_player_gallery' ) ) {
$id = get_the_ID(); $id = get_the_ID();
$defaults = array( $defaults = array(
'number' => -1,
'orderby' => 'default', 'orderby' => 'default',
'order' => 'ASC', 'order' => 'ASC',
'itemtag' => 'dl', 'itemtag' => 'dl',
@@ -13,6 +14,7 @@ if ( !function_exists( 'sportspress_player_gallery' ) ) {
'captiontag' => 'dd', 'captiontag' => 'dd',
'columns' => 3, 'columns' => 3,
'size' => 'thumbnail', 'size' => 'thumbnail',
'show_all_players_link' => false,
); );
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
@@ -59,8 +61,6 @@ if ( !function_exists( 'sportspress_player_gallery' ) ) {
uasort( $data, 'sportspress_sort_list_players' ); uasort( $data, 'sportspress_sort_list_players' );
endif; endif;
$i = 0;
$gallery_style = $gallery_div = ''; $gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) ) if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = " $gallery_style = "
@@ -86,28 +86,33 @@ if ( !function_exists( 'sportspress_player_gallery' ) ) {
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div ); $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
foreach( $data as $id => $statistics ): $i = 0;
$caption = get_the_title( $id ); if ( is_int( $r['number'] ) && $r['number'] > 0 )
$limit = $r['number'];
$thumbnail = get_the_post_thumbnail( $id, $size ); foreach( $data as $player_id => $statistics ):
$caption = get_the_title( $player_id );
$thumbnail = get_the_post_thumbnail( $player_id, $size );
if ( $thumbnail ): if ( $thumbnail ):
if ( isset( $limit ) && $i >= $limit ) continue;
$output .= "<{$itemtag} class='gallery-item'>"; $output .= "<{$itemtag} class='gallery-item'>";
$output .= " $output .= "
<{$icontag} class='gallery-icon portrait'>" <{$icontag} class='gallery-icon portrait'>"
. '<a href="' . get_permalink( $id ) . '">' . $thumbnail . '</a>' . '<a href="' . get_permalink( $player_id ) . '">' . $thumbnail . '</a>'
. "</{$icontag}>"; . "</{$icontag}>";
if ( $captiontag && trim($caption) ) { if ( $captiontag && trim($caption) ) {
$output .= " $output .= '<a href="' . get_permalink( $player_id ) . '">' . "
<{$captiontag} class='wp-caption-text gallery-caption'> <{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($caption) . " " . wptexturize($caption) . "
</{$captiontag}>"; </{$captiontag}>" . '</a>';
} }
$output .= "</{$itemtag}>"; $output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 ) if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />'; $output .= '<br style="clear: both" />';
endif; endif;
endforeach; endforeach;
@@ -116,6 +121,9 @@ if ( !function_exists( 'sportspress_player_gallery' ) ) {
<br style='clear: both;' /> <br style='clear: both;' />
</div>\n"; </div>\n";
if ( $r['show_all_players_link'] )
$output .= '<a class="sp-player-list-link" href="' . get_permalink( $id ) . '">' . __( 'View all players', 'sportspress' ) . '</a>';
return apply_filters( 'sportspress_player_gallery', $output ); return apply_filters( 'sportspress_player_gallery', $output );
} }

View File

@@ -0,0 +1,102 @@
<?php
class SportsPress_Widget_Player_Gallery extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_player_gallery widget_sp_player_gallery', 'description' => __( 'Display a gallery of players.', 'sportspress' ) );
parent::__construct('sp_player_gallery', __( 'SportsPress Player Gallery', 'sportspress' ), $widget_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$id = empty($instance['id']) ? null : $instance['id'];
$number = empty($instance['number']) ? null : $instance['number'];
$orderby = empty($instance['orderby']) ? 'default' : $instance['orderby'];
$order = empty($instance['order']) ? 'ASC' : $instance['order'];
$show_all_players_link = empty($instance['show_all_players_link']) ? false : $instance['show_all_players_link'];
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo '<div id="sp_player_list_wrap">';
echo sportspress_player_gallery( $id, array( 'number' => $number, 'orderby' => $orderby , 'order' => $order, 'show_all_players_link' => $show_all_players_link ) );
echo '</div>';
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['id'] = intval($new_instance['id']);
$instance['number'] = intval($new_instance['number']);
$instance['orderby'] = strip_tags($new_instance['orderby']);
$instance['order'] = strip_tags($new_instance['order']);
$instance['show_all_players_link'] = $new_instance['show_all_players_link'];
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => '', 'number' => 5, 'orderby' => 'default', 'order' => 'ASC', 'show_all_players_link' => true ) );
$title = strip_tags($instance['title']);
$id = intval($instance['id']);
$number = intval($instance['number']);
$orderby = strip_tags($instance['orderby']);
$order = strip_tags($instance['order']);
$show_all_players_link = $instance['show_all_players_link'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'sportspress' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('id'); ?>"><?php printf( __( 'Select %s:', 'sportspress' ), __( 'Player List', 'sportspress' ) ); ?></label>
<?php
$args = array(
'post_type' => 'sp_list',
'name' => $this->get_field_name('id'),
'id' => $this->get_field_id('id'),
'selected' => $id,
'values' => 'ID',
'class' => 'widefat',
);
if ( ! sportspress_dropdown_pages( $args ) ):
sportspress_post_adder( 'sp_list', __( 'Add New', 'sportspress' ) );
endif;
?>
</p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number of players to show:', 'sportspress' ); ?></label>
<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo esc_attr($number); ?>" size="3"></p>
<p><label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:', 'sportspress' ); ?></label>
<?php
$args = array(
'prepend_options' => array(
'default' => __( 'Default', 'sportspress' ),
'number' => __( 'Number', 'sportspress' ),
'name' => __( 'Name', 'sportspress' ),
'eventsplayed' => __( 'Played', 'sportspress' )
),
'post_type' => 'sp_statistic',
'name' => $this->get_field_name('orderby'),
'id' => $this->get_field_id('orderby'),
'selected' => $orderby,
'values' => 'slug',
'class' => 'sp-select-orderby widefat',
);
if ( ! sportspress_dropdown_pages( $args ) ):
sportspress_post_adder( 'sp_list', __( 'Add New', 'sportspress' ) );
endif;
?>
</p>
<p><label for="<?php echo $this->get_field_id('order'); ?>"><?php _e( 'Sort Order:', 'sportspress' ); ?></label>
<select name="<?php echo $this->get_field_name('order'); ?>" id="<?php echo $this->get_field_id('order'); ?>" class="sp-select-order widefat" <?php disabled( $orderby, 'default' ); ?>>
<option value="ASC" <?php selected( 'ASC', $order ); ?>><?php _e( 'Ascending', 'sportspress' ); ?></option>
<option value="DESC" <?php selected( 'DESC', $order ); ?>><?php _e( 'Descending', 'sportspress' ); ?></option>
</select></p>
<p><input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id('show_all_players_link'); ?>" name="<?php echo $this->get_field_name('show_all_players_link'); ?>" value="1" <?php checked( $show_all_players_link, 1 ); ?>>
<label for="<?php echo $this->get_field_id('show_all_players_link'); ?>"><?php _e( 'Display link to view all players', 'sportspress' ); ?></label></p>
<?php
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget( "SportsPress_Widget_Player_Gallery" );' ) );

View File

@@ -86,8 +86,9 @@ require_once dirname( __FILE__ ) . '/admin/terms/position.php';
require_once dirname( __FILE__ ) . '/admin/widgets/countdown.php'; require_once dirname( __FILE__ ) . '/admin/widgets/countdown.php';
require_once dirname( __FILE__ ) . '/admin/widgets/events-calendar.php'; require_once dirname( __FILE__ ) . '/admin/widgets/events-calendar.php';
require_once dirname( __FILE__ ) . '/admin/widgets/events-list.php'; require_once dirname( __FILE__ ) . '/admin/widgets/events-list.php';
require_once dirname( __FILE__ ) . '/admin/widgets/player-list.php';
require_once dirname( __FILE__ ) . '/admin/widgets/league-table.php'; require_once dirname( __FILE__ ) . '/admin/widgets/league-table.php';
require_once dirname( __FILE__ ) . '/admin/widgets/player-list.php';
require_once dirname( __FILE__ ) . '/admin/widgets/player-gallery.php';
// Tools // Tools
require_once dirname( __FILE__ ) . '/admin/tools/importers.php'; require_once dirname( __FILE__ ) . '/admin/tools/importers.php';