Skip to content

Commit

Permalink
Updated menuitem protection logics
Browse files Browse the repository at this point in the history
  • Loading branch information
itsariful committed Apr 25, 2018
1 parent a15adcc commit 2c36380
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/model/class-ms-model-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function init_member() {
}

// No subscription: Assign the base membership, which only denies access.
if ( is_user_logged_in() && ! $this->member->has_membership() ) {
if ( ! $this->member->has_membership() ) {
$this->member->add_membership(
MS_Model_Membership::get_base()->id
);
Expand Down
19 changes: 17 additions & 2 deletions app/rule/menuitem/class-ms-rule-menuitem-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
*/
class MS_Rule_MenuItem_Model extends MS_Rule {

/**
* An array that holds all menu-IDs.
* This is static, so it has correct values even when multiple memberships
* are evaluated.
*
* @var array
*/
static protected $all_items = array();

/**
* An array that holds all menu-IDs that are available for the current user.
* This is static, so it has correct values even when multiple memberships
Expand Down Expand Up @@ -121,6 +130,9 @@ protected function can_access_menu( $item, $admin_has_access = true ) {
*/
public function prepare_menuitem( $item ) {
if ( ! empty( $item ) ) {

self::$all_items[$item->ID] = $item->ID;

if ( $this->can_access_menu( $item ) ) {
self::$allowed_items[$item->ID] = $item->ID;
}
Expand Down Expand Up @@ -151,11 +163,14 @@ public function prepare_menuitem( $item ) {
*/
public function protect_menuitems( $items, $menu, $args ) {
if ( ! empty( $items ) ) {

$denied_items = array_diff( self::$all_items, self::$allowed_items );

foreach ( $items as $key => $item ) {
if ( ! isset( self::$allowed_items[ $item->ID ] ) ) {
if ( isset( $denied_items[ $item->ID ] ) ) {
unset( $items[ $key ] );
} else if ( $item->menu_item_parent > 0 ) {
if ( ! isset( self::$allowed_items[ $item->menu_item_parent ] ) ) {
if ( isset( $denied_items[ $item->menu_item_parent ] ) ) {
unset( $items[ $key ] );
}
}
Expand Down

0 comments on commit 2c36380

Please sign in to comment.