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

  1. <?php 
  2. class MS_View_Shortcode_RegisterUser 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. // If user is logged in, they obviously cannot register again ;) 
  12. if ( is_user_logged_in() ) { return ''; } 
  13.  
  14. $fields = $this->prepare_fields(); 
  15.  
  16. $this->add_action( 
  17. 'ms_shortcode_register_form_end',  
  18. 'add_scripts' 
  19. ); 
  20.  
  21. // When redirecting to login form we want to keep the previously submitted form data. 
  22. $url_data = $_POST; 
  23. $url_data['do-login'] = '1'; 
  24. $login_url = esc_url_raw( add_query_arg( $url_data ) ); 
  25.  
  26. if ( ! empty( $_REQUEST['do-login'] ) ) { 
  27. $register_url = esc_url_raw( remove_query_arg( 'do-login' ) ); 
  28.  
  29. $back_link = array( 
  30. 'url' => $register_url,  
  31. 'class' => 'alignleft',  
  32. 'value' => __( 'Cancel', 'membership2' ),  
  33. ); 
  34.  
  35. $html = do_shortcode( 
  36. sprintf( 
  37. '[%s show_note=false title="%s"]',  
  38. MS_Helper_Shortcode::SCODE_LOGIN,  
  39. __( 'Login', 'membership2' ) 
  40. ); 
  41. $html .= MS_Helper_Html::html_link( $back_link, true ); 
  42. return $html; 
  43.  
  44. $login_link = array( 
  45. 'title' => __( 'Login', 'membership2' ),  
  46. 'url' => $login_url,  
  47. 'class' => 'alignleft',  
  48. 'value' => __( 'Already have a user account?', 'membership2' ),  
  49. ); 
  50.  
  51. $register_button = array( 
  52. 'id' => 'register',  
  53. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  54. 'value' => $this->data['label_register'],  
  55. ); 
  56.  
  57. /** 
  58. * The register button can be modified via a custom filter. 
  59. * Either update the array properties or replace the array with some 
  60. * HTML string that will be output. 
  61. * 
  62. * @since 1.0.1.2 
  63. * @param array|string $register_button 
  64. * @param array $data 
  65. */ 
  66. $register_button = apply_filters( 
  67. 'ms_shortcode_register_button',  
  68. $register_button,  
  69. $this->data 
  70. ); 
  71.  
  72. $title = $this->data['title']; 
  73. ob_start(); 
  74.  
  75. $reg_url = MS_Model_Pages::get_page_url( MS_Model_Pages::MS_PAGE_REGISTER ); 
  76. $reg_url = esc_url_raw( 
  77. add_query_arg( 'action', 'register_user', $reg_url ) 
  78. ); 
  79.  
  80. // Default WP action hook 
  81. do_action( 'before_signup_form' ); 
  82. ?> 
  83. <div class="ms-membership-form-wrapper"> 
  84. <?php $this->render_errors(); ?> 
  85. <form 
  86. id="ms-shortcode-register-user-form" 
  87. class="form-membership" 
  88. action="<?php echo esc_url( $reg_url ); ?>" 
  89. method="post"> 
  90.  
  91. <?php wp_nonce_field( $this->data['action'] ); ?> 
  92. <?php if ( ! empty( $title ) ) : ?> 
  93. <legend> 
  94. <?php echo $title; ?> 
  95. </legend> 
  96. <?php endif; ?> 
  97.  
  98. <?php foreach ( $fields as $field ) { 
  99. if ( is_string( $field ) ) { 
  100. MS_Helper_Html::html_element( $field ); 
  101. } elseif ( MS_Helper_Html::INPUT_TYPE_HIDDEN == $field['type'] ) { 
  102. MS_Helper_Html::html_element( $field ); 
  103. } else { 
  104. ?> 
  105. <div class="ms-form-element ms-form-element-<?php echo esc_attr( $field['id'] ); ?>"> 
  106. <?php MS_Helper_Html::html_element( $field ); ?> 
  107. </div> 
  108. <?php 
  109.  
  110. echo '<div class="ms-extra-fields">'; 
  111.  
  112. /** 
  113. * Trigger default WordPress action to allow other plugins 
  114. * to add custom fields to the registration form. 
  115. * 
  116. * signup_extra_fields Defined in wp-signup.php which is used 
  117. * for Multisite signup process. 
  118. * 
  119. * register_form Defined in wp-login.php which is only used for 
  120. * Single site registration process. 
  121. * 
  122. * @since 1.0.0 
  123. */ 
  124. if ( is_multisite() ) { 
  125. $empty_error = new WP_Error(); 
  126. do_action( 'signup_extra_fields', $empty_error ); 
  127. } else { 
  128. do_action( 'register_form' ); // Always on the register form. 
  129.  
  130. echo '</div>'; 
  131.  
  132. MS_Helper_Html::html_element( $register_button ); 
  133.  
  134. if ( is_wp_error( $this->error ) ) { 
  135. /** 
  136. * Display registration errors. 
  137. * 
  138. * @since 1.0.0 
  139. */ 
  140. do_action( 'registration_errors', $this->error ); 
  141.  
  142. /** 
  143. * This hook is intended to output hidden fields or JS code 
  144. * at the end of the form tag. 
  145. * 
  146. * @since 1.0.1.0 
  147. */ 
  148. do_action( 'ms_shortcode_register_form_end', $this ); 
  149. ?> 
  150. </form> 
  151. <?php 
  152. if ( $this->data['loginlink'] ) { 
  153. MS_Helper_Html::html_link( $login_link ); 
  154. ?> 
  155. </div> 
  156. <?php 
  157. // Default WP action hook
  158.  
  159. // Intentionally removed, because this hook should be only used in the 
  160. // blog-signup form. Not during user registration. 
  161. //do_action( 'signup_blogform', array() ); 
  162.  
  163. do_action( 'after_signup_form' ); 
  164.  
  165. $html = ob_get_clean(); 
  166. $html = apply_filters( 'ms_compact_code', $html ); 
  167.  
  168. return apply_filters( 
  169. 'ms_shortcode_register',  
  170. $html,  
  171. $this->data 
  172. ); 
  173.  
  174. /** 
  175. * Prepares the fields that are displayed in the form. 
  176. * 
  177. * @since 1.0.0 
  178. * @return array 
  179. */ 
  180. public function prepare_fields() { 
  181. $data = $this->data; 
  182.  
  183. $fields = array( 
  184. 'membership_id' => array( 
  185. 'id' => 'membership_id',  
  186. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  187. 'value' => $data['membership_id'],  
  188. ),  
  189.  
  190. 'first_name' => array( 
  191. 'id' => 'first_name',  
  192. 'title' => $data['label_first_name'],  
  193. 'placeholder' => $data['hint_first_name'],  
  194. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  195. 'value' => $data['first_name'],  
  196. ),  
  197.  
  198. 'last_name' => array( 
  199. 'id' => 'last_name',  
  200. 'title' => $data['label_last_name'],  
  201. 'placeholder' => $data['hint_last_name'],  
  202. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  203. 'value' => $data['last_name'],  
  204. ),  
  205.  
  206. 'username' => array( 
  207. 'id' => 'username',  
  208. 'title' => $data['label_username'],  
  209. 'placeholder' => $data['hint_username'],  
  210. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  211. 'value' => $data['username'],  
  212. ),  
  213.  
  214. 'email' => array( 
  215. 'id' => 'email',  
  216. 'title' => $data['label_email'],  
  217. 'placeholder' => $data['hint_email'],  
  218. 'type' => MS_Helper_Html::INPUT_TYPE_TEXT,  
  219. 'value' => $data['email'],  
  220. ),  
  221.  
  222. 'password' => array( 
  223. 'id' => 'password',  
  224. 'title' => $data['label_password'],  
  225. 'placeholder' => $data['hint_password'],  
  226. 'type' => MS_Helper_Html::INPUT_TYPE_PASSWORD,  
  227. 'value' => '',  
  228. ),  
  229.  
  230. 'password2' => array( 
  231. 'id' => 'password2',  
  232. 'title' => $data['label_password2'],  
  233. 'placeholder' => $data['hint_password2'],  
  234. 'type' => MS_Helper_Html::INPUT_TYPE_PASSWORD,  
  235. 'value' => '',  
  236. ),  
  237.  
  238. 'action' => array( 
  239. 'id' => 'action',  
  240. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  241. 'value' => $data['action'],  
  242. ),  
  243.  
  244. 'step' => array( 
  245. 'id' => 'step',  
  246. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  247. 'value' => $data['step'],  
  248. ),  
  249. ); 
  250.  
  251. return apply_filters( 
  252. 'ms_shortcode_register_form_fields',  
  253. $fields,  
  254. $this 
  255. ); 
  256.  
  257. /** 
  258. * Outputs the javascript used by the registration form. 
  259. * 
  260. * @since 1.0.1.0 
  261. */ 
  262. static public function add_scripts() { 
  263. static $Scripts_Done = false; 
  264.  
  265. // Make sure to only execute that function once. 
  266. if ( $Scripts_Done ) { return; } 
  267. $Scripts_Done = true; 
  268.  
  269. $rule_data = array( 
  270. 'username' => array( 
  271. 'required' => true,  
  272. ),  
  273. 'password' => array( 
  274. 'required' => true,  
  275. 'minlength' => 5,  
  276. ),  
  277. 'password2' => array( 
  278. 'required' => true,  
  279. 'equalTo' => '#password',  
  280. ),  
  281. ); 
  282.  
  283. /** 
  284. * Allow other plugins or Add-ons to modify the validation rules on the 
  285. * registration page. 
  286. * 
  287. * @since 1.0.1.0 
  288. * @var array 
  289. */ 
  290. $rule_data = apply_filters( 
  291. 'ms_shortcode_register_form_rules',  
  292. $rule_data 
  293. ); 
  294.  
  295. ob_start(); 
  296. ?> 
  297. jQuery(function() { 
  298. var args = { 
  299. onkeyup: false,  
  300. errorClass: 'ms-validation-error',  
  301. rules: <?php echo json_encode( $rule_data ); ?> 
  302. }; 
  303.  
  304. jQuery( '#ms-shortcode-register-user-form' ).validate( args ); 
  305. }); 
  306. <?php 
  307. $script = ob_get_clean(); 
  308. lib3()->ui->script( $script ); 
  309.  
  310. /** 
  311. * Renders error messages. 
  312. * 
  313. * @since 1.0.0 
  314. * @internal 
  315. */ 
  316. protected function render_errors() { 
  317. $errors = MS_Controller_Frontend::$register_errors; 
  318.  
  319. if ( ! empty( $errors ) ) { 
  320. ?> 
  321. <div class="ms-alert-box ms-alert-error"> 
  322. <?php echo $errors; ?> 
  323. </div> 
  324. <?php 
  325.  
.