/app/rule/menuitem/class-ms-rule-menuitem-model.php

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