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

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