/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. * Support menu protection on admin-side. 
  77. * 
  78. * @since 1.0.2.4 
  79. */ 
  80. public function protect_admin_content() { 
  81. $this->protect_content(); 
  82.  
  83. /** 
  84. * Checks if the specified menu-ID is allowed by this rule. 
  85. * 
  86. * @since 1.0.0 
  87. * 
  88. * @param object $item The menu item object. 
  89. * @return bool 
  90. */ 
  91. protected function can_access_menu( $item, $admin_has_access = true ) { 
  92. $result = false; 
  93.  
  94. if ( parent::has_access( $item->ID, $admin_has_access ) ) { 
  95. $result = true; 
  96.  
  97. return $result; 
  98.  
  99. /** 
  100. * Set the protection flag for each menu item. 
  101. * 
  102. * This function is called before function protect_menuitems() below. 
  103. * Here we evaluate each menu item by itself to see if the user has access 
  104. * to the menu item and collect all accessible menu items in a static/shared 
  105. * array so we have correct information when evaluating multiple memberships. 
  106. * 
  107. * Relevant Action Hooks: 
  108. * - wp_setup_nav_menu_item 
  109. * 
  110. * @since 1.0.0 
  111. * 
  112. * @param array $item A single menu item. 
  113. * @param mixed $args The menu select args. 
  114. */ 
  115. public function prepare_menuitem( $item ) { 
  116. if ( ! empty( $item ) ) { 
  117. if ( $this->can_access_menu( $item ) ) { 
  118. self::$allowed_items[$item->ID] = $item->ID; 
  119.  
  120. return apply_filters( 
  121. 'ms_rule_menuitem_model_prepare_menuitems',  
  122. $item,  
  123. $this 
  124. ); 
  125.  
  126. /** 
  127. * Remove menu items that are protected. 
  128. * 
  129. * Menu-Item protection is split into two steps to ensure correct 
  130. * menu-visibility when users are members of multiple memberships. 
  131. * http://premium.wpmudev.org/forums/topic/multiple-membership-types-defaults-to-less-access-protected-content 
  132. * 
  133. * Relevant Action Hooks: 
  134. * - wp_get_nav_menu_items 
  135. * 
  136. * @since 1.0.0 
  137. * 
  138. * @param array $items The menu items. 
  139. * @param object $menu The menu object. 
  140. * @param mixed $args The menu select args. 
  141. */ 
  142. public function protect_menuitems( $items, $menu, $args ) { 
  143. if ( ! empty( $items ) ) { 
  144. foreach ( $items as $key => $item ) { 
  145. if ( ! isset( self::$allowed_items[ $item->ID ] ) ) { 
  146. unset( $items[ $key ] ); 
  147.  
  148. return apply_filters( 
  149. 'ms_rule_menuitem_model_protect_menuitems',  
  150. $items,  
  151. $menu,  
  152. $args,  
  153. $this 
  154. ); 
  155.  
  156. /** 
  157. * Reset the rule value data. This does not remove all items but only the 
  158. * items that belong to the specified menu. 
  159. * 
  160. * @since 1.0.0 
  161. * @param $menu_id The menu_id to reset children menu item rules. 
  162. * @return array The reset rule value. 
  163. */ 
  164. public function reset_menu_rule_values( $menu_id ) { 
  165. $items = wp_get_nav_menu_items( $menu_id ); 
  166.  
  167. if ( ! empty( $items ) ) { 
  168. foreach ( $items as $item ) { 
  169. unset( $this->rule_value[ $item->ID ] ); 
  170.  
  171. $this->rule_value = apply_filters( 
  172. 'ms_rule_menuitem_model_reset_menu_rule_values',  
  173. $this->rule_value,  
  174. $this 
  175. ); 
  176.  
  177. /** 
  178. * Menu table always displays all menu items on one page. 
  179. * 
  180. * @since 1.0.0 
  181. * @param array $option [description] 
  182. * @return int Number of items to display on one page 
  183. */ 
  184. protected function get_items_per_page( $option ) { 
  185. return 0; 
  186.  
  187. /** 
  188. * Customize the URL used for the view-list 
  189. * 
  190. * @since 1.0.0 
  191. * @param string $url The URL 
  192. * @return string The URL 
  193. */ 
  194. public function view_url( $url ) { 
  195. $menu_id = MS_Controller::get_request_field( 'menu_id', 0, 'REQUEST' ); 
  196. $url = esc_url_raw( add_query_arg( 'menu_id', $menu_id, $url ) ); 
  197. return $url; 
  198.  
  199. /** 
  200. * Get content to protect. 
  201. * 
  202. * @since 1.0.0 
  203. * @param $args The query post args 
  204. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  205. * @return array The contents array. 
  206. */ 
  207. public function get_contents( $args = null ) { 
  208. $contents = array(); 
  209.  
  210. if ( ! empty( $args['menu_id'] ) ) { 
  211. $menu_id = $args['menu_id']; 
  212. $items = wp_get_nav_menu_items( $menu_id ); 
  213.  
  214. if ( ! empty( $items ) ) { 
  215. foreach ( $items as $item ) { 
  216. $item_id = $item->ID; 
  217. $contents[ $item_id ] = $item; 
  218. $contents[ $item_id ]->id = $item_id; 
  219. $contents[ $item_id ]->title = esc_html( $item->title ); 
  220. $contents[ $item_id ]->name = esc_html( $item->title ); 
  221. $contents[ $item_id ]->parent_id = $menu_id; 
  222. $contents[ $item_id ]->type = MS_Rule_MenuItem::RULE_ID; 
  223. $contents[ $item_id ]->access = $this->get_rule_value( $contents[ $item_id ]->id ); 
  224.  
  225. $filter = self::get_exclude_include( $args ); 
  226. if ( is_array( $filter->include ) ) { 
  227. $contents = array_intersect_key( $contents, array_flip( $filter->include ) ); 
  228. } elseif ( is_array( $filter->exclude ) ) { 
  229. $contents = array_diff_key( $contents, array_flip( $filter->exclude ) ); 
  230.  
  231. return apply_filters( 
  232. 'ms_rule_menuitem_model_get_contents',  
  233. $contents,  
  234. $args,  
  235. $this 
  236. ); 
  237.  
  238. /** 
  239. * Get the total content count. 
  240. * 
  241. * @since 1.0.0 
  242. * 
  243. * @param $args The query post args 
  244. * @return int The total content count. 
  245. */ 
  246. public function get_content_count( $args = null ) { 
  247. $items = $this->get_contents( $args ); 
  248. $count = count( $items ); 
  249.  
  250. return apply_filters( 
  251. 'ms_rule_menuitem_model_get_content_count',  
  252. $count,  
  253. $args 
  254. ); 
  255.  
  256. /** 
  257. * Get a list of all menus (only the menu details, without menu-items). 
  258. * 
  259. * @since 1.0.0 
  260. * 
  261. * @return array { 
  262. * @type string $menu_id The menu id. 
  263. * @type string $name The menu name. 
  264. * } 
  265. */ 
  266. public function get_menu_array() { 
  267. $contents = array(); 
  268. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  269.  
  270. $count_args = array(); 
  271. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  272. $count_args['membership_id'] = $_REQUEST['membership_id']; 
  273.  
  274. if ( ! empty( $navs ) ) { 
  275. foreach ( $navs as $nav ) { 
  276. $count_args['menu_id'] = $nav->term_id; 
  277. $total = $this->get_content_count( $count_args ); 
  278.  
  279. $menu_url = esc_url_raw( 
  280. add_query_arg( 
  281. array( 'menu_id' => $nav->term_id ) 
  282. ); 
  283.  
  284. $contents[ $nav->term_id ] = array( 
  285. 'label' => $nav->name,  
  286. 'url' => $menu_url,  
  287. 'count' => $total,  
  288. ); 
  289.  
  290. if ( empty( $contents ) ) { 
  291. $contents[] = array( 
  292. 'label' => __( '(No Menus Available)', 'membership2' ) 
  293. ); 
  294.  
  295. return apply_filters( 
  296. 'ms_rule_menuitem_model_get_menu_array',  
  297. $contents,  
  298. $this 
  299. ); 
  300.  
.