Add API functions for cleaner templates

This commit is contained in:
Brian Miyaji
2014-10-25 01:33:34 +11:00
parent 9886d4b4c6
commit e2c28e873c
7 changed files with 104 additions and 27 deletions

View File

@@ -60,9 +60,7 @@ class SP_Calendar extends SP_Custom_Post {
$this->from = get_post_meta( $this->ID, 'sp_date_from', true );
if ( ! $this->to ):
$to = new DateTime( get_post_meta( $this->ID, 'sp_date_to', true ) );
$to->modify( '+1 day' );
$this->to = $to->format( 'Y-m-d' );
$this->to = get_post_meta( $this->ID, 'sp_date_to', true );
endif;
}
@@ -163,11 +161,12 @@ class SP_Calendar extends SP_Custom_Post {
remove_filter( 'posts_where', array( $this, 'range' ) );
return $events;
}
public function range( $where = '' ) {
$where .= " AND post_date BETWEEN '" . $this->from . "' AND '" . $this->to . "'";
$to = new DateTime( $this->to );
$to->modify( '+1 day' );
$where .= " AND post_date BETWEEN '" . $this->from . "' AND '" . $to->format( 'Y-m-d' ) . "'";
return $where;
}
}

View File

@@ -111,6 +111,59 @@ class SP_Event extends SP_Custom_Post{
endif;
}
public function main_results() {
// Get main result option
$main_result = get_option( 'sportspress_main_result', null );
// Get teams from event
$teams = get_post_meta( $this->ID, 'sp_team', false );
// Initialize output
$output = array();
// Return empty array if there are no teams
if ( ! $teams ) return $output;
// Get results from event
$results = get_post_meta( $this->ID, 'sp_results', true );
// Loop through teams
foreach ( $teams as $team_id ) {
// Skip if not a team
if ( ! $team_id ) continue;
// Get team results from all results
$team_results = sp_array_value( $results, $team_id, null );
// Get main or last result
if ( $main_result ) {
// Get main result from team results
$team_result = sp_array_value( $team_results, $main_result, null );
} else {
// If there are any team results available
if ( is_array( $team_results ) ) {
// Get last result that is not outcome
unset( $team_results['outcome'] );
$team_result = end( $team_results );
} else {
// Give team null result
$team_result = null;
}
}
if ( null != $team_result ) {
$output[] = $team_result;
}
}
return $output;
}
public function lineup_filter( $v ) {
return sp_array_value( $v, 'status', 'lineup' ) == 'lineup';
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* SportsPress API Functions
*
* API functions for admin and front-end templates.
*
* @author ThemeBoy
* @category Core
* @package SportsPress/Functions
* @version 1.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/*
* General functions
*/
function sp_get_time( $post = null ) {
return get_post_time( get_option( 'time_format' ), false, $post, true );
}
function sp_time( $post = null ) {
echo sp_get_time( $post );
}
/*
* Event functions
*/
function sp_get_main_results( $post = null ) {
$event = new SP_Event( $post );
return $event->main_results();
}
function sp_main_results( $post = null ) {
$results = sp_get_main_results( $post );
echo implode( ' - ', $results );
}

View File

@@ -16,6 +16,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
include( 'sp-conditional-functions.php' );
include( 'sp-formatting-functions.php' );
include( 'sp-deprecated-functions.php' );
include( 'sp-api-functions.php' );
/**
* Get template part.

View File

@@ -12,10 +12,11 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$primary_result = get_option( 'sportspress_primary_result', null );
$defaults = array(
'id' => null,
'status' => 'default',
'date' => 'default',
'date_to' => 'default',
'date_from' => 'default',
'date_to' => 'default',
'number' => -1,
'link_teams' => get_option( 'sportspress_link_teams', 'no' ) == 'yes' ? true : false,
'paginated' => get_option( 'sportspress_event_blocks_paginated', 'yes' ) == 'yes' ? true : false,

View File

@@ -15,8 +15,8 @@ $defaults = array(
'id' => null,
'status' => 'default',
'date' => 'default',
'date_to' => 'default',
'date_from' => 'default',
'date_to' => 'default',
'initial' => true,
'caption_tag' => 'h4',
'show_all_events_link' => false,

View File

@@ -9,14 +9,12 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$primary_result = get_option( 'sportspress_primary_result', null );
$defaults = array(
'id' => null,
'status' => 'default',
'date' => 'default',
'date_to' => 'default',
'date_from' => 'default',
'date_to' => 'default',
'number' => -1,
'link_teams' => get_option( 'sportspress_link_teams', 'no' ) == 'yes' ? true : false,
'link_venues' => get_option( 'sportspress_link_venues', 'yes' ) == 'yes' ? true : false,
@@ -107,10 +105,10 @@ endif;
if ( isset( $limit ) && $i >= $limit ) continue;
$teams = get_post_meta( $event->ID, 'sp_team' );
$results = get_post_meta( $event->ID, 'sp_results', true );
$video = get_post_meta( $event->ID, 'sp_video', true );
$main_results = array();
$main_results = sp_get_main_results( $event );
$teams_output = '';
$teams_array = '';
@@ -118,27 +116,15 @@ endif;
foreach ( $teams as $team ):
$name = get_the_title( $team );
if ( $name ):
$team_results = sp_array_value( $results, $team, null );
if ( $primary_result ):
$team_result = sp_array_value( $team_results, $primary_result, null );
else:
if ( is_array( $team_results ) ):
unset( $team_results['outcome'] );
$team_result = end( $team_results );
else:
$team_result = null;
endif;
endif;
if ( $link_teams ):
$team_output = '<a href="' . get_post_permalink( $team ) . '">' . $name . '</a>';
else:
$team_output = $name;
endif;
$team_result = sp_array_value( $main_results, $team, null );
if ( $team_result != null ):
$main_results[] = $team_result;
if ( $usecolumns != null && ! in_array( 'time', $usecolumns ) ):
$team_output .= ' (' . $team_result . ')';
endif;
@@ -175,7 +161,7 @@ endif;
if ( ! empty( $main_results ) ):
echo implode( ' - ', $main_results );
else:
echo '<date>&nbsp;' . get_post_time( 'H:i:s', false, $event ) . '</date>' . get_post_time( get_option( 'time_format' ), false, $event, true );
echo '<date>&nbsp;' . get_post_time( 'H:i:s', false, $event ) . '</date>' . sp_get_time( $event );
endif;
echo '</a></td>';
endif;