Skip to content

Commit

Permalink
Make it easier to disable co-authors for specific post types by conso…
Browse files Browse the repository at this point in the history
…lidating the code

Co-Authors is always enabled for post types that support 'author'. Our coauthors_supported_post_types filter allows you to remove the co-authors meta box for specific post types and instead use the standard authors.

It's initialized on init late so we can make sure all post types are properly registered

Closes #63
  • Loading branch information
danielbachhuber committed Oct 12, 2012
1 parent 4e66781 commit 5fb2b9d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 93 deletions.
154 changes: 64 additions & 90 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class coauthors_plus {

var $_pages_whitelist = array( 'post.php', 'post-new.php' );

var $supported_post_types = array();

var $ajax_search_fields = array( 'display_name', 'first_name', 'last_name', 'user_login', 'ID', 'user_email' );

/**
Expand All @@ -59,6 +61,7 @@ function __construct() {

// Register our models
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'init', array( $this, 'action_init_late' ), 100 );

// Load admin_init function
add_action( 'admin_init', array( $this,'admin_init' ) );
Expand Down Expand Up @@ -89,7 +92,7 @@ function __construct() {
add_action( 'add_meta_boxes', array( $this, 'remove_authors_box' ) );

// Removes the author dropdown from the post quick edit
add_action( 'load-edit.php', array( $this, 'remove_quick_edit_authors_box' ) );
add_action( 'admin_head', array( $this, 'remove_quick_edit_authors_box' ) );

// 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 @@ -117,22 +120,6 @@ function action_init() {
// Allow Co-Authors Plus to be easily translated
load_plugin_textdomain( 'co-authors-plus', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

// Register new taxonomy so that we can store all of the relationships
$args = array(
'hierarchical' => false,
'update_count_callback' => array( $this, '_update_users_posts_count' ),
'label' => false,
'query_var' => false,
'rewrite' => false,
'sort' => true,
'show_ui' => false
);
$supported_post_types = array(
'post',
'page',
);
register_taxonomy( $this->coauthor_taxonomy, apply_filters( 'coauthors_supported_post_types', $supported_post_types ), $args );

// Load the Guest Authors functionality if needed
if ( $this->is_guest_authors_enabled() ) {
require_once( dirname( __FILE__ ) . '/php/class-coauthors-guest-authors.php' );
Expand All @@ -148,6 +135,30 @@ function action_init() {
}

}

/**
* Register the 'author' taxonomy and add post type support
*/
function action_init_late() {

// Register new taxonomy so that we can store all of the relationships
$args = array(
'hierarchical' => false,
'update_count_callback' => array( $this, '_update_users_posts_count' ),
'label' => false,
'query_var' => false,
'rewrite' => false,
'sort' => true,
'show_ui' => false
);
$post_types_with_authors = array_values( get_post_types() );
foreach( $post_types_with_authors as $key => $name ) {
if ( ! post_type_supports( $name, 'author' ) )
unset( $post_types_with_authors[$key] );
}
$this->supported_post_types = apply_filters( 'coauthors_supported_post_types', $post_types_with_authors );
register_taxonomy( $this->coauthor_taxonomy, $this->supported_post_types, $args );
}

/**
* Initialize the plugin for the admin
Expand All @@ -158,7 +169,7 @@ function admin_init() {
// Add the main JS script and CSS file
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Add necessary JS variables
add_action( 'admin_print_scripts', array( $this, 'js_vars' ) );
add_action( 'admin_head', array( $this, 'js_vars' ) );

// Hooks to add additional coauthors to author column to Edit page
add_filter( 'manage_posts_columns', array( $this, '_filter_manage_posts_columns' ) );
Expand All @@ -172,24 +183,6 @@ function admin_init() {

}

/**
* Checks to see if the post_type supports authors
*/
function authors_supported( $post_type ) {

if( ! function_exists( 'post_type_supports' ) && in_array( $post_type, array( 'post', 'page' ) ) )
return true;

// Hacky way to prevent issues on the Edit page
if( isset( $this->_edit_page_post_type_supported ) && $this->_edit_page_post_type_supported )
return true;

if( post_type_supports( $post_type, 'author' ) )
return true;

return false;
}

/**
* Check whether the guest authors functionality is enabled or not
* Guest authors can be disabled entirely with:
Expand Down Expand Up @@ -275,53 +268,39 @@ function get_coauthor_by( $key, $value ) {
}

/**
* Gets the current global post type if one is set
* Whether or not Co-Authors Plus is enabled for this post type
* Must be called after init
*
* @since 0.7
*
* @param string $post_type The name of the post type we're considering
* @return bool Whether or not it's enabled
*/
function get_current_post_type() {
global $post, $typenow, $current_screen;

// "Cache" it!
if( isset( $this->_current_post_type ) )
return $this->_current_post_type;

if( $post && $post->post_type )
$post_type = $post->post_type;
elseif( $typenow )
$post_type = $typenow;
elseif( $current_screen && isset( $current_screen->post_type ) )
$post_type = $current_screen->post_type;
elseif( isset( $_REQUEST['post_type'] ) )
$post_type = sanitize_key( $_REQUEST['post_type'] );
else
$post_type = '';

if( $post_type )
$this->_current_post_type = $post_type;

return $post_type;
function is_post_type_enabled( $post_type = null ) {

if ( ! $post_type )
$post_type = get_post_type();

return (bool) in_array( $post_type, $this->supported_post_types );
}

/**
* Removes the standard WordPress Author box.
* We don't need it because the Co-Authors one is way cooler.
*/
function remove_authors_box() {

$post_type = $this->get_current_post_type();

if( $this->authors_supported( $post_type ) )
remove_meta_box( $this->coreauthors_meta_box_name, $post_type, 'normal' );

if ( $this->is_post_type_enabled() )
remove_meta_box( $this->coreauthors_meta_box_name, get_post_type(), 'normal' );
}

/**
* Adds a custom Authors box
*/
function add_coauthors_box() {

$post_type = $this->get_current_post_type();

if( $this->authors_supported( $post_type ) && $this->current_user_can_set_authors() )
add_meta_box($this->coauthors_meta_box_name, __('Post Authors', 'co-authors-plus'), array( $this, 'coauthors_meta_box' ), $post_type, apply_filters( 'coauthors_meta_box_context', 'normal'), apply_filters( 'coauthors_meta_box_priority', 'high'));

if( $this->is_post_type_enabled() && $this->current_user_can_set_authors() )
add_meta_box( $this->coauthors_meta_box_name, __('Post Authors', 'co-authors-plus'), array( $this, 'coauthors_meta_box' ), get_post_type(), apply_filters( 'coauthors_meta_box_context', 'normal'), apply_filters( 'coauthors_meta_box_priority', 'high'));
}

/**
Expand Down Expand Up @@ -396,21 +375,20 @@ function coauthors_meta_box( $post ) {
* It's a bit hacky, but the only way I can figure out :(
*/
function remove_quick_edit_authors_box() {
$post_type = $this->get_current_post_type();
if( post_type_supports( $post_type, 'author' )) {
$this->_edit_page_post_type_supported = true;
remove_post_type_support( $post_type, 'author' );
}
global $pagenow;

if ( 'edit.php' == $pagenow && $this->is_post_type_enabled() )
remove_post_type_support( get_post_type(), 'author' );
}

/**
* Add coauthors to author column on edit pages
* @param array $post_columns
*/
function _filter_manage_posts_columns($posts_columns) {
$new_columns = array();
function _filter_manage_posts_columns( $posts_columns ) {

if ( ! $this->_edit_page_post_type_supported )
$new_columns = array();
if ( ! $this->is_post_type_enabled() )
return $posts_columns;

foreach ($posts_columns as $key => $value) {
Expand Down Expand Up @@ -819,7 +797,7 @@ function current_user_can_set_authors( ) {

// TODO: Enable Authors to set Co-Authors

$post_type = $this->get_current_post_type();
$post_type = get_post_type();
// TODO: need to fix this; shouldn't just say no if don't have post_type
if( ! $post_type ) return false;

Expand Down Expand Up @@ -986,10 +964,8 @@ function filter_terms_clauses( $pieces ) {
*/
function enqueue_scripts($hook_suffix) {
global $pagenow, $post;

$post_type = $this->get_current_post_type();

if ( !$this->is_valid_page() || !$this->authors_supported( $post_type ) || !$this->current_user_can_set_authors() )
if ( !$this->is_valid_page() || ! $this->is_post_type_enabled() || !$this->current_user_can_set_authors() )
return;

wp_enqueue_script( 'jquery' );
Expand All @@ -1013,23 +989,21 @@ function enqueue_scripts($hook_suffix) {
* Adds necessary javascript variables to admin pages
*/
function js_vars() {

$post_type = $this->get_current_post_type();

if( $this->is_valid_page() && $this->authors_supported( $post_type ) && $this->current_user_can_set_authors() ) {
?>

if ( ! $this->is_valid_page() || ! $this->is_post_type_enabled() || ! $this-> current_user_can_set_authors() )
return;
?>
<script type="text/javascript">
// AJAX link used for the autosuggest
var coAuthorsPlus_ajax_suggest_link = '<?php echo add_query_arg(
array(
'action' => 'coauthors_ajax_suggest',
'post_type' => $post_type,
'post_type' => get_post_type(),
),
wp_nonce_url( 'admin-ajax.php', 'coauthors-search' )
); ?>';
</script>
<?php
}
<?php
} // END: js_vars()

/**
Expand All @@ -1038,7 +1012,7 @@ function js_vars() {
function is_valid_page() {
global $pagenow;

return in_array( $pagenow, $this->_pages_whitelist );
return (bool)in_array( $pagenow, $this->_pages_whitelist );
}

function get_post_id() {
Expand Down
4 changes: 1 addition & 3 deletions php/class-coauthors-guest-authors.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ function action_admin_enqueue_scripts() {
function action_add_meta_boxes() {
global $coauthors_plus;

$post_type = $coauthors_plus->get_current_post_type();

if ( $post_type == $this->post_type ) {
if ( get_post_type() == $this->post_type ) {
// Remove the submitpost metabox because we have our own
remove_meta_box( 'submitdiv', $post_type, 'side' );
remove_meta_box( 'slugdiv', $post_type, 'normal' );
Expand Down

0 comments on commit 5fb2b9d

Please sign in to comment.