Skip to content

Instantly share code, notes, and snippets.

@gyrus
Last active August 27, 2015 19:24
Show Gist options
  • Save gyrus/a71be00739c1c28de223 to your computer and use it in GitHub Desktop.
Save gyrus/a71be00739c1c28de223 to your computer and use it in GitHub Desktop.
WordPress custom post type authors system - http://sltaylor.co.uk/blog/custom-post-types-authors-custom-roles/
<?php
add_action( 'cmb2_init', 'pilau_cmb2_custom_fields' );
/**
* Register custom fields
*/
function pilau_cmb2_custom_fields() {
// Loop through CPTs
foreach ( get_post_types( array( '_builtin' => false ), 'objects' ) as $cpt ) {
// Check for support for custom author
if ( post_type_supports( $cpt->name, 'pilau-author' ) ) {
$cpt_author_options = array();
// Gather users that can 'edit_posts' for this CPT
foreach ( pilau_get_users_by_capability( $cpt->cap->edit_posts ) as $cpt_author ) {
$cpt_author_options[ $cpt_author->ID ] = $cpt_author->display_name;
}
// Register box
$cmb = new_cmb2_box( array(
'id' => 'cpt_author_' . $cpt->name . '_box',
'title' => __( 'Author' ),
'object_types' => array( $cpt->name ),
'show_on_cb' => 'pilau_cmb2_show_on_custom',
'show_on_custom' => array(
'user_can' => 'edit_others_post_types',
),
'context' => 'side',
'priority' => 'low',
'show_names' => false,
));
$cmb->add_field( array(
'name' => __( 'Author' ),
'id' => '_pilau_author',
'type' => 'select',
'options' => $cpt_author_options,
'default' => get_current_user_id(),
'on_front' => false,
) );
}
}
}
/**
* Get users by capability
*/
function pilau_get_users_by_capability( $cap ) {
static $all_users = null;
$users_with_cap = array();
// Only get all users once per request!
if ( $all_users === null ) {
$all_users = get_users();
}
// Filter by capability
foreach( $all_users as $user ) {
if ( $user->has_cap( $cap ) ) {
$users_with_cap[] = $user;
}
}
return $users_with_cap;
}
/**
* Callback to handle custom show_on restrictions
*
* @return bool
*/
function pilau_cmb2_show_on_custom( $cmb ) {
$show = true;
$show_on_custom = $cmb->prop( 'show_on_custom' );
if ( ! empty( $show_on_custom ) ) {
// Loop through restrictions
foreach ( $show_on_custom as $show_on_type => $show_on_condition ) {
switch ( $show_on_type ) {
case 'user_can': {
// Special capabilities
switch ( $show_on_condition ) {
case 'publish_post_types':
case 'edit_others_post_types': {
$screen = get_current_screen();
$cap_for_posts = str_replace( '_post_types', '_posts', $show_on_condition );
$post_type_object = get_post_type_object( $screen->post_type );
$cap = $post_type_object->cap->$cap_for_posts;
break;
}
default: {
$cap = $show_on_condition;
break;
}
}
// Check user capability
$show = current_user_can( $cap );
break;
}
case 'mime_types': {
// Check for media MIME types
if ( ! empty( $show_on_condition ) && is_array( $show_on_condition ) && in_array( 'attachment', $cmb->prop( 'object_types' ) ) ) {
$show = in_array( get_post_mime_type(), $show_on_condition );
}
break;
}
case 'exclude_ids': {
// Check for excluding based on IDs
if ( ! empty( $show_on_condition ) && is_array( $show_on_condition ) ) {
$show = ! in_array( $cmb->object_id, $show_on_condition );
}
break;
}
case 'include_ids': {
// Check for including based on IDs
if ( ! empty( $show_on_condition ) && is_array( $show_on_condition ) ) {
$show = in_array( $cmb->object_id, $show_on_condition );
}
break;
}
}
// If a condition has failed, break out
if ( ! $show ) {
break;
}
}
}
return $show;
}
add_filter( 'cmb2_override__pilau_author_meta_save', 'pilau_cpt_author_meta_save_override', 0, 4 );
/**
* Override CPT author meta save in order to store as post author
*/
function pilau_cpt_author_meta_save_override( $override, $data_args, $args, $field ) {
// Checks to avoid infinite loops
// @link https://codex.wordpress.org/Function_Reference/wp_update_post#Caution_-_Infinite_loop
if ( ! wp_is_post_revision( $data_args['id'] ) ) {
// Remove filter to avoid loop
remove_filter( 'cmb2_override__pilau_author_meta_save', 'pilau_cpt_author_meta_save_override', 0 );
// Update post author
// Will return non-null value to short-circuit normal meta save
$override = wp_update_post( array(
'ID' => $data_args['id'],
'post_author' => $data_args['value'],
));
// Add filter back
add_filter( 'cmb2_override__pilau_author_meta_save', 'pilau_cpt_author_meta_save_override', 0 );
}
return $override;
}
add_filter( 'cmb2_override__pilau_author_meta_value', 'pilau_cpt_author_meta_value_override', 0, 4 );
/**
* Override CPT author meta value in order to fetch post author
*/
function pilau_cpt_author_meta_value_override( $data, $object_id, $data_args, $field ) {
return get_post_field( 'post_author', $object_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment