diff --git a/assets/js/admin/quickeditor.js b/assets/js/admin/quickeditor.js new file mode 100644 index 00000000..68d765aa --- /dev/null +++ b/assets/js/admin/quickeditor.js @@ -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); \ No newline at end of file diff --git a/includes/admin/class-sp-admin-assets.php b/includes/admin/class-sp-admin-assets.php index 570f58ef..b758fdfb 100755 --- a/includes/admin/class-sp-admin-assets.php +++ b/includes/admin/class-sp-admin-assets.php @@ -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' ); + } } } diff --git a/includes/admin/post-types/class-sp-admin-cpt-player.php b/includes/admin/post-types/class-sp-admin-cpt-player.php index 2072e410..60d1840f 100755 --- a/includes/admin/post-types/class-sp-admin-cpt-player.php +++ b/includes/admin/post-types/class-sp-admin-cpt-player.php @@ -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 ); + ?> +
+
+ +
+
+ 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 ) ); + ?> +
+
+ + + + + + +
+
+ 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; + ?> +
+
+ + + + + + +
+
+ '' ); + 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;