Move common custom post functions into abstract
This commit is contained in:
66
includes/abstracts/abstract-sp-custom-post.php
Normal file
66
includes/abstracts/abstract-sp-custom-post.php
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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 );
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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', '' ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user