Refactor officials as custom post type

This commit is contained in:
Brian Miyaji
2017-11-01 16:44:28 +11:00
parent 80cdb3c228
commit 70dd825ba7
14 changed files with 1020 additions and 120 deletions

View File

@@ -26,7 +26,6 @@ class SP_Admin_Menus {
add_action( 'admin_menu', array( $this, 'config_menu' ), 7 );
add_action( 'admin_menu', array( $this, 'leagues_menu' ), 20 );
add_action( 'admin_menu', array( $this, 'seasons_menu' ), 21 );
add_action( 'admin_menu', array( $this, 'officials_menu' ), 22 );
add_action( 'admin_head', array( $this, 'menu_highlight' ) );
add_action( 'admin_head', array( $this, 'menu_rename' ) );
@@ -69,13 +68,6 @@ class SP_Admin_Menus {
add_submenu_page( 'sportspress', __( 'Seasons', 'sportspress' ), __( 'Seasons', 'sportspress' ), 'manage_sportspress', 'edit-tags.php?taxonomy=sp_season');
}
/**
* Add menu item
*/
public function officials_menu() {
add_submenu_page( 'sportspress', __( 'Officials', 'sportspress' ), __( 'Officials', 'sportspress' ), 'manage_sportspress', 'edit-tags.php?taxonomy=sp_officials');
}
/**
* Highlights the correct top level admin menu item for post type add screens.
*
@@ -228,11 +220,6 @@ class SP_Admin_Menus {
$submenu['edit.php?post_type=sp_staff'] = array_filter( $submenu['edit.php?post_type=sp_staff'], array( $this, 'remove_seasons' ) );
endif;
if ( isset( $submenu['edit.php?post_type=sp_official'] ) ):
$submenu['edit.php?post_type=sp_official'] = array_filter( $submenu['edit.php?post_type=sp_official'], array( $this, 'remove_leagues' ) );
$submenu['edit.php?post_type=sp_official'] = array_filter( $submenu['edit.php?post_type=sp_official'], array( $this, 'remove_seasons' ) );
endif;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);

View File

@@ -0,0 +1,299 @@
<?php
/**
* Admin functions for the officials post type
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Post_Types
* @version 2.5
*/
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_Official' ) ) :
/**
* SP_Admin_CPT_Official Class
*/
class SP_Admin_CPT_Official extends SP_Admin_CPT {
/**
* Constructor
*/
public function __construct() {
$this->type = 'sp_official';
// Post title fields
add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 );
// Admin columns
add_filter( 'manage_edit-sp_official_columns', array( $this, 'edit_columns' ) );
add_action( 'manage_sp_official_posts_custom_column', array( $this, 'custom_columns' ), 2, 2 );
// Filtering
add_action( 'restrict_manage_posts', array( $this, 'filters' ) );
add_filter( 'parse_query', array( $this, 'filters_query' ) );
// Quick edit
add_action( 'quick_edit_custom_box', array( $this, 'quick_edit_teams' ), 10, 2 );
add_action( 'save_post', array( $this, 'quick_save' ) );
// Bulk edit
add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit_teams' ), 10, 2 );
add_action( 'wp_ajax_save_bulk_edit_sp_official', array( $this, 'bulk_save' ) );
// Call SP_Admin_CPT constructor
parent::__construct();
}
/**
* Change title boxes in admin.
* @param string $text
* @param object $post
* @return string
*/
public function enter_title_here( $text, $post ) {
if ( $post->post_type == 'sp_official' )
return __( 'Name', 'sportspress' );
return $text;
}
/**
* Change the columns shown in admin.
*/
public function edit_columns( $existing_columns ) {
unset( $existing_columns['author'], $existing_columns['date'] );
$columns = array_merge( array(
'cb' => '<input type="checkbox" />',
'title' => null,
'sp_duty' => __( 'Duties', 'sportspress' ),
'sp_team' => __( 'Teams', 'sportspress' ),
), $existing_columns, array(
'title' => __( 'Name', 'sportspress' )
) );
return apply_filters( 'sportspress_official_admin_columns', $columns );
}
/**
* Define our custom columns shown in admin.
* @param string $column
*/
public function custom_columns( $column, $post_id ) {
switch ( $column ):
case 'sp_duty':
echo get_the_terms( $post_id, 'sp_duty' ) ? the_terms( $post_id, 'sp_duty' ) : '&mdash;';
break;
case 'sp_team':
$current_teams = get_post_meta( $post_id, 'sp_current_team', false );
$past_teams = get_post_meta( $post_id, 'sp_past_team', false );
$current_teams = array_filter( $current_teams );
$past_teams = array_filter( $past_teams );
echo '<span class="hidden sp-official-teams" data-current-teams="' . implode( ',', $current_teams ) . '" data-past-teams="' . implode( ',', $past_teams ) . '"></span>';
$teams = (array)get_post_meta( $post_id, 'sp_team', false );
$teams = array_filter( $teams );
$teams = array_unique( $teams );
if ( empty( $teams ) ):
echo '&mdash;';
else:
foreach( $teams as $team_id ):
if ( ! $team_id ) continue;
$team = get_post( $team_id );
if ( $team ):
echo $team->post_title;
if ( in_array( $team_id, $current_teams ) ):
echo '<span class="dashicons dashicons-yes" title="' . __( 'Current Team', 'sportspress' ) . '"></span>';
endif;
echo '<br>';
endif;
endforeach;
endif;
break;
endswitch;
}
/**
* Show a category filter box
*/
public function filters() {
global $typenow, $wp_query;
if ( $typenow != 'sp_official' )
return;
if ( taxonomy_exists( 'sp_duty' ) ):
$selected = isset( $_REQUEST['sp_duty'] ) ? $_REQUEST['sp_duty'] : null;
$args = array(
'show_option_all' => __( 'Show all duties', 'sportspress' ),
'taxonomy' => 'sp_duty',
'name' => 'sp_duty',
'selected' => $selected
);
sp_dropdown_taxonomies( $args );
endif;
$selected = isset( $_REQUEST['team'] ) ? $_REQUEST['team'] : null;
$args = array(
'post_type' => 'sp_team',
'name' => 'team',
'show_option_none' => __( 'Show all teams', 'sportspress' ),
'selected' => $selected,
'values' => 'ID',
);
wp_dropdown_pages( $args );
}
/**
* Filter in admin based on options
*
* @param mixed $query
*/
public function filters_query( $query ) {
global $typenow, $wp_query;
if ( $typenow == 'sp_official' ) {
if ( ! empty( $_GET['team'] ) ) {
$query->query_vars['meta_value'] = $_GET['team'];
$query->query_vars['meta_key'] = 'sp_team';
}
}
}
/**
* Quick edit teams
*
* @param string $column_name
* @param string $post_type
*/
public function quick_edit_teams( $column_name, $post_type ) {
if ( $this->type !== $post_type ) return;
if ( 'sp_team' !== $column_name ) return;
$teams = get_posts( array(
'post_type' => 'sp_team',
'numberposts' => -1,
'post_status' => 'publish',
) );
if ( ! $teams ) return;
?>
<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<span class="title inline-edit-categories-label"><?php _e( 'Current Teams', 'sportspress' ); ?></span>
<input type="hidden" name="sp_current_team[]" value="0">
<ul class="cat-checklist">
<?php foreach ( $teams as $team ) { ?>
<li><label class="selectit"><input value="<?php echo $team->ID; ?>" type="checkbox" name="sp_current_team[]"> <?php echo $team->post_title; ?></label></li>
<?php } ?>
</ul>
<span class="title inline-edit-categories-label"><?php _e( 'Past Teams', 'sportspress' ); ?></span>
<input type="hidden" name="sp_past_team[]" value="0">
<ul class="cat-checklist">
<?php foreach ( $teams as $team ) { ?>
<li><label class="selectit"><input value="<?php echo $team->ID; ?>" type="checkbox" name="sp_past_team[]"> <?php echo $team->post_title; ?></label></li>
<?php } ?>
</ul>
</div>
</fieldset>
<?php
}
/**
* Save quick edit boxes
*
* @param int $post_id
*/
public function quick_save( $post_id ) {
if ( empty( $_POST ) ) return $post_id;
if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;;
$_POST += array( "{$this->type}_edit_nonce" => '' );
if ( ! wp_verify_nonce( $_POST["{$this->type}_edit_nonce"], plugin_basename( __FILE__ ) ) ) return $post_id;;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
if ( isset( $post->post_type ) && $post->post_type == 'revision' ) return $post_id;
sp_update_post_meta_recursive( $post_id, 'sp_current_team', sp_array_value( $_POST, 'sp_current_team', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_past_team', sp_array_value( $_POST, 'sp_past_team', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_team', array_merge( array( sp_array_value( $_POST, 'sp_current_team', array() ) ), sp_array_value( $_POST, 'sp_past_team', array() ) ) );
}
/**
* Bulk edit teams
*
* @param string $column_name
* @param string $post_type
*/
public function bulk_edit_teams( $column_name, $post_type ) {
if ( $this->type !== $post_type ) return;
if ( 'sp_team' !== $column_name ) return;
static $print_nonce = true;
if ( $print_nonce ) {
$print_nonce = false;
wp_nonce_field( plugin_basename( __FILE__ ), 'sp_official_edit_nonce' );
}
$teams = get_posts( array(
'post_type' => 'sp_team',
'numberposts' => -1,
'post_status' => 'publish',
) );
if ( ! $teams ) return;
?>
<fieldset class="inline-edit-col-right">
<div class="inline-edit-col">
<span class="title inline-edit-categories-label"><?php _e( 'Current Teams', 'sportspress' ); ?></span>
<input type="hidden" name="sp_current_team[]" value="0">
<ul class="cat-checklist">
<?php foreach ( $teams as $team ) { ?>
<li><label class="selectit"><input value="<?php echo $team->ID; ?>" type="checkbox" name="sp_current_team[]"> <?php echo $team->post_title; ?></label></li>
<?php } ?>
</ul>
<span class="title inline-edit-categories-label"><?php _e( 'Past Teams', 'sportspress' ); ?></span>
<input type="hidden" name="sp_past_team[]" value="0">
<ul class="cat-checklist">
<?php foreach ( $teams as $team ) { ?>
<li><label class="selectit"><input value="<?php echo $team->ID; ?>" type="checkbox" name="sp_past_team[]"> <?php echo $team->post_title; ?></label></li>
<?php } ?>
</ul>
</div>
</fieldset>
<?php
}
/**
* Save bulk edit boxes
*/
public function bulk_save() {
$_POST += array( "nonce" => '' );
if ( ! wp_verify_nonce( $_POST["nonce"], plugin_basename( __FILE__ ) ) ) return;
$post_ids = ( ! empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : array();
$current_teams = sp_array_value( $_POST, 'current_teams', array() );
$past_teams = sp_array_value( $_POST, 'past_teams', array() );
$teams = array_merge( $current_teams, $past_teams );
if ( ! empty( $post_ids ) && is_array( $post_ids ) ) {
foreach ( $post_ids as $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) continue;
sp_add_post_meta_recursive( $post_id, 'sp_current_team', $current_teams );
sp_add_post_meta_recursive( $post_id, 'sp_past_team', $past_teams );
sp_add_post_meta_recursive( $post_id, 'sp_team', $teams );
}
}
die();
}
}
endif;
return new SP_Admin_CPT_Official();

View File

@@ -2,10 +2,11 @@
/**
* Event Officials
*
* @author Rob Tucker <rtucker-scs>
* @category Admin
* @author Rob Tucker <rtucker-scs>
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 2.3
* @version 2.5
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@@ -19,43 +20,37 @@ class SP_Meta_Box_Event_Officials {
* Output the metabox
*/
public static function output( $post ) {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
$abbreviation = get_post_meta( $post->ID, 'sp_abbreviation', true );
$redirect = get_post_meta( $post->ID, 'sp_redirect', true );
$url = get_post_meta( $post->ID, 'sp_url', true );
if ( taxonomy_exists( 'sp_officials' ) ):
$officials = get_the_terms( $post->ID, 'sp_officials' );
$official_ids = array();
if ( $officials ):
foreach ( $officials as $official ):
$official_ids[] = $official->term_id;
endforeach;
endif;
endif;
?>
<?php if ( taxonomy_exists( 'sp_officials' ) ) { ?>
<p><strong><?php _e( 'Officials', 'sportspress' ); ?></strong></p>
<p><?php
$args = array(
'taxonomy' => 'sp_officials',
'name' => 'tax_input[sp_officials][]',
'selected' => $official_ids,
'values' => 'term_id',
'placeholder' => sprintf( __( 'Select %s', 'sportspress' ), __( 'Officials', 'sportspress' ) ),
'class' => 'widefat',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_taxonomies( $args );
?></p>
<?php } ?>
<?php
$duties = get_terms( array(
'taxonomy' => 'sp_duty',
'hide_empty' => false,
) );
$officials = (array) get_post_meta( $post->ID, 'sp_officials', true );
foreach ( $duties as $duty ) {
?>
<p><strong><?php echo $duty->name; ?></strong></p>
<p><?php
$args = array(
'post_type' => 'sp_official',
'name' => 'sp_officials[' . $duty->term_id . '][]',
'selected' => sp_array_value( $officials, $duty->term_id, array() ),
'values' => 'ID',
'placeholder' => sprintf( __( 'Select %s', 'sportspress' ), __( 'Officials', 'sportspress' ) ),
'class' => 'widefat',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_pages( $args );
?></p>
<?php
}
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_officials', sp_array_value( $_POST, 'sp_officials', 'official' ) );
update_post_meta( $post_id, 'sp_officials', sp_array_value( $_POST, 'sp_officials', array() ) );
}
}

View File

@@ -0,0 +1,120 @@
<?php
/**
* Official Details
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta_Boxes
* @version 2.5
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* SP_Meta_Box_Official_Details
*/
class SP_Meta_Box_Official_Details {
/**
* Output the metabox
*/
public static function output( $post ) {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
$continents = SP()->countries->continents;
$nationalities = get_post_meta( $post->ID, 'sp_nationality', false );
foreach ( $nationalities as $index => $nationality ):
if ( 2 == strlen( $nationality ) ):
$legacy = SP()->countries->legacy;
$nationality = strtolower( $nationality );
$nationality = sp_array_value( $legacy, $nationality, null );
$nationalities[ $index ] = $nationality;
endif;
endforeach;
if ( taxonomy_exists( 'sp_duty' ) ):
$positions = get_the_terms( $post->ID, 'sp_duty' );
$position_ids = array();
if ( $positions ):
foreach ( $positions as $position ):
$position_ids[] = $position->term_id;
endforeach;
endif;
endif;
$teams = get_posts( array( 'post_type' => 'sp_team', 'posts_per_page' => -1 ) );
$past_teams = array_filter( get_post_meta( $post->ID, 'sp_past_team', false ) );
$current_teams = array_filter( get_post_meta( $post->ID, 'sp_current_team', false ) );
?>
<p><strong><?php _e( 'Nationality', 'sportspress' ); ?></strong></p>
<p><select id="sp_nationality" name="sp_nationality[]" data-placeholder="<?php printf( __( 'Select %s', 'sportspress' ), __( 'Nationality', 'sportspress' ) ); ?>" class="widefat chosen-select<?php if ( is_rtl() ): ?> chosen-rtl<?php endif; ?>" multiple="multiple">
<option value=""></option>
<?php foreach ( $continents as $continent => $countries ): ?>
<optgroup label="<?php echo $continent; ?>">
<?php foreach ( $countries as $code => $country ): ?>
<option value="<?php echo $code; ?>" <?php selected ( in_array( $code, $nationalities ) ); ?>><?php echo $country; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select></p>
<?php if ( taxonomy_exists( 'sp_duty' ) ) { ?>
<p><strong><?php _e( 'Duties', 'sportspress' ); ?></strong></p>
<p><?php
$args = array(
'taxonomy' => 'sp_duty',
'name' => 'tax_input[sp_duty][]',
'selected' => $position_ids,
'values' => 'term_id',
'placeholder' => sprintf( __( 'Select %s', 'sportspress' ), __( 'Duties', 'sportspress' ) ),
'class' => 'widefat',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_taxonomies( $args );
?></p>
<?php } ?>
<p><strong><?php _e( 'Current Teams', 'sportspress' ); ?></strong></p>
<p><?php
$args = array(
'post_type' => 'sp_team',
'name' => 'sp_current_team[]',
'selected' => $current_teams,
'values' => 'ID',
'placeholder' => sprintf( __( 'Select %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ),
'class' => 'sp-current-teams widefat',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_pages( $args );
?></p>
<p><strong><?php _e( 'Past Teams', 'sportspress' ); ?></strong></p>
<p><?php
$args = array(
'post_type' => 'sp_team',
'name' => 'sp_past_team[]',
'selected' => $past_teams,
'values' => 'ID',
'placeholder' => sprintf( __( 'Select %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ),
'class' => 'sp-past-teams widefat',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_pages( $args );
?></p>
<?php
}
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_update_post_meta_recursive( $post_id, 'sp_nationality', sp_array_value( $_POST, 'sp_nationality', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_current_team', sp_array_value( $_POST, 'sp_current_team', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_past_team', sp_array_value( $_POST, 'sp_past_team', array() ) );
sp_update_post_meta_recursive( $post_id, 'sp_team', array_merge( array( sp_array_value( $_POST, 'sp_current_team', array() ) ), sp_array_value( $_POST, 'sp_past_team', array() ) ) );
}
}

View File

@@ -255,7 +255,7 @@ class SP_Settings_Modules extends SP_Settings_Page {
</td></tr>
<?php } else { ?>
<tr><td>
<input type="checkbox" name="sportspress_load_<?php echo $id; ?>_module" id="sportspress_load_<?php echo $id; ?>_module" <?php checked( 'yes' == get_option( 'sportspress_load_' . $id . '_module', 'yes' ) ); ?>>
<input type="checkbox" name="sportspress_load_<?php echo $id; ?>_module" id="sportspress_load_<?php echo $id; ?>_module" <?php checked( 'yes' == get_option( 'sportspress_load_' . $id . '_module', sp_array_value( $module, 'default', 'yes' ) ) ); ?>>
<label for="sportspress_load_<?php echo $id; ?>_module">
<i class="<?php echo sp_array_value( $module, 'icon', 'dashicons dashicons-admin-generic' ); ?>"></i>
<?php echo sp_array_value( $module, 'label', $id ); ?>