MS_Rule_ReplaceLocation_Model

Membership Replace-Menu Rule class.

Defined (1)

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

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