MS_View_Shortcode_Login

The Membership 2 MS View Shortcode Login class.

Defined (1)

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

/app/view/shortcode/class-ms-view-shortcode-login.php  
  1. class MS_View_Shortcode_Login extends MS_View { 
  2.  
  3. /** 
  4. * Returns the HTML code. 
  5. * @since 1.0.0 
  6. * @return string 
  7. */ 
  8. public function to_html() { 
  9. $res_html = ''; 
  10. $res_form = ''; 
  11. $html = ''; 
  12.  
  13. $valid_forms = array( 
  14. 'login',  
  15. 'logout',  
  16. 'reset',  
  17. 'lost',  
  18. ); 
  19.  
  20. extract( $this->data ); 
  21.  
  22. if ( ! isset( $form ) || ! in_array( $form, $valid_forms ) ) { 
  23. if ( MS_Model_Member::is_logged_in() ) { 
  24. $form = 'logout'; 
  25. } elseif ( isset( $action ) && 'resetpass' === $action ) { 
  26. $form = 'reset'; 
  27. } elseif ( 'lostpass' == $_GET['show'] ) { 
  28. $form = 'lost'; 
  29. } else { 
  30. $form = 'login'; 
  31.  
  32. $this->data['form'] = $form; 
  33.  
  34. /** 
  35. * Provide a customized login form. 
  36. * Possible filters to provide a customized login form: 
  37. * - 'ms_shortcode_custom_form-login' 
  38. * - 'ms_shortcode_custom_form-logout' 
  39. * - 'ms_shortcode_custom_form-reset' 
  40. * - 'ms_shortcode_custom_form-lost' 
  41. * @since 1.0.0 
  42. */ 
  43. $html = apply_filters( 
  44. 'ms_shortcode_custom_form-' . $form,  
  45. '',  
  46. $this->data 
  47. ); 
  48.  
  49. if ( ! empty( $html ) ) { 
  50. return $html; 
  51. } else { 
  52. $html = ''; 
  53.  
  54. if ( 'logout' === $form ) { 
  55. return $this->logout_form(); 
  56. } elseif ( 'reset' === $form ) { 
  57. return $this->reset_form(); 
  58. } else { 
  59. if ( empty( $redirect_login ) ) { 
  60. $redirect_login = MS_Helper_Utility::get_current_url(); 
  61.  
  62. // Build the Login Form. 
  63. $res_form .= $prefix; 
  64. $res_form .= $this->login_form( $redirect_login ); 
  65. $res_form .= $this->lostpass_form(); 
  66.  
  67. // Wrap form in optional wrappers. 
  68. if ( ! empty( $wrapwith ) ) { 
  69. $res_form .= sprintf( '<%s class="%s">', esc_attr( $wrapwith ), esc_attr( $wrapwithclass ) ); 
  70. $res_form = sprintf( 
  71. '<%1$s class="%2$s">%3$s</%1$s>',  
  72. esc_attr( $wrapwith ),  
  73. esc_attr( $wrapwithclass ),  
  74. $res_form 
  75. ); 
  76. if ( ! empty( $item ) ) { 
  77. $res_form = sprintf( 
  78. '<%1$s class="%2$s">%3$s</%1$s>',  
  79. esc_attr( $item ),  
  80. esc_attr( $itemclass ),  
  81. $res_form 
  82. ); 
  83. if ( ! empty( $holder ) ) { 
  84. $res_form = sprintf( 
  85. '<%1$s class="%2$s">%3$s</%1$s>',  
  86. esc_attr( $holder ),  
  87. esc_attr( $holderclass ),  
  88. $res_form 
  89. ); 
  90.  
  91. // Complete the HTML output. 
  92. if ( $header ) { 
  93. $html .= $this->login_header_html(); 
  94. $html .= $res_form; 
  95.  
  96. if ( $register && ! MS_Model_Member::is_logged_in() ) { 
  97. if ( MS_Model_Member::can_register() ) { 
  98. $link = sprintf( 
  99. '<a href="%1$s">%2$s</a>',  
  100. MS_Controller_Frontend::get_registration_url( 'register' ),  
  101. __( 'Register', 'membership2' ) 
  102. ); 
  103.  
  104. /** 
  105. * Filter documented in wp-includes/general-template.php 
  106. */ 
  107. $html .= apply_filters( 'register', $link ); 
  108.  
  109. // Load the ajax script that handles the Ajax login functions. 
  110. wp_enqueue_script( 'ms-ajax-login' ); 
  111.  
  112. lib3()->ui->data( 
  113. 'ms_ajax_login',  
  114. array( 
  115. 'ajaxurl' => admin_url( 'admin-ajax.php' ),  
  116. 'loadingmessage' => __( 'Please wait...', 'membership2' ),  
  117. 'errormessage' => __( 'Request failed, please try again.', 'membership2' ),  
  118. ); 
  119. // Remove linebreaks to bypass the "wpautop" filter. 
  120. $html = str_replace( array( "\r\n", "\r", "\n" ), '', $html ); 
  121.  
  122. $html = '<div class="ms-membership-form-wrapper">' . $html . '</div>'; 
  123. $html = apply_filters( 'ms_compact_code', $html ); 
  124.  
  125. /** 
  126. * Possible filters to provide a customized login form: 
  127. * - 'ms_shortcode_form-login' 
  128. * - 'ms_shortcode_form-logout' 
  129. * - 'ms_shortcode_form-reset' 
  130. * - 'ms_shortcode_form-lost' 
  131. */ 
  132. return apply_filters( 
  133. 'ms_shortcode_form-' . $form,  
  134. $html,  
  135. $this->data 
  136. ); 
  137.  
  138. /** 
  139. * Returns HTML partial with the header of the login form. 
  140. * @since 1.0.0 
  141. * @return string 
  142. */ 
  143. private function login_header_html() { 
  144. extract( $this->data ); 
  145.  
  146. ob_start(); 
  147. ?> 
  148. <legend><?php echo esc_html( $title ); ?></legend> 
  149. <?php if ( $show_note ) : ?> 
  150. <div class="ms-alert-box ms-alert-error"> 
  151. <?php _e( 'Please log in to access this page.', 'membership2' ); ?> 
  152. </div> 
  153. <?php endif; 
  154.  
  155. return ob_get_clean(); 
  156.  
  157. /** 
  158. * Returns HTML partial with the actual login form. 
  159. * @since 1.0.0 
  160. * @param string $redirect_to URL to redirect to right after login. 
  161. * @return string 
  162. */ 
  163. private function login_form( $redirect_to = null ) { 
  164. if ( empty( $redirect_to ) ) { 
  165. // Default redirect is back to the current page 
  166. $redirect_to = lib3()->net->current_url(); 
  167.  
  168. $defaults = array( 
  169. 'redirect_login' => $redirect_to,  
  170. 'label_username' => __( 'Username', 'membership2' ),  
  171. 'label_password' => __( 'Password', 'membership2' ),  
  172. 'label_remember' => __( 'Remember Me', 'membership2' ),  
  173. 'label_log_in' => __( 'Log In', 'membership2' ),  
  174. 'id_login_form' => 'loginform',  
  175. 'id_username' => 'user_login',  
  176. 'id_password' => 'user_pass',  
  177. 'id_remember' => 'rememberme',  
  178. 'id_login' => 'wp-submit',  
  179. 'show_remember' => true,  
  180. 'value_username' => '',  
  181. 'value_remember' => false, // Set this to true to default the "Remember me" checkbox to checked 
  182. ); 
  183.  
  184. /** 
  185. * Filter the default login form output arguments. 
  186. * @since 1.0.0 
  187. * @see wp_login_form() 
  188. * @param array $defaults An array of default login form arguments. 
  189. */ 
  190. $args = wp_parse_args( 
  191. $this->data,  
  192. apply_filters( 'login_form_defaults', $defaults ) 
  193. ); 
  194.  
  195. extract( $args ); 
  196.  
  197. $show_form = 'login' === $form ? '' : 'display:none'; 
  198. $form_class = 'ms-form ms-form-login'; 
  199. if ( $show_labels ) { 
  200. $form_class .= ' ms-has-labels'; 
  201. } else { 
  202. $form_class .= ' ms-no-labels'; 
  203. if ( $autofocus ) { 
  204. $form_class .= ' autofocus'; 
  205.  
  206. ob_start(); 
  207. ?> 
  208. <form 
  209. name="<?php echo esc_attr( $id_login_form ); ?>" 
  210. id="<?php echo esc_attr( $id_login_form ); ?>" 
  211. action="login" method="post" 
  212. class="<?php echo esc_attr( $form_class ); ?>" 
  213. style="<?php echo esc_attr( $show_form ); ?>"> 
  214.  
  215. <div class="form"> 
  216. <?php wp_nonce_field( 'ms-ajax-login' ); ?> 
  217. <?php echo apply_filters( 'login_form_top', '', $args ); ?> 
  218. <?php if ( 'top' === $nav_pos ) : ?> 
  219. <div class="status" style="display:none"></div> 
  220. <?php endif; ?> 
  221. <p class="login-username ms-field"> 
  222. <?php if ( $show_labels ) : ?> 
  223. <label for="<?php echo esc_attr( $id_username ); ?>"> 
  224. <?php echo esc_html( $label_username ); ?> 
  225. </label> 
  226. <?php endif; ?> 
  227. <input 
  228. type="text" 
  229. name="log" 
  230. id="<?php echo esc_attr( $id_username ); ?>" 
  231. class="input focus" 
  232. value="<?php echo esc_attr( $value_username ); ?>" 
  233. size="20" 
  234. placeholder="<?php echo esc_html( $label_username ); ?>"> 
  235. </p> 
  236. <p class="login-password ms-field"> 
  237. <?php if ( $show_labels ) : ?> 
  238. <label for="<?php echo esc_attr( $id_password ); ?>"> 
  239. <?php echo esc_html( $label_password ); ?> 
  240. </label> 
  241. <?php endif; ?> 
  242. <input 
  243. type="password" 
  244. name="pwd" 
  245. id="<?php echo esc_attr( $id_password ); ?>" 
  246. class="input" 
  247. value="" 
  248. size="20" 
  249. placeholder="<?php echo esc_html( $label_password ); ?>"> 
  250. </p> 
  251. <?php echo apply_filters( 'login_form_middle', '', $args ); ?> 
  252. <?php if ( 'top' === $nav_pos ) : ?> 
  253. <div class="nav"> 
  254. <p><a class="lost" href="#lostpassword"> 
  255. <?php _e( 'Lost your password?', 'membership2' ); ?> 
  256. </a></p> 
  257. </div> 
  258. <?php endif; ?> 
  259. <?php if ( $show_remember ) : ?> 
  260. <p class="login-remember ms-field"> 
  261. <input 
  262. name="rememberme" 
  263. type="checkbox" 
  264. id="<?php echo esc_attr( $id_remember ); ?>" 
  265. value="forever" 
  266. <?php checked( $value_remember ); ?> /> 
  267. <label for="<?php echo esc_attr( $id_remember ); ?>"> 
  268. <?php echo esc_html( $label_remember ); ?> 
  269. </label> 
  270. </p> 
  271. <?php endif; ?> 
  272. <?php do_action( 'login_form' );?> 
  273. <p class="login-submit"> 
  274. <input 
  275. type="submit" 
  276. name="wp-submit" 
  277. id="<?php echo esc_attr( $id_login ); ?>" 
  278. class="button-primary" 
  279. value="<?php echo esc_attr( $label_log_in ); ?>" /> 
  280. <input type="hidden" name="redirect_to" value="<?php echo esc_url( $redirect_login ); ?>" /> 
  281. </p> 
  282. <?php echo apply_filters( 'login_form_bottom', '', $args ); ?> 
  283. <?php if ( 'bottom' === $nav_pos ) : ?> 
  284. <div class="status" style="display:none"></div> 
  285. </div> 
  286. <div class="nav"> 
  287. <p><a class="lost" href="#lostpassword"><?php _e( 'Lost your password?', 'membership2' ); ?></a></p> 
  288. <?php endif; ?> 
  289. </div> 
  290. </form> 
  291. <?php 
  292. $html = ob_get_clean(); 
  293. $html = apply_filters( 'ms_compact_code', $html ); 
  294. return $html; 
  295.  
  296. /** 
  297. * Returns the HTML partial of the lost-password form 
  298. * @since 1.0.0 
  299. * @return string 
  300. */ 
  301. private function lostpass_form() { 
  302. $defaults = array( 
  303. 'label_lost_username' => __( 'Username or E-mail', 'membership2' ),  
  304. 'label_lostpass' => __( 'Reset Password', 'membership2' ),  
  305. 'id_lost_form' => 'lostpasswordform',  
  306. 'id_lost_username' => 'user_login',  
  307. 'id_lostpass' => 'wp-submit',  
  308. 'value_username' => '',  
  309. ); 
  310.  
  311. /** 
  312. * Filter the default login form output arguments. 
  313. * @since 1.0.0 
  314. * @see wp_login_form() 
  315. * @param array $defaults An array of default login form arguments. 
  316. */ 
  317. $args = wp_parse_args( $this->data, apply_filters( 'login_form_defaults', $defaults ) ); 
  318.  
  319. extract( $args ); 
  320.  
  321. $show_form = 'lost' === $form ? '' : 'display:none'; 
  322. $form_class = 'ms-form ms-form-lost'; 
  323. if ( $show_labels ) { 
  324. $form_class .= ' ms-has-labels'; 
  325. } else { 
  326. $form_class .= ' ms-no-labels'; 
  327.  
  328. ob_start(); 
  329. do_action( 'lost_password' ); 
  330. ?> 
  331. <form 
  332. name="<?php echo esc_attr( $id_lost_form ); ?>" 
  333. id="<?php echo esc_attr( $id_lost_form ); ?>" 
  334. action="lostpassword" 
  335. method="post" 
  336. class="<?php echo esc_attr( $form_class ); ?>" 
  337. style="<?php echo esc_attr( $show_form ); ?>"> 
  338. <div class="form"> 
  339. <?php wp_nonce_field( 'ms-ajax-lostpass' ); ?> 
  340. <?php echo apply_filters( 'lostpass_form_top', '', $args ); ?> 
  341. <?php if ( 'top' === $nav_pos ) : ?> 
  342. <div class="status" style="display:none"></div> 
  343. <?php endif; ?> 
  344. <p class="lostpassword-username ms-field"> 
  345. <?php if ( $show_labels ) : ?> 
  346. <label for="<?php echo esc_attr( $id_lost_username ); ?>" > 
  347. <?php echo esc_html( $label_lost_username ); ?> 
  348. </label> 
  349. <?php endif; ?> 
  350. <input 
  351. type="text" 
  352. name="user_login" 
  353. id="<?php echo esc_attr( $id_lost_username ); ?>" 
  354. class="input focus" 
  355. value="<?php echo esc_attr( $value_username ); ?>" 
  356. size="20" 
  357. placeholder="<?php echo esc_html( $label_lost_username ); ?>" /> 
  358. </p> 
  359. <?php echo apply_filters( 'lostpass_form_middle', '', $args ); ?> 
  360. <?php if ( 'top' === $nav_pos ) : ?> 
  361. <div class="nav"> 
  362. <p><a class="login" href="#login"><?php _e( 'Log in', 'membership2' ); ?></a></p> 
  363. </div> 
  364. <?php endif; ?> 
  365. <?php 
  366. /** 
  367. * Fires inside the lostpassword <form> tags, before the hidden fields. 
  368. * @since 1.0.0 
  369. */ 
  370. do_action( 'lostpassword_form' ); ?> 
  371. <p class="submit"> 
  372. <input 
  373. type="submit" 
  374. name="wp-submit" 
  375. id="<?php echo esc_attr( $id_lostpass ); ?>" 
  376. class="button-primary" 
  377. value="<?php echo esc_attr( $label_lostpass ); ?>" /> 
  378. </p> 
  379. <?php echo apply_filters( 'lostpass_form_bottom', '', $args ); ?> 
  380. <?php if ( 'bottom' === $nav_pos ) : ?> 
  381. <div class="status" style="display:none"></div> 
  382. </div> 
  383. <div class="nav"> 
  384. <p><a class="login" href="#login"><?php _e( 'Log in', 'membership2' ); ?></a></p> 
  385. <?php endif; ?> 
  386. </div> 
  387. </form> 
  388. <?php 
  389. $html = ob_get_clean(); 
  390. $html = apply_filters( 'ms_compact_code', $html ); 
  391.  
  392. return $html; 
  393.  
  394. /** 
  395. * Returns HTML partial that contains the logout form 
  396. * @since 1.0.0 
  397. * @return string 
  398. */ 
  399. private function logout_form() { 
  400. if ( ! MS_Model_Member::is_logged_in() ) { return ''; } 
  401. $member = MS_Model_Member::get_current_member(); 
  402.  
  403. extract( $this->data ); 
  404.  
  405. if ( empty( $redirect_logout ) ) { 
  406. $redirect_logout = MS_Helper_Utility::home_url( '/' ); 
  407.  
  408. $yourname = sprintf( 
  409. __( 'You are logged in as %s.', 'membership2' ),  
  410. ucfirst( $member->name ) 
  411. ); 
  412.  
  413. $yourname = apply_filters( 
  414. 'ms_shortcode_logout_message',  
  415. $yourname,  
  416. $member 
  417. ); 
  418.  
  419. $logout_text = apply_filters( 
  420. 'ms_shortcode_logout_link_text',  
  421. __( 'Logout', 'membership2' ),  
  422. $member 
  423. ); 
  424.  
  425. $redirect_logout = apply_filters( 
  426. 'ms_shortcode_logout_redirect',  
  427. $redirect_logout,  
  428. $member 
  429. ); 
  430.  
  431. $html = sprintf( 
  432. '%1$s <a class="login_button" href="%2$s">%3$s</a>',  
  433. $yourname,  
  434. wp_logout_url( $redirect_logout ),  
  435. $logout_text 
  436. ); 
  437.  
  438. if ( ! empty( $holder ) ) { 
  439. $html = sprintf( 
  440. '<%1$s class="%2$s">%3$s</%1$s>',  
  441. esc_attr( $holder ),  
  442. esc_attr( $holderclass ),  
  443. $html 
  444. ); 
  445.  
  446. return $html; 
  447.  
  448. /** 
  449. * Returns HTML partial that contains password-reset form. 
  450. * Based on WordPress core code from wp-login.php 
  451. * @since 1.0.0 
  452. * @return string 
  453. */ 
  454. private function reset_form() { 
  455. static $Reset_Result = null; 
  456.  
  457. if ( null === $Reset_Result ) { 
  458. lib3()->array->equip_get( 'login', 'key' ); 
  459. lib3()->array->equip_post( 'pass1', 'pass2' ); 
  460. $rp_login = wp_unslash( $_GET['login'] ); 
  461. $rp_key = wp_unslash( $_GET['key'] ); 
  462. $err_msg = new WP_Error(); 
  463. $fatal_error = false; 
  464.  
  465. lib3()->array->strip_slashes( $_POST, 'pass1', 'pass2' ); 
  466. $pass1 = $_POST['pass1']; 
  467. $pass2 = $_POST['pass2']; 
  468.  
  469. // Get the user object and validate the key. 
  470. if ( $rp_login && $rp_key ) { 
  471. $user = check_password_reset_key( $rp_key, $rp_login ); 
  472. } else { 
  473. $user = false; 
  474.  
  475. if ( ! $user || is_wp_error( $user ) ) { 
  476. // If the user was not found then show an error message. 
  477. if ( $user && 'expired_key' == $user->get_error_code() ) { 
  478. $fatal_error = true; 
  479. $err_msg->add( 
  480. 'password_expired_key',  
  481. __( 'Sorry, this reset-key is not valid anymore. Please request a new reset email and try again.', 'membership2' ) 
  482. ); 
  483. } else { 
  484. $fatal_error = true; 
  485. $err_msg->add( 
  486. 'password_invalid_key',  
  487. __( 'Sorry, we did not find a valid reset-key. Please request a new reset email and try again.', 'membership2' ) 
  488. ); 
  489. } else { 
  490. // If the user provided a new password, then check it now. 
  491. if ( $pass1 && $pass1 != $pass2 ) { 
  492. $pass1 = false; 
  493. $err_msg->add( 
  494. 'password_reset_mismatch',  
  495. __( 'The passwords do not match, try again.', 'membership2' ) 
  496. ); 
  497.  
  498. if ( $fatal_error && count( $err_msg->errors ) ) { 
  499. $url = esc_url_raw( 
  500. add_query_arg( 
  501. array( 'show' => 'lostpass' ),  
  502. remove_query_arg( array( 'action', 'key', 'login' ) ) 
  503. ); 
  504.  
  505. $Reset_Result = sprintf( 
  506. '[ms-note type="warning"]%s[/ms-note]<a href="%s">%s</a>',  
  507. $err_msg->get_error_message(),  
  508. $url,  
  509. __( 'Request a new password-reset key', 'membership2' ) 
  510. ); 
  511. } elseif ( $pass1 ) { 
  512. // This action is documented in wp-login.php 
  513. do_action( 'validate_password_reset', $err_msg, $user ); 
  514.  
  515. reset_password( $user, $_POST['pass1'] ); 
  516.  
  517. // All done! Show success message and link to login form 
  518. $url = esc_url_raw( 
  519. remove_query_arg( array( 'action', 'key', 'login' ) ) 
  520. ); 
  521.  
  522. $Reset_Result = sprintf( 
  523. '[ms-note type="info"]%s[/ms-note]<a href="%s">%s</a>',  
  524. __( 'Your Password has been reset.', 'membership2' ),  
  525. $url,  
  526. __( 'Login with your new password', 'membership2' ) 
  527. ); 
  528. } else { 
  529. // This action is documented in wp-login.php 
  530. do_action( 'validate_password_reset', $err_msg, $user ); 
  531.  
  532. wp_enqueue_script( 'utils' ); 
  533. wp_enqueue_script( 'user-profile' ); 
  534.  
  535. ob_start(); 
  536. if ( count( $err_msg->errors ) ) { 
  537. printf( 
  538. '[ms-note type="warning"]%s[/ms-note]',  
  539. implode( '<br>', $err_msg->get_error_messages() ) 
  540. ); 
  541. ?> 
  542. <form name="resetpassform" id="resetpassform" 
  543. action="" method="post" autocomplete="off" class="ms-form"> 
  544. <input type="hidden" id="user_login" 
  545. value="<?php echo esc_attr( $rp_login ); ?>" autocomplete="off"/> 
  546.  
  547. <p class="user-pass1-wrap"> 
  548. <label for="pass1"><?php _e('New password') ?></label><br /> 
  549. <div class="wp-pwd"> 
  550. <span class="password-input-wrapper"> 
  551. <input type="password" data-reveal="1" data-pw="<?php echo esc_attr( wp_generate_password( 16 ) ); ?>" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" aria-describedby="pass-strength-result" /> 
  552. </span> 
  553. <div id="pass-strength-result" class="hide-if-no-js" aria-live="polite"><?php _e( 'Strength indicator' ); ?></div> 
  554. </div> 
  555. </p> 
  556. <p class="user-pass2-wrap"> 
  557. <label for="pass2"><?php _e('Confirm new password') ?></label><br /> 
  558. <input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" /> 
  559. </p> 
  560.  
  561. <p class="description indicator-hint"><?php echo wp_get_password_hint(); ?></p> 
  562.  
  563. <br class="clear"/> 
  564.  
  565. <?php 
  566. // This action is documented in wp-login.php 
  567. do_action( 'resetpass_form', $user ); 
  568. ?> 
  569. <p class="submit"> 
  570. <input type="hidden" name="rp_key" value="<?php echo esc_attr( $rp_key ); ?>" /> 
  571.  
  572. <button type="submit" name="wp-submit" id="wp-submit" 
  573. class="button button-primary button-large"> 
  574. <?php _e( 'Reset Password', 'membership2' ); ?> 
  575. </button> 
  576. </p> 
  577. </form> 
  578. <?php 
  579. $html = ob_get_clean(); 
  580. $Reset_Result = apply_filters( 'ms_compact_code', $html ); 
  581.  
  582. $Reset_Result = do_shortcode( $Reset_Result ); 
  583.  
  584. return $Reset_Result; 
  585.