MS_Controller_Rule

Controller to manage billing and invoices.

Defined (1)

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

/app/controller/class-ms-controller-rule.php  
  1. class MS_Controller_Rule extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_CHANGE_MEMBERSHIPS = 'ms_rule_change_memberships'; 
  9. const AJAX_ACTION_UPDATE_MATCHING = 'ms_rule_update_matching'; 
  10. const AJAX_ACTION_UPDATE_DRIPPED = 'ms_rule_update_dripped'; 
  11.  
  12. /** 
  13. * Prepare the Rule manager. 
  14. * @since 1.0.0 
  15. */ 
  16. public function __construct() { 
  17. parent::__construct(); 
  18.  
  19. $this->add_ajax_action( 
  20. self::AJAX_ACTION_CHANGE_MEMBERSHIPS,  
  21. 'ajax_action_change_memberships' 
  22. ); 
  23. $this->add_ajax_action( 
  24. self::AJAX_ACTION_UPDATE_MATCHING,  
  25. 'ajax_action_update_matching' 
  26. ); 
  27. $this->add_ajax_action( 
  28. self::AJAX_ACTION_UPDATE_DRIPPED,  
  29. 'ajax_action_update_dripped' 
  30. ); 
  31.  
  32. $this->add_action( 
  33. 'ms_controller_protection_admin_page',  
  34. 'edit_rule_manager' 
  35. ); 
  36.  
  37. /** 
  38. * Handle Ajax change-memberships action. 
  39. * Related Action Hooks: 
  40. * - wp_ajax_change_memberships 
  41. * @since 1.0.0 
  42. */ 
  43. public function ajax_action_change_memberships() { 
  44. $msg = 0; 
  45. $this->_resp_reset(); 
  46.  
  47. $required = array( 'rule', 'item' ); 
  48. if ( ! $this->is_admin_user() ) { 
  49. $this->_resp_err( 'permission denied' ); 
  50. } elseif ( ! $this->verify_nonce() ) { 
  51. $this->_resp_err( 'toggle-rule: nonce' ); 
  52. } elseif ( ! self::validate_required( $required ) ) { 
  53. $this->_resp_err( 'toggle-rule: required' ); 
  54.  
  55. if ( $this->_resp_ok() ) { 
  56. $values = array(); 
  57. if ( isset( $_POST['values'] ) && is_array( $_POST['values'] ) ) { 
  58. $values = $_POST['values']; 
  59.  
  60. $msg = $this->assign_memberships( 
  61. $_POST['rule'],  
  62. $_POST['item'],  
  63. $values 
  64. ); 
  65. $msg .= $this->_resp_code(); 
  66.  
  67. echo $msg; 
  68. exit; 
  69.  
  70. /** 
  71. * Handle Ajax update rule-matchong action. 
  72. * Related Action Hooks: 
  73. * - wp_ajax_update_matching 
  74. * @since 1.0.0 
  75. */ 
  76. public function ajax_action_update_matching() { 
  77. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_NOT_UPDATED; 
  78. $this->_resp_reset(); 
  79.  
  80. $required = array( 'rule_type' ); 
  81. $isset = array( 'item', 'value' ); 
  82.  
  83. if ( $this->_resp_ok() && ! $this->verify_nonce() ) { $this->_resp_err( 'update-matching-01' ); } 
  84. if ( $this->_resp_ok() && ! self::validate_required( $required ) ) { $this->_resp_err( 'update-matching-02' ); } 
  85. if ( $this->_resp_ok() && ! self::validate_required( $isset, 'POST', false ) ) { $this->_resp_err( 'update-matching-03' ); } 
  86.  
  87. if ( $this->_resp_ok() ) { 
  88. lib3()->array->strip_slashes( $_POST, 'value' ); 
  89.  
  90. $rule_type = $_POST['rule_type']; 
  91. $msg = $this->save_rule_values( 
  92. $rule_type,  
  93. $_POST['item'],  
  94. $_POST['value'],  
  95. false 
  96. ); 
  97. $msg .= $this->_resp_code(); 
  98.  
  99. echo $msg; 
  100. exit; 
  101.  
  102. /** 
  103. * Save rules for a rule type. 
  104. * First reset all rules, then save the incoming rules. 
  105. * The menu rule type is only reset for the parent menu_id group (clears all children submenus). 
  106. * @since 1.0.0 
  107. * @param string $rule_type The rule type to update. 
  108. * @param string[] $rule_ids The content identifiers. 
  109. * @param int|int[] $rule_values The rule values. 
  110. * @param bool $reset If set to false then the exiting rule values will be kept. 
  111. */ 
  112. private function save_rule_values( $rule_type, $rule_ids, $rule_values, $reset = true ) { 
  113. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_NOT_UPDATED; 
  114. if ( ! $this->is_admin_user() ) { 
  115. return $msg; 
  116.  
  117. $membership = $this->get_membership(); 
  118.  
  119. if ( $membership->is_valid() ) { 
  120. $rule = $membership->get_rule( $rule_type ); 
  121.  
  122. if ( $reset ) { 
  123. if ( MS_Rule_MenuItem::RULE_ID === $rule->rule_type 
  124. && ! empty( $_POST['menu_id'] ) 
  125. ) { 
  126. $rule->reset_menu_rule_values( $_POST['menu_id'] ); 
  127. } else { 
  128. $rule->reset_rule_values(); 
  129.  
  130. if ( ! is_array( $rule_ids ) ) { 
  131. $rule_ids = array( $rule_ids ); 
  132.  
  133. foreach ( $rule_ids as $id ) { 
  134. if ( ! empty( $id ) ) { 
  135. if ( is_array( $rule_values ) ) { 
  136. if ( isset( $rule_values[ $id ] ) ) { 
  137. $rule_value = $rule_values[ $id ]; 
  138. } else { 
  139. continue; 
  140. } else { 
  141. $rule_value = $rule_values; 
  142. $rule->set_access( $id, $rule_value ); 
  143. $membership->set_rule( $rule_type, $rule ); 
  144. $membership->save(); 
  145. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED; 
  146.  
  147. return apply_filters( 
  148. 'ms_controller_rule_save_rule_values',  
  149. $msg,  
  150. $rule_type,  
  151. $rule_ids,  
  152. $rule_values,  
  153. $this 
  154. ); 
  155.  
  156. /** 
  157. * Handle Ajax update dripped rules action. 
  158. * Related Action Hooks: 
  159. * - wp_ajax_update_dripped 
  160. * @since 1.0.0 
  161. */ 
  162. public function ajax_action_update_dripped() { 
  163. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_NOT_UPDATED; 
  164. $this->_resp_reset(); 
  165.  
  166. $fields = array( 
  167. 'membership_ids',  
  168. 'rule_type',  
  169. 'item_id',  
  170. ); 
  171.  
  172. if ( ! $this->verify_nonce( 'inline' ) ) { 
  173. $this->_resp_err( 'Invalid Nonce' ); 
  174. } elseif ( ! $this->is_admin_user() ) { 
  175. $this->_resp_err( 'Access Denied' ); 
  176. } elseif ( ! self::validate_required( $fields, 'POST', false ) ) { 
  177. $this->_resp_err( 'Missing Data' ); 
  178.  
  179. if ( $this->_resp_ok() ) { 
  180. $ids = $_POST['membership_ids']; 
  181. $rule_type = $_POST['rule_type']; 
  182. $item_id = $_POST['item_id']; 
  183.  
  184. if ( ! is_array( $ids ) ) { 
  185. $ids = explode( ', ', $ids ); 
  186.  
  187. // Loop all specified memberships and set the rule values. 
  188. foreach ( $ids as $id ) { 
  189. if ( empty( $_POST['ms_' . $id] ) ) { continue; } 
  190. $data = lib3()->array->get( $_POST['ms_' . $id] ); 
  191. lib3()->array->equip( $data, 'dripped_type', 'date', 'delay_unit', 'delay_type' ); 
  192.  
  193. $membership = MS_Factory::load( 'MS_Model_Membership', $id ); 
  194. $rule = $membership->get_rule( $rule_type ); 
  195.  
  196. $rule->set_dripped_value( 
  197. $item_id,  
  198. $data['dripped_type'],  
  199. $data['date'],  
  200. $data['delay_unit'],  
  201. $data['delay_type'] 
  202. ); 
  203.  
  204. $membership->set_rule( $rule_type, $rule ); 
  205. $membership->save(); 
  206.  
  207. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED; 
  208.  
  209. if ( MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED == $msg ) { 
  210. // If everything went well then get a refershed version of the list-table. 
  211. $GLOBALS['hook_suffix'] = 'membership-2_page_' . MS_Controller_Plugin::MENU_SLUG . '-protection'; 
  212. $table = apply_filters( 'ms_rule_listtable-' . $rule_type, null ); 
  213.  
  214. if ( $table ) { 
  215. $args = array( 
  216. 'offset' => isset( $_POST['offset'] ) ? $_POST['offset'] : 0,  
  217. 'number' => isset( $_POST['number'] ) ? $_POST['number'] : 20,  
  218. ); 
  219.  
  220. $items = $table->get_model()->get_contents( $args ); 
  221.  
  222. if ( isset( $items[ $item_id ] ) ) { 
  223. echo $table->display_rows( array( $items[ $item_id ] ) ); 
  224. } else { 
  225. $msg .= $this->_resp_code(); 
  226. echo $msg; 
  227.  
  228. exit; 
  229.  
  230. /** 
  231. * Handles Membership Rule form submissions. 
  232. * Related Action Hooks: 
  233. * - ms_controller_membership_edit_manager 
  234.   
  235. * @since 1.0.0 
  236. */ 
  237. public function edit_rule_manager( $rule_type ) { 
  238. $redirect = false; 
  239.  
  240. if ( isset( $_POST['rule'] ) ) { 
  241. $rule_type = $_POST['rule']; 
  242.  
  243. do_action( 'ms_controller_rule_edir_rule_manager', $rule_type, $this ); 
  244.  
  245. /** 
  246. * Rule single action 
  247. */ 
  248. if ( $this->verify_nonce( null, 'GET' ) ) { 
  249. $msg = $this->rule_list_do_action( 
  250. $_GET['action'],  
  251. $rule_type,  
  252. array( $_GET['item'] ) 
  253. ); 
  254. $redirect = esc_url_raw( 
  255. add_query_arg( 
  256. array( 'msg' => $msg),  
  257. remove_query_arg( array( 'action', 'item', '_wpnonce' ) ) 
  258. ); 
  259.  
  260. if ( $redirect ) { 
  261. wp_safe_redirect( $redirect ); 
  262. exit; 
  263.  
  264. /** 
  265. * Execute action in Rule model. 
  266. * @since 1.0.0 
  267. * @param string $action The action to execute. 
  268. * @param int[] $items The item ids which action will be taken. 
  269. * @return int Resulting message id. 
  270. */ 
  271. private function rule_list_do_action( $action, $rule_type, $items ) { 
  272. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_NOT_UPDATED; 
  273. if ( ! $this->is_admin_user() ) { 
  274. return $msg; 
  275.  
  276. $membership = $this->get_membership(); 
  277. if ( empty( $membership ) ) { 
  278. return $msg; 
  279.  
  280. $rule = $membership->get_rule( $rule_type ); 
  281. if ( ! empty( $rule ) ) { 
  282. foreach ( $items as $item ) { 
  283. switch ( $action ) { 
  284. case 'give_access': 
  285. $rule->give_access( $item ); 
  286. break; 
  287.  
  288. case 'no_access': 
  289. $rule->remove_access( $item ); 
  290. break; 
  291. $membership->set_rule( $rule_type, $rule ); 
  292. $membership->save(); 
  293. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED; 
  294.  
  295. return apply_filters( 
  296. 'ms_controller_rule_rule_list_do_action',  
  297. $msg,  
  298. $action,  
  299. $rule_type,  
  300. $items,  
  301. $this 
  302. ); 
  303.  
  304. /** 
  305. * Assigns (or removes) memberships from a rule-item. 
  306. * @since 1.0.0 
  307. * @param string $rule_type [description] 
  308. * @param string $item [description] 
  309. * @param array $memberships Memberships that will be assigned to the 
  310. * rule-item. Memberships that are not mentioned are removed. 
  311. * @return string [description] 
  312. */ 
  313. private function assign_memberships( $rule_type, $item, $memberships ) { 
  314. $base = MS_Model_Membership::get_base(); 
  315. $rule = $base->get_rule( $rule_type ); 
  316.  
  317. $memberships = apply_filters( 
  318. 'ms_controller_rule_assign_memberships',  
  319. $memberships,  
  320. $rule,  
  321. $item,  
  322. $this 
  323. ); 
  324.  
  325. $rule->set_memberships( $item, $memberships ); 
  326.  
  327. do_action( 
  328. 'ms_controller_rule_assign_memberships_done',  
  329. $rule,  
  330. $item,  
  331. $memberships,  
  332. $this 
  333. ); 
  334.  
  335. return MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED; 
  336.  
  337. /** 
  338. * Save Url Groups tab. 
  339. * @since 1.0.0 
  340. * @param array $fields The POST fields 
  341. */ 
  342. private function save_url_group( $fields ) { 
  343.  
  344. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_NOT_UPDATED; 
  345. if ( ! $this->is_admin_user() ) { 
  346. return $msg; 
  347.  
  348. $membership = $this->get_membership(); 
  349. if ( empty( $membership ) ) { 
  350. return $msg; 
  351.  
  352. if ( is_array( $fields ) ) { 
  353. $rule_type = MS_Rule_Url::RULE_ID; 
  354. $rule = $membership->get_rule( $rule_type ); 
  355.  
  356. foreach ( $fields as $field => $value ) { 
  357. $rule->$field = $value; 
  358. $membership->set_rule( $rule_type, $rule ); 
  359. $membership->save(); 
  360. $msg = MS_Helper_Membership::MEMBERSHIP_MSG_UPDATED; 
  361.  
  362. return apply_filters( 'ms_controller_rule_save_url_group', $msg ); 
  363.  
  364. /** 
  365. * Get membership from request. 
  366. * @since 1.0.0 
  367. * @return MS_Model_Membership or null if not found. 
  368. */ 
  369. private function get_membership() { 
  370. $membership_id = 0; 
  371.  
  372. if ( ! empty( $_GET['membership_id'] ) ) { 
  373. $membership_id = $_GET['membership_id']; 
  374. } elseif ( ! empty( $_POST['membership_id'] ) ) { 
  375. $membership_id = $_POST['membership_id']; 
  376.  
  377. $membership = null; 
  378. if ( ! empty( $membership_id ) ) { 
  379. $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id ); 
  380. } else { 
  381. $membership = MS_Model_Membership::get_base(); 
  382.  
  383. return apply_filters( 'ms_controller_rule_get_membership', $membership, $this );