daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest May 16th, 2018 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class Post_Type_Waybill {
  5.  
  6.   static function setup() {
  7.     // add_filter( 'user_has_cap', __CLASS__ .'::disallow_new_post_button', 10, 2 );
  8.     // add_filter( 'user_has_cap', __CLASS__ .'::disallow_new_post_page', 10, 2 );
  9.     add_action( 'init', __CLASS__ .'::waybill_capabilities', 0 );
  10.     add_action( 'init', __CLASS__ .'::waybill_post_type', 1 );
  11.     // add_action( 'admin_menu', __CLASS__ .'::admin_menu' );
  12.   }
  13.  
  14.   /**
  15.    * Disallow New Post Page
  16.    * Prevents users from accessing the "add new waybill" page
  17.    * @return array
  18.    */
  19.   static function disallow_new_post_page( $allcaps, $caps ) {
  20.     if ( ! did_action( 'admin_init' ) ) {
  21.       return $allcaps;
  22.     }
  23.     $screen = get_current_screen();
  24.     if ( ! $screen ) {
  25.       return $allcaps;
  26.     }
  27.     if ( $screen->id == 'waybills' && $screen->action == 'add' ) {
  28.       return [];
  29.     }
  30.     return $allcaps;
  31.   }
  32.  
  33.   /**
  34.    * Disallow New Post Button
  35.    * Removes the "add new" button on the listing page
  36.    * @return array
  37.    */
  38.   static function disallow_new_post_button( $allcaps, $caps ) {
  39.     if ( ! did_action( 'all_admin_notices' ) ) {
  40.       return $allcaps;
  41.     }
  42.     $screen = get_current_screen();
  43.     if ( 'edit-waybills' != $screen->id ) {
  44.       return $allcaps;
  45.     }
  46.     $cap = reset( $caps );
  47.     if ( 'edit_waybills' !== $cap ) {
  48.       return $allcaps;
  49.     }
  50.     if ( isset( $allcaps[ $cap ] ) ) {
  51.       $allcaps[ $cap ] = false;
  52.     }
  53.     // Remove this filter
  54.     remove_filter( 'user_has_cap', __CLASS__ .'::has_cap', 10 );
  55.     return $allcaps;
  56.   }
  57.  
  58.  
  59.   /**
  60.    * Waybill capabilities
  61.    * Enables administrators and shop managers to edit waybills
  62.    */
  63.   static function waybill_capabilities() {
  64.     $allowed_roles = ['administrator', 'shop_manager'];
  65.     foreach ( $allowed_roles as $role_name ) {
  66.       $role = get_role( $role_name );
  67.       if ( ! $role ) {
  68.         continue;
  69.       }
  70.       $role->add_cap( 'edit_waybill' );
  71.       $role->add_cap( 'read_waybill' );
  72.       $role->add_cap( 'edit_waybills' );
  73.       $role->remove_cap( 'delete_waybill' );
  74.       $role->add_cap( 'publish_waybills' );
  75.       $role->add_cap( 'read_private_waybills' );
  76.     }
  77.   }
  78.  
  79.   /**
  80.    * Waybill post type
  81.    */
  82.   static function waybill_post_type() {
  83.     $labels = array(
  84.       'name'                  => _x( 'Waybills', 'Post Type General Name', 'text_domain' ),
  85.       'singular_name'         => _x( 'Waybill', 'Post Type Singular Name', 'text_domain' ),
  86.       'menu_name'             => __( 'Waybills', 'text_domain' ),
  87.       'name_admin_bar'        => __( 'Waybill', 'text_domain' ),
  88.       'archives'              => __( 'Item Archives', 'text_domain' ),
  89.       'attributes'            => __( 'Item Attributes', 'text_domain' ),
  90.       'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
  91.       'all_items'             => __( 'All Items', 'text_domain' ),
  92.       'add_new_item'          => __( 'Add New Item', 'text_domain' ),
  93.       'add_new'               => __( 'Add New', 'text_domain' ),
  94.       'new_item'              => __( 'New Item', 'text_domain' ),
  95.       'edit_item'             => __( 'Edit Item', 'text_domain' ),
  96.       'update_item'           => __( 'Update Item', 'text_domain' ),
  97.       'view_item'             => __( 'View Item', 'text_domain' ),
  98.       'view_items'            => __( 'View Items', 'text_domain' ),
  99.       'search_items'          => __( 'Search Item', 'text_domain' ),
  100.       'not_found'             => __( 'Not found', 'text_domain' ),
  101.       'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
  102.       'featured_image'        => __( 'Featured Image', 'text_domain' ),
  103.       'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
  104.       'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
  105.       'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
  106.       'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
  107.       'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
  108.       'items_list'            => __( 'Items list', 'text_domain' ),
  109.       'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
  110.       'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
  111.     );
  112.     $args = array(
  113.       'label'                 => __( 'Waybill', 'text_domain' ),
  114.       'description'           => __( 'Waybill information page.', 'text_domain' ),
  115.       'labels'                => $labels,
  116.       'supports'              => array(),
  117.       'taxonomies'            => array(),
  118.       'hierarchical'          => false,
  119.       'public'                => false,
  120.       'show_ui'               => true,
  121.       'show_in_menu'          => true,
  122.       'menu_position'         => 5,
  123.       'menu_icon'             => 'dashicons-admin-page',
  124.       'show_in_admin_bar'     => false,
  125.       'show_in_nav_menus'     => false,
  126.       'can_export'            => false,
  127.       'has_archive'           => false,
  128.       'exclude_from_search'   => true,
  129.       'publicly_queryable'    => false,
  130.       'capability_type'       => 'waybill',
  131.       'capabilities'          => [
  132.         'edit_post'          => 'edit_waybill',
  133.         'read_post'          => 'read_waybill',
  134.         'delete_post'        => 'delete_waybill',
  135.         'edit_posts'         => 'edit_waybills',
  136.         'edit_others_posts'  => 'edit_others_waybills',
  137.         'publish_posts'      => 'publish_waybills',
  138.         'read_private_posts' => 'read_private_waybills',
  139.         'delete_posts'       => 'delete_waybills',
  140.       ],
  141.     );
  142.     register_post_type( 'waybill', $args );
  143.     remove_post_type_support('waybill', 'title');
  144.     remove_post_type_support('waybill', 'editor');
  145.   }
  146.  
  147.   /**
  148.    * Admin menu
  149.    * removes the "add new waybill" link from the admin menu
  150.    */
  151.   static function admin_menu() {
  152.     global $submenu;
  153.     if ( ! isset( $submenu['edit.php?post_type=waybill'] ) ) {
  154.       return;
  155.     }
  156.     unset( $submenu[ 'edit.php?post_type=waybill' ][ 10 ] );
  157.   }
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top