markoheijnen /
tabify-edit-screen
| 1 | <?php |
||
| 2 | |||
| 3 | class Tabify_Edit_Screen_Feature_Permissions { |
||
| 4 | /** |
||
| 5 | * Set hooks |
||
| 6 | * |
||
| 7 | * @since 0.9.0 |
||
| 8 | */ |
||
| 9 | public function __construct() { |
||
| 10 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 11 | |||
| 12 | add_action( 'tabify_settings_tab_title_box', array( $this, 'settings_tab_title_box' ) ); |
||
| 13 | add_action( 'tabify_settings_tab_title_after', array( $this, 'settings_tab_title_after' ), 10, 3 ); |
||
| 14 | |||
| 15 | // Remove meta boxes when no permissions |
||
| 16 | add_filter( 'tabify_tab_posttype_tabs', array( $this, 'posttype_tabs' ), 10, 2 ); |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Enqueue script for the settings page |
||
| 21 | * |
||
| 22 | * @since 0.9.0 |
||
| 23 | */ |
||
| 24 | public function enqueue_scripts() { |
||
| 25 | $screen = get_current_screen(); |
||
|
0 ignored issues
–
show
|
|||
| 26 | |||
| 27 | if ( 'settings_page_tabify-edit-screen' != $screen->base ) { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | wp_register_script( 'tabify-edit-screen-permissions', plugins_url( '/permissions.js', __FILE__ ), array( 'jquery' ), '1.0' ); |
||
| 32 | wp_enqueue_script( 'tabify-edit-screen-permissions' ); |
||
| 33 | |||
| 34 | $data = array( |
||
| 35 | 'everyone' => __( 'Everyone', 'tabify-edit-screen' ), |
||
| 36 | 'onerole' => __( '1 role', 'tabify-edit-screen' ), |
||
| 37 | 'multirole' => __( '%s role', 'tabify-edit-screen' ) |
||
| 38 | ); |
||
| 39 | wp_localize_script( 'tabify-edit-screen-permissions', 'tabify_permissions', $data ); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Add button next tot title of a tab |
||
| 44 | * |
||
| 45 | * @since 0.9.0 |
||
| 46 | */ |
||
| 47 | public function settings_tab_title_box( $tab ) { |
||
| 48 | if ( $tab['permissions'] && is_array( $tab['permissions'] ) ) { |
||
| 49 | $count = count( $tab['permissions'] ); |
||
| 50 | $btn_permissions = sprintf( _n( '1 role', '%s roles', $count, 'tabify-edit-screen' ), $count ); |
||
| 51 | } |
||
| 52 | else { |
||
| 53 | $btn_permissions = __( 'Everyone', 'tabify-edit-screen' ); |
||
| 54 | } |
||
| 55 | |||
| 56 | echo '<button class="tabify-tab-permissions button button-secondary hide-if-no-js" type="button">' . $btn_permissions . '</button>'; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Display the settings after the title |
||
| 61 | * |
||
| 62 | * @since 0.9.0 |
||
| 63 | */ |
||
| 64 | public function settings_tab_title_after( $tab, $section, $type ) { |
||
| 65 | echo '<div class="tabify-tab-permission-box">'; |
||
| 66 | |||
| 67 | $all_roles = $this->get_roles(); |
||
| 68 | foreach ( $all_roles as $key => $role ) { |
||
| 69 | $name = 'tabify[' . $type . '][' . $section . '][tabs][' . $tab['id'] . '][permissions][]'; |
||
| 70 | $checked = in_array( $key, $tab['permissions'] ) ? ' checked="checked"' : ''; |
||
| 71 | |||
| 72 | echo '<label>'; |
||
| 73 | echo '<input name="' . $name . '" type="checkbox" value="' . $key . '"' . $checked . '/>'; |
||
| 74 | echo $role['name']; |
||
| 75 | echo '</label>'; |
||
| 76 | } |
||
| 77 | |||
| 78 | echo '</div>'; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Remove meta boxes when no permissions |
||
| 83 | * |
||
| 84 | * @since 0.9.0 |
||
| 85 | */ |
||
| 86 | public function posttype_tabs( $tabs, $post_type ) { |
||
| 87 | foreach( $tabs as $index => $tab ) { |
||
| 88 | if ( ! isset( $tab['permissions'] ) ) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | $current_user = wp_get_current_user(); |
||
| 93 | foreach ( $current_user->roles as $role ) { |
||
| 94 | if ( in_array( $role, $tab['permissions'] ) ) { |
||
| 95 | continue 2; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | foreach ( $tab['items'] as $item ) { |
||
| 100 | if ( 'titlediv' == $item ) { |
||
| 101 | remove_post_type_support( $post_type, 'title' ); |
||
| 102 | } |
||
| 103 | elseif ( 'postdivrich' == $item ) { |
||
| 104 | remove_post_type_support( $post_type, 'editor' ); |
||
| 105 | } |
||
| 106 | else { |
||
| 107 | // Just guess the context of the meta box |
||
| 108 | remove_meta_box( $item, $post_type, 'normal' ); |
||
| 109 | remove_meta_box( $item, $post_type, 'advanced' ); |
||
| 110 | remove_meta_box( $item, $post_type, 'side' ); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | unset( $tabs[ $index ] ); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $tabs; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the roles of WordPress |
||
| 122 | * |
||
| 123 | * @since 0.9.0 |
||
| 124 | */ |
||
| 125 | private function get_roles() { |
||
| 126 | if ( ! function_exists( 'wp_roles' ) ) { |
||
| 127 | global $wp_roles; |
||
| 128 | |||
| 129 | if ( ! isset( $wp_roles ) ) { |
||
| 130 | $wp_roles = new WP_Roles(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | else { |
||
| 134 | $wp_roles = wp_roles(); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $wp_roles->roles; |
||
| 138 | } |
||
| 139 | |||
| 140 | } |
||
| 141 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.