Add ability to quick and bulk edit player's teams and squad number

This commit is contained in:
Brian Miyaji
2016-12-08 19:15:48 +11:00
parent f4d525b943
commit 43810ef0a7
3 changed files with 257 additions and 1 deletions

View File

@@ -0,0 +1,71 @@
(function($) {
// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;
// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {
// "call" the original WP edit function
// we don't want to leave WordPress hanging
$wp_inline_edit.apply( this, arguments );
// now we take care of our business
// get the post ID
var $post_id = 0;
if ( typeof( id ) == 'object' ) {
$post_id = parseInt( this.getId( id ) );
}
if ( $post_id > 0 ) {
// define the edit row
var $edit_row = $( '#edit-' + $post_id );
var $post_row = $( '#post-' + $post_id );
// get the data
var $number = $( '.column-sp_number', $post_row ).text();
// populate the data
$( ':input[name="sp_number"]', $edit_row ).val( $number );
}
};
$( document ).on( 'click', '#bulk_edit', function() {
// define the bulk edit row
var $bulk_row = $( '#bulk-edit' );
// get the selected post ids that are being edited
var $post_ids = new Array();
$bulk_row.find( '#bulk-titles' ).children().each( function() {
$post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) );
});
// get the data
var $current_teams = [];
$bulk_row.find( 'input[name="sp_current_team[]"]:checked' ).each(function() {
$current_teams.push( $(this).val() );
});
var $past_teams = [];
$bulk_row.find( 'input[name="sp_past_team[]"]:checked' ).each(function() {
$past_teams.push( $(this).val() );
});
// save the data
$.ajax({
url: ajaxurl, // this is a variable that WordPress has already defined for us
type: 'POST',
async: false,
cache: false,
data: {
action: 'save_bulk_edit_sp_player',
post_ids: $post_ids,
current_teams: $current_teams,
past_teams: $past_teams,
nonce: $("#sp_player_edit_nonce").val()
}
});
});
})(jQuery);

View File

@@ -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' );
}
}
}

View File

@@ -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;