MS_Rule_ReplaceMenu_Model

Membership Replace-Menu Rule class.

Defined (1)

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

/app/rule/replacemenu/class-ms-rule-replacemenu-model.php  
  1. class MS_Rule_ReplaceMenu_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * Rule type. 
  5. * @since 1.0.0 
  6. * @var string $rule_type 
  7. */ 
  8. protected $rule_type = MS_Rule_ReplaceMenu::RULE_ID; 
  9.  
  10. /** 
  11. * An array of all available menu items. 
  12. * @var array 
  13. */ 
  14. protected $menus = array(); 
  15.  
  16. /** 
  17. * Mapping of menu_ids that should be replaced. 
  18. * @var array 
  19. */ 
  20. protected $replacements; 
  21.  
  22. /** 
  23. * Returns the active flag for a specific rule. 
  24. * State depends on Add-on 
  25. * @since 1.0.0 
  26. * @return bool 
  27. */ 
  28. static public function is_active() { 
  29. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  30. return 'menu' == $settings->menu_protection; 
  31.  
  32. /** 
  33. * Verify access to the current content. 
  34. * This rule will return NULL (not relevant), because the menus are 
  35. * protected via a wordpress hook instead of protecting the current page. 
  36. * @since 1.0.0 
  37. * @param string $id The content id to verify access. 
  38. * @return bool|null True if has access, false otherwise. 
  39. * Null means: Rule not relevant for current page. 
  40. */ 
  41. public function has_access( $id, $admin_has_access = true ) { 
  42. return null; 
  43.  
  44. /** 
  45. * Set initial protection. 
  46. * @since 1.0.0 
  47. */ 
  48. public function protect_content() { 
  49. parent::protect_content(); 
  50.  
  51. /** 
  52. * Replace the "menu" attribute of the wp_nav_menu() call 
  53. */ 
  54. $this->add_filter( 'wp_nav_menu_args', 'replace_menus' ); 
  55.  
  56. /** 
  57. * Replace specific menus for certain members. 
  58. * Relevant Action Hooks: 
  59. * - wp_nav_menu_args 
  60. * @since 1.0.0 
  61. * @param mixed $args Attributes of the call to wp_nav_menu(). 
  62. * @return mixed The updated attributes. 
  63. */ 
  64. public function replace_menus( $args ) { 
  65. // We ignore the base membership for this rule-type. 
  66. if ( $this->is_base_rule ) { return $args; } 
  67.  
  68. $id = $args['menu']; 
  69.  
  70. if ( ! is_numeric( $id ) ) { 
  71. // Get the nav menu based on the theme_location 
  72. $locations = get_nav_menu_locations(); 
  73. if ( $args['theme_location'] && isset( $locations[ $args['theme_location'] ] ) ) { 
  74. $id = $locations[ $args['theme_location'] ]; 
  75.  
  76. if ( is_numeric( $id ) ) { 
  77. $replacements = $this->get_replacements(); 
  78.  
  79. if ( isset( $replacements[ $id ] ) ) { 
  80. $args['menu'] = $replacements[ $id ]; 
  81. $args['theme_location'] = ''; 
  82.  
  83. return apply_filters( 
  84. 'ms_rule_replacemenu_model_replace_menus',  
  85. $args,  
  86. $this 
  87. ); 
  88.  
  89. /** 
  90. * Get menu array. 
  91. * @since 1.0.0 
  92. * @return array { 
  93. * @type string $menu_id The menu id. 
  94. * @type string $name The menu name. 
  95. * } 
  96. */ 
  97. public function get_menus() { 
  98. if ( empty( $this->menus ) ) { 
  99. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  100.  
  101. if ( ! empty( $navs ) ) { 
  102. $this->menus = array(); 
  103.  
  104. foreach ( $navs as $nav ) { 
  105. $this->menus[ $nav->term_id ] = $nav->name; 
  106.  
  107. $this->menus = apply_filters( 
  108. 'ms_rule_replacemenu_model_get_menus',  
  109. $this->menus,  
  110. $this 
  111. ); 
  112.  
  113. if ( empty( $this->menus ) ) { 
  114. $this->menus = array( 
  115. __( 'No menus found.', 'membership2' ),  
  116. ); 
  117.  
  118. return $this->menus; 
  119.  
  120. /** 
  121. * Get content to protect. 
  122. * @since 1.0.0 
  123. * @param $args The query post args 
  124. * @return array The contents array. 
  125. */ 
  126. public function get_contents( $args = null ) { 
  127. $contents = array(); 
  128.  
  129. $menus = $this->get_menus(); 
  130.  
  131. if ( is_array( $menus ) ) { 
  132. foreach ( $menus as $key => $name ) { 
  133. $val = 0; 
  134. $saved = $this->get_rule_value( $key ); 
  135. $post_title = ''; 
  136. $access = false; 
  137.  
  138. if ( is_numeric( $saved ) && isset( $menus[ $saved ] ) ) { 
  139. $val = absint( $saved ); 
  140. $access = true; 
  141. $post_title = sprintf( 
  142. '%s → %s',  
  143. strip_tags( $name ),  
  144. $menus[$saved] 
  145. ); 
  146.  
  147. $contents[ $key ] = (object) array( 
  148. 'access' => $access,  
  149. 'title' => $name,  
  150. 'value' => $val,  
  151. 'post_title' => $post_title,  
  152. 'id' => $key,  
  153. 'type' => $this->rule_type,  
  154. ); 
  155.  
  156. if ( ! empty( $args['rule_status'] ) ) { 
  157. $contents = $this->filter_content( $args['rule_status'], $contents ); 
  158.  
  159. return apply_filters( 
  160. 'ms_rule_replacemenu_model_get_contents',  
  161. $contents,  
  162. $args,  
  163. $this 
  164. ); 
  165.  
  166. /** 
  167. * Returns an array that contains menu_ids that should be replaced. 
  168. * @since 1.0.0 
  169. * @return array { 
  170. * $original => $replacement 
  171. * } 
  172. */ 
  173. protected function get_replacements() { 
  174. if ( ! is_array( $this->replacements ) ) { 
  175. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  176. $this->replacements = array(); 
  177. $menus = $this->get_menus(); 
  178.  
  179. foreach ( $menus as $menu_id => $name ) { 
  180. $replace = $this->get_rule_value( $menu_id ); 
  181. if ( ! $replace ) { continue; } 
  182.  
  183. $replacement = $base_rule->get_rule_value( $menu_id ); 
  184.  
  185. if ( is_numeric( $replacement ) && $replacement > 0 ) { 
  186. $this->replacements[ $menu_id ] = intval( $replacement ); 
  187.  
  188. return $this->replacements; 
  189.  
  190. /** 
  191. * Returns an array of matching options that are displayed in a select 
  192. * list for each item. 
  193. * @since 1.0.0 
  194. * @return array 
  195. */ 
  196. public function get_matching_options( $args = null ) { 
  197. $options = array( 
  198. 0 => __( '( No replacement )', 'membership2' ),  
  199. ); 
  200.  
  201. $options += $this->get_menus(); 
  202.  
  203. return apply_filters( 
  204. 'ms_rule_replacemenu_model_get_matching_options',  
  205. $options,  
  206. $args,  
  207. $this 
  208. ); 
  209.  
  210. /** 
  211. * Get rule value for a specific content. 
  212. * @since 1.0.0 
  213. * @param string $id The content id to get rule value for. 
  214. * @return boolean The rule value for the requested content. Default $rule_value_default. 
  215. */ 
  216. public function get_rule_value( $id ) { 
  217. if ( is_scalar( $id ) && isset( $this->rule_value[ $id ] ) ) { 
  218. if ( $this->is_base_rule ) { 
  219. // The base-rule actually saves a menu_id as rule value. 
  220. $value = intval( $this->rule_value[ $id ] ); 
  221. } else { 
  222. // Non-Base rules only save a boolean flag 
  223. $value = (bool) $this->rule_value[ $id ]; 
  224. } else { 
  225. // Default response is NULL: "Not-Denied" 
  226. $value = MS_Model_Rule::RULE_VALUE_UNDEFINED; 
  227.  
  228. return apply_filters( 
  229. 'ms_get_rule_value',  
  230. $value,  
  231. $id,  
  232. $this->rule_type,  
  233. $this 
  234. ); 
  235.  
  236. /** 
  237. * Set access status to content. 
  238. * @since 1.0.0 
  239. * @param string $id The content id to set access to. 
  240. * @param int $access The access status to set. 
  241. */ 
  242. public function set_access( $id, $replace ) { 
  243. $delete = false; 
  244.  
  245. if ( ! $this->is_base_rule ) { 
  246. if ( MS_Model_Rule::RULE_VALUE_NO_ACCESS == $replace ) { 
  247. $delete = true; 
  248. } else { 
  249. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  250. $replace = true; 
  251.  
  252. if ( $delete ) { 
  253. unset( $this->rule_value[ $id ] ); 
  254. } else { 
  255. $this->rule_value[ $id ] = $replace; 
  256.  
  257. do_action( 'ms_rule_replacemenu_set_access', $id, $replace, $this ); 
  258.  
  259. /** 
  260. * Give access to content. 
  261. * @since 1.0.0 
  262. * @param string $id The content id to give access. 
  263. */ 
  264. public function give_access( $id ) { 
  265. if ( $this->is_base_rule ) { 
  266. // The base rule can only be updated via Ajax! 
  267. $cur_val = $this->get_rule_value( $id ); 
  268. if ( empty( $cur_val ) ) { 
  269. $this->set_access( $id, true ); 
  270. return; 
  271. } else { 
  272. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  273. $value = $base_rule->get_rule_value( $id ); 
  274.  
  275. $this->set_access( $id, $value ); 
  276.  
  277. do_action( 'ms_rule_replacemenu_give_access', $id, $this ); 
  278.  
  279. /** 
  280. * Serializes this rule in a single array. 
  281. * We don't use the PHP `serialize()` function to serialize the whole object 
  282. * because a lot of unrequired and duplicate data will be serialized 
  283. * @since 1.0.0 
  284. * @return array The serialized values of the Rule. 
  285. */ 
  286. public function serialize() { 
  287. $result = $this->rule_value; 
  288. return $result; 
  289.  
  290. /** 
  291. * Populates the rule_value array with the specified value list. 
  292. * This function is used when de-serializing a membership to re-create the 
  293. * rules associated with the membership. 
  294. * @since 1.0.0 
  295. * @param array $values A list of allowed IDs. 
  296. */ 
  297. public function populate( $values ) { 
  298. $this->rule_value = array(); 
  299. foreach ( $values as $menu_id => $replacement ) { 
  300. $this->rule_value[$menu_id] = $replacement; 
  301.