-
-
Save 2ndkauboy/b18f98c31254aefedcd4cd1b05921acd to your computer and use it in GitHub Desktop.
Custom Post Type for portraits including a custom user role with the permission to upload files but only showing the user's own files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Portrait Custom Post Type | |
* Description: This plugin adds a Custom Post Type "Portrait" and a custom author role | |
* Version: 1.0.0 | |
* Author: Bernhard Kau | |
* Author URI: http://kau-boys.de | |
* Plugin URI: https://gist.github.com/2ndkauboy/b18f98c31254aefedcd4cd1b05921acd | |
* Text Domain: cpt-portrait | |
* Domain Path: /languages | |
* License: GPLv3 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0 | |
*/ | |
/** | |
* Add a CPT portraits | |
*/ | |
function cpt_portrait_init() { | |
register_post_type( 'portrait', array( | |
'labels' => array( | |
'name' => __( 'Portraits', 'cpt-portrait' ), | |
'singular_name' => __( 'Portrait', 'cpt-portrait' ), | |
'all_items' => __( 'All Portraits', 'cpt-portrait' ), | |
'new_item' => __( 'New Portrait', 'cpt-portrait' ), | |
'add_new' => __( 'Add New', 'cpt-portrait' ), | |
'add_new_item' => __( 'Add New Portrait', 'cpt-portrait' ), | |
'edit_item' => __( 'Edit Portrait', 'cpt-portrait' ), | |
'view_item' => __( 'View Portrait', 'cpt-portrait' ), | |
'search_items' => __( 'Search Portraits', 'cpt-portrait' ), | |
'not_found' => __( 'No Portraits found', 'cpt-portrait' ), | |
'not_found_in_trash' => __( 'No Portraits found in trash', 'cpt-portrait' ), | |
'parent_item_colon' => __( 'Parent Portrait', 'cpt-portrait' ), | |
'menu_name' => __( 'Portraits', 'cpt-portrait' ), | |
), | |
'public' => true, | |
'hierarchical' => false, | |
'show_ui' => true, | |
'show_in_nav_menus' => true, | |
'supports' => array( 'title', 'editor', 'author' ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'has_archive' => true, | |
'rewrite' => true, | |
'query_var' => true, | |
'menu_icon' => 'dashicons-businessman', | |
'show_in_rest' => true, | |
'rest_base' => 'portrait', | |
'rest_controller_class' => 'WP_REST_Posts_Controller', | |
'map_meta_cap' => true, | |
'capabilities' => array( | |
// Meta capabilities | |
'edit_post' => 'edit_portrait', | |
'read_post' => 'read_portrait', | |
'delete_post' => 'delete_portrait', | |
// Primitive capabilities used outside of map_meta_cap(): | |
'edit_posts' => 'edit_portraits', | |
'edit_others_posts' => 'edit_others_portraits', | |
'publish_posts' => 'publish_portraits', | |
'read_private_posts' => 'read_private_portraits', | |
// Primitive capabilities used within map_meta_cap(): | |
'read' => 'read', | |
'delete_posts' => 'delete_portraits', | |
'delete_private_posts' => 'delete_private_portraits', | |
'delete_published_posts' => 'delete_published_portraits', | |
'delete_others_posts' => 'delete_others_portraits', | |
'edit_private_posts' => 'edit_private_portraits', | |
'edit_published_posts' => 'edit_published_portraits', | |
'create_posts' => 'create_portraits', | |
), | |
) ); | |
} | |
add_action( 'init', 'cpt_portrait_init' ); | |
/** | |
* Sets the messages for the CPT portraits | |
* | |
* @param array $messages The messages array. | |
* | |
* @return array | |
*/ | |
function cpt_portrait_updated_messages( $messages ) { | |
global $post; | |
$permalink = get_permalink( $post ); | |
$messages['portrait'] = array( | |
0 => '', // Unused. Messages start at index 1. | |
1 => sprintf( __('Portrait updated. <a target="_blank" href="%s">View Portrait</a>', 'cpt-portrait'), esc_url( $permalink ) ), | |
2 => __('Custom field updated.', 'cpt-portrait'), | |
3 => __('Custom field deleted.', 'cpt-portrait'), | |
4 => __('Portrait updated.', 'cpt-portrait'), | |
/* translators: %s: date and time of the revision */ | |
5 => isset($_GET['revision']) ? sprintf( __('Portrait restored to revision from %s', 'cpt-portrait'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, | |
6 => sprintf( __('Portrait published. <a href="%s">View Portrait</a>', 'cpt-portrait'), esc_url( $permalink ) ), | |
7 => __('Portrait saved.', 'cpt-portrait'), | |
8 => sprintf( __('Portrait submitted. <a target="_blank" href="%s">Preview Portrait</a>', 'cpt-portrait'), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ), | |
9 => sprintf( __('Portrait scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Portrait</a>', 'cpt-portrait'), | |
// translators: Publish box date format, see http://php.net/date | |
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ), | |
10 => sprintf( __('Portrait draft updated. <a target="_blank" href="%s">Preview Portrait</a>', 'cpt-portrait'), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ), | |
); | |
return $messages; | |
} | |
add_filter( 'post_updated_messages', 'cpt_portrait_updated_messages' ); | |
/** | |
* Sets the capabilities for the CPT portraits | |
*/ | |
function cpt_portrait_set_capabilities() { | |
add_role( 'portraiteditor', __( 'Portrait Editor', 'cpt-portrait' ) ); | |
$portraiteditor = get_role( 'portraiteditor' ); | |
$portraiteditor->add_cap( 'read' ); | |
$portraiteditor->add_cap( 'upload_files' ); | |
$portraiteditor->add_cap( 'edit_portraits', true ); | |
$portraiteditor->add_cap( 'edit_others_portraits', false ); | |
$portraiteditor->add_cap( 'publish_portraits', false ); | |
$portraiteditor->add_cap( 'read_private_portraits', true ); | |
$portraiteditor->add_cap( 'delete_portraits', true ); | |
$portraiteditor->add_cap( 'delete_private_portraits', false ); | |
$portraiteditor->add_cap( 'delete_published_portraits', false ); | |
$portraiteditor->add_cap( 'delete_others_portraits', false ); | |
$portraiteditor->add_cap( 'edit_private_portraits', false ); | |
$portraiteditor->add_cap( 'edit_published_portraits', true ); | |
$portraiteditor->add_cap( 'create_portraits', false ); | |
$administrator = get_role( 'administrator' ); | |
$administrator->add_cap( 'edit_portraits', true ); | |
$administrator->add_cap( 'edit_others_portraits', true ); | |
$administrator->add_cap( 'publish_portraits', true ); | |
$administrator->add_cap( 'read_private_portraits', true ); | |
$administrator->add_cap( 'delete_portraits', true ); | |
$administrator->add_cap( 'delete_private_portraits', true ); | |
$administrator->add_cap( 'delete_published_portraits', true ); | |
$administrator->add_cap( 'delete_others_portraits', true ); | |
$administrator->add_cap( 'edit_private_portraits', true ); | |
$administrator->add_cap( 'edit_published_portraits', true ); | |
$administrator->add_cap( 'create_portraits', true ); | |
} | |
register_activation_hook( __FILE__, 'cpt_portrait_set_capabilities' ); | |
/** | |
* Modifies the user query to show administrators and subsribers in the authors drop down | |
* | |
* @param array $query_args The query arguments for wp_dropdown_users(). | |
* @param array $r The default arguments for wp_dropdown_users(). | |
* | |
* @return mixed | |
*/ | |
function cpt_portrait_wp_dropdown_users_args( $query_args, $r ) { | |
if ( 'portrait' === get_current_screen()->post_type ) { | |
$query_args['role__in'] = array( 'administrator', 'portraiteditor' ); | |
$query_args['who'] = ''; // This will reset the need of the user to be a post author | |
} | |
return $query_args; | |
} | |
add_filter( 'wp_dropdown_users_args', 'cpt_portrait_wp_dropdown_users_args', 10, 2 ); | |
function cpt_portrait_show_current_user_attachments( $query = array() ) { | |
$user_id = get_current_user_id(); | |
if ( $user_id && ! current_user_can( 'edit_others_portraits' ) ) { | |
$query['author'] = $user_id; | |
} | |
return $query; | |
} | |
add_filter( 'ajax_query_attachments_args', 'cpt_portrait_show_current_user_attachments' ); | |
function cpt_portrait_has_cap_delete_attachments( $allcaps, $cap, $args ) { | |
// Bail out if the capability is not "delete_post" or "edit_post" | |
if ( 'delete_post' != $args[0] && 'edit_post' != $args[0] ) { | |
return $allcaps; | |
} | |
// Bail out for users who can already edit others posts | |
if ( $allcaps[ 'edit_others_portraits' ] ) { | |
return $allcaps; | |
} | |
// Load the post data | |
$post = get_post( $args[2] ); | |
// Bail out if no post was found or it's not an attachment | |
if ( empty( $post ) || 'attachment' != $post->post_type ) { | |
return $allcaps; | |
} | |
// Bail out if the user is not the post author | |
if ( $args[1] != $post->post_author ) { | |
return $allcaps; | |
} | |
// If we haven't bailed out until this point, allow the user to delete the image | |
$allcaps[ $cap[0] ] = true; | |
return $allcaps; | |
} | |
add_filter( 'user_has_cap', 'cpt_portrait_has_cap_delete_attachments', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment