Add ability to quick and bulk edit player's teams and squad number
This commit is contained in:
@@ -96,6 +96,8 @@ class SP_Admin_Assets {
|
||||
|
||||
wp_register_script( 'sportspress-admin-widgets', SP()->plugin_url() . '/assets/js/admin/widgets.js', array( 'jquery' ), SP_VERSION, true );
|
||||
|
||||
wp_register_script( 'sportspress-admin-quickeditor', SP()->plugin_url() . '/assets/js/admin/quickeditor.js', array( 'jquery' ), SP_VERSION, true );
|
||||
|
||||
// SportsPress admin pages
|
||||
if ( in_array( $screen->id, sp_get_screen_ids() ) || strpos( $screen->id, 'sportspress-config' )) {
|
||||
wp_enqueue_script( 'jquery' );
|
||||
@@ -139,6 +141,11 @@ class SP_Admin_Assets {
|
||||
if ( in_array( $screen->id, array( 'sp_result', 'sp_performance', 'sp_column', 'sp_statistic' ) ) ) {
|
||||
wp_enqueue_script( 'sportspress-admin-equationbuilder' );
|
||||
}
|
||||
|
||||
// Quick edit
|
||||
if ( in_array( $screen->id, array( 'edit-sp_player' ) ) ) {
|
||||
wp_enqueue_script( 'sportspress-admin-quickeditor' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +29,22 @@ class SP_Admin_CPT_Player extends SP_Admin_CPT {
|
||||
// Post title fields
|
||||
add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 );
|
||||
|
||||
// Admin Columns
|
||||
// Admin columns
|
||||
add_filter( 'manage_edit-sp_player_columns', array( $this, 'edit_columns' ) );
|
||||
add_action( 'manage_sp_player_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_number' ), 10, 2 );
|
||||
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_player', array( $this, 'bulk_save' ) );
|
||||
|
||||
// Call SP_Admin_CPT constructor
|
||||
parent::__construct();
|
||||
@@ -88,6 +97,7 @@ class SP_Admin_CPT_Player extends SP_Admin_CPT {
|
||||
case 'sp_team':
|
||||
$teams = (array)get_post_meta( $post_id, 'sp_team', false );
|
||||
$teams = array_filter( $teams );
|
||||
$teams = array_unique( $teams );
|
||||
if ( empty( $teams ) ):
|
||||
echo '—';
|
||||
else:
|
||||
@@ -186,6 +196,174 @@ class SP_Admin_CPT_Player extends SP_Admin_CPT {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Quick edit squad number
|
||||
*
|
||||
* @param string $column_name
|
||||
* @param string $post_type
|
||||
*/
|
||||
public function quick_edit_number( $column_name, $post_type ) {
|
||||
if ( $this->type !== $post_type ) return;
|
||||
if ( 'sp_number' !== $column_name ) return;
|
||||
|
||||
static $print_nonce = true;
|
||||
if ( $print_nonce ) {
|
||||
$print_nonce = false;
|
||||
wp_nonce_field( plugin_basename( __FILE__ ), 'sp_player_edit_nonce' );
|
||||
}
|
||||
|
||||
$number = get_post_meta( get_the_ID(), 'sp_number', true );
|
||||
?>
|
||||
<fieldset class="inline-edit-col-right">
|
||||
<div class="inline-edit-col">
|
||||
<label>
|
||||
<span class="title"><?php _e( 'Squad Number', 'sportspress' ); ?></span>
|
||||
<span class="input-text-wrap"><input type="text" name="sp_number" class="inline-edit-menu-order-input" value="<?php echo $number; ?>"></span>
|
||||
</label>
|
||||
</div>
|
||||
</fieldset>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
$post_id = get_the_ID();
|
||||
|
||||
$current_teams = array_filter( get_post_meta( $post_id, 'sp_current_team', false ) );
|
||||
$past_teams = array_filter( get_post_meta( $post_id, 'sp_past_team', false ) );
|
||||
?>
|
||||
<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 checked( in_array( $team->ID, $current_teams ) ); ?>> <?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 checked( in_array( $team->ID, $past_teams ) ); ?>> <?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;
|
||||
|
||||
if ( isset( $_POST[ 'sp_number' ] ) ) {
|
||||
update_post_meta( $post_id, 'sp_number', $_POST[ 'sp_number' ] );
|
||||
}
|
||||
|
||||
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_player_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;
|
||||
|
||||
Reference in New Issue
Block a user