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

/wp-content/plugins/jh-portfolio/helper/tj-accounts/tj-accounts.functions.php

https://github.com/adamcarlile/Alex-Goy
PHP | 483 lines | 286 code | 98 blank | 99 comment | 78 complexity | eb44a49589d763bbbf613565ea111f68 MD5 | raw file
  1. <?php
  2. /**
  3. * TT New User
  4. * Creates a new user with args passed through an array or string of arguments. Passing arguments works the same
  5. * as functions such as query_posts(). Params are show as variable names which you must use when passing args
  6. * NOTE: wp_nonce_field( 'register' ) must be used on the register form
  7. *
  8. * @Param: username [string] - The desired username for the new user
  9. * @Param: email [string] - The desired email address for the new user
  10. * @Param: use_password [bool] [default: false] - Whether to specify a password on registration
  11. * @Param: password [string] - If use_password is true, the desired password for the new user
  12. * @Param: use_tos [bool] [default: true] - Whether the user needs to accept Terms of Service
  13. * @Param: tos [string] - If use_tos is true, the value to the accept Terms of Service checkbox
  14. * @Param: unique_email [bool] [default: false] - Set to true if only one username is allowed per email address
  15. * @Param: do_redirect [bool] [default: true] Whether to redirect the user after registration is complete
  16. * @Param: redirect [string] [default: User Profile Page] - The url to redirect the user to after successful login
  17. * @Param: send_email [bool] [default: true] Whether to send an email containing the username and password of the newly registered user
  18. * @Param: profile_info [array] [dafault: false] An array containing values to be used in wp_update_user() such as first_name, last_name
  19. * @Param: validate [bool] [default: true]
  20. * @param: require_verify_email [bool] [default: false] Sends the user an email with a Activate Account link to activate their account
  21. * @param: override_nonce [bool] [default: false] Bypasses the nonce check, not recommended in most situations
  22. * @return: The ID of the newly registered user [on error returns error string]
  23. * @author: Joe Hoyle
  24. * @version 1.0
  25. **/
  26. function tja_new_user( $args ) {
  27. //Check the nonce field
  28. if( $args['override_nonce'] !== true )
  29. check_admin_referer('register');
  30. if( is_user_logged_in() ) {
  31. tj_error_message( 'You are already logged in', 'register' );
  32. return new WP_Error( 'already-logged-in');
  33. }
  34. include_once( ABSPATH . '/wp-includes/registration.php' );
  35. $checks = array(
  36. 'use_password' => false,
  37. 'tos' => '',
  38. 'use_tos' => true,
  39. 'unique_email' => false,
  40. 'do_redirect' => true,
  41. 'redirect' => '',
  42. 'send_email' => false,
  43. );
  44. $defaults = array(
  45. 'user_login' => '',
  46. 'user_email' => '',
  47. 'user_pass' => false,
  48. 'role' => 'subscriber',
  49. 'validate' => true,
  50. );
  51. $original_args = $args;
  52. $default_args = array_merge( $defaults, $checks );
  53. //Strip any tags then may have been put into the array
  54. strip_tags( (string) $args );
  55. $args = wp_parse_args( $args, $default_args );
  56. extract( $args, EXTR_SKIP );
  57. $validation = apply_filters( 'tja_registration_info', $args );
  58. unset( $args['user_pass2'] );
  59. unset( $user_pass2 );
  60. if ( $validation['status'] === 'error' && $validate == true ) {
  61. return $validation;
  62. }
  63. // Merge arrays overwritting defaults, remove any non-standard keys keys with empty values.
  64. $user_vars = array_filter( array_intersect_key( array_merge( $defaults, $args ), $defaults ) );
  65. //Check for require_verify_email, send email and store temp data
  66. if( $require_verify_email ) {
  67. $original_args['require_verify_email'] = false;
  68. $unverified_users = (array) get_option('unverified_users');
  69. $unverified_users[time()] = $original_args;
  70. update_option( 'unverified_users', $unverified_users );
  71. $message = "Please click the link below to activate your account for " . get_bloginfo() . "\n \n";
  72. $message .= '<a href="' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '">' . get_bloginfo('url') . '/login/?verify_email=' . $user_vars['user_email'] . '&key=' . time() . '</a>';
  73. $headers = 'From: ' . get_bloginfo() . ' <noreply@' . get_bloginfo( 'url' ) . '>' . "\r\n";
  74. $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n\r\n";
  75. wp_mail( $user_vars['user_email'], 'Please activate your account for ' . get_bloginfo(), $message, $headers );
  76. return tj_return_success( 'sent-email-activation', '<p class="message success">You have been sent an activation email, please follow the link in the email sent to ' . $user_vars['user_email'] . '</p>' );
  77. }
  78. $user_id = wp_insert_user( $user_vars );
  79. if ( $role ) :
  80. $user = new WP_User( $user_id );
  81. $user->set_role( $role );
  82. endif;
  83. // Get any remaining variable that were passed
  84. $meta_vars = array_diff_key( $original_args, $defaults, $checks );
  85. foreach ( (array) $meta_vars as $key => $value ) :
  86. update_usermeta( $user_id, $key, $value );
  87. endforeach;
  88. $user = get_userdata( $user_id );
  89. //Send Notifcation email if specified
  90. if ( $send_email == true )
  91. $email = tja_email_registration_success( $user, $user_pass );
  92. //If they chose a password, login them in
  93. if ( $use_password == 'true' && $user->ID > 0 ) :
  94. wp_login($user->user_login, $user_pass);
  95. wp_clearcookie();
  96. wp_setcookie($user->user_login, $user_pass, false);
  97. endif;
  98. //Redirect the user if is set
  99. if ( $redirect !== '' && $user->ID && $do_redirect == true ) wp_redirect( $redirect );
  100. return $user_id;
  101. }
  102. /**
  103. * tja_validate_registration function.
  104. *
  105. * @access public
  106. * @param mixed $args
  107. * @return void
  108. */
  109. function tja_validate_registration( $args ) {
  110. //Username
  111. if( ($user = get_user_by('login', $args['user_login'])) && $user->ID )
  112. return array( 'status' => 'error', 'text' => 'username-exists', 'message' => 'Sorry, the username: ' . $args['user_login'] . ' already exists.' );
  113. //Password
  114. if( $args['user_pass'] != $args['user_pass2'] )
  115. return array( 'status' => 'error', 'text' => 'password-mismatch', 'message' => 'The passwords you entered do not match.' );
  116. //Email
  117. if( !is_email( $args['user_email'] ) ) {
  118. tj_error_message( 'The email address you entered is not valid', 'register' );
  119. return new WP_Error( 'invalid-email' );
  120. }
  121. if( $args['unique_email'] == true && get_user_by_email( $args['user_email'] ) && $args['user_email'] ) {
  122. tj_error_message( 'The email address you entered is already in use', 'register' );
  123. return new WP_Error( 'email-in-use' );
  124. }
  125. }
  126. add_filter( 'tja_registration_info', 'tja_validate_registration' );
  127. function tja_email_registration_success( $user, $user_pass ) {
  128. if( file_exists( $file = get_stylesheet_directory() . '/email.register.php' ) ) {
  129. ob_start();
  130. include( $file );
  131. $message = ob_get_contents();
  132. ob_end_clean();
  133. } elseif( file_exists( $file = 'tt-accounts.email.register.php' ) ) {
  134. ob_start();
  135. include( $file );
  136. $message = ob_get_contents();
  137. ob_end_clean();
  138. } else {
  139. return false;
  140. }
  141. add_filter( 'wp_mail_content_type', 'wp_mail_content_type_html' );
  142. return wp_mail( $user->user_email, apply_filters( 'tja_register_email_subject', 'New account registered for ' . get_bloginfo() ), $message, 'content-type=text/html' );
  143. }
  144. // This forces the inbuilt mail function to send html emails instead of plain text emails.
  145. function wp_mail_content_type_html( $content_type ) {
  146. return 'text/html';
  147. }
  148. /**
  149. * Logs a user in
  150. *
  151. * @Param: username (string)
  152. * @Param: password (string)
  153. * @Param: password_hashed (bool) [default: false]
  154. * @Param: redirect_to (string) [optional]
  155. * @Param: remember (bool) [default: false]
  156. * @Param: allow_email_login (bool) [default: true]
  157. *
  158. * @Return: error array (message => string, number => (int) true on success
  159. * 101: already logged in
  160. 102: no username
  161. 103: unrocognized username
  162. 104: incorrect password
  163. 105: success
  164. **/
  165. function tja_log_user_in( $args ) {
  166. if ( is_user_logged_in() ) :
  167. tj_error_message( 'You are already logged in', 'login' );
  168. return new WP_Error( 'already-logged-in' );
  169. endif;
  170. if ( empty( $args['username'] ) ) :
  171. return new WP_Error( 'no-username' );
  172. endif;
  173. $user = tja_parse_user( $args['username'] );
  174. $defaults = array(
  175. 'remember' => false,
  176. 'allow_email_login' => true
  177. );
  178. // Strip any tags then may have been put into the array
  179. foreach( $args as $i => $a ) {
  180. if( is_string( $a ) )
  181. $args[ $i ] = strip_tags( $a );
  182. }
  183. $args = wp_parse_args( $args, $defaults );
  184. extract( $args, EXTR_SKIP );
  185. if ( !is_numeric( $user->ID ) ) :
  186. tj_error_message( 'The username you entered was not recognized', 'login' );
  187. return new WP_Error( 'unrecognized-username');
  188. endif;
  189. if ( $password_hashed != true ) :
  190. if ( !wp_check_password( $password, $user->user_pass ) ) :
  191. tj_error_message( 'The password you entered is incorrect', 'login' );
  192. return new WP_Error('incorrect-password');
  193. endif;
  194. else :
  195. if ( $password != $user->user_pass ) :
  196. tj_error_message( 'The password you entered is incorrect', 'login' );
  197. return new WP_Error('incorrect-password');
  198. endif;
  199. endif;
  200. wp_set_auth_cookie( $user->ID, $remember );
  201. do_action( 'tja_log_user_in', $user);
  202. if ( $redirect_to == 'referer' )
  203. $redirect_to = wp_get_referer();
  204. if ( $redirect_to )
  205. wp_redirect( apply_filters( 'tja_login_redirect', $redirect_to, $user ) );
  206. return true;
  207. }
  208. function tja_lost_password( $email ) {
  209. if( !get_user_by_email( $email ) ) {
  210. return new WP_Error('unrognized-email');
  211. }
  212. $_POST['user_email'] = $email;
  213. $_POST['user_login'] = $email;
  214. //grab the retrieve password function from wp-login.php
  215. ob_start();
  216. include_once( trailingslashit(ABSPATH) . 'wp-login.php' );
  217. ob_end_clean();
  218. add_filter( 'retrieve_password_message', 'tja_lost_password_email', 10, 2 );
  219. add_filter( 'wp_mail_content_type', 'wp_mail_content_type_html' );
  220. $errors = retrieve_password();
  221. if( !is_wp_error( $errors ) ) {
  222. return array( 'status' => 'success', 'text' => 'success' );
  223. }
  224. tj_error_message( 'There was an unknown error', 'lost-password' );
  225. return new WP_Error('unknown');
  226. }
  227. function tja_lost_password_email( $message, $key ) {
  228. $user = get_user_by_email(trim($_POST['user_login']));
  229. $reset_url = get_bloginfo( 'lost_password_url', 'display' ) . '?action=rp&key=' . $key . '&login=' . $user->user_login;
  230. if( file_exists( $file = get_stylesheet_directory() . '/email.lost-password.php' ) ) {
  231. ob_start();
  232. include( $file );
  233. $message = ob_get_contents();
  234. ob_end_clean();
  235. }
  236. return $message;
  237. }
  238. /**
  239. * Updates a users Information
  240. *
  241. * Can take a variety of arguments all in the form of a userInfo array.
  242. *
  243. * For starters you can pass any of the default wordpress user fields, you can also pass
  244. * an avatar to upload or an image url to use as an avatar.
  245. * You can also pass any amount of additonal fields which will be added to the
  246. * 'profile_info' usermeta.
  247. * Note this function does not do any stripping or sanitizing, all that should be done before the data gets here.
  248. *
  249. * @PARAM: (array) of user information
  250. * @RETURN: (string) success/error message
  251. * @AUTHOR: Tom Willmot
  252. * @VERSION: 1.0
  253. **/
  254. function tja_update_user_info( $info ) {
  255. // If an email was passed, check that it is valid
  256. if ( !preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $info['user_email'] ) && is_string( $info['user_email'] ) && strpos( $info['user_email'], 'apps+' ) !== 0 ) return '<p class="message error">Please enter a valid email address</p>';
  257. // If an ID wasn't passed then use the current user
  258. if ( !$info['ID'] ) :
  259. global $current_user;
  260. $info['ID'] = $current_user->ID;
  261. endif;
  262. if ( !$info['ID'] ) return false;
  263. // prepare the array for wp_update_user
  264. $userdata['ID'] = $info['ID'];
  265. if ( $info['user_email'] ) $userdata['user_email'] = $info['user_email'];
  266. if ( $info['display_name'] )$userdata['display_name'] = $info['display_name'];
  267. if ( $info['first_name'] )$userdata['first_name'] = $info['first_name'];
  268. if ( $info['last_name'] )$userdata['last_name'] = $info['last_name'];
  269. if ( $info['description'] )$userdata['description'] = $info['description'];
  270. if ( $info['user_pass'] ) $userdata['user_pass'] = $info['user_pass'];
  271. require_once( ABSPATH . 'wp-includes/registration.php' );
  272. $user_id = wp_update_user( $userdata );
  273. // User avatar
  274. if( $info['user_avatar'] ) {
  275. require_once(ABSPATH . 'wp-admin/includes/admin.php');
  276. $file = wp_handle_upload( $info['user_avatar'], array( 'test_form' => false ) );
  277. $info['user_avatar_path'] = $file['file'];
  278. unset( $info['user_avatar'] );
  279. }
  280. // Remove everything we have already used
  281. foreach ($info as $key => $inf) { if(is_string($inf) && $inf == '') $info[$key] = ' '; }
  282. $meta_info = array_diff( $info, $userdata );
  283. // Anything left gets added to usermeta as a seperate user-meta field
  284. if ( !empty( $meta_info ) ) :
  285. foreach( (array) $meta_info as $key => $value ) :
  286. update_usermeta( $info['ID'], $key, $value );
  287. endforeach;
  288. endif;
  289. return $user_id;
  290. }
  291. /**
  292. * tja_parse_user function.
  293. *
  294. * @access public
  295. * @param mixed $user. (default: null)
  296. * @return void
  297. */
  298. function tja_parse_user( $user = null ) {
  299. if ( is_object( $user ) && is_numeric( $user->ID ) ) return get_userdata( $user->ID );
  300. if ( is_object( $user ) && is_numeric( $user->user_id ) ) return get_userdata( $user->user_id );
  301. if ( is_array( $user ) && is_numeric( $user['ID'] ) ) return get_userdata( $user['ID'] );
  302. if ( is_numeric( $user ) ) return get_userdata( $user );
  303. if ( is_string( $user ) ) {
  304. if( strpos( $user, "@" ) > 0 && $user = get_user_by_email( $user ) )
  305. return $user;
  306. return get_userdatabylogin( $user );
  307. }
  308. if ( is_null( $user ) ) :
  309. global $current_user;
  310. return get_userdata( $current_user->ID );
  311. endif;
  312. }
  313. function tja_login_message() {
  314. if( !$_GET['message'] )
  315. return;
  316. echo '<p class="message error">' . tja_get_message( (int) $_GET['message'] ) . '</p>' . "\n";
  317. }
  318. function tja_register_message() {
  319. if( !$_GET['message'] )
  320. return;
  321. echo '<p class="message error">' . tja_get_message( (int) $_GET['message'] ) . '</p>' . "\n";
  322. }
  323. function tja_get_message( $code = null ) {
  324. if( $code === null ) $code = (int) $_GET['message'];
  325. $codes = tja_message_codes();
  326. return $codes[$code];
  327. }
  328. function tja_get_the_message() {
  329. if( !$_GET['message'] )
  330. return;
  331. echo '<p class="message error">' . tja_get_message( (int) $_GET['message'] ) . '</p>' . "\n";
  332. }
  333. function tja_message_codes() {
  334. $codes = array();
  335. $codes[101] = 'You are already logged in.';
  336. $codes[102] = 'Please enter a username.';
  337. $codes[103] = 'The username you entered has not been recognised.';
  338. $codes[104] = 'The password you entered is incorrect.';
  339. $codes[105] = 'Successfully logged in';
  340. $codes[200] = 'Successfully registered';
  341. $codes[201] = 'You are already logged in.';
  342. $codes[202] = 'Sorry, that username already exists.';
  343. $codes[203] = 'The passwords you entered do not match.';
  344. $codes[204] = 'The email address you entered is not valid';
  345. $codes[205] = 'The email address you entered is already in use.';
  346. $codes[206] = 'You have been sent an activation email, please follow the link in the email.';
  347. $codes[300] = 'You have been emailed a link to reset yoru password, please check your email.';
  348. $codes[301] = 'The email address you entered was not recognized';
  349. $codes[302] = 'There was a problem, please contact the site administrator';
  350. $codes[400] = 'Successfully updated your profile.';
  351. return apply_filters( 'tja_message_codes', $codes );
  352. }
  353. //url functions
  354. function tja_get_user_url( $authordata = null ) {
  355. if( !$authordata ) global $authordata;
  356. $authordata = tja_parse_user( $authordata );
  357. return get_bloginfo('url') . '/users/' . $authordata->user_login . '/';
  358. }
  359. //get user functions
  360. function tja_get_avatar( $user, $width, $height, $crop = true, $try_normal = true ) {
  361. if( tja_is_facebook_user( $user ) ) {
  362. return tja_get_facebook_avatar( $user, $width, $height );
  363. }
  364. elseif( $avatar = tja_get_avatar_upload( $user, $width, $height, $crop ) ) {
  365. return $avatar;
  366. }
  367. elseif( $try_normal === true ) {
  368. preg_match( '/src=\'([^\']*)/', get_avatar( $user->user_email, $width ), $matches );
  369. return $matches[1];
  370. }
  371. }
  372. function tja_get_avatar_upload( $user, $w, $h, $c ) {
  373. if( $user->user_avatar_path )
  374. return tj_phpthumb_it( $user->user_avatar_path, $w, $h, $c );
  375. }
  376. /**
  377. * Checks if a given user is a facebook user
  378. *
  379. * @param object $user
  380. * @return bool
  381. */
  382. function tja_is_facebook_user( $user ) {
  383. return (bool) $user->fbuid;
  384. }
  385. ?>