MS_View_Shortcode_RegisterUser

The Membership 2 MS View Shortcode RegisterUser class.

Defined (1)

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

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