/app/class-ms-controller.php

  1. <?php 
  2. /** 
  3. * Abstract class for all Controllers. 
  4. * 
  5. * All controllers will extend or inherit from the MS_Controller class. 
  6. * Methods of this class will control the flow and behaviour of the plugin 
  7. * by using MS_Model and MS_View objects. 
  8. * 
  9. * @since 1.0.0 
  10. * 
  11. * @uses MS_Model 
  12. * @uses MS_View 
  13. * 
  14. * @package Membership2 
  15. */ 
  16. class MS_Controller extends MS_Hooker { 
  17.  
  18. /** 
  19. * Ajax response flag. 
  20. * 
  21. * @since 1.0.0 
  22. * 
  23. * @see _resp_ok() 
  24. * @var bool 
  25. */ 
  26. private $_resp_valid = true; 
  27.  
  28. /** 
  29. * Ajax response error-code. 
  30. * 
  31. * @since 1.0.0 
  32. * 
  33. * @see _resp_code() 
  34. * @var string 
  35. */ 
  36. private $_resp_code = ''; 
  37.  
  38. /** 
  39. * Parent constuctor of all controllers. 
  40. * 
  41. * @since 1.0.0 
  42. */ 
  43. public function __construct() { 
  44. /** 
  45. * Actions to execute when constructing the parent controller. 
  46. * 
  47. * @since 1.0.0 
  48. * @param object $this The MS_Controller object. 
  49. */ 
  50. do_action( 'ms_controller_construct', $this ); 
  51.  
  52. /** 
  53. * Does admin-side initialization. This function is called by the 
  54. * MS_Controller_Plugin object and is only executed when is_admin() is true. 
  55. * 
  56. * @since 1.0.0 
  57. */ 
  58. public function admin_init() { 
  59. // Nothing by default. Can be overwritten by child classes. 
  60.  
  61. /** 
  62. * Get action from request. 
  63. * 
  64. * @since 1.0.0 
  65. * 
  66. * @return string 
  67. */ 
  68. public function get_action() { 
  69. if ( empty( $_REQUEST['action'] ) ) { 
  70. $action = ''; 
  71. } else { 
  72. $action = $_REQUEST['action']; 
  73.  
  74. return apply_filters( 'ms_controller_get_action', $action, $this ); 
  75.  
  76. /** 
  77. * Verify nonce. 
  78. * 
  79. * @since 1.0.0 
  80. * 
  81. * @param string $action The action name to verify nonce. 
  82. * @param string $request_method POST or GET 
  83. * @param string $nonce_field The nonce field name 
  84. * @return boolean True if verified, false otherwise. 
  85. */ 
  86. public function verify_nonce( $action = null, $request_method = 'POST', $nonce_field = '_wpnonce' ) { 
  87. $verified = false; 
  88. switch ( $request_method ) { 
  89. case 'GET': 
  90. $request_fields = $_GET; 
  91. break; 
  92.  
  93. case 'REQUEST': 
  94. case 'any': 
  95. $request_fields = $_REQUEST; 
  96. break; 
  97.  
  98. case 'POST': 
  99. default: 
  100. $request_fields = $_POST; 
  101. break; 
  102.  
  103. if ( empty( $action ) ) { 
  104. $action = ! empty( $request_fields['action'] ) ? $request_fields['action'] : ''; 
  105.  
  106. if ( ! empty( $request_fields[ $nonce_field ] ) 
  107. && wp_verify_nonce( $request_fields[ $nonce_field ], $action ) 
  108. ) { 
  109. $verified = true; 
  110.  
  111. return apply_filters( 
  112. 'ms_controller_verify_nonce',  
  113. $verified,  
  114. $action,  
  115. $request_method,  
  116. $nonce_field,  
  117. $this 
  118. ); 
  119.  
  120. /** 
  121. * Verify if current user can perform management actions. 
  122. * 
  123. * @since 1.0.0 
  124. * 
  125. * @return boolean True if can, false otherwise. 
  126. */ 
  127. public function is_admin_user() { 
  128. $is_admin_user = MS_Model_Member::is_admin_user(); 
  129.  
  130. return apply_filters( 
  131. 'ms_controller_is_admin_user',  
  132. $is_admin_user 
  133. ); 
  134.  
  135. /** 
  136. * Verify required fields aren't empty. 
  137. * 
  138. * @since 1.0.0 
  139. * 
  140. * @param string[] $fields The array of fields to validate. 
  141. * @param string $request_method POST or GET 
  142. * @param bool $not_empty If true use empty method, else use isset method. 
  143. * @return bool True all fields are validated 
  144. */ 
  145. static public function validate_required( $fields, $request_method = 'POST', $not_empty = true ) { 
  146. $validated = true; 
  147. $request_fields = null; 
  148.  
  149. switch ( $request_method ) { 
  150. case 'GET': 
  151. $request_fields = $_GET; 
  152. break; 
  153.  
  154. case 'REQUEST': 
  155. case 'any': 
  156. $request_fields = $_REQUEST; 
  157. break; 
  158.  
  159. default: 
  160. case 'POST': 
  161. $request_fields = $_POST; 
  162. break; 
  163.  
  164.  
  165. foreach ( $fields as $field ) { 
  166. if ( $not_empty ) { 
  167. if ( empty( $request_fields[ $field ] ) ) { 
  168. $validated = false; 
  169. break; 
  170. } else { 
  171. if ( ! isset( $request_fields[ $field ] ) ) { 
  172. $validated = false; 
  173. break; 
  174.  
  175. return apply_filters( 
  176. 'ms_controller_validate_required',  
  177. $validated,  
  178. $fields 
  179. ); 
  180.  
  181. /** 
  182. * Get field from request parameters. 
  183. * 
  184. * @since 1.0.0 
  185. * 
  186. * @param string $id The field ID 
  187. * @param mixed $default The default value of the field. 
  188. * @param string $request_method POST or GET 
  189. * @return mixed The value of the request field. 
  190. */ 
  191. static public function get_request_field( $id, $default = '', $request_method = 'POST' ) { 
  192. $value = $default; 
  193. $request_fields = null; 
  194.  
  195. switch ( $request_method ) { 
  196. case 'GET': 
  197. $request_fields = $_GET; 
  198. break; 
  199.  
  200. case 'REQUEST': 
  201. $request_fields = $_REQUEST; 
  202. break; 
  203.  
  204. default: 
  205. case 'POST': 
  206. $request_fields = $_POST; 
  207. break; 
  208.  
  209.  
  210. if ( isset( $request_fields[ $id ] ) ) { 
  211. $value = $request_fields[ $id ]; 
  212.  
  213. return apply_filters( 
  214. 'ms_controller_get_request_field',  
  215. $value,  
  216. $id,  
  217. $default 
  218. ); 
  219.  
  220. /** 
  221. * Reset the response flags. 
  222. * The _resp_ functions are mainly used by Ajax handlers to simplify error 
  223. * tracking. 
  224. * 
  225. * Implemented in file ms-class-controller-rule.php 
  226. * 
  227. * @since 1.0.0 
  228. */ 
  229. protected function _resp_reset() { 
  230. $this->_resp_valid = true; 
  231. $this->_resp_code = ''; 
  232.  
  233. /** 
  234. * Returns current state of the response-valid flag. 
  235. * The flag can only be set to true via _resp_reset() 
  236. * And set to false by _resp_err() 
  237. * 
  238. * @since 1.0.0 
  239. * 
  240. * @return bool 
  241. */ 
  242. protected function _resp_ok() { 
  243. return ( true === $this->_resp_valid ); 
  244.  
  245. /** 
  246. * Returns the error code. 
  247. * The error code can be defined via _resp_err() 
  248. * 
  249. * @since 1.0.0 
  250. * 
  251. * @return string 
  252. */ 
  253. protected function _resp_code() { 
  254. if ( strlen( $this->_resp_code ) > 0 ) { 
  255. return ':' . $this->_resp_code; 
  256. return ''; 
  257.  
  258. /** 
  259. * Flag the current response as invalid and optionally define an error code. 
  260. * 
  261. * @since 1.0.0 
  262. * 
  263. * @param string $code Optional error code 
  264. */ 
  265. protected function _resp_err( $code = '' ) { 
  266. $this->_resp_valid = false; 
  267. $this->_resp_code = (string) $code; 
  268.  
.