template_path() . "{$slug}-{$name}.php" ) );
}
// Get default slug-name.php
if ( ! $template && $name && file_exists( SP()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) {
$template = SP()->plugin_path() . "/templates/{$slug}-{$name}.php";
}
// If template file doesn't exist, look in yourtheme/slug.php and yourtheme/sportspress/slug.php
if ( ! $template ) {
$template = locate_template( array( "{$slug}.php", SP()->template_path() . "{$slug}.php" ) );
}
// Allow 3rd party plugin filter template file from their plugin
$template = apply_filters( 'sportspress_get_template_part', $template, $slug, $name );
if ( $template ) {
load_template( $template, false );
}
}
/**
* Get templates passing attributes and including the file.
*
* @access public
* @param mixed $template_name
* @param array $args (default: array())
* @param string $template_path (default: '')
* @param string $default_path (default: '')
* @return void
*/
function sp_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
if ( $args && is_array( $args ) ) {
extract( $args );
}
$located = sp_locate_template( $template_name, $template_path, $default_path );
if ( ! file_exists( $located ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( '%s does not exist.', $located ), '0.7' );
return;
}
do_action( 'sportspress_before_template', $template_name, $template_path, $located, $args );
include( $located );
do_action( 'sportspress_after_template', $template_name, $template_path, $located, $args );
}
/**
* Locate a template and return the path for inclusion.
*
* This is the load order:
*
* yourtheme / $template_path / $template_name
* yourtheme / $template_name
* $default_path / $template_name
*
* @access public
* @param mixed $template_name
* @param string $template_path (default: '')
* @param string $default_path (default: '')
* @return string
*/
function sp_locate_template( $template_name, $template_path = '', $default_path = '' ) {
if ( ! $template_path ) {
$template_path = SP()->template_path();
}
if ( ! $default_path ) {
$default_path = SP()->plugin_path() . '/templates/';
}
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
trailingslashit( $template_path ) . $template_name,
$template_name
)
);
// Get default template
if ( ! $template ) {
$template = $default_path . $template_name;
}
// Return what we found
return apply_filters('sportspress_locate_template', $template, $template_name, $template_path);
}
function sp_substr( $string = '', $start = 0, $length = null ) {
if ( function_exists( 'mb_substr' ) ) {
return mb_substr( $string, $start, $length );
} else {
return substr( $string, $start, $length );
}
}
function sp_strtoupper( $string = '' ) {
if ( function_exists( 'mb_strtoupper' ) ) {
return mb_strtoupper( $string );
} else {
return strtoupper( $string );
}
}
/**
* Get the timezone string.
*
* @access public
* @return string
*/
function sp_get_timezone() {
$tzstring = get_option( 'timezone_string' );
// Remove old Etc mappings. Fallback to gmt_offset.
if ( false !== strpos( $tzstring, 'Etc/GMT' ) )
$tzstring = '';
if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string exists
$current_offset = get_option( 'gmt_offset' );
if ( 0 == $current_offset )
$tzstring = 'UTC+0';
elseif ( $current_offset < 0 )
$tzstring = 'UTC' . $current_offset;
else
$tzstring = 'UTC+' . $current_offset;
}
return $tzstring;
}
/* deprecated functions below */
if ( !function_exists( 'date_diff' ) ) {
class DateInterval {
public $y;
public $m;
public $d;
public $h;
public $i;
public $s;
public $invert;
public $days;
public function format($format) {
$format = str_replace('%R%y',
($this->invert ? '-' : '+') . $this->y, $format);
$format = str_replace('%R%m',
($this->invert ? '-' : '+') . $this->m, $format);
$format = str_replace('%R%d',
($this->invert ? '-' : '+') . $this->d, $format);
$format = str_replace('%R%h',
($this->invert ? '-' : '+') . $this->h, $format);
$format = str_replace('%R%i',
($this->invert ? '-' : '+') . $this->i, $format);
$format = str_replace('%R%s',
($this->invert ? '-' : '+') . $this->s, $format);
$format = str_replace('%y', $this->y, $format);
$format = str_replace('%m', $this->m, $format);
$format = str_replace('%d', $this->d, $format);
$format = str_replace('%h', $this->h, $format);
$format = str_replace('%i', $this->i, $format);
$format = str_replace('%s', $this->s, $format);
return $format;
}
}
function date_diff(DateTime $date1, DateTime $date2) {
$diff = new DateInterval();
if($date1 > $date2) {
$tmp = $date1;
$date1 = $date2;
$date2 = $tmp;
$diff->invert = 1;
} else {
$diff->invert = 0;
}
$diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y'));
$diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n'));
if($diff->m < 0) {
$diff->y -= 1;
$diff->m = $diff->m + 12;
}
$diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j'));
if($diff->d < 0) {
$diff->m -= 1;
$diff->d = $diff->d + ((int) $date1->format('t'));
}
$diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G'));
if($diff->h < 0) {
$diff->d -= 1;
$diff->h = $diff->h + 24;
}
$diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i'));
if($diff->i < 0) {
$diff->h -= 1;
$diff->i = $diff->i + 60;
}
$diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s'));
if($diff->s < 0) {
$diff->i -= 1;
$diff->s = $diff->s + 60;
}
$start_ts = $date1->format('U');
$end_ts = $date2->format('U');
$days = $end_ts - $start_ts;
$diff->days = round($days / 86400);
if (($diff->h > 0 || $diff->i > 0 || $diff->s > 0))
$diff->days += ((bool) $diff->invert)
? 1
: -1;
return $diff;
}
}
if ( !function_exists( 'sp_flush_rewrite_rules' ) ) {
function sp_flush_rewrite_rules() {
// Flush rewrite rules
$post_types = new SP_Post_types();
$post_types->register_taxonomies();
$post_types->register_post_types();
flush_rewrite_rules();
}
}
if ( !function_exists( 'sp_add_link' ) ) {
function sp_add_link( $string, $link = false, $active = true ) {
if ( empty( $link ) || ! $active ) return $string;
return '' . $string . '';
}
}
if ( !function_exists( 'sp_nonce' ) ) {
function sp_nonce() {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
}
}
if ( !function_exists( 'sp_get_option' ) ) {
function sp_get_option( $option, $default = null ) {
if ( isset( $_POST[ $option ] ) )
return $_POST[ $option ];
else
return get_option( $option, $default );
}
}
if ( !function_exists( 'sp_array_between' ) ) {
function sp_array_between ( $array = array(), $delimiter = 0, $index = 0 ) {
$keys = array_keys( $array, $delimiter );
if ( array_key_exists( $index, $keys ) ):
$offset = $keys[ $index ];
$end = sizeof( $array );
if ( array_key_exists( $index + 1, $keys ) )
$end = $keys[ $index + 1 ];
$length = $end - $offset;
$array = array_slice( $array, $offset, $length );
endif;
return $array;
}
}
if ( !function_exists( 'sp_array_value' ) ) {
function sp_array_value( $arr = array(), $key = 0, $default = null ) {
return ( isset( $arr[ $key ] ) ? $arr[ $key ] : $default );
}
}
if ( !function_exists( 'sp_array_combine' ) ) {
function sp_array_combine( $keys = array(), $values = array(), $key_order = false ) {
if ( ! is_array( $keys ) ) return array();
if ( ! is_array( $values ) ) $values = array();
$output = array();
if ( $key_order ):
foreach( $keys as $key ):
if ( array_key_exists( $key, $values ) )
$output[ $key ] = $values[ $key ];
else
$output[ $key ] = array();
endforeach;
else:
foreach ( $values as $key => $value ):
if ( in_array( $key, $keys ) ):
$output[ $key ] = $value;
endif;
endforeach;
foreach ( $keys as $key ):
if ( $key !== false && ! array_key_exists( $key, $output ) )
$output[ $key ] = array();
endforeach;
endif;
return $output;
}
}
if ( !function_exists( 'sp_numbers_to_words' ) ) {
function sp_numbers_to_words( $str ) {
$output = str_replace( array( '%', '1st', '2nd', '3rd', '5th', '8th', '9th', '10', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ), array( 'percent', 'first', 'second', 'third', 'fifth', 'eight', 'ninth', 'ten', 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ), $str );
return $output;
}
}
if ( !function_exists( 'sp_column_active' ) ) {
function sp_column_active( $array = null, $value = null ) {
return $array == null || in_array( $value, $array );
}
}
if ( !function_exists( 'sp_get_the_term_id' ) ) {
function sp_get_the_term_id( $post_id, $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && sizeof( $terms ) > 0 ):
$term = reset( $terms );
if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
return $term->term_id;
else
return 0;
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_get_the_term_ids' ) ) {
function sp_get_the_term_ids( $post_id, $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
$term_ids = array();
if ( is_array( $terms ) && sizeof( $terms ) > 0 ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
}
$term_ids = sp_add_auto_term( $term_ids, $post_id, $taxonomy );
return $term_ids;
}
}
if ( !function_exists( 'sp_get_the_term_id_or_meta' ) ) {
function sp_get_the_term_id_or_meta( $post_id, $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && sizeof( $terms ) > 0 ):
$term = reset( $terms );
if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
return $term->term_id;
else
return 0;
else:
return get_post_meta( $post_id, $taxonomy, true );
endif;
}
}
if ( !function_exists( 'sp_add_auto_term' ) ) {
function sp_add_auto_term( $term_ids, $post_id, $taxonomy ) {
switch ( $taxonomy ) {
case 'sp_league':
if ( get_post_meta( $post_id, 'sp_main_league', true ) ) {
$term_id = get_option( 'sportspress_league', false );
if ( $term_id ) $term_ids[] = $term_id;
}
break;
case 'sp_season':
if ( get_post_meta( $post_id, 'sp_current_season', true ) ) {
$term_id = get_option( 'sportspress_season', false );
if ( $term_id ) $term_ids[] = $term_id;
}
break;
}
return $term_ids;
}
}
if ( !function_exists( 'sp_get_url' ) ) {
function sp_get_url( $post_id ) {
$url = get_post_meta( $post_id, 'sp_url', true );
if ( ! $url ) return;
return ' ' . $url . '';
}
}
if ( !function_exists( 'sp_get_post_abbreviation' ) ) {
function sp_get_post_abbreviation( $post_id ) {
$abbreviation = get_post_meta ( $post_id, 'sp_abbreviation', true );
if ( $abbreviation ):
return $abbreviation;
else:
return mb_substr( get_the_title( $post_id ), 0, 1 );
endif;
}
}
if ( !function_exists( 'sp_get_post_condition' ) ) {
function sp_get_post_condition( $post_id ) {
$condition = get_post_meta ( $post_id, 'sp_condition', true );
$main_result = get_option( 'sportspress_primary_result', null );
$result = get_page_by_path( $main_result, ARRAY_A, 'sp_result' );
$label = sp_array_value( $result, 'post_title', __( 'Primary', 'sportspress' ) );
if ( $condition ):
$conditions = array(
'0' => '—',
'>' => sprintf( __( 'Most %s', 'sportspress' ), $label ),
'<' => sprintf( __( 'Least %s', 'sportspress' ), $label ),
'=' => sprintf( __( 'Equal %s', 'sportspress' ), $label ),
'else' => sprintf( __( 'Default', 'sportspress' ), $label ),
);
return sp_array_value( $conditions, $condition, '—' );
else:
return '—';
endif;
}
}
if ( !function_exists( 'sp_get_post_precision' ) ) {
function sp_get_post_precision( $post_id ) {
$precision = get_post_meta ( $post_id, 'sp_precision', true );
if ( $precision ):
return $precision;
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_get_post_calculate' ) ) {
function sp_get_post_calculate( $post_id ) {
$calculate = get_post_meta ( $post_id, 'sp_calculate', true );
if ( $calculate ):
return str_replace(
array( 'total', 'average' ),
array( __( 'Total', 'sportspress' ), __( 'Average', 'sportspress' ) ),
$calculate
);
else:
return __( 'Total', 'sportspress' );
endif;
}
}
if ( !function_exists( 'sp_get_post_equation' ) ) {
function sp_get_post_equation( $post_id ) {
$equation = get_post_meta ( $post_id, 'sp_equation', true );
if ( $equation ):
$equation = str_replace(
array( '/', '(', ')', '+', '-', '*', '_', '$' ),
array( '÷', '(', ')', '+', '−', '×', '@', '' ),
trim( $equation )
);
return '' . implode( ' ', explode( ' ', $equation ) ) . '';
else:
return '—';
endif;
}
}
if ( !function_exists( 'sp_get_post_order' ) ) {
function sp_get_post_order( $post_id ) {
$priority = get_post_meta ( $post_id, 'sp_priority', true );
if ( $priority ):
return $priority . ' ' . str_replace(
array( 'DESC', 'ASC' ),
array( '↓', '↑' ),
get_post_meta ( $post_id, 'sp_order', true )
);
else:
return '—';
endif;
}
}
if ( !function_exists( 'sp_get_post_section' ) ) {
function sp_get_post_section( $post_id ) {
$section = get_post_meta ( $post_id, 'sp_section', true );
if ( isset( $section ) ):
$options = apply_filters( 'sportspress_performance_sections', array( -1 => __( 'All', 'sportspress' ), 0 => __( 'Offense', 'sportspress' ), 1 => __( 'Defense', 'sportspress' ) ) );
return sp_array_value( $options, $section, __( 'All', 'sportspress' ) );
else:
return __( 'All', 'sportspress' );
endif;
}
}
if ( !function_exists( 'sp_get_post_format' ) ) {
function sp_get_post_format( $post_id ) {
$format = get_post_meta ( $post_id, 'sp_format', true );
if ( isset( $format ) ):
$options = apply_filters( 'sportspress_performance_formats', array( 'number' => __( 'Number', 'sportspress' ), 'time' => __( 'Time', 'sportspress' ), 'text' => __( 'Text', 'sportspress' ), 'equation' => __( 'Equation', 'sportspress' ), 'checkbox' => __( 'Checkbox', 'sportspress' ) ) );
return sp_array_value( $options, $format, __( 'Number', 'sportspress' ) );
else:
return __( 'Number', 'sportspress' );
endif;
}
}
if ( !function_exists( 'sp_get_format_placeholder' ) ) {
function sp_get_format_placeholder( $key = 'number' ) {
$placeholders = apply_filters( 'sportspress_format_placeholders', array(
'number' => 0,
'time' => '0:00',
'text' => ' ',
'checkbox' => ' ',
) );
return sp_array_value( $placeholders, $key, 0 );
}
}
if ( !function_exists( 'sp_get_term_sections' ) ) {
function sp_get_term_sections( $t_id ) {
$term_meta = get_option( "taxonomy_$t_id" );
if ( isset( $term_meta['sp_sections'] ) ) {
$sections = $term_meta['sp_sections'];
} else {
$sections = apply_filters( 'sportspress_performance_sections', array( 0 => __( 'Offense', 'sportspress' ), 1 => __( 'Defense', 'sportspress' ) ) );
$sections = array_keys( $sections );
}
if ( '' === $sections ) {
$sections = array();
}
return $sections;
}
}
if ( !function_exists( 'sp_get_default_mode' ) ) {
function sp_get_default_mode() {
$mode = get_option( 'sportspress_mode', 'team' );
if ( empty( $mode ) ) {
$mode = 'team';
}
return $mode;
}
}
if ( !function_exists( 'sp_get_post_mode' ) ) {
function sp_get_post_mode( $post_id ) {
$mode = get_post_meta( $post_id, 'sp_mode', true );
if ( empty( $mode ) ) {
$mode = sp_get_default_mode();
}
return $mode;
}
}
if ( !function_exists( 'sp_get_post_mode_type' ) ) {
function sp_get_post_mode_type( $post_id ) {
$mode = sp_get_post_mode( $post_id );
$post_type = "sp_$mode";
if ( ! in_array( $post_type, sp_primary_post_types() ) ) {
$post_type = sp_get_default_mode();
}
return $post_type;
}
}
if ( !function_exists( 'sp_get_post_mode_label' ) ) {
function sp_get_post_mode_label( $post_id, $singular = false ) {
$labels = array(
'team' => array(
__( 'Teams', 'sportspress' ),
__( 'Team', 'sportspress' ),
),
'player' => array(
__( 'Players', 'sportspress' ),
__( 'Player', 'sportspress' ),
),
);
$mode = sp_get_post_mode( $post_id );
if ( ! array_key_exists( $mode, $labels ) ) {
$mode = 'team';
}
$index = intval( $singular );
return $labels[ $mode ][ $index ];
}
}
if ( !function_exists( 'sp_dropdown_statuses' ) ) {
function sp_dropdown_statuses( $args = array() ) {
$defaults = array(
'show_option_default' => false,
'name' => 'sp_status',
'id' => null,
'selected' => null,
'class' => null,
);
$args = array_merge( $defaults, $args );
printf( '' );
return true;
}
}
if ( !function_exists( 'sp_dropdown_dates' ) ) {
function sp_dropdown_dates( $args = array() ) {
$defaults = array(
'show_option_default' => false,
'name' => 'sp_date',
'id' => null,
'selected' => null,
'class' => null,
);
$args = array_merge( $defaults, $args );
printf( '' );
return true;
}
}
if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
function sp_dropdown_taxonomies( $args = array() ) {
$defaults = array(
'show_option_blank' => false,
'show_option_all' => false,
'show_option_none' => false,
'show_option_auto' => false,
'taxonomy' => null,
'name' => null,
'id' => null,
'selected' => null,
'hide_empty' => false,
'values' => 'slug',
'class' => null,
'property' => null,
'placeholder' => null,
'chosen' => false,
'parent' => 0,
'include_children' => true,
);
$args = array_merge( $defaults, $args );
if ( ! $args['taxonomy'] ) return false;
$name = ( $args['name'] ) ? $args['name'] : $args['taxonomy'];
$id = ( $args['id'] ) ? $args['id'] : $name;
unset( $args['name'] );
unset( $args['id'] );
$class = $args['class'];
unset( $args['class'] );
$property = $args['property'];
unset( $args['property'] );
$placeholder = $args['placeholder'];
unset( $args['placeholder'] );
$selected = $args['selected'];
unset( $args['selected'] );
$chosen = $args['chosen'];
unset( $args['chosen'] );
$terms = get_terms( $args['taxonomy'], $args );
printf( '', $args['taxonomy'] );
if ( $terms ):
printf( '' );
return true;
else:
return false;
endif;
}
}
if ( !function_exists( 'sp_dropdown_pages' ) ) {
function sp_dropdown_pages( $args = array() ) {
$defaults = array(
'prepend_options' => null,
'append_options' => null,
'show_option_blank' => false,
'show_option_all' => false,
'show_option_none' => false,
'show_dates' => false,
'option_all_value' => 0,
'option_none_value' => -1,
'name' => 'page_id',
'id' => null,
'selected' => null,
'numberposts' => -1,
'posts_per_page' => -1,
'child_of' => 0,
'order' => 'ASC',
'orderby' => 'title',
'hierarchical' => 1,
'exclude' => null,
'include' => null,
'meta_key' => null,
'meta_value' => null,
'authors' => null,
'exclude_tree' => null,
'post_type' => 'page',
'post_status' => 'publish',
'values' => 'post_name',
'class' => null,
'property' => null,
'placeholder' => null,
'chosen' => false,
'filter' => false,
);
$args = array_merge( $defaults, $args );
$name = $args['name'];
unset( $args['name'] );
$id = ( $args['id'] ) ? $args['id'] : $name;
unset( $args['id'] );
$values = $args['values'];
unset( $args['values'] );
$class = $args['class'];
unset( $args['class'] );
$property = $args['property'];
unset( $args['property'] );
$placeholder = $args['placeholder'];
unset( $args['placeholder'] );
$selected = $args['selected'];
unset( $args['selected'] );
$chosen = $args['chosen'];
unset( $args['chosen'] );
$filter = $args['filter'];
unset( $args['filter'] );
$posts = get_posts( $args );
if ( $posts || $args['prepend_options'] || $args['append_options'] ):
printf( '' );
return true;
else:
return false;
endif;
}
}
if ( !function_exists( 'sp_posts' ) ) {
function sp_posts( $post_id = null, $meta = 'post' ) {
if ( ! isset( $post_id ) )
global $post_id;
$ids = get_post_meta( $post_id, $meta, false );
if ( ( $key = array_search( 0, $ids ) ) !== false )
unset( $ids[ $key ] );
$i = 0;
$count = count( $ids );
if ( isset( $ids ) && $ids && is_array( $ids ) && !empty( $ids ) ):
foreach ( $ids as $id ):
if ( !$id ) continue;
$parents = get_post_ancestors( $id );
$keys = array_keys( $parents );
$values = array_reverse( array_values( $parents ) );
if ( ! empty( $keys ) && ! empty( $values ) ):
$parents = array_combine( $keys, $values );
foreach ( $parents as $parent ):
if ( !in_array( $parent, $ids ) )
edit_post_link( get_the_title( $parent ), '', '', $parent );
echo ' - ';
endforeach;
endif;
$title = get_the_title( $id );
if ( ! $title )
continue;
if ( empty( $title ) )
$title = __( '(no title)', 'sportspress' );
edit_post_link( $title, '', '', $id );
if ( ++$i !== $count )
echo ', ';
endforeach;
endif;
}
}
if ( !function_exists( 'sp_post_checklist' ) ) {
function sp_post_checklist( $post_id = null, $meta = 'post', $display = 'block', $filters = null, $index = null, $slug = null ) {
if ( ! isset( $post_id ) )
global $post_id;
if ( ! isset( $slug ) )
$slug = $meta;
?>
labels->singular_name; ?>
ID, $taxonomy ); $term_ids = array(); if ( $terms ): foreach ( $terms as $term ): $term_ids[] = $term->term_id; endforeach; endif; // Set auto option $auto = false; if ( in_array( $post_type, sp_secondary_post_types() ) ) { switch ( $taxonomy ) { case 'sp_league': $auto = __( 'Main League', 'sportspress' ); if ( get_post_meta( $post->ID, 'sp_main_league', true ) ) $term_ids[] = 'auto'; break; case 'sp_season': $auto = __( 'Current Season', 'sportspress' ); if ( get_post_meta( $post->ID, 'sp_current_season', true ) ) $term_ids[] = 'auto'; break; } } $args = array( 'show_option_auto' => $auto, 'taxonomy' => $taxonomy, 'name' => 'tax_input[' . $taxonomy . '][]', 'selected' => $term_ids, 'values' => 'term_id', 'class' => 'sp-has-dummy widefat' . ( $trigger ? ' sp-ajax-trigger' : '' ), 'chosen' => true, 'placeholder' => $placeholder ? $placeholder : __( 'All', 'sportspress' ), ); if ( $multiple ) { $args['property'] = 'multiple'; } if ( ! sp_dropdown_taxonomies( $args ) ): sp_taxonomy_adder( $taxonomy, $post_type, $obj->labels->add_new_item ); endif; ?>