Only allow specific users to drag and drop posts on the calendar

In v0.7, we reintroduced the ability to drag and drop posts on the calendar. It can work in one of two ways: you can drag and drop for scheduling purposes and not affect the publication date, or you can change the publication date.

Users can drag and drop a post if they have permissions to modify the post. This closely how core works: contributors can modify their own unpublished posts, authors can modify their own unpublished and published posts, and editors can modify everyone’s content. You may not want to allow every user to drag and drop posts on the calendar though.

First, read the introduction to extending Edit Flow. Once you’ve done so, you can modify the code snippet below to work for your needs. Basically, what’s happening is we’re removing some code for users who don’t match our criteria.

[sourcecode language=”php”]<?php
/**
* Only allow specific users to drag and drop posts on the calendar
* The easiest way to do this is just removing the class that makes them sortable
*
* @see http://editflow.org/extend/only-allow-specific-users-to-drag-and-drop-posts-on-the-calendar/
*
* @param array $post_classes A list of the classes to be printed for each item
* @return array $post_classes The possibly modified post classes
*/
function efx_restrict_calendar_drag_and_drop( $post_classes ) {

// This takes a whitelist approach; only user logins appearing in
// the $permitted_users array will be permitted to drag and drop
$permitted_users = array(
// ‘daniel’, // Add whichever usernames you’d like as pieces of the array and uncomment
);
// Unset the ‘sortable’ class if the user isn’t in our whitelist
// The ‘sortable’ class is used by jQuery to determine whether a post can be moved
if ( !in_array( wp_get_current_user()->user_login, $permitted_users ) ) {
foreach( $post_classes as $key => $post_class ) {
if ( ‘sortable’ == $post_class )
unset( $post_classes[$key] );
}
}
return $post_classes;
}
add_action( ‘ef_calendar_table_td_li_classes’, ‘efx_restrict_calendar_drag_and_drop’ );[/sourcecode]

With a bit of PHP knowledge, you can easily modify the snippet to blacklist users instead of whitelisting them (e.g. if ( in_array( wp_get_current_user()->user_login, $blacklisted_users ) ), or handle access based on user capabilities (e.g. if ( !current_user_can( 'publish_posts' ) ).