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.