-
-
Save 2ndkauboy/fbe41513687f5f9f7df98f012f1be691 to your computer and use it in GitHub Desktop.
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/fbe41513687f5f9f7df98f012f1be691 | |
* 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( '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', true ); | |
$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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment