Add sp_spec CPT for Event Specs

a) Create sp_spec
b) Add it to Config Page
c) Add Admin Meta-Box
d) Add Event Meta-Box
This commit is contained in:
savvasha
2018-02-16 13:53:56 +02:00
parent 87887a38aa
commit fa1877747c
12 changed files with 323 additions and 3 deletions

View File

@@ -143,7 +143,7 @@ class SP_Admin_Sports {
update_post_meta( $id, 'sp_equation', sp_array_value( $result, 'equation', null ) );
}
// Make sure statistics and metrics have greater menu order than performance
// Make sure statistics, metrics and specs have greater menu order than performance
$i = 0;
// Performance
@@ -192,6 +192,17 @@ class SP_Admin_Sports {
$id = self::insert_preset_post( $post, $i + $index );
$i ++;
}
// Event Specs
$post_type = 'sp_spec';
$specs = sp_array_value( $preset, 'specs', array() );
self::delete_preset_posts( $post_type );
foreach ( $specs as $index => $spec ) {
$post = self::get_post_array( $spec, $post_type );
if ( empty( $post ) ) continue;
$id = self::insert_preset_post( $post, $i + $index );
$i ++;
}
// Statistics
$post_type = 'sp_statistic';

View File

@@ -0,0 +1,70 @@
<?php
/**
* Admin functions for the columns post type
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Post_Types
* @version 0.9
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Admin_CPT' ) )
include( 'class-sp-admin-cpt.php' );
if ( ! class_exists( 'SP_Admin_CPT_Spec' ) ) :
/**
* SP_Admin_CPT_Spec Class
*/
class SP_Admin_CPT_Spec extends SP_Admin_CPT {
/**
* Constructor
*/
public function __construct() {
$this->type = 'sp_spec';
// Admin Columns
add_filter( 'manage_edit-sp_spec_columns', array( $this, 'edit_columns' ) );
add_action( 'manage_sp_spec_posts_custom_column', array( $this, 'custom_columns' ), 2, 2 );
// Call SP_Admin_CPT constructor
parent::__construct();
}
/**
* Change the columns shown in admin.
*/
public function edit_columns( $existing_columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Label', 'sportspress' ),
'sp_key' => __( 'Variable', 'sportspress' ),
'sp_description' => __( 'Description', 'sportspress' ),
);
return apply_filters( 'sportspress_spec_admin_columns', $columns );
}
/**
* Define our custom columns shown in admin.
* @param string $column
*/
public function custom_columns( $column, $post_id ) {
switch ( $column ):
case 'sp_key':
global $post;
echo $post->post_name;
break;
case 'sp_description':
global $post;
echo '<span class="description">' . $post->post_excerpt . '</span>';
break;
endswitch;
}
}
endif;
return new SP_Admin_CPT_Spec();

View File

@@ -93,6 +93,15 @@ class SP_Admin_Meta_Boxes {
'priority' => 'high',
),
),
'sp_spec' => array(
'details' => array(
'title' => __( 'Specs', 'sportspress' ),
'save' => 'SP_Meta_Box_Spec_Details::save',
'output' => 'SP_Meta_Box_Spec_Details::output',
'context' => 'normal',
'priority' => 'high',
),
),
'sp_statistic' => array(
'details' => array(
'title' => __( 'Details', 'sportspress' ),
@@ -158,6 +167,13 @@ class SP_Admin_Meta_Boxes {
'context' => 'normal',
'priority' => 'high',
),
'specs' => array(
'title' => __( 'Specs', 'sportspress' ),
'save' => 'SP_Meta_Box_Event_Specs::save',
'output' => 'SP_Meta_Box_Event_Specs::output',
'context' => 'side',
'priority' => 'default',
),
),
'sp_team' => array(
'details' => array(

View File

@@ -0,0 +1,53 @@
<?php
/**
* Player Metrics
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 1.9.7
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* SP_Meta_Box_Event_Specs
*/
class SP_Meta_Box_Event_Specs {
/**
* Output the metabox
*/
public static function output( $post ) {
$metrics = get_post_meta( $post->ID, 'sp_specs', true );
$args = array(
'post_type' => 'sp_spec',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$vars = get_posts( $args );
if ( $vars ):
foreach ( $vars as $var ):
?>
<p><strong><?php echo $var->post_title; ?></strong></p>
<p><input type="text" name="sp_specs[<?php echo $var->post_name; ?>]" value="<?php echo esc_attr( sp_array_value( $metrics, $var->post_name, '' ) ); ?>" /></p>
<?php
endforeach;
else:
sp_post_adder( 'sp_spec', __( 'Add New', 'sportspress' ) );
endif;
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_specs', sp_array_value( $_POST, 'sp_specs', array() ) );
}
}

View File

@@ -0,0 +1,62 @@
<?php
/**
* Spec Details
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/**
* SP_Meta_Box_Spec_Details
*/
class SP_Meta_Box_Spec_Details extends SP_Meta_Box_Config {
/**
* Output the metabox
*/
public static function output( $post ) {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
$visible = get_post_meta( $post->ID, 'sp_visible', true );
if ( '' === $visible ) $visible = 1;
?>
<p><strong><?php _e( 'Variable', 'sportspress' ); ?></strong></p>
<p>
<input name="sp_default_key" type="hidden" id="sp_default_key" value="<?php echo $post->post_name; ?>">
<input name="sp_key" type="text" id="sp_key" value="<?php echo $post->post_name; ?>">
</p>
<p>
<strong><?php _e( 'Visible', 'sportspress' ); ?></strong>
<i class="dashicons dashicons-editor-help sp-desc-tip" title="<?php _e( 'Display in event pages?', 'sportspress' ); ?>"></i>
</p>
<ul class="sp-visible-selector">
<li>
<label class="selectit">
<input name="sp_visible" id="sp_visible_yes" type="radio" value="1" <?php checked( $visible ); ?>>
<?php _e( 'Yes', 'sportspress' ); ?>
</label>
</li>
<li>
<label class="selectit">
<input name="sp_visible" id="sp_visible_no" type="radio" value="0" <?php checked( ! $visible ); ?>>
<?php _e( 'No', 'sportspress' ); ?>
</label>
</li>
</ul>
<?php
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
self::delete_duplicate( $_POST );
update_post_meta( $post_id, 'sp_visible', sp_array_value( $_POST, 'sp_visible', 1 ) );
}
}

View File

@@ -34,6 +34,8 @@ function sp_get_screen_ids() {
'sp_column',
'edit-sp_metric',
'sp_metric',
'edit-sp_spec',
'sp_spec',
'edit-sp_statistic',
'sp_statistic',
'edit-sp_event',

View File

@@ -296,6 +296,55 @@ $columns = get_option( 'sportspress_player_columns', 'auto' );
</tr>
</tbody>
</table>
<table class="form-table">
<tbody>
<?php
$args = array(
'post_type' => 'sp_spec',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$data = get_posts( $args );
?>
<tr valign="top">
<th scope="row" class="titledesc">
<?php _e( 'Event Specs', 'sportspress' ) ?>
<p class="description"><?php _e( 'Add more details to an event.', 'sportspress' ); ?></p>
</th>
<td class="forminp">
<table class="widefat sp-admin-config-table">
<thead>
<tr>
<th scope="col"><?php _e( 'Label', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Variable', 'sportspress' ); ?></th>
<th scope="col"><?php _e( 'Description', 'sportspress' ); ?></th>
<th scope="col" class="edit"></th>
</tr>
</thead>
<?php if ( $data ): $i = 0; foreach ( $data as $row ): ?>
<tr<?php if ( $i % 2 == 0 ) echo ' class="alternate"'; ?>>
<td class="row-title"><?php echo $row->post_title; ?></td>
<td><code><?php echo $row->post_name; ?></code></td>
<td><p class="description"><?php echo $row->post_excerpt; ?></p></td>
<td class="edit"><a class="button" href="<?php echo get_edit_post_link( $row->ID ); ?>"><?php _e( 'Edit', 'sportspress' ); ?></s></td>
</tr>
<?php $i++; endforeach; else: ?>
<tr class="alternate">
<td colspan="4"><?php _e( 'No results found.', 'sportspress' ); ?></td>
</tr>
<?php endif; ?>
</table>
<div class="tablenav bottom">
<a class="button alignleft" href="<?php echo admin_url( 'edit.php?post_type=sp_spec' ); ?>"><?php _e( 'View All', 'sportspress' ); ?></a>
<a class="button button-primary alignright" href="<?php echo admin_url( 'post-new.php?post_type=sp_spec' ); ?>"><?php _e( 'Add New', 'sportspress' ); ?></a>
<br class="clear">
</div>
</td>
</tr>
</tbody>
</table>
<table class="form-table">
<tbody>
<?php

View File

@@ -707,4 +707,24 @@ class SP_Event extends SP_Custom_Post{
public function sort_timeline( $a, $b ) {
return $a['time'] - $b['time'];
}
/**
* Returns formatted event specs
*
* @access public
* @return array
*/
public function specs( $neg = null ) {
$specs = (array)get_post_meta( $this->ID, 'sp_specs', true );
$spec_labels = (array)sp_get_var_labels( 'sp_spec', $neg, false );
$data = array();
foreach ( $spec_labels as $key => $value ):
$spec = sp_array_value( $specs, $key, null );
if ( $spec == null )
continue;
$data[ $value ] = sp_array_value( $specs, $key, '&nbsp;' );
endforeach;
return $data;
}
}

View File

@@ -342,6 +342,36 @@ class SP_Post_types {
)
)
);
register_post_type( 'sp_spec',
apply_filters( 'sportspress_register_post_type_spec',
array(
'labels' => array(
'name' => __( 'Event Specs', 'sportspress' ),
'singular_name' => __( 'Event Spec', 'sportspress' ),
'add_new_item' => __( 'Add New Event Spec', 'sportspress' ),
'edit_item' => __( 'Edit Event Spec', 'sportspress' ),
'new_item' => __( 'New', 'sportspress' ),
'view_item' => __( 'View', 'sportspress' ),
'search_items' => __( 'Search', 'sportspress' ),
'not_found' => __( 'No results found.', 'sportspress' ),
'not_found_in_trash' => __( 'No results found.', 'sportspress' ),
),
'public' => false,
'show_ui' => true,
'capability_type' => 'sp_config',
'map_meta_cap' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'supports' => array( 'title', 'page-attributes', 'excerpt' ),
'has_archive' => false,
'show_in_nav_menus' => false,
'can_export' => false,
'show_in_menu' => false,
)
)
);
register_post_type( 'sp_performance',
apply_filters( 'sportspress_register_post_type_performance',

View File

@@ -78,7 +78,7 @@ if ( ! function_exists( 'sp_importable_post_types' ) ) {
*/
if ( ! function_exists( 'sp_config_types' ) ) {
function sp_config_types() {
return apply_filters( 'sportspress_config_types', array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_performance', 'sp_metric', 'sp_statistic' ) );
return apply_filters( 'sportspress_config_types', array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_performance', 'sp_metric', 'sp_spec', 'sp_statistic' ) );
}
}

View File

@@ -253,7 +253,7 @@ function sportspress_post_updated_messages( $messages ) {
global $typenow, $post;
if ( in_array( $typenow, array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_metric', 'sp_performance' ) ) ):
if ( in_array( $typenow, array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_metric', 'sp_spec', 'sp_performance' ) ) ):
$obj = get_post_type_object( $typenow );
for ( $i = 0; $i <= 10; $i++ ):

View File

@@ -17,6 +17,11 @@ $scrollable = get_option( 'sportspress_enable_scrollable_tables', 'yes' ) == 'ye
$data = array();
$event = new SP_Event( $id );
$specs_before = $event->specs( true );
$specs_after = $event->specs( false );
if ( 'yes' === get_option( 'sportspress_event_show_date', 'yes' ) ) {
$date = get_the_time( get_option('date_format'), $id );
$data[ __( 'Date', 'sportspress' ) ] = $date;
@@ -55,6 +60,8 @@ if ( 'yes' === get_option( 'sportspress_event_show_full_time', 'yes' ) ) {
$data = apply_filters( 'sportspress_event_details', $data, $id );
$data = array_merge( $specs_before, $data, $specs_after );
if ( ! sizeof( $data ) ) return;
?>
<div class="sp-template sp-template-event-details">