/app/view/shortcode/class-ms-view-shortcode-login.php

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