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