diff --git a/includes/class-sp-calendar.php b/includes/class-sp-calendar.php index 0bd2d566..87ac677d 100644 --- a/includes/class-sp-calendar.php +++ b/includes/class-sp-calendar.php @@ -39,6 +39,12 @@ class SP_Calendar extends SP_Secondary_Post { /** @var int The team ID. */ public $team; + + /** @var array The teams IDs. */ + public $teams_past; + + /** @var string The event date. */ + public $date_before; /** @var int The player ID. */ public $player; @@ -282,6 +288,24 @@ class SP_Calendar extends SP_Secondary_Post { ); endif; + // If we are showing past meetings filter by team's id and current event date + if ( $this->teams_past ): + foreach ( $this->teams_past as $team_past ): + $args['meta_query'][] = array( + 'key' => 'sp_team', + 'value' => $team_past, + 'compare' => '=', + ); + endforeach; + $args['date_query'] = array( + array( + 'before' => $this->date_before, + 'inclusive' => false, + ) + + ); + endif; + if ( $this->player ): $args['meta_query'][] = array( 'key' => 'sp_player', @@ -401,6 +425,17 @@ class SP_Calendar extends SP_Secondary_Post { else: $events = null; endif; + + // Filter out unessecary events if we are showing past meetings + if ( $this->teams_past ){ + $events_past = array(); + foreach ( $events as $single_event ) { + if ( get_post_meta( $single_event->ID,'sp_team' ) === $this->teams_past ){ + $events_past[] = $single_event; + } + } + $events = $events_past; + } // Remove any calendar selection filters remove_filter( 'posts_where', array( $this, 'range' ) ); diff --git a/modules/sportspress-event-past-meetings.php b/modules/sportspress-event-past-meetings.php new file mode 100644 index 00000000..374fca63 --- /dev/null +++ b/modules/sportspress-event-past-meetings.php @@ -0,0 +1,142 @@ +define_constants(); + + // Actions + + // Filters + add_filter( 'sportspress_event_templates', array( $this, 'templates' ) ); + add_filter( 'sportspress_text', array( $this, 'add_text_options' ) ); + add_filter( 'sportspress_event_settings', array( $this, 'add_settings' ) ); + } + + /** + * Define constants. + */ + private function define_constants() { + if ( !defined( 'SP_EVENT_PAST_MEETINGS_VERSION' ) ) + define( 'SP_EVENT_PAST_MEETINGS_VERSION', '2.7.0' ); + + if ( !defined( 'SP_EVENT_PAST_MEETINGS_URL' ) ) + define( 'SP_EVENT_PAST_MEETINGS_URL', plugin_dir_url( __FILE__ ) ); + + if ( !defined( 'SP_EVENT_PAST_MEETINGS_DIR' ) ) + define( 'SP_EVENT_PAST_MEETINGS_DIR', plugin_dir_path( __FILE__ ) ); + } + + /** + * Add templates to event layout. + * + * @return array + */ + public function templates( $templates = array() ) { + $templates['past_meetings'] = array( + 'title' => __( 'Past Meetings', 'sportspress' ), + 'option' => 'sportspress_event_show_past_meetings', + 'action' => array( $this, 'output' ), + 'default' => 'yes', + ); + + return $templates; + } + + /** + * Output Past Meetings. + * + * @access public + * @return void + */ + public function output() { + // Get timelines format option + $format = get_option( 'sportspress_past_meetings_format', 'blocks' ); + $teams = get_post_meta( get_the_ID(),'sp_team' ); + if ( 'list' === $format ): + sp_get_template( 'event-list.php', array( + 'teams_past' => $teams, + 'date_before' => get_post_time('Y-m-d', true), + 'title_format' => 'homeaway', + 'time_format' => 'separate', + 'columns' => array( 'event', 'time', 'results' ), + 'order' => 'DESC', + ) ); + else: + sp_get_template( 'event-blocks.php', array( + 'teams_past' => $teams, + 'date_before' => get_post_time('Y-m-d', true), + 'order' => 'DESC', + ) ); + endif; + } + + /** + * Add text options + */ + public function add_text_options( $options = array() ) { + return array_merge( $options, array( + __( 'Past Meetings', 'sportspress' ), + ) ); + } + + /** + * Add settings. + * + * @return array + */ + public function add_settings( $settings ) { + + $settings = array_merge( $settings, + array( + array( 'title' => __( 'Past Meetings', 'sportspress' ), 'type' => 'title', 'id' => 'past_meetings_options' ), + ), + + apply_filters( 'sportspress_past_meetings_options', array( + array( + 'title' => __( 'Layout', 'sportspress' ), + 'id' => 'sportspress_past_meetings_format', + 'default' => 'horizontal', + 'type' => 'radio', + 'options' => array( + 'blocks'=> __( 'Blocks', 'sportspress' ), + 'list' => __( 'List', 'sportspress' ), + ), + ), + ) ), + + array( + array( 'type' => 'sectionend', 'id' => 'past_meetings_options' ), + ) + ); + return $settings; + } + +} + +endif; + +new SportsPress_Event_Past_Meetings(); diff --git a/templates/event-blocks.php b/templates/event-blocks.php index 2d535159..2d2fe66c 100644 --- a/templates/event-blocks.php +++ b/templates/event-blocks.php @@ -26,6 +26,8 @@ $defaults = array( 'season' => null, 'venue' => null, 'team' => null, + 'teams_past' => null, + 'date_before' => null, 'player' => null, 'number' => -1, 'show_team_logo' => get_option( 'sportspress_event_blocks_show_logos', 'yes' ) == 'yes' ? true : false, @@ -73,6 +75,10 @@ if ( $venue ) $calendar->venue = $venue; if ( $team ) $calendar->team = $team; +if ( $teams_past ) + $calendar->teams_past = $teams_past; +if ( $date_before ) + $calendar->date_before = $date_before; if ( $player ) $calendar->player = $player; if ( $order != 'default' ) diff --git a/templates/event-list.php b/templates/event-list.php index 724cc682..7fe7e35a 100644 --- a/templates/event-list.php +++ b/templates/event-list.php @@ -25,6 +25,8 @@ $defaults = array( 'season' => null, 'venue' => null, 'team' => null, + 'teams_past' => null, + 'date_before' => null, 'player' => null, 'number' => -1, 'show_team_logo' => get_option( 'sportspress_event_list_show_logos', 'no' ) == 'yes' ? true : false, @@ -71,6 +73,10 @@ if ( $venue ) $calendar->venue = $venue; if ( $team ) $calendar->team = $team; +if ( $teams_past ) + $calendar->teams_past = $teams_past; +if ( $date_before ) + $calendar->date_before = $date_before; if ( $player ) $calendar->player = $player; if ( $order != 'default' )