MS_Controller

Abstract class for all Controllers.

Defined (1)

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

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