Created
April 10, 2020 18:53
-
-
Save jasonknight/57fbe4a420d6c9137ddf4b61cc9886d9 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 | |
function listing_post_type() { | |
$labels = array( | |
'name' => _x( 'Listing', 'Post Type studio', 'text_domain' ), | |
'singular_name' => _x( 'Listing', 'Post Type studio', 'text_domain' ), | |
'menu_name' => __( 'Listings', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Listing:', 'text_domain' ), | |
'all_items' => __( 'All Listings', 'text_domain' ), | |
'view_item' => __( 'View Listing', 'text_domain' ), | |
'add_new_item' => __( 'Add New Listing', 'text_domain' ), | |
'add_new' => __( 'Add New', 'text_domain' ), | |
'edit_item' => __( 'Edit Listing', 'text_domain' ), | |
'update_item' => __( 'Update Listing', 'text_domain' ), | |
'search_items' => __( 'Search Listings', 'text_domain' ), | |
'not_found' => __( 'Not found', 'text_domain' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), | |
); | |
$args = array( | |
'label' => __( 'listing', 'text_domain' ), | |
'description' => __( 'listing Description', 'text_domain' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'post-formats', ), | |
'taxonomies' => array( ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'menu_icon' => '', | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => ['listing','listings'], | |
'map_meta_cap' => true, | |
); | |
add_real_estate_agent_role(); | |
\add_filter('register_post_type_args', function ($args,$post_type) { | |
if ( 'listing' != $post_type ) | |
return $args; | |
$caps = []; | |
$v = ['read','edit','edit_others','edit_published','publish','read_private','delete','delete_others','delete_published',]; | |
foreach ( $v as $n ) { | |
$caps["{$n}_post"] = "{$n}_listing"; | |
$caps["{$n}_posts"] = "{$n}_listings"; | |
} | |
$args['capabilities'] = $caps; | |
return $args; | |
},20,2); | |
\register_post_type( 'listing', $args ); | |
} | |
function add_real_estate_agent_role() { | |
wp_roles()->remove_role('real_estate_agent'); | |
$caps = [ | |
'level_0' => true, | |
'level_1' => true, | |
'level_2' => true, | |
'read' => true, | |
'edit_listing' => true, | |
'read_listing' => true, | |
'delete_listing' => true, | |
'edit_listings' => true, | |
'edit_others_listings' => false, | |
'publish_listings' => true, | |
'read_private_listings' => true, | |
'delete_listings' => true, | |
'delete_private_listings' => true, | |
'delete_published_listings' => true, | |
'delete_others_listings' => false, | |
'edit_private_listings' => true, | |
'edit_published_listings' => true, | |
]; | |
wp_roles()->add_role('real_estate_agent',__('Real Estate Agent'), $caps); | |
// The following is because some plugins are bad bad citizens | |
// and will redirect us away from the admin unless we make them | |
// stop the badness. | |
$user = wp_get_current_user(); | |
// Because for some reason, admins can't see it | |
if ( current_user_can('administrator') ) { | |
$user->add_role('real_estate_agent'); | |
} | |
$agent = current_user_can('real_estate_agent'); | |
add_filter( 'woocommerce_prevent_automatic_wizard_redirect', function ($b) use($agent) { | |
if ( $agent ) | |
return false; | |
return $b; | |
}, 20, 1 ); | |
add_filter('template_redirect', function () use ($agent) { | |
if ( ! function_exists('is_account_page') ) | |
return; | |
if ( is_admin() ) | |
return; | |
if( $agent && is_account_page() && !curren_user_can('administrator') ) | |
wp_redirect( '/wp-admin/edit.php?post_type=listing' ); | |
}); | |
// Allow 'real_estate_agent' User Role to view the Dashboard | |
add_filter( 'woocommerce_prevent_admin_access', function ( $b ) use ($agent) { | |
if( $agent ) | |
return false;; | |
return $b; | |
},20,1); | |
// Show admin bar for 'real_estate_agent' User Role | |
add_filter( 'show_admin_bar', function ( $show ) use($agent) { | |
if ( $agent ) | |
$show = true; | |
return $show; | |
},20,1); | |
} | |
\add_action( 'init', __NAMESPACE__ . '\listing_post_type', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment