Add birthdays to core

This commit is contained in:
Brian Miyaji
2016-02-26 02:34:38 +11:00
parent 134cd5775b
commit aa304767a8
4 changed files with 356 additions and 0 deletions

View File

@@ -48,6 +48,11 @@
background: #fdfdfd;
}
.post-type-sp_player .curtime #timestamp:before {
font-family: sportspress, dashicons;
content: "\f453";
}
.sp-link:before {
font: normal 20px/1 dashicons;
speak: none;

View File

@@ -0,0 +1,72 @@
<?php
class SP_Widget_Birthdays extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_sportspress widget_birthdays widget_sp_birthdays', 'description' => __( 'Display players and staff on their birthday.', 'sportspress' ) );
parent::__construct('sportspress-birthdays', __( 'Birthdays', 'sportspress' ), $widget_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$date = empty($instance['date']) ? 'day' : strip_tags($instance['date']);
do_action( 'sportspress_before_widget', $args, $instance, 'birthdays' );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
// Action to hook into
do_action( 'sportspress_before_widget_template', $args, $instance, 'birthdays' );
sp_get_template( 'birthdays.php', array( 'date' => $date ) );
// Action to hook into
do_action( 'sportspress_after_widget_template', $args, $instance, 'birthdays' );
echo $after_widget;
do_action( 'sportspress_after_widget', $args, $instance, 'birthdays' );
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['date'] = strip_tags($new_instance['date']);
// Filter to hook into
$instance = apply_filters( 'sportspress_widget_update', $instance, $new_instance, $old_instance, 'birthdays' );
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'date' => 'day' ) );
$title = strip_tags($instance['title']);
$date = strip_tags($instance['date']);
$options = array(
'day' => __( 'Today', 'sportspress' ),
'month' => __( 'This month', 'sportspress' ),
);
// Action to hook into
do_action( 'sportspress_before_widget_template_form', $this, $instance, 'birthdays' );
?>
<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('date'); ?>"><?php _e( 'Birthday:', 'sportspress' ); ?></label>
<select name="<?php echo $this->get_field_name('date'); ?>" id="<?php echo $this->get_field_id('date'); ?>" class="postform widefat">
<?php foreach ( $options as $value => $label ) { ?>
<option value="<?php echo $value; ?>" <?php selected( $value, $date ); ?>><?php echo $label; ?></option>
<?php } ?>
</select>
</p>
<?php
// Action to hook into
do_action( 'sportspress_after_widget_template_form', $this, $instance, 'birthdays' );
}
}
register_widget( 'SP_Widget_Birthdays' );

View File

@@ -0,0 +1,203 @@
<?php
/*
Plugin Name: SportsPress Birthdays
Plugin URI: http://themeboy.com/
Description: Add birthdays to players and staff.
Author: ThemeBoy
Author URI: http://themeboy.com/
Version: 1.6
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'SportsPress_Birthdays' ) ) :
/**
* Main SportsPress Birthdays Class
*
* @class SportsPress_Birthdays
* @version 1.6
*/
class SportsPress_Birthdays {
/**
* Constructor
*/
public function __construct() {
// Define constants
$this->define_constants();
add_filter( 'gettext', array( $this, 'gettext' ), 20, 3 );
add_filter( 'sportspress_player_options', array( $this, 'add_player_options' ) );
add_filter( 'sportspress_staff_options', array( $this, 'add_staff_options' ) );
add_filter( 'sportspress_player_details', array( $this, 'add_player_details' ), 20, 2 );
add_filter( 'sportspress_staff_details', array( $this, 'add_staff_details' ), 20, 2 );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'sportspress_widgets', array( $this, 'widgets' ) );
}
/**
* Define constants.
*/
private function define_constants() {
if ( !defined( 'SP_BIRTHDAYS_VERSION' ) )
define( 'SP_BIRTHDAYS_VERSION', '1.6' );
if ( !defined( 'SP_BIRTHDAYS_URL' ) )
define( 'SP_BIRTHDAYS_URL', plugin_dir_url( __FILE__ ) );
if ( !defined( 'SP_BIRTHDAYS_DIR' ) )
define( 'SP_BIRTHDAYS_DIR', plugin_dir_path( __FILE__ ) );
}
/**
* Text filter.
*/
public function gettext( $translated_text, $untranslated_text, $domain ) {
if ( ! is_admin() ) return $translated_text;
global $typenow;
if ( 'default' == $domain && in_array( $typenow, array( 'sp_player', 'sp_staff' ) ) ):
switch ( $untranslated_text ):
case 'Scheduled for: <b>%1$s</b>':
case 'Published on: <b>%1$s</b>':
case 'Schedule for: <b>%1$s</b>':
case 'Publish on: <b>%1$s</b>':
return __( 'Birthday: <b>%1$s</b>', 'sportspress' );
case 'Publish <b>immediately</b>':
return __( 'Birthday', 'sportspress' );
case 'M j, Y @ G:i':
return 'M j, Y';
case '%1$s %2$s, %3$s @ %4$s : %5$s':
$hour = '<input type="hidden" id="hh" name="hh" value="00" readonly />';
$minute = '<input type="hidden" id="mn" name="mn" value="00" readonly />';
return '%1$s %2$s, %3$s' . $hour . $minute;
endswitch;
endif;
return $translated_text;
}
/**
* Add options to player settings page.
*
* @return array
*/
public function add_player_options( $options ) {
$options = array_merge( $options, array(
array(
'title' => __( 'Birthday', 'sportspress' ),
'desc' => __( 'Display birthday', 'sportspress' ),
'id' => 'sportspress_player_show_birthday',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Display age', 'sportspress' ),
'id' => 'sportspress_player_show_age',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
),
) );
return $options;
}
/**
* Add options to staff settings page.
*
* @return array
*/
public function add_staff_options( $options ) {
$options = array_merge( $options, array(
array(
'title' => __( 'Birthday', 'sportspress' ),
'desc' => __( 'Display birthday', 'sportspress' ),
'id' => 'sportspress_staff_show_birthday',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start',
),
array(
'desc' => __( 'Display age', 'sportspress' ),
'id' => 'sportspress_staff_show_age',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
),
) );
return $options;
}
/**
* Add data to player details template.
*
* @return array
*/
public function add_player_details( $data, $post_id ) {
if ( 'yes' == get_option( 'sportspress_player_show_birthday', 'no' ) ) {
$data[ __( 'Birthday', 'sportspress' ) ] = get_the_date( get_option( 'date_format' ), $post_id );
}
if ( 'yes' == get_option( 'sportspress_player_show_age', 'no' ) ) {
$data[ __( 'Age', 'sportspress' ) ] = $this->get_age( get_the_date( 'm-d-Y' ) );
}
return $data;
}
/**
* Add data to staff details template.
*
* @return array
*/
public function add_staff_details( $data, $post_id ) {
if ( 'yes' == get_option( 'sportspress_staff_show_birthday', 'no' ) ) {
$data[ __( 'Birthday', 'sportspress' ) ] = get_the_date( get_option( 'date_format' ), $post_id );
}
if ( 'yes' == get_option( 'sportspress_staff_show_age', 'no' ) ) {
$data[ __( 'Age', 'sportspress' ) ] = $this->get_age( get_the_date( 'm-d-Y' ) );
}
return $data;
}
public static function admin_enqueue_scripts() {
wp_enqueue_style( 'sportspress-birthdays-admin', SP_BIRTHDAYS_URL . 'css/admin.css', array( 'sportspress-admin-menu-styles' ), time() );
}
/**
* Register widgets
*/
public static function widgets() {
include_once( SP()->plugin_path() . '/includes/widgets/class-sp-widget-birthdays.php' );
}
/**
* Get age from date.
* Adapted from http://stackoverflow.com/questions/3776682/php-calculate-age.
*
* @return int
*/
public static function get_age( $date ) {
$date = explode( '-', $date );
$age = ( date( 'md', date( 'U', mktime( 0, 0, 0, $date[0], $date[1], $date[2] ) ) ) > date('md')
? ( ( date( 'Y' ) - $date[2] ) - 1 )
: ( date( 'Y' ) - $date[2] ) );
return $age;
}
}
endif;
new SportsPress_Birthdays();

76
templates/birthdays.php Normal file
View File

@@ -0,0 +1,76 @@
<?php
/**
* Birthdays
*
* @author ThemeBoy
* @package SportsPress_Birthdays
* @version 1.9
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$html5 = current_theme_supports( 'html5', 'gallery' );
$defaults = array(
'date' => 'day',
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'size' => 'sportspress-fit-medium',
'show_player_birthday' => get_option( 'sportspress_player_show_birthday', 'no' ) == 'yes' ? true : false,
'show_staff_birthday' => get_option( 'sportspress_staff_show_birthday', 'no' ) == 'yes' ? true : false,
'link_players' => get_option( 'sportspress_link_players', 'yes' ) == 'yes' ? true : false,
'link_staff' => get_option( 'sportspress_link_staff', 'yes' ) == 'yes' ? true : false,
);
extract( $defaults, EXTR_SKIP );
$args = array(
'post_type' => array( 'sp_player', 'sp_staff' ),
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'ASC',
'monthnum' => date('n'),
);
if ( $date == 'day' ) {
$args['day'] = date('j');
}
$posts = get_posts( $args );
foreach ( $posts as $post ) {
echo '<div class="sp-template sp-template-birthdays sp-template-birthday-gallery sp-template-gallery">';
if ( 'sp_staff' == $post->post_type ) {
$link_posts = $link_staff;
$show_birthday = $show_staff_birthday;
} else {
$link_posts = $link_players;
$show_birthday = $show_player_birthday;
}
$birthday = get_the_date( get_option( 'date_format') , $post->ID );
if ( $show_birthday && $birthday && $group !== $birthday ) {
echo '<h4 class="sp-table-caption">' . $birthday . '</h4>';
}
echo '<div class="gallery">';
$caption = $post->post_title;
$caption = trim( $caption );
sp_get_template( 'player-gallery-thumbnail.php', array(
'id' => $post->ID,
'itemtag' => $itemtag,
'icontag' => $icontag,
'captiontag' => $captiontag,
'caption' => $caption,
'size' => $size,
'link_posts' => $link_posts,
) );
echo '<br style="clear: both;" />';
echo "</div></div>\n";
}