MS_Controller_Dialog

Controller to manage Membership popup dialogs.

Defined (1)

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

/app/controller/class-ms-controller-dialog.php  
  1. class MS_Controller_Dialog extends MS_Controller { 
  2.  
  3. /** 
  4. * Prepare the Dialog manager. 
  5. * @since 1.0.0 
  6. * @internal 
  7. */ 
  8. public function __construct() { 
  9. parent::__construct(); 
  10.  
  11. // Listen to Ajax requests that want to display a popup. 
  12. $this->add_ajax_action( 'ms_dialog', 'ajax_dialog' ); 
  13.  
  14. // Listen to Ajax requests that submit form data. 
  15. $this->add_ajax_action( 'ms_submit', 'ajax_submit' ); 
  16.  
  17. // Login. For IE this hook is listening to guests + logged in users. 
  18. $this->add_ajax_action( 'ms_login', 'ajax_login', true, true ); 
  19. $this->add_ajax_action( 'ms_lostpass', 'ajax_lostpass', true, true ); 
  20.  
  21. /** 
  22. * Ajax handler. Returns the HTML code of an popup dialog. 
  23. * The process is terminated after this handler. 
  24. * @since 1.0.0 
  25. * @internal 
  26. */ 
  27. public function ajax_dialog() { 
  28. $data = ''; 
  29.  
  30. if ( isset( $_REQUEST['dialog'] ) ) { 
  31. $dialog = $_REQUEST['dialog']; 
  32. $dlg = MS_Factory::create( 'MS_' . $dialog ); 
  33.  
  34. $dlg->prepare(); 
  35.  
  36. $data = array( 
  37. 'id' => $dialog,  
  38. 'title' => $dlg->title,  
  39. 'content' => $dlg->content,  
  40. 'height' => $dlg->height,  
  41. 'width' => $dlg->width,  
  42. 'modal' => $dlg->modal,  
  43. ); 
  44.  
  45. $this->respond( $data ); 
  46.  
  47. /** 
  48. * Ajax handler. Handles incoming form data that was submitted via ajax. 
  49. * Typically this form is displayed inside a popup. 
  50. * @since 1.0.0 
  51. * @internal 
  52. */ 
  53. public function ajax_submit() { 
  54. $data = ''; 
  55.  
  56. if ( isset( $_REQUEST['dialog'] ) ) { 
  57. $dialog = $_REQUEST['dialog']; 
  58. $dlg = MS_Factory::create( 'MS_' . $dialog ); 
  59. $data = $dlg->submit(); 
  60.  
  61. $this->respond( $data ); 
  62.  
  63. /** 
  64. * Ajax handler. Used by shortcode `ms-membership-login` to login via ajax. 
  65. * @since 1.0.0 
  66. * @internal 
  67. */ 
  68. public function ajax_login() { 
  69. $resp = array(); 
  70.  
  71. // First check the nonce, if it fails the function will break 
  72. check_ajax_referer( 'ms-ajax-login' ); 
  73.  
  74. /** 
  75. * The login fields have alternative names: 
  76. * - username or log 
  77. * - password or pwd 
  78. * - remember or rememberme 
  79. */ 
  80. lib3()->array->equip_post( 
  81. 'username',  
  82. 'password',  
  83. 'remember',  
  84. 'log',  
  85. 'pwd',  
  86. 'rememberme' 
  87. ); 
  88.  
  89. if ( empty( $_POST['username'] ) && ! empty( $_POST['log'] ) ) { 
  90. $_POST['username'] = $_POST['log']; 
  91. if ( empty( $_POST['password'] ) && ! empty( $_POST['pwd'] ) ) { 
  92. $_POST['password'] = $_POST['pwd']; 
  93. if ( empty( $_POST['remember'] ) && ! empty( $_POST['rememberme'] ) ) { 
  94. $_POST['remember'] = $_POST['rememberme']; 
  95.  
  96. lib3()->array->equip_post( 'username', 'password', 'remember' ); 
  97. lib3()->array->strip_slashes( $_POST, 'password' ); 
  98.  
  99. // Nonce is checked, get the POST data and sign user on 
  100. $info = array( 
  101. 'user_login' => $_POST['username'],  
  102. 'user_password' => $_POST['password'],  
  103. 'remember' => (bool) $_POST['remember'],  
  104. ); 
  105.  
  106. $user_signon = wp_signon( $info, false ); 
  107. if ( is_wp_error( $user_signon ) ) { 
  108. $resp['error'] = __( 'Wrong username or password', 'membership2' ); 
  109. } else { 
  110. $member = MS_Factory::load( 'MS_Model_Member', $user_signon->ID ); 
  111.  
  112. // Also used in class-ms-model-member.php (signon_user) 
  113. wp_set_current_user( $member->id ); 
  114. wp_set_auth_cookie( $member->id ); 
  115. do_action( 'wp_login', $member->username, $user_signon ); 
  116. do_action( 'ms_model_member_signon_user', $user_signon, $member ); 
  117.  
  118. $resp['loggedin'] = true; 
  119. $resp['success'] = __( 'Logging in...', 'membership2' ); 
  120.  
  121. /** 
  122. * Allows a custom redirection after login. 
  123. * Empty value will use the default redirect option of the login form. 
  124. * @since 1.0.0 
  125. */ 
  126. $resp['redirect'] = apply_filters( 'ms-ajax-login-redirect', '', $member ); 
  127.  
  128. $this->respond( $resp ); 
  129.  
  130. /** 
  131. * Ajax handler. Used by shortcode `ms-membership-login` to recover password 
  132. * @since 1.0.0 
  133. * @internal 
  134. */ 
  135. public function ajax_lostpass() { 
  136. $resp = array(); 
  137.  
  138. // First check the nonce, if it fails the function will break 
  139. check_ajax_referer( 'ms-ajax-lostpass' ); 
  140.  
  141. // Nonce is checked, get the POST data and sign user on 
  142. $errors = new WP_Error(); 
  143.  
  144. if ( empty( $_POST['user_login'] ) ) { 
  145. $resp['error'] = __( 'Enter a username or e-mail address.', 'membership2' ); 
  146. } else if ( strpos( $_POST['user_login'], '@' ) ) { 
  147. $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) ); 
  148. if ( empty( $user_data ) ) { 
  149. $resp['error'] = __( 'There is no user registered with that email address.', 'membership2' ); 
  150. } else { 
  151. $login = trim( $_POST['user_login'] ); 
  152. $user_data = get_user_by( 'login', $login ); 
  153.  
  154. do_action( 'lostpassword_post' ); 
  155.  
  156. if ( ! empty( $resp['error'] ) ) { 
  157. $this->respond( $resp ); 
  158.  
  159. if ( ! $user_data ) { 
  160. $resp['error'] = __( 'Invalid username or e-mail.', 'membership2' ); 
  161. $this->respond( $resp ); 
  162.  
  163. // Redefining user_login ensures we return the right case in the email. 
  164. $user_login = $user_data->user_login; 
  165. $user_email = $user_data->user_email; 
  166.  
  167. do_action( 'retreive_password', $user_login ); // Legacy (misspelled) 
  168. do_action( 'retrieve_password', $user_login ); 
  169.  
  170. $allow = apply_filters( 'allow_password_reset', true, $user_data->ID ); 
  171.  
  172. if ( ! $allow ) { 
  173. $resp['error'] = __( 'Password reset is not allowed for this user', 'membership2' ); 
  174. $this->respond( $resp ); 
  175. } elseif ( is_wp_error( $allow ) ) { 
  176. return $allow; 
  177.  
  178. // Save an event about the password reset; also send the email template. 
  179. $member = MS_Factory::load( 'MS_Model_Member', $user_data->ID ); 
  180. MS_Model_Event::save_event( MS_Model_Event::TYPE_MS_RESETPASSWORD, $member ); 
  181.  
  182. // Send our default email if the user does not have a custom email template in place. 
  183. if ( ! apply_filters( 'ms_sent_reset_password_email', false ) ) { 
  184. // Get a new reset-key. 
  185. $reset = $member->new_password_reset_key(); 
  186.  
  187. $schema = is_ssl() ? 'https' : 'http'; 
  188.  
  189. $message = sprintf( 
  190. __( 'Someone requested that the password be reset for the following account: %sIf this was a mistake, just ignore this email and nothing will happen.%s', 'membership2' ),  
  191. "\r\n\r\n" . network_home_url( '/', $schema ) . "\r\n" . 
  192. sprintf( __( 'Your username: %s', 'membership2' ), $user_login ) . "\r\n\r\n",  
  193. "\r\n\r\n" . $reset->url . "\r\n" 
  194. ); 
  195.  
  196. if ( is_multisite() ) { 
  197. $blogname = $GLOBALS['current_site']->site_name; 
  198. } else { 
  199. $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  200.  
  201. $title = sprintf( __( '[%s] Password Reset' ), $blogname ); 
  202.  
  203. $title = apply_filters( 'retrieve_password_title', $title ); 
  204. $message = apply_filters( 'retrieve_password_message', $message, $reset->key, $reset->url ); 
  205.  
  206. if ( $message && ! wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) ) { 
  207. $resp['error'] = __( 'The e-mail could not be sent.' ) . '<br />' . 
  208. __( 'Possible reason: your host may have disabled the mail() function.' ); 
  209. } else { 
  210. $resp['success'] = __( 'Check your e-mail for the confirmation link.', 'membership2' ); 
  211. } else { 
  212. $resp['success'] = __( 'Check your e-mail for the confirmation link.', 'membership2' ); 
  213.  
  214. $this->respond( $resp ); 
  215.  
  216. /** 
  217. * Output Ajax response (in JSON format) and terminate the process. 
  218. * @since 1.0.0 
  219. * @param array $resp The data to output. 
  220. */ 
  221. private function respond( $resp ) { 
  222. echo json_encode( $resp ); 
  223. exit(); 
  224.  
  225. };