MS_Rule_Url

The WordPress Core MS Rule Url class.

Defined (1)

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

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