1 | <?php |
||
3 | class Tabify_Edit_Screen_Feature_Permissions { |
||
4 | |||
5 | public function __construct() { |
||
6 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
|
|||
7 | |||
8 | add_action( 'tabify_settings_tab_title_box', array( $this, 'settings_tab_title_box' ) ); |
||
9 | add_action( 'tabify_settings_tab_title_after', array( $this, 'settings_tab_title_after' ), 10, 3 ); |
||
10 | |||
11 | // Remove meta boxes when no permissions |
||
12 | add_filter( 'tabify_tab_posttype_tabs', array( $this, 'posttype_tabs' ), 10, 2 ); |
||
13 | } |
||
14 | |||
15 | |||
16 | public function enqueue_scripts() { |
||
17 | $screen = get_current_screen(); |
||
18 | |||
19 | if ( 'settings_page_tabify-edit-screen' != $screen->base ) { |
||
20 | return; |
||
21 | } |
||
22 | |||
23 | wp_register_script( 'tabify-edit-screen-permissions', plugins_url( '/permissions.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
||
24 | wp_enqueue_script( 'tabify-edit-screen-permissions' ); |
||
25 | |||
26 | $data = array( |
||
27 | 'everyone' => __( 'Everyone', 'tabify-edit-screen' ), |
||
28 | 'onerole' => __( '1 role', 'tabify-edit-screen' ), |
||
29 | 'multirole' => __( '%s role', 'tabify-edit-screen' ) |
||
30 | ); |
||
31 | wp_localize_script( 'tabify-edit-screen-permissions', 'tabify_permissions', $data ); |
||
32 | } |
||
33 | |||
34 | |||
35 | public function settings_tab_title_box( $tab ) { |
||
36 | if ( $tab['permissions'] && is_array( $tab['permissions'] ) ) { |
||
37 | $count = count( $tab['permissions'] ); |
||
38 | $btn_permissions = sprintf( _n( '1 role', '%s roles', $count, 'tabify-edit-screen' ), $count ); |
||
39 | } |
||
40 | else { |
||
41 | $btn_permissions = __( 'Everyone', 'tabify-edit-screen' ); |
||
42 | } |
||
43 | |||
44 | echo '<button class="tabify-tab-permissions button button-secondary hide-if-no-js" type="button">' . $btn_permissions . '</button>'; |
||
45 | } |
||
46 | |||
47 | public function settings_tab_title_after( $tab, $section, $type ) { |
||
62 | } |
||
63 | |||
64 | |||
65 | public function posttype_tabs( $tabs, $post_type ) { |
||
66 | foreach( $tabs as $index => $tab ) { |
||
67 | if ( ! isset( $tab['permissions'] ) ) { |
||
68 | continue; |
||
69 | } |
||
70 | |||
71 | $current_user = wp_get_current_user(); |
||
72 | foreach ( $current_user->roles as $role ) { |
||
73 | if ( in_array( $role, $tab['permissions'] ) ) { |
||
74 | continue 2; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | foreach ( $tab['items'] as $item ) { |
||
79 | if ( 'titlediv' == $item ) { |
||
80 | remove_post_type_support( $post_type, 'title' ); |
||
81 | } |
||
82 | elseif ( 'postdivrich' == $item ) { |
||
83 | remove_post_type_support( $post_type, 'editor' ); |
||
84 | } |
||
85 | else { |
||
86 | // Just guess the context of the meta box |
||
87 | remove_meta_box( $item, $post_type, 'normal' ); |
||
88 | remove_meta_box( $item, $post_type, 'advanced' ); |
||
89 | remove_meta_box( $item, $post_type, 'side' ); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | unset( $tabs[ $index ] ); |
||
94 | } |
||
95 | |||
96 | return $tabs; |
||
97 | } |
||
98 | |||
99 | |||
100 | private function get_roles() { |
||
113 | } |
||
114 | |||
115 | } |
||
116 |