MS_Rule_MenuItem_Model

Membership Menu Rule class.

Defined (1)

The class is defined in the following location(s).

/app/rule/menuitem/class-ms-rule-menuitem-model.php  
  1. class MS_Rule_MenuItem_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * An array that holds all menu-IDs that are available for the current user. 
  5. * This is static, so it has correct values even when multiple memberships 
  6. * are evaluated. 
  7. * @var array 
  8. */ 
  9. static protected $allowed_items = array(); 
  10.  
  11. /** 
  12. * Rule type. 
  13. * @since 1.0.0 
  14. * @var string $rule_type 
  15. */ 
  16. protected $rule_type = MS_Rule_MenuItem::RULE_ID; 
  17.  
  18. /** 
  19. * Returns the active flag for a specific rule. 
  20. * State depends on Add-on 
  21. * @since 1.0.0 
  22. * @return bool 
  23. */ 
  24. static public function is_active() { 
  25. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  26. return 'item' == $settings->menu_protection; 
  27.  
  28. /** 
  29. * Initialize the rule 
  30. * @since 1.0.0 
  31. */ 
  32. public function prepare_obj() { 
  33. $this->add_filter( 'ms_rule_menuitem_listtable_url', 'view_url' ); 
  34.  
  35. /** 
  36. * Verify access to the current content. 
  37. * This rule will return NULL (not relevant), because the menus are 
  38. * protected via a wordpress hook instead of protecting the current page. 
  39. * @since 1.0.0 
  40. * @param string $id The content id to verify access. 
  41. * @return bool|null True if has access, false otherwise. 
  42. * Null means: Rule not relevant for current page. 
  43. */ 
  44. public function has_access( $id, $admin_has_access = true ) { 
  45. return null; 
  46.  
  47. /** 
  48. * Set initial protection. 
  49. * @since 1.0.0 
  50. */ 
  51. public function protect_content() { 
  52. parent::protect_content(); 
  53.  
  54. $this->add_filter( 'wp_setup_nav_menu_item', 'prepare_menuitem', 10, 3 ); 
  55. $this->add_filter( 'wp_get_nav_menu_items', 'protect_menuitems', 10, 3 ); 
  56.  
  57. /** 
  58. * Checks if the specified menu-ID is allowed by this rule. 
  59. * @since 1.0.0 
  60. * @param object $item The menu item object. 
  61. * @return bool 
  62. */ 
  63. protected function can_access_menu( $item, $admin_has_access = true ) { 
  64. $result = false; 
  65.  
  66. if ( parent::has_access( $item->ID, $admin_has_access ) ) { 
  67. $result = true; 
  68.  
  69. return $result; 
  70.  
  71. /** 
  72. * Set the protection flag for each menu item. 
  73. * This function is called before function protect_menuitems() below. 
  74. * Here we evaluate each menu item by itself to see if the user has access 
  75. * to the menu item and collect all accessible menu items in a static/shared 
  76. * array so we have correct information when evaluating multiple memberships. 
  77. * Relevant Action Hooks: 
  78. * - wp_setup_nav_menu_item 
  79. * @since 1.0.0 
  80. * @param array $item A single menu item. 
  81. * @param mixed $args The menu select args. 
  82. */ 
  83. public function prepare_menuitem( $item ) { 
  84. if ( ! empty( $item ) ) { 
  85. if ( $this->can_access_menu( $item ) ) { 
  86. self::$allowed_items[$item->ID] = $item->ID; 
  87.  
  88. return apply_filters( 
  89. 'ms_rule_menuitem_model_prepare_menuitems',  
  90. $item,  
  91. $this 
  92. ); 
  93.  
  94. /** 
  95. * Remove menu items that are protected. 
  96. * Menu-Item protection is split into two steps to ensure correct 
  97. * menu-visibility when users are members of multiple memberships. 
  98. * http://premium.wpmudev.org/forums/topic/multiple-membership-types-defaults-to-less-access-protected-content 
  99. * Relevant Action Hooks: 
  100. * - wp_get_nav_menu_items 
  101. * @since 1.0.0 
  102. * @param array $items The menu items. 
  103. * @param object $menu The menu object. 
  104. * @param mixed $args The menu select args. 
  105. */ 
  106. public function protect_menuitems( $items, $menu, $args ) { 
  107. if ( ! empty( $items ) ) { 
  108. foreach ( $items as $key => $item ) { 
  109. if ( ! isset( self::$allowed_items[ $item->ID ] ) ) { 
  110. unset( $items[ $key ] ); 
  111.  
  112. return apply_filters( 
  113. 'ms_rule_menuitem_model_protect_menuitems',  
  114. $items,  
  115. $menu,  
  116. $args,  
  117. $this 
  118. ); 
  119.  
  120. /** 
  121. * Reset the rule value data. This does not remove all items but only the 
  122. * items that belong to the specified menu. 
  123. * @since 1.0.0 
  124. * @param $menu_id The menu_id to reset children menu item rules. 
  125. * @return array The reset rule value. 
  126. */ 
  127. public function reset_menu_rule_values( $menu_id ) { 
  128. $items = wp_get_nav_menu_items( $menu_id ); 
  129.  
  130. if ( ! empty( $items ) ) { 
  131. foreach ( $items as $item ) { 
  132. unset( $this->rule_value[ $item->ID ] ); 
  133.  
  134. $this->rule_value = apply_filters( 
  135. 'ms_rule_menuitem_model_reset_menu_rule_values',  
  136. $this->rule_value,  
  137. $this 
  138. ); 
  139.  
  140. /** 
  141. * Menu table always displays all menu items on one page. 
  142. * @since 1.0.0 
  143. * @param array $option [description] 
  144. * @return int Number of items to display on one page 
  145. */ 
  146. protected function get_items_per_page( $option ) { 
  147. return 0; 
  148.  
  149. /** 
  150. * Customize the URL used for the view-list 
  151. * @since 1.0.0 
  152. * @param string $url The URL 
  153. * @return string The URL 
  154. */ 
  155. public function view_url( $url ) { 
  156. $menu_id = MS_Controller::get_request_field( 'menu_id', 0, 'REQUEST' ); 
  157. $url = esc_url_raw( add_query_arg( 'menu_id', $menu_id, $url ) ); 
  158. return $url; 
  159.  
  160. /** 
  161. * Get content to protect. 
  162. * @since 1.0.0 
  163. * @param $args The query post args 
  164. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  165. * @return array The contents array. 
  166. */ 
  167. public function get_contents( $args = null ) { 
  168. $contents = array(); 
  169.  
  170. if ( ! empty( $args['menu_id'] ) ) { 
  171. $menu_id = $args['menu_id']; 
  172. $items = wp_get_nav_menu_items( $menu_id ); 
  173.  
  174. if ( ! empty( $items ) ) { 
  175. foreach ( $items as $item ) { 
  176. $item_id = $item->ID; 
  177. $contents[ $item_id ] = $item; 
  178. $contents[ $item_id ]->id = $item_id; 
  179. $contents[ $item_id ]->title = esc_html( $item->title ); 
  180. $contents[ $item_id ]->name = esc_html( $item->title ); 
  181. $contents[ $item_id ]->parent_id = $menu_id; 
  182. $contents[ $item_id ]->type = MS_Rule_MenuItem::RULE_ID; 
  183. $contents[ $item_id ]->access = $this->get_rule_value( $contents[ $item_id ]->id ); 
  184.  
  185. $filter = self::get_exclude_include( $args ); 
  186. if ( is_array( $filter->include ) ) { 
  187. $contents = array_intersect_key( $contents, array_flip( $filter->include ) ); 
  188. } elseif ( is_array( $filter->exclude ) ) { 
  189. $contents = array_diff_key( $contents, array_flip( $filter->exclude ) ); 
  190.  
  191. return apply_filters( 
  192. 'ms_rule_menuitem_model_get_contents',  
  193. $contents,  
  194. $args,  
  195. $this 
  196. ); 
  197.  
  198. /** 
  199. * Get the total content count. 
  200. * @since 1.0.0 
  201. * @param $args The query post args 
  202. * @return int The total content count. 
  203. */ 
  204. public function get_content_count( $args = null ) { 
  205. $items = $this->get_contents( $args ); 
  206. $count = count( $items ); 
  207.  
  208. return apply_filters( 
  209. 'ms_rule_menuitem_model_get_content_count',  
  210. $count,  
  211. $args 
  212. ); 
  213.  
  214. /** 
  215. * Get a list of all menus (only the menu details, without menu-items). 
  216. * @since 1.0.0 
  217. * @return array { 
  218. * @type string $menu_id The menu id. 
  219. * @type string $name The menu name. 
  220. * } 
  221. */ 
  222. public function get_menu_array() { 
  223. $contents = array(); 
  224. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  225.  
  226. $count_args = array(); 
  227. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  228. $count_args['membership_id'] = $_REQUEST['membership_id']; 
  229.  
  230. if ( ! empty( $navs ) ) { 
  231. foreach ( $navs as $nav ) { 
  232. $count_args['menu_id'] = $nav->term_id; 
  233. $total = $this->get_content_count( $count_args ); 
  234.  
  235. $menu_url = esc_url_raw( 
  236. add_query_arg( 
  237. array( 'menu_id' => $nav->term_id ) 
  238. ); 
  239.  
  240. $contents[ $nav->term_id ] = array( 
  241. 'label' => $nav->name,  
  242. 'url' => $menu_url,  
  243. 'count' => $total,  
  244. ); 
  245.  
  246. if ( empty( $contents ) ) { 
  247. $contents[] = array( 
  248. 'label' => __( '(No Menus Available)', 'membership2' ) 
  249. ); 
  250.  
  251. return apply_filters( 
  252. 'ms_rule_menuitem_model_get_menu_array',  
  253. $contents,  
  254. $this 
  255. ); 
  256.