diff --git a/includes/abstracts/abstract-sp-custom-post.php b/includes/abstracts/abstract-sp-custom-post.php new file mode 100644 index 00000000..14b42e62 --- /dev/null +++ b/includes/abstracts/abstract-sp-custom-post.php @@ -0,0 +1,66 @@ +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; + } + + +} diff --git a/includes/admin/post-types/class-sp-admin-cpt-calendar.php b/includes/admin/post-types/class-sp-admin-cpt-calendar.php index 90af5567..c1cea6c3 100644 --- a/includes/admin/post-types/class-sp-admin-cpt-calendar.php +++ b/includes/admin/post-types/class-sp-admin-cpt-calendar.php @@ -92,7 +92,8 @@ class SP_Admin_CPT_Calendar extends SP_Admin_CPT { endif; break; case 'sp_events': - echo sizeof( sp_get_calendar_data( $post_id ) ); + $calendar = new SP_Calendar( $post_id ); + echo sizeof( $calendar->data() ); break; case 'sp_views': echo sp_get_post_views( $post_id ); diff --git a/includes/class-sp-calendar.php b/includes/class-sp-calendar.php index a2f5de02..7c873dc0 100644 --- a/includes/class-sp-calendar.php +++ b/includes/class-sp-calendar.php @@ -10,46 +10,7 @@ * @category Class * @author ThemeBoy */ -class SP_Calendar { - - /** @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; - } +class SP_Calendar extends SP_Custom_Post { /** * Returns formatted data @@ -135,5 +96,4 @@ class SP_Calendar { return $events; } - } diff --git a/includes/class-sp-event.php b/includes/class-sp-event.php index 5f3444f8..120895a9 100644 --- a/includes/class-sp-event.php +++ b/includes/class-sp-event.php @@ -10,46 +10,7 @@ * @category Class * @author ThemeBoy */ -class SP_Event { - - /** @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; - } +class SP_Event extends SP_Custom_Post{ /** * Returns formatted data @@ -299,5 +260,4 @@ class SP_Event { return $merged; endif; } - } diff --git a/includes/class-sp-league-table.php b/includes/class-sp-league-table.php index 4fb7c9a7..74c2fd94 100644 --- a/includes/class-sp-league-table.php +++ b/includes/class-sp-league-table.php @@ -10,50 +10,11 @@ * @category Class * @author ThemeBoy */ -class SP_League_Table { - - /** @var int The league table (post) ID. */ - public $ID; - - /** @var object The actual post object. */ - public $post; +class SP_League_Table extends SP_Custom_Post{ /** @var array The sort priorities array. */ 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 * @@ -379,5 +340,4 @@ class SP_League_Table { // Default sort by alphabetical return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); } - } diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 137b0e21..3238c324 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -10,50 +10,11 @@ * @category Class * @author ThemeBoy */ -class SP_Player_List { - - /** @var int The player list (post) ID. */ - public $ID; - - /** @var object The actual post object. */ - public $post; +class SP_Player_List extends SP_Custom_Post { /** @var array The sort priorities array. */ 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 * diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index e0dd666b..8f5af6d1 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -10,66 +10,35 @@ * @category Class * @author ThemeBoy */ -class SP_Player { - - /** @var int The player (post) ID. */ - public $ID; - - /** @var object The actual post object. */ - public $post; +class SP_Player extends SP_Custom_Post { /** - * __construct function. + * Returns past teams * * @access public - * @param mixed $post + * @return array */ - public function __construct( $post ) { - if ( $post instanceof WP_Post || $post instanceof SP_Player ): - $this->ID = absint( $post->ID ); - $this->post = $post; - else: - $this->ID = absint( $post ); - $this->post = get_post( $this->ID ); - endif; + public function past_teams() { + return get_post_meta( $this->ID, 'sp_past_team', false ); } /** - * __get function. + * Returns formatted player metrics * * @access public - * @param mixed $key - * @return bool + * @return array */ - public function __get( $key ) { - 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 ); - - // Get labels from metric variables - $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); - - $data = array(); - - foreach( $metric_labels as $key => $value ): - - $metric = sp_array_value( $metrics, $key, null ); - if ( $metric == null ) - continue; - - $data[ $value ] = sp_array_value( $metrics, $key, ' ' ); - - endforeach; - - return $data; - else: - $value = get_post_meta( $this->ID, 'sp_' . $key, true ); - endif; - - return $value; + public function metrics() { + $metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true ); + $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); + $data = array(); + foreach( $metric_labels as $key => $value ): + $metric = sp_array_value( $metrics, $key, null ); + if ( $metric == null ) + continue; + $data[ $value ] = sp_array_value( $metrics, $key, ' ' ); + endforeach; + return $data; } /** diff --git a/includes/class-sp-team.php b/includes/class-sp-team.php index 46415737..51dfb337 100644 --- a/includes/class-sp-team.php +++ b/includes/class-sp-team.php @@ -10,46 +10,7 @@ * @category Class * @author ThemeBoy */ -class SP_Team { - - /** @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; - } +class SP_Team extends SP_Custom_Post { /** * Returns formatted data @@ -274,5 +235,4 @@ class SP_Team { return $merged; endif; } - } diff --git a/includes/widgets/class-sp-widget-event-list.php b/includes/widgets/class-sp-widget-event-list.php index f731427d..a3640efd 100644 --- a/includes/widgets/class-sp-widget-event-list.php +++ b/includes/widgets/class-sp-widget-event-list.php @@ -2,7 +2,7 @@ class SP_Widget_Event_List extends WP_Widget { 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); } diff --git a/includes/widgets/class-sp-widget-league-table.php b/includes/widgets/class-sp-widget-league-table.php index 6fc85524..fd1bf93d 100644 --- a/includes/widgets/class-sp-widget-league-table.php +++ b/includes/widgets/class-sp-widget-league-table.php @@ -2,7 +2,7 @@ class SP_Widget_League_Table extends WP_Widget { 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); } diff --git a/includes/widgets/class-sp-widget-player-list.php b/includes/widgets/class-sp-widget-player-list.php index 53c9c751..19922457 100644 --- a/includes/widgets/class-sp-widget-player-list.php +++ b/includes/widgets/class-sp-widget-player-list.php @@ -2,7 +2,7 @@ class SP_Widget_Player_list extends WP_Widget { 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); } diff --git a/sportspress.php b/sportspress.php index a7f08ecd..5a30ccc2 100644 --- a/sportspress.php +++ b/sportspress.php @@ -208,6 +208,9 @@ final class SportsPress { // 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) include_once( 'includes/class-sp-countries.php' ); // Defines continents and countries include_once( 'includes/class-sp-formats.php' ); // Defines custom post type formats diff --git a/templates/player-details.php b/templates/player-details.php index eb02dc64..8acf360f 100644 --- a/templates/player-details.php +++ b/templates/player-details.php @@ -24,8 +24,8 @@ $player = new SP_Player( $id ); $nationality = $player->nationality; $current_team = $player->current_team; -$past_teams = $player->past_teams; -$metrics = $player->metrics; +$past_teams = $player->past_teams(); +$metrics = $player->metrics(); $common = array(); if ( $nationality ):