| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class CPT |
| 8 |
{ |
| 9 |
|
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
add_action( 'init', [$this, 'postType'] ); |
| 13 |
add_action( 'init', [$this, 'add_role'] ); |
| 14 |
add_action( 'admin_menu', [$this, 'remove_add_new_submenu']); |
| 15 |
add_filter( 'wp_untrash_post_status', [$this, 'template_restore_to_published'], 10, 3 ); |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
public function add_role(){ |
| 20 |
$roles = array(get_role(('administrator'))); |
| 21 |
|
| 22 |
foreach($roles as $role) { |
| 23 |
|
| 24 |
if($role) { |
| 25 |
$role->add_cap( 'edit_emailkit' ); |
| 26 |
$role->add_cap( 'edit_others_emailkit' ); |
| 27 |
$role->add_cap( 'publish_emailkit' ); |
| 28 |
$role->add_cap( 'read_emailkit' ); |
| 29 |
$role->add_cap( 'read_private_emailkit' ); |
| 30 |
$role->add_cap( 'delete_emailkit' ); |
| 31 |
$role->add_cap( 'edit_published_emailkit' ); |
| 32 |
$role->add_cap( 'delete_published_' ); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Emailkit Template CPT |
| 39 |
*/ |
| 40 |
function postType() |
| 41 |
{ |
| 42 |
$labels = array( |
| 43 |
|
| 44 |
'name' => esc_html_x('EmailKit', 'Post type general name', 'emailkit'), |
| 45 |
'singular_name' => esc_html_x('EmailKit', 'Post type singular name', 'emailkit'), |
| 46 |
'menu_name' => esc_html_x('EmailKit', 'Admin Menu text', 'emailkit'), |
| 47 |
'name_admin_bar' => esc_html_x('EmailKit', 'Add New on Toolbar', 'emailkit'), |
| 48 |
'add_new' => esc_html__('Add New Email', 'emailkit'), |
| 49 |
'add_new_item' => esc_html__('Add New Email Template', 'emailkit'), |
| 50 |
'new_item' => esc_html__('New Email', 'emailkit'), |
| 51 |
'edit_item' => esc_html__('Edit Email Template', 'emailkit'), |
| 52 |
'view_item' => esc_html__('View Email', 'emailkit'), |
| 53 |
'all_items' => esc_html__('All Emails', 'emailkit'), |
| 54 |
'search_items' => esc_html__('Search Emails', 'emailkit'), |
| 55 |
'parent_item_colon' => esc_html__('Parent Emails:', 'emailkit'), |
| 56 |
'not_found' => esc_html__('No email found.', 'emailkit'), |
| 57 |
'not_found_in_trash' => esc_html__('No email found in Trash.', 'emailkit'), |
| 58 |
'archives' => esc_html_x('Email archives', '', 'emailkit'), |
| 59 |
'insert_into_item' => esc_html_x('Insert into email', '', 'emailkit'), |
| 60 |
'uploaded_to_this_item' => esc_html_x('Uploaded to this email', '', 'emailkit'), |
| 61 |
'filter_items_list' => esc_html_x('Filter emails list', '', 'emailkit'), |
| 62 |
'items_list_navigation' => esc_html_x('Emails list navigation', '', 'emailkit'), |
| 63 |
'items_list' => esc_html_x('Emails list', '', 'emailkit'), |
| 64 |
|
| 65 |
); |
| 66 |
|
| 67 |
$rewrite = [ |
| 68 |
'slug' => 'emailkit-template', |
| 69 |
'with_front' => true, |
| 70 |
'pages' => false, |
| 71 |
'feeds' => false, |
| 72 |
]; |
| 73 |
|
| 74 |
$args = array( |
| 75 |
|
| 76 |
'label' => esc_html__('Email Template', 'emailkit'), |
| 77 |
'description' => esc_html__('Post Type Description', 'emailkit'), |
| 78 |
'labels' => $labels, |
| 79 |
'supports' => array('title'), |
| 80 |
'hierarchical' => false, |
| 81 |
'public' => true, |
| 82 |
'show_ui' => true, |
| 83 |
'show_in_menu' => 'emailkit-menu', |
| 84 |
'menu_icon' => 'dashicons-email', |
| 85 |
'menu_position' => 25, |
| 86 |
'show_in_admin_bar' => true, |
| 87 |
'show_in_nav_menus' => true, |
| 88 |
'can_export' => true, |
| 89 |
'has_archive' => true, |
| 90 |
'exclude_from_search' => false, |
| 91 |
'publicly_queryable' => false, |
| 92 |
'rewrite' => $rewrite, |
| 93 |
'capability_type' => 'page', |
| 94 |
'capabilities' => array( |
| 95 |
'publish_posts' => 'publish_emailkit', |
| 96 |
'edit_posts' => 'edit_emailkit', |
| 97 |
'delete_posts' => 'delete_emailkit', |
| 98 |
'read_private_posts' => 'read_private_emailkit', |
| 99 |
'edit_post' => 'edit_emailkit', |
| 100 |
'delete_post' => 'delete_emailkit', |
| 101 |
'read_post' => 'read_emailkit', |
| 102 |
), |
| 103 |
// '_edit_link' => 'post.php?post=%d&builder=emailkit' |
| 104 |
|
| 105 |
); |
| 106 |
|
| 107 |
register_post_type('emailkit', $args); |
| 108 |
} |
| 109 |
|
| 110 |
function remove_add_new_submenu() { |
| 111 |
global $submenu; |
| 112 |
unset($submenu['edit.php?post_type=emailkit'][10]); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Restore template to published mode by default it restore as draft status |
| 117 |
*/ |
| 118 |
function template_restore_to_published ( $new_status, $post_id, $previous_status ) { |
| 119 |
|
| 120 |
$post_type = get_post_type($post_id); |
| 121 |
|
| 122 |
if ( $post_type == 'emailkit-template' ) { |
| 123 |
return 'publish'; |
| 124 |
} |
| 125 |
|
| 126 |
return $previous_status; |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|