/app/rule/url/class-ms-rule-url.php

  1. <?php 
  2.  
  3. class MS_Rule_Url extends MS_Controller { 
  4.  
  5. /** 
  6. * The rule ID. 
  7. * 
  8. * @type string 
  9. */ 
  10. const RULE_ID = 'url'; 
  11.  
  12. // Form actions 
  13. const ACTION_ADD = 'rule_url_add'; 
  14. const ACTION_DELETE = 'rule_url_delete'; 
  15.  
  16. /** 
  17. * Setup the rule. 
  18. * 
  19. * @since 1.1.0 
  20. */ 
  21. public function prepare_obj() { 
  22. if ( MS_Rule_Url_Model::is_active() ) { 
  23. MS_Model_Rule::register_rule( 
  24. self::RULE_ID,  
  25. __CLASS__,  
  26. __( 'URL Restrictions', MS_TEXT_DOMAIN ),  
  27. ); 
  28.  
  29. $this->add_filter( 
  30. 'ms_view_protectedcontent_define-' . self::RULE_ID,  
  31. 'handle_render_callback', 10, 2 
  32. ); 
  33.  
  34. $this->add_filter( 
  35. 'ms_rule_listtable-' . self::RULE_ID,  
  36. 'return_listtable' 
  37. ); 
  38.  
  39. $this->process_form(); 
  40.  
  41. /** 
  42. * Tells Membership2 Admin to display this form to manage this rule. 
  43. * 
  44. * @since 1.1.0 
  45. * 
  46. * @param array $callback (Invalid callback) 
  47. * @param array $data The data collection. 
  48. * @return array Correct callback. 
  49. */ 
  50. public function handle_render_callback( $callback, $data ) { 
  51. $view = MS_Factory::load( 'MS_Rule_Url_View' ); 
  52.  
  53. $view->data = $data; 
  54. $callback = array( $view, 'to_html' ); 
  55.  
  56. return $callback; 
  57.  
  58. /** 
  59. * Returns the ListTable object for this rule. 
  60. * 
  61. * @since 1.1.0 
  62. * 
  63. * @return MS_Helper_ListTable 
  64. */ 
  65. public function return_listtable( $empty ) { 
  66. $base = MS_Model_Membership::get_base(); 
  67. $rule = $base->get_rule( self::RULE_ID ); 
  68. return new MS_Rule_Url_ListTable( $rule ); 
  69.  
  70. /** 
  71. * Processes a form submit and changes the rule values, if valid form data 
  72. * is found. 
  73. * 
  74. * @since 1.1.0 
  75. */ 
  76. private function process_form() { 
  77. $action = false; 
  78. if ( isset( $_POST['rule_action'] ) ) { 
  79. $action = $_POST['rule_action']; 
  80.  
  81. if ( ! $this->verify_nonce( $action ) ) { 
  82. $action = false; 
  83. } elseif ( isset( $_GET['rule_action'] ) ) { 
  84. $action = $_GET['rule_action']; 
  85.  
  86. if ( ! $this->verify_nonce( $action, 'GET' ) ) { 
  87. $action = false; 
  88.  
  89. if ( empty( $action ) ) { return; } 
  90. $redirect = false; 
  91.  
  92. switch ( $action ) { 
  93. case self::ACTION_ADD: 
  94. lib2()->array->strip_slashes( $_POST, 'url_value' ); 
  95.  
  96. $url = $_POST['url_value']; 
  97. $base = MS_Model_Membership::get_base(); 
  98. $rule = $base->get_rule( self::RULE_ID ); 
  99. $rule->add_url( $url ); 
  100. $base->set_rule( self::RULE_ID, $rule ); 
  101. $base->save(); 
  102.  
  103. $redirect = true; 
  104. break; 
  105.  
  106. case self::ACTION_DELETE: 
  107. $id = $_REQUEST['item']; 
  108. $base = MS_Model_Membership::get_base(); 
  109. $rule = $base->get_rule( self::RULE_ID ); 
  110. $rule->delete_url( $id ); 
  111. $base->set_rule( self::RULE_ID, $rule ); 
  112. $base->save(); 
  113.  
  114. $redirect = true; 
  115. break; 
  116.  
  117. if ( $redirect ) { 
  118. $target = esc_url_raw( 
  119. remove_query_arg( 
  120. array( '_wpnonce', 'item', 'rule_action' ) 
  121. ); 
  122. wp_safe_redirect( $target ); 
  123. exit; 
  124.  
.