Move common custom post functions into abstract

This commit is contained in:
Brian Miyaji
2014-04-27 17:35:57 +10:00
parent 284b0f1a5f
commit 100f172adc
13 changed files with 99 additions and 259 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* Abstract Custom Post Class
*
* The SportsPress custom post class handles individual post data.
*
* @class SP_Custom_Post
* @version 0.8
* @package SportsPress/Abstracts
* @category Abstract Class
* @author ThemeBoy
*/
abstract class SP_Custom_Post {
/** @var int The post ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Custom_Post ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __isset function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __isset( $key ) {
return metadata_exists( 'post', $this->ID, 'sp_' . $key );
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
}

View File

@@ -92,7 +92,8 @@ class SP_Admin_CPT_Calendar extends SP_Admin_CPT {
endif; endif;
break; break;
case 'sp_events': case 'sp_events':
echo sizeof( sp_get_calendar_data( $post_id ) ); $calendar = new SP_Calendar( $post_id );
echo sizeof( $calendar->data() );
break; break;
case 'sp_views': case 'sp_views':
echo sp_get_post_views( $post_id ); echo sp_get_post_views( $post_id );

View File

@@ -10,46 +10,7 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Calendar { class SP_Calendar extends SP_Custom_Post {
/** @var int The calendar (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Calendar ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/** /**
* Returns formatted data * Returns formatted data
@@ -135,5 +96,4 @@ class SP_Calendar {
return $events; return $events;
} }
} }

View File

@@ -10,46 +10,7 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Event { class SP_Event extends SP_Custom_Post{
/** @var int The event (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Event ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/** /**
* Returns formatted data * Returns formatted data
@@ -299,5 +260,4 @@ class SP_Event {
return $merged; return $merged;
endif; endif;
} }
} }

View File

@@ -10,50 +10,11 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_League_Table { class SP_League_Table extends SP_Custom_Post{
/** @var int The league table (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/** @var array The sort priorities array. */ /** @var array The sort priorities array. */
public $priorities; public $priorities;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_League_Table ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/** /**
* Returns formatted data * Returns formatted data
* *
@@ -379,5 +340,4 @@ class SP_League_Table {
// Default sort by alphabetical // Default sort by alphabetical
return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
} }
} }

View File

@@ -10,50 +10,11 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Player_List { class SP_Player_List extends SP_Custom_Post {
/** @var int The player list (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/** @var array The sort priorities array. */ /** @var array The sort priorities array. */
public $priorities; public $priorities;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Player_List ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/** /**
* Returns formatted data * Returns formatted data
* *

View File

@@ -10,66 +10,35 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Player { class SP_Player extends SP_Custom_Post {
/** @var int The player (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/** /**
* __construct function. * Returns past teams
* *
* @access public * @access public
* @param mixed $post * @return array
*/ */
public function __construct( $post ) { public function past_teams() {
if ( $post instanceof WP_Post || $post instanceof SP_Player ): return get_post_meta( $this->ID, 'sp_past_team', false );
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
} }
/** /**
* __get function. * Returns formatted player metrics
* *
* @access public * @access public
* @param mixed $key * @return array
* @return bool
*/ */
public function __get( $key ) { public function metrics() {
if ( ! isset( $key ) ):
return $this->post;
elseif ( 'past_teams' == $key ):
return get_post_meta( $this->ID, 'sp_past_team', false );
elseif ( 'metrics' == $key ):
$metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true ); $metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true );
// Get labels from metric variables
$metric_labels = (array)sp_get_var_labels( 'sp_metric' ); $metric_labels = (array)sp_get_var_labels( 'sp_metric' );
$data = array(); $data = array();
foreach( $metric_labels as $key => $value ): foreach( $metric_labels as $key => $value ):
$metric = sp_array_value( $metrics, $key, null ); $metric = sp_array_value( $metrics, $key, null );
if ( $metric == null ) if ( $metric == null )
continue; continue;
$data[ $value ] = sp_array_value( $metrics, $key, '&nbsp;' ); $data[ $value ] = sp_array_value( $metrics, $key, '&nbsp;' );
endforeach; endforeach;
return $data; return $data;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
} }
/** /**

View File

@@ -10,46 +10,7 @@
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Team { class SP_Team extends SP_Custom_Post {
/** @var int The team (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Team ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/** /**
* Returns formatted data * Returns formatted data
@@ -274,5 +235,4 @@ class SP_Team {
return $merged; return $merged;
endif; endif;
} }
} }

View File

@@ -2,7 +2,7 @@
class SP_Widget_Event_List extends WP_Widget { class SP_Widget_Event_List extends WP_Widget {
function __construct() { function __construct() {
$widget_ops = array('classname' => 'widget_calendar widget_sp_event_list', 'description' => __( 'A list of events.', 'sportspress' ) ); $widget_ops = array('classname' => 'widget_sp_event_list', 'description' => __( 'A list of events.', 'sportspress' ) );
parent::__construct('sp_event_list', __( 'SportsPress Events List', 'sportspress' ), $widget_ops); parent::__construct('sp_event_list', __( 'SportsPress Events List', 'sportspress' ), $widget_ops);
} }

View File

@@ -2,7 +2,7 @@
class SP_Widget_League_Table extends WP_Widget { class SP_Widget_League_Table extends WP_Widget {
function __construct() { function __construct() {
$widget_ops = array('classname' => 'widget_calendar widget_league_table widget_sp_league_table', 'description' => __( 'Display a league table.', 'sportspress' ) ); $widget_ops = array('classname' => 'widget_league_table widget_sp_league_table', 'description' => __( 'Display a league table.', 'sportspress' ) );
parent::__construct('sp_league_table', __( 'SportsPress League Table', 'sportspress' ), $widget_ops); parent::__construct('sp_league_table', __( 'SportsPress League Table', 'sportspress' ), $widget_ops);
} }

View File

@@ -2,7 +2,7 @@
class SP_Widget_Player_list extends WP_Widget { class SP_Widget_Player_list extends WP_Widget {
function __construct() { function __construct() {
$widget_ops = array('classname' => 'widget_calendar widget_player_list widget_sp_player_list', 'description' => __( 'Display a list of players.', 'sportspress' ) ); $widget_ops = array('classname' => 'widget_player_list widget_sp_player_list', 'description' => __( 'Display a list of players.', 'sportspress' ) );
parent::__construct('sp_player_list', __( 'SportsPress Player List', 'sportspress' ), $widget_ops); parent::__construct('sp_player_list', __( 'SportsPress Player List', 'sportspress' ), $widget_ops);
} }

View File

@@ -208,6 +208,9 @@ final class SportsPress {
// Post types // Post types
include_once( 'includes/class-sp-post-types.php' ); // Registers post types include_once( 'includes/class-sp-post-types.php' ); // Registers post types
// Include abstract classes
include_once( 'includes/abstracts/abstract-sp-custom-post.php' ); // Custom posts
// Classes (used on all pages) // Classes (used on all pages)
include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries
include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats

View File

@@ -24,8 +24,8 @@ $player = new SP_Player( $id );
$nationality = $player->nationality; $nationality = $player->nationality;
$current_team = $player->current_team; $current_team = $player->current_team;
$past_teams = $player->past_teams; $past_teams = $player->past_teams();
$metrics = $player->metrics; $metrics = $player->metrics();
$common = array(); $common = array();
if ( $nationality ): if ( $nationality ):