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