Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bulk edit of coauthors #747

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ function __construct() {
// Removes the co-author dropdown from the post quick edit
add_action( 'admin_head', array( $this, 'remove_quick_edit_authors_box' ) );

// Add co-author select field to Bulk Edit actions form
add_action( 'bulk_edit_custom_box', array( $this, '_action_bulk_edit_custom_box' ), 10, 2 );
add_action( 'wp_insert_post_data', array( $this, 'action_bulk_edit_update_coauthors' ), 10, 2 );

// Restricts WordPress from blowing away term order on bulk edit
add_filter( 'wp_get_object_terms', array( $this, 'filter_wp_get_object_terms' ), 10, 4 );

Expand Down Expand Up @@ -590,6 +594,53 @@ function _action_quick_edit_custom_box( $column_name, $post_type ) {
<?php
}

/**
* Bulk Edit co-authors box.
*/
function _action_bulk_edit_custom_box( $column_name, $post_type ) {
if ( 'coauthors' != $column_name || ! $this->is_post_type_enabled( $post_type ) || ! $this->current_user_can_set_authors() ) {
return;
}
?>
<label class="bulk-edit-group bulk-edit-coauthors">
<span class="title"><?php esc_html_e( 'Authors', 'co-authors-plus' ) ?></span>
<div id="coauthors-edit" class="hide-if-no-js">
<p><?php echo wp_kses( __( 'Leave the field below blank to keep the authors unchanged. Any change here will overwrite all previously assigned authors.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
</label>
<?php
}

/**
* Set coauthors from bulk edit.
*/
function action_bulk_edit_update_coauthors( $post_data, $postarr ) {

if ( ( ! isset( $postarr['post'] ) ) || ( ! $this->is_post_type_enabled( $post_data['post_type'] ) ) ) {
return $post_data;
}

foreach( $postarr['post'] as $post_id ) {
$post = get_post( $post_id );

if ( $this->current_user_can_set_authors( $post ) && isset( $postarr['coauthors'] ) ) {
$coauthors = array_map( 'sanitize_title', (array) $postarr['coauthors'] );
$this->add_coauthors( $post_id, $coauthors );
} else {
// If the user can't set authors and a co-author isn't currently set, we need to explicitly set one
if ( ! $this->has_author_terms( $post_id ) ) {
$user = get_userdata( $post->post_author );
if ( $user ) {
$this->add_coauthors( $post_id, array( $user->user_nicename ) );
}
}
}
}

return $post_data;
}

/**
* When we update the terms at all, we should update the published post count for each user
*/
Expand Down
10 changes: 10 additions & 0 deletions css/co-authors-plus.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
margin-left: 5em;
}

#bulk-edit label.bulk-edit-group {
clear: both;
padding: 0 .5em;
margin-top: .5em;
}

#bulk-edit label.bulk-edit-group #coauthors-list {
margin-left: 5.6em;
}

#coauthors-list {
width: 100%;
padding: 0 5px;
Expand Down
24 changes: 24 additions & 0 deletions js/co-authors-plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ jQuery( document ).ready(function () {
}
else if ( 'edit-php' == adminpage ) {

// 'Inline edit'
var wpInlineEdit = inlineEditPost.edit;

inlineEditPost.edit = function( id ) {
Expand Down Expand Up @@ -429,6 +430,29 @@ jQuery( document ).ready(function () {

}
}

// 'Bulk edit'
var coauthors_initialized_on_bulk_edit = false;
var wpBulkEdit = inlineEditPost.setBulk;

inlineEditPost.setBulk = function() {

wpBulkEdit.apply( this, arguments );

// Initialize co-authors, but only on the first 'Bulk edit' interaction.
if ( ! coauthors_initialized_on_bulk_edit ) {
var bulk_right_column = jQuery( '#bulk-edit .inline-edit-col-right' );

// Move the Authors section in the right column of the Bulk section.
jQuery( '#bulk-edit .bulk-edit-coauthors' ).appendTo( bulk_right_column );
// Quick fix to give the last column its 'real' height because
// the float:left; for the Post Format dropdown does not help positioning the Authors section we just added
bulk_right_column.find( 'div.inline-edit-col' ).addClass( 'wp-clearfix' );

coauthors_initialize( [] );
coauthors_initialized_on_bulk_edit = true;
}
}
}

});
Expand Down