Skip to content

Instantly share code, notes, and snippets.

@angelorocha
Last active October 22, 2021 20:13
Show Gist options
  • Save angelorocha/96dd54b22a04d9418e60a171c82dca97 to your computer and use it in GitHub Desktop.
Save angelorocha/96dd54b22a04d9418e60a171c82dca97 to your computer and use it in GitHub Desktop.
WordPress class to generate post types
<?php
/**
* Class WPSSCpt
* Generate custom post types
*/
class WPSSCpt {
/**
* Class instance
*
* @var object|null $instance
*/
protected static ?object $instance = null;
/**
* Define custom post type unique key
*
* @var string $cpt_key
*/
public static string $cpt_key;
/**
* Define custom post type name.
*
* @var string $cpt_name
*/
public static string $cpt_name;
/**
* Define custom post type labels, keep empty to default labels
*
* @var array $cpt_labels
*/
public static array $cpt_labels = [];
/**
* Define custom post type capabilities, keep empty to custom capabilities
*
* @var string
*/
public static string $cpt_capabilities = '';
/**
* Set roles with access to post type, administrator always have permission
*
* @var array|string[]
*/
public static array $add_caps_to_role = [];
/**
* Define custom post type labels, keep empty to default args
*
* @var array $cpt_args
*/
public static array $cpt_args = [];
/**
* WPSSCpt constructor.
* Use the following attributes to configure your CPT:
*
* @var string $cpt_key Unique post type key. Eg.: gallery_cpt
* @var string $cpt_name The post type name. Eg.: Gallery
* @var array $cpt_labels The post type labels. Eg.: $my_class_instance::$cpt_labels = ['featured_image' => 'Gallery Cover']
* @var string $cpt_capabilities The post type capabilities type, use to clone capabilities from another post type.
* @var array $add_caps_to_role Define roles to manage CPT, by default, administrator always have access. Eg.: $my_class_instance::$add_caps_to_role = ['editor', 'my_role']
* @var array $cpt_args The post type args, to define custom args to your cpt, use: $my_class_instance::$cpt_args = ['menu_position' => 6];
*/
public function __construct() {
add_action('admin_init', [ $this, 'set_caps_to_role' ]);
add_action('enter_title_here', [ $this, 'custom_post_type_input_title_placeholder' ], 10, 1);
}
/**
* Init custom post type
*/
public static function make_cpt() {
$instance = self::instance();
register_post_type(self::$cpt_key, $instance->cpt_args());
}
/**
* Get class instance
*
* @return object|WPSSCpt|null
*/
public static function instance() {
if( is_null(self::$instance) ):
self::$instance = new self();
endif;
return self::$instance;
}
/**
* Define custom post type default args
*
* @return array
*/
private function cpt_args(): array {
$args = self::$cpt_args;
$args = [
'label' => self::$cpt_name,
'description' => (isset($args['description']) ? $args['description'] : ""),
'labels' => self::cpt_labels(),
'supports' => (isset($args['supports']) ? $args['supports'] : [ 'title', 'editor', 'thumbnail' ]),
'taxonomies' => (isset($args['taxonomies']) ? $args['taxonomies'] : []),
'hierarchical' => (isset($args['hierarchical']) ? $args['hierarchical'] : false),
'public' => (isset($args['public']) ? $args['public'] : true),
'show_ui' => (isset($args['show_ui']) ? $args['show_ui'] : true),
'show_in_menu' => (isset($args['show_in_menu']) ? $args['show_in_menu'] : true),
'menu_position' => (isset($args['menu_position']) ? $args['menu_position'] : 5),
'menu_icon' => (isset($args['menu_icon']) ? $args['menu_icon'] : 'dashicons-arrow-right'),
'show_in_admin_bar' => (isset($args['show_in_admin_bar']) ? $args['show_in_admin_bar'] : true),
'show_in_nav_menus' => (isset($args['show_in_nav_menus']) ? $args['show_in_nav_menus'] : true),
'can_export' => (isset($args['can_export']) ? $args['can_export'] : true),
'has_archive' => (isset($args['has_archive']) ? $args['has_archive'] : self::cpt_rewrite()['slug']),
'exclude_from_search' => (isset($args['exclude_from_search']) ? $args['exclude_from_search'] : false),
'publicly_queryable' => (isset($args['publicly_queryable']) ? $args['publicly_queryable'] : true),
'rewrite' => self::cpt_rewrite(),
'show_in_rest' => (isset($args['show_in_rest']) ? $args['show_in_rest'] : true),
'rest_base' => (isset($args['rest_base']) ? $args['rest_base'] : self::cpt_rewrite()['slug']),
];
if( self::$cpt_capabilities ):
$args['capability_type'] = self::$cpt_capabilities;
else:
$args['capabilities'] = self::cpt_caps();
endif;
return $args;
}
/**
* Define custom post type labels
*
* @return array
*/
private function cpt_labels(): array {
$labels = self::$cpt_labels;
$cpt_name = self::$cpt_name;
$singular_name = (isset($labels['singular_name']) ? $labels['singular_name'] : $cpt_name);
return [
'name' => $cpt_name,
'singular_name' => $cpt_name,
'menu_name' => $cpt_name,
'name_admin_bar' => $cpt_name,
'archives' => $cpt_name,
'attributes' => (isset($labels['attributes']) ? $labels['attributes'] : __("Item Attributes", "wpss")),
'parent_item_colon' => (isset($labels['parent_item_colon']) ? $labels['parent_item_colon'] : __("Parent Item:", "wpss")),
'all_items' => (isset($labels['all_items']) ? $labels['all_items'] : __("All Items", "wpss")),
'add_new_item' => (isset($labels['add_new_item']) ? $labels['add_new_item'] : __("Add New $singular_name", "wpss")),
'add_new' => (isset($labels['add_new']) ? $labels['add_new'] : __("Add New", "wpss")),
'new_item' => (isset($labels['new_item']) ? $labels['new_item'] : __("New $singular_name", "wpss")),
'edit_item' => (isset($labels['edit_item']) ? $labels['edit_item'] : __("Edit $singular_name Video", "wpss")),
'update_item' => (isset($labels['update_item']) ? $labels['update_item'] : __("Update $singular_name", "wpss")),
'view_item' => (isset($labels['view_item']) ? $labels['view_item'] : __("View $singular_name", "wpss")),
'view_items' => (isset($labels['view_items']) ? $labels['view_items'] : __("View $cpt_name", "wpss")),
'search_items' => (isset($labels['search_items']) ? $labels['search_items'] : __("Search $cpt_name", "wpss")),
'not_found' => (isset($labels['not_found']) ? $labels['not_found'] : __("Not found", "wpss")),
'not_found_in_trash' => (isset($labels['not_found_in_trash']) ? $labels['not_found_in_trash'] : __("Not found in trash", "wpss")),
'featured_image' => (isset($labels['featured_image']) ? $labels['featured_image'] : __("Featured image", "wpss")),
'set_featured_image' => (isset($labels['set_featured_image']) ? $labels['set_featured_image'] : __("Set featured image", "wpss")),
'remove_featured_image' => (isset($labels['remove_featured_image']) ? $labels['remove_featured_image'] : __("Remove featured image", "wpss")),
'use_featured_image' => (isset($labels['use_featured_image']) ? $labels['use_featured_image'] : __("Use as featured image", "wpss")),
'insert_into_item' => (isset($labels['insert_into_item']) ? $labels['insert_into_item'] : __("Insert into item", "wpss")),
'uploaded_to_this_item' => (isset($labels['uploaded_to_this_item']) ? $labels['uploaded_to_this_item'] : __("Upload to this item", "wpss")),
'items_list' => (isset($labels['items_list']) ? $labels['items_list'] : __("Items list", "wpss")),
'items_list_navigation' => (isset($labels['items_list_navigation']) ? $labels['items_list_navigation'] : __("Items list navigation", "wpss")),
'filter_items_list' => (isset($labels['filter_items_list']) ? $labels['filter_items_list'] : __("Filter items list", "wpss")),
];
}
/**
* Define custom post type capabilities
*
* @return string[]
*/
private function cpt_caps(): array {
$cpt_key = self::$cpt_key;
return [
'edit_post' => "edit_{$cpt_key}",
'read_post' => "read_{$cpt_key}",
'delete_post' => "delete_{$cpt_key}",
'edit_posts' => "edit_{$cpt_key}s",
'edit_others_posts' => "edit_others_{$cpt_key}s",
'publish_posts' => "publish_{$cpt_key}s",
'delete_posts' => "delete_{$cpt_key}s",
'delete_private_posts' => "delete_private_{$cpt_key}s",
'delete_published_posts' => "delete_published_{$cpt_key}s",
'delete_others_posts' => "delete_others_{$cpt_key}s",
'read_private_posts' => "read_private_{$cpt_key}s",
'edit_published_posts' => "edit_published_{$cpt_key}s",
'edit_private_posts' => "edit_private_{$cpt_key}s",
];
}
/**
* Set role permissions to custom post type
*/
public function set_caps_to_role(): void {
$roles = self::$add_caps_to_role;
$roles[] = 'administrator';
foreach( $roles as $role ):
foreach( self::cpt_caps() as $cap ):
get_role($role)->add_cap($cap);
endforeach;
endforeach;
}
/**
* Define a custom input placeholder to post type edit screen
*
* @param $input
*
* @return mixed|string
*/
public function custom_post_type_input_title_placeholder($input): string {
global $post_type;
$default_placeholder = sprintf(__("Input the title of %s", "wpss"), self::$cpt_name);
if( is_admin() && self::$cpt_key === $post_type ):
if( isset(self::$cpt_args['input_placeholder']) ):
return self::$cpt_args['input_placeholder'];
else:
return $default_placeholder;
endif;
endif;
return $input;
}
/**
* Define custom post type rewrite params
*
* @return array
*/
private function cpt_rewrite(): array {
$rewrite = sanitize_title(self::$cpt_name);
if( isset($this->cpt_args['rewrite']) ):
$rewrite = sanitize_title(self::$cpt_args['rewrite']);
endif;
return [
'slug' => $rewrite,
'with_front' => true,
'pages' => true,
'feeds' => true,
];
}
}
/** Post type sample */
add_action('init', 'new_cpt_gallery');
function new_cpt_gallery() {
$gallery = new WPSSCpt();
$gallery::$cpt_key = 'my_gallery';
$gallery::$cpt_name = 'Gallery';
$gallery::$cpt_labels = [
'singular_name' => 'Album',
'featured_image' => 'Album Cover',
'set_featured_image' => 'Set album cover',
];
$gallery::$cpt_args = [
'supports' => [ 'title', 'editor', 'thumbnail' ],
'menu_icon' => 'dashicons-format-image',
'input_placeholder' => 'Type this album title',
];
$gallery::$add_caps_to_role = [ 'editor' ];
$gallery::make_cpt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment