PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/public/wp-content/plugins/wp-user-manager/includes/forms/class-wpum-form-register.php

https://bitbucket.org/cuongphanwp/thesis-management
PHP | 562 lines | 245 code | 133 blank | 184 comment | 56 complexity | 2687c51ce917360abe46a1f2f961ece6 MD5 | raw file
Possible License(s): MIT, 0BSD, CC-BY-4.0, CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, JSON, BSD-2-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * WP User Manager Forms
  4. *
  5. * @package wp-user-manager
  6. * @author Alessandro Tesoro
  7. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  8. * @since 1.0.0
  9. */
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) exit;
  12. /**
  13. * WPUM_Form_Register Class
  14. *
  15. * @since 1.0.0
  16. */
  17. class WPUM_Form_Register extends WPUM_Form {
  18. /**
  19. * The name of the form
  20. */
  21. public static $form_name = 'register';
  22. /**
  23. * Password Method
  24. */
  25. public static $random_password = true;
  26. /**
  27. * Init the form.
  28. *
  29. * @access public
  30. * @since 1.0.0
  31. * @return void
  32. */
  33. public static function init() {
  34. add_action( 'wp', array( __CLASS__, 'process' ) );
  35. // Validate and process passwords.
  36. if( wpum_get_option( 'custom_passwords' ) ) {
  37. self::$random_password = false;
  38. add_filter( 'wpum/form/validate=register', array( __CLASS__, 'validate_password' ), 10, 3 );
  39. if( wpum_get_option('login_after_registration') ) {
  40. add_action( 'wpum/form/register/done', array( __CLASS__, 'do_login' ), 11, 3 );
  41. }
  42. }
  43. // Make sure the submitted email is valid and not in use.
  44. add_filter( 'wpum/form/validate=register', array( __CLASS__, 'validate_email' ), 10, 3 );
  45. // Add a very basic honeypot spam prevention field.
  46. if( wpum_get_option( 'enable_honeypot' ) ) {
  47. add_action( 'wpum_get_registration_fields', array( __CLASS__, 'add_honeypot' ) );
  48. add_filter( 'wpum/form/validate=register', array( __CLASS__, 'validate_honeypot' ), 10, 3 );
  49. }
  50. /**
  51. * Adds a "terms" checkbox field to the signup form.
  52. */
  53. if( wpum_get_option('enable_terms') ) {
  54. add_action( 'wpum_get_registration_fields', array( __CLASS__, 'add_terms' ) );
  55. }
  56. // Allow user to select a user role upon registration.
  57. if( wpum_get_option( 'allow_role_select' ) ) {
  58. add_action( 'wpum_get_registration_fields', array( __CLASS__, 'add_role' ) );
  59. add_filter( 'wpum/form/validate=register', array( __CLASS__, 'validate_role' ), 10, 3 );
  60. add_action( 'wpum/form/register/success', array( __CLASS__, 'save_role' ), 10, 10 );
  61. }
  62. // Prevent users from using specific usernames if enabled.
  63. $exclude_usernames = wpum_get_option( 'exclude_usernames' );
  64. if( ! empty( $exclude_usernames ) ) {
  65. add_filter( 'wpum/form/validate=register', array( __CLASS__, 'validate_username' ), 10, 3 );
  66. }
  67. // Store uploaded avatars into the database.
  68. if( wpum_get_option('custom_avatars') && WPUM()->fields->show_on_registration( 'user_avatar' ) ) {
  69. add_action( 'wpum/form/register/success', array( __CLASS__, 'save_avatar' ), 10, 3 );
  70. }
  71. // Redirect to a page after successfull registration.
  72. if( wpum_get_option('login_after_registration') && wpum_get_option( 'custom_passwords' ) && wpum_get_option( 'registration_redirect' ) ) {
  73. add_filter( 'wpum_redirect_after_automatic_login', array( __CLASS__, 'adjust_redirect_url' ), 10, 2 );
  74. } elseif( ! wpum_get_option('login_after_registration') || ! wpum_get_option( 'custom_passwords' ) ) {
  75. if( wpum_get_option( 'registration_redirect' ) )
  76. add_action( 'wpum/form/register/done', array( __CLASS__, 'redirect_on_success' ), 9999, 3 );
  77. }
  78. }
  79. /**
  80. * Define registration fields
  81. *
  82. * @access public
  83. * @since 1.0.0
  84. * @return void
  85. */
  86. public static function get_registration_fields() {
  87. if ( self::$fields ) {
  88. return;
  89. }
  90. self::$fields = array(
  91. 'register' => wpum_get_registration_fields()
  92. );
  93. }
  94. /**
  95. * Validate the password field.
  96. *
  97. * @access public
  98. * @since 1.0.0
  99. * @return void
  100. */
  101. public static function validate_password( $passed, $fields, $values ) {
  102. $pwd = $values['register']['password'];
  103. $pwd_strenght = wpum_get_option('password_strength');
  104. $containsLetter = preg_match( '/[A-Z]/', $pwd );
  105. $containsDigit = preg_match( '/\d/', $pwd );
  106. $containsSpecial = preg_match( '/[^a-zA-Z\d]/', $pwd );
  107. if( $pwd_strenght == 'weak' ) {
  108. if( strlen( $pwd ) < 8)
  109. return new WP_Error( 'password-validation-error', __( 'Password must be at least 8 characters long.', 'wpum' ) );
  110. }
  111. if( $pwd_strenght == 'medium' ) {
  112. if( ! $containsLetter || ! $containsDigit || strlen( $pwd ) < 8 )
  113. return new WP_Error( 'password-validation-error', __( 'Password must be at least 8 characters long and contain at least 1 number and 1 uppercase letter.', 'wpum' ) );
  114. }
  115. if( $pwd_strenght == 'strong' ) {
  116. if( ! $containsLetter || ! $containsDigit || ! $containsSpecial || strlen( $pwd ) < 8 )
  117. return new WP_Error( 'password-validation-error', __( 'Password must be at least 8 characters long and contain at least 1 number and 1 uppercase letter and 1 special character.', 'wpum' ) );
  118. }
  119. return $passed;
  120. }
  121. /**
  122. * Autologin.
  123. *
  124. * @access public
  125. * @since 1.0.0
  126. * @return void
  127. */
  128. public static function do_login( $user_id, $values ) {
  129. $userdata = get_userdata( $user_id );
  130. $data = array();
  131. $data['user_login'] = $userdata->user_login;
  132. $data['user_password'] = $values['register']['password'];
  133. $data['rememberme'] = true;
  134. $user_login = wp_signon( $data, false );
  135. wp_redirect( apply_filters( 'wpum_redirect_after_automatic_login', get_permalink(), $user_id ) );
  136. exit;
  137. }
  138. /**
  139. * Adjust the redirect url of the automatic login functionality.
  140. * This is triggered when a custom successfull registration page has been assigned.
  141. *
  142. * @param string $permalink original url.
  143. * @param int $user_id the id of the user.
  144. * @return string the new url.
  145. */
  146. public static function adjust_redirect_url( $permalink, $user_id ) {
  147. return wpum_registration_redirect_url();
  148. }
  149. /**
  150. * Validate email field.
  151. *
  152. * @access public
  153. * @since 1.0.0
  154. * @return void
  155. */
  156. public static function validate_email( $passed, $fields, $values ) {
  157. $mail = $values['register'][ 'user_email' ];
  158. if( email_exists( $mail ) )
  159. return new WP_Error( 'email-validation-error', __( 'Email address already exists.', 'wpum' ) );
  160. return $passed;
  161. }
  162. /**
  163. * Add Honeypot field markup.
  164. *
  165. * @access public
  166. * @since 1.0.0
  167. * @return void
  168. */
  169. public static function add_honeypot( $fields ) {
  170. $fields[ 'comments' ] = array(
  171. 'label' => 'Comments',
  172. 'type' => 'textarea',
  173. 'required' => false,
  174. 'placeholder' => '',
  175. 'priority' => 9999,
  176. 'class' => 'wpum-honeypot-field'
  177. );
  178. return $fields;
  179. }
  180. /**
  181. * Validate the honeypot field.
  182. *
  183. * @access public
  184. * @since 1.0.0
  185. * @return void
  186. */
  187. public static function validate_honeypot( $passed, $fields, $values ) {
  188. $fake_field = $values['register'][ 'comments' ];
  189. if( $fake_field )
  190. return new WP_Error( 'honeypot-validation-error', __( 'Failed Honeypot validation', 'wpum' ) );
  191. return $passed;
  192. }
  193. /**
  194. * Add Terms field.
  195. *
  196. * @access public
  197. * @since 1.0.0
  198. * @return void
  199. */
  200. public static function add_terms( $fields ) {
  201. $fields[ 'terms' ] = array(
  202. 'label' => __('Terms &amp; Conditions', 'wpum'),
  203. 'type' => 'checkbox',
  204. 'description' => sprintf(__('By registering to this website you agree to the <a href="%s" target="_blank">terms &amp; conditions</a>.', 'wpum'), get_permalink( wpum_get_option('terms_page') ) ),
  205. 'required' => true,
  206. 'priority' => 9999,
  207. );
  208. return $fields;
  209. }
  210. /**
  211. * Add Role field.
  212. *
  213. * @access public
  214. * @since 1.0.0
  215. * @return void
  216. */
  217. public static function add_role( $fields ) {
  218. $fields[ 'role' ] = array(
  219. 'label' => __('Select Role', 'wpum'),
  220. 'type' => 'select',
  221. 'required' => true,
  222. 'options' => wpum_get_allowed_user_roles(),
  223. 'description' => __('Select your user role', 'wpum'),
  224. 'priority' => 9999,
  225. 'value' => get_option( 'default_role' )
  226. );
  227. return $fields;
  228. }
  229. /**
  230. * Validate the role field.
  231. *
  232. * @access public
  233. * @since 1.0.0
  234. * @return void
  235. */
  236. public static function validate_role( $passed, $fields, $values ) {
  237. $role_field = $values['register'][ 'role' ];
  238. $selected_roles = array_flip( wpum_get_option( 'register_roles' ) );
  239. if( !array_key_exists( $role_field , $selected_roles ) )
  240. return new WP_Error( 'role-validation-error', __( 'Select a valid role from the list.', 'wpum' ) );
  241. return $passed;
  242. }
  243. /**
  244. * Save the role.
  245. *
  246. * @access public
  247. * @since 1.0.0
  248. * @return void
  249. */
  250. public static function save_role( $user_id, $values ) {
  251. $user = new WP_User( $user_id );
  252. $user->set_role( $values['register'][ 'role' ] );
  253. }
  254. /**
  255. * Validate username field.
  256. *
  257. * @access public
  258. * @since 1.0.0
  259. * @return void
  260. */
  261. public static function validate_username( $passed, $fields, $values ) {
  262. $nickname = $values['register'][ 'username' ];
  263. if( wpum_get_option('exclude_usernames') && array_key_exists( $nickname , wpum_get_disabled_usernames() ) )
  264. return new WP_Error( 'nickname-validation-error', __( 'This nickname cannot be used.', 'wpum' ) );
  265. // Check for nicknames if permalink structure requires unique nicknames.
  266. if( get_option('wpum_permalink') == 'nickname' ) :
  267. if( wpum_nickname_exists( $nickname ) )
  268. return new WP_Error( 'username-validation-error', __( 'This nickname cannot be used.', 'wpum' ) );
  269. endif;
  270. return $passed;
  271. }
  272. /**
  273. * Add avatar to user custom field.
  274. * Also deletes previously selected avatar.
  275. *
  276. * @access public
  277. * @since 1.0.0
  278. * @return void
  279. */
  280. public static function save_avatar( $user_id, $values ) {
  281. $avatar_field = $values['register'][ 'user_avatar' ];
  282. if( !empty( $avatar_field ) && is_array( $avatar_field ) ) {
  283. update_user_meta( $user_id, "current_user_avatar", esc_url( $avatar_field['url'] ) );
  284. update_user_meta( $user_id, '_current_user_avatar_path', $avatar_field['path'] );
  285. }
  286. }
  287. /**
  288. * Redirect user to a page upon successfull registration.
  289. *
  290. * @param int $user_id id of the newly registered user.
  291. * @param array $values list of values submitted into the registration form.
  292. * @return void
  293. */
  294. public static function redirect_on_success( $user_id, $values ) {
  295. if( wpum_registration_redirect_url() ) {
  296. wp_redirect( wpum_registration_redirect_url() );
  297. exit;
  298. }
  299. }
  300. /**
  301. * Process the submission.
  302. *
  303. * @access public
  304. * @since 1.0.0
  305. * @return void
  306. */
  307. public static function process() {
  308. // Get fields
  309. self::get_registration_fields();
  310. // Get posted values
  311. $values = self::get_posted_fields();
  312. if ( empty( $_POST['wpum_submit_form'] ) ) {
  313. return;
  314. }
  315. if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'register' ) ) {
  316. return;
  317. }
  318. // Validate required
  319. if ( is_wp_error( ( $return = self::validate_fields( $values, self::$form_name ) ) ) ) {
  320. self::add_error( $return->get_error_message() );
  321. return;
  322. }
  323. // Let's do the registration
  324. self::do_registration( $values['register']['username'], $values['register']['user_email'], $values );
  325. }
  326. /**
  327. * Do registration.
  328. *
  329. * @access public
  330. * @since 1.0.0
  331. * @return void
  332. */
  333. public static function do_registration( $username, $email, $values ) {
  334. // Try registration
  335. if( self::$random_password ) {
  336. $do_user = self::random_psw_registration( $username, $email );
  337. $pwd = $do_user['pwd'];
  338. } else {
  339. $pwd = $values['register']['password'];
  340. $do_user = wp_create_user( $username, $pwd, $email );
  341. }
  342. // Check for errors.
  343. $do_user = isset( $do_user['do_user'] ) ? $do_user['do_user'] : $do_user;
  344. if ( is_wp_error( $do_user ) ) {
  345. foreach ($do_user->errors as $error) {
  346. self::add_error( $error[0] );
  347. }
  348. return;
  349. } else {
  350. $user_id = $do_user;
  351. // Set some meta if available
  352. if( array_key_exists( 'first_name' , $values['register'] ) )
  353. update_user_meta( $user_id, 'first_name', $values['register']['first_name'] );
  354. if( array_key_exists( 'last_name' , $values['register'] ) )
  355. update_user_meta( $user_id, 'last_name', $values['register']['last_name'] );
  356. if( array_key_exists( 'user_url' , $values['register'] ) )
  357. wp_update_user( array( 'ID' => $user_id, 'user_url' => $values['register']['user_url'] ) );
  358. if( array_key_exists( 'description' , $values['register'] ) )
  359. update_user_meta( $user_id, 'description', $values['register']['description'] );
  360. if( self::$random_password ) :
  361. self::add_confirmation( apply_filters( 'wpum/form/register/success/message', __( 'Registration complete. We have sent you a confirmation email with your password.', 'wpum' ) ) );
  362. else :
  363. self::add_confirmation( apply_filters( 'wpum/form/register/success/message', __( 'Registration complete.', 'wpum' ) ) );
  364. endif;
  365. // Add ability to extend registration process.
  366. do_action( "wpum/form/register/success" , $user_id, $values );
  367. // Send notification if password is manually added by the user.
  368. wpum_new_user_notification( $do_user, $pwd );
  369. // Needed to close the registration process properly.
  370. do_action( "wpum/form/register/done" , $user_id, $values );
  371. }
  372. }
  373. /**
  374. * Generate random password and register user
  375. *
  376. * @since 1.0.3
  377. * @param string $username username
  378. * @param string $email email
  379. * @return mixed
  380. */
  381. public static function random_psw_registration( $username, $email ) {
  382. // Generate something random for a password.
  383. $pwd = wp_generate_password( 20, false );
  384. $do_user = wp_create_user( $username, $pwd, $email );
  385. return array( 'do_user' => $do_user, 'pwd' => $pwd );
  386. }
  387. /**
  388. * Output the form.
  389. *
  390. * @access public
  391. * @since 1.0.0
  392. * @return void
  393. */
  394. public static function output( $atts = array() ) {
  395. // Get fields
  396. self::get_registration_fields();
  397. if( isset( $_POST['submit_wpum_register'] ) ) {
  398. // Show errors from fields
  399. self::show_errors();
  400. // Show confirmation messages
  401. self::show_confirmations();
  402. }
  403. // Display template
  404. if( !get_option( 'users_can_register' ) ) :
  405. // Display error message
  406. $message = array(
  407. 'id' => 'wpum-registrations-disabled',
  408. 'type' => 'notice',
  409. 'text' => __( 'Registrations are currently disabled.', 'wpum' )
  410. );
  411. wpum_message( $message );
  412. elseif( is_user_logged_in() ) :
  413. get_wpum_template( 'already-logged-in.php',
  414. array(
  415. 'args' => $atts
  416. )
  417. );
  418. // Show register form if not logged in
  419. else :
  420. get_wpum_template( 'forms/registration-form.php',
  421. array(
  422. 'atts' => $atts,
  423. 'form' => self::$form_name,
  424. 'register_fields' => self::get_fields( 'register' ),
  425. )
  426. );
  427. endif;
  428. }
  429. }