diff --git a/assets/css/admin.css b/assets/css/admin.css index 62457efd..c14e3047 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -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; diff --git a/includes/widgets/class-sp-widget-birthdays.php b/includes/widgets/class-sp-widget-birthdays.php new file mode 100644 index 00000000..98c999b3 --- /dev/null +++ b/includes/widgets/class-sp-widget-birthdays.php @@ -0,0 +1,72 @@ + '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' ); + ?> +
+
+ ++ + +
+ 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: %1$s': + case 'Published on: %1$s': + case 'Schedule for: %1$s': + case 'Publish on: %1$s': + return __( 'Birthday: %1$s', 'sportspress' ); + case 'Publish immediately': + 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 = ''; + $minute = ''; + 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(); diff --git a/templates/birthdays.php b/templates/birthdays.php new file mode 100644 index 00000000..41ba86da --- /dev/null +++ b/templates/birthdays.php @@ -0,0 +1,76 @@ + '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 '