/bp-members/bp-members-screens.php

  1. <?php 
  2. /** 
  3. * BuddyPress Member Screens. 
  4. * 
  5. * Handlers for member screens that aren't handled elsewhere. 
  6. * 
  7. * @package BuddyPress 
  8. * @subpackage MembersScreens 
  9. * @since 1.5.0 
  10. */ 
  11.  
  12. // Exit if accessed directly. 
  13. defined( 'ABSPATH' ) || exit; 
  14.  
  15. /** 
  16. * Handle the display of the profile page by loading the correct template file. 
  17. * 
  18. * @since 1.5.0 
  19. */ 
  20. function bp_members_screen_display_profile() { 
  21.  
  22. /** 
  23. * Fires right before the loading of the Member profile screen template file. 
  24. * 
  25. * @since 1.5.0 
  26. */ 
  27. do_action( 'bp_members_screen_display_profile' ); 
  28.  
  29. /** 
  30. * Filters the template to load for the Member profile page screen. 
  31. * 
  32. * @since 1.5.0 
  33. * 
  34. * @param string $template Path to the Member template to load. 
  35. */ 
  36. bp_core_load_template( apply_filters( 'bp_members_screen_display_profile', 'members/single/home' ) ); 
  37.  
  38. /** 
  39. * Handle the display of the members directory index. 
  40. * 
  41. * @since 1.5.0 
  42. */ 
  43. function bp_members_screen_index() { 
  44. if ( bp_is_members_directory() ) { 
  45. bp_update_is_directory( true, 'members' ); 
  46.  
  47. /** 
  48. * Fires right before the loading of the Member directory index screen template file. 
  49. * 
  50. * @since 1.5.0 
  51. */ 
  52. do_action( 'bp_members_screen_index' ); 
  53.  
  54. /** 
  55. * Filters the template to load for the Member directory page screen. 
  56. * 
  57. * @since 1.5.0 
  58. * 
  59. * @param string $value Path to the member directory template to load. 
  60. */ 
  61. bp_core_load_template( apply_filters( 'bp_members_screen_index', 'members/index' ) ); 
  62. add_action( 'bp_screens', 'bp_members_screen_index' ); 
  63.  
  64. /** 
  65. * Handle the loading of the signup screen. 
  66. * 
  67. * @since 1.1.0 
  68. */ 
  69. function bp_core_screen_signup() { 
  70. $bp = buddypress(); 
  71.  
  72. if ( ! bp_is_current_component( 'register' ) || bp_current_action() ) 
  73. return; 
  74.  
  75. // Not a directory. 
  76. bp_update_is_directory( false, 'register' ); 
  77.  
  78. // If the user is logged in, redirect away from here. 
  79. if ( is_user_logged_in() ) { 
  80.  
  81. $redirect_to = bp_is_component_front_page( 'register' ) 
  82. ? bp_get_members_directory_permalink() 
  83. : bp_get_root_domain(); 
  84.  
  85. /** 
  86. * Filters the URL to redirect logged in users to when visiting registration page. 
  87. * 
  88. * @since 1.5.1 
  89. * 
  90. * @param string $redirect_to URL to redirect user to. 
  91. */ 
  92. bp_core_redirect( apply_filters( 'bp_loggedin_register_page_redirect_to', $redirect_to ) ); 
  93.  
  94. return; 
  95.  
  96. $bp->signup->step = 'request-details'; 
  97.  
  98. if ( !bp_get_signup_allowed() ) { 
  99. $bp->signup->step = 'registration-disabled'; 
  100.  
  101. // If the signup page is submitted, validate and save. 
  102. } elseif ( isset( $_POST['signup_submit'] ) && bp_verify_nonce_request( 'bp_new_signup' ) ) { 
  103.  
  104. /** 
  105. * Fires before the validation of a new signup. 
  106. * 
  107. * @since 2.0.0 
  108. */ 
  109. do_action( 'bp_signup_pre_validate' ); 
  110.  
  111. // Check the base account details for problems. 
  112. $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] ); 
  113.  
  114. // If there are errors with account details, set them for display. 
  115. if ( !empty( $account_details['errors']->errors['user_name'] ) ) 
  116. $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0]; 
  117.  
  118. if ( !empty( $account_details['errors']->errors['user_email'] ) ) 
  119. $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0]; 
  120.  
  121. // Check that both password fields are filled in. 
  122. if ( empty( $_POST['signup_password'] ) || empty( $_POST['signup_password_confirm'] ) ) 
  123. $bp->signup->errors['signup_password'] = __( 'Please make sure you enter your password twice', 'buddypress' ); 
  124.  
  125. // Check that the passwords match. 
  126. if ( ( !empty( $_POST['signup_password'] ) && !empty( $_POST['signup_password_confirm'] ) ) && $_POST['signup_password'] != $_POST['signup_password_confirm'] ) 
  127. $bp->signup->errors['signup_password'] = __( 'The passwords you entered do not match.', 'buddypress' ); 
  128.  
  129. $bp->signup->username = $_POST['signup_username']; 
  130. $bp->signup->email = $_POST['signup_email']; 
  131.  
  132. // Now we've checked account details, we can check profile information. 
  133. if ( bp_is_active( 'xprofile' ) ) { 
  134.  
  135. // Make sure hidden field is passed and populated. 
  136. if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) { 
  137.  
  138. // Let's compact any profile field info into an array. 
  139. $profile_field_ids = explode( ', ', $_POST['signup_profile_field_ids'] ); 
  140.  
  141. // Loop through the posted fields formatting any datebox values then validate the field. 
  142. foreach ( (array) $profile_field_ids as $field_id ) { 
  143. bp_xprofile_maybe_format_datebox_post_data( $field_id ); 
  144.  
  145. // Create errors for required fields without values. 
  146. if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST[ 'field_' . $field_id ] ) && ! bp_current_user_can( 'bp_moderate' ) ) 
  147. $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' ); 
  148.  
  149. // This situation doesn't naturally occur so bounce to website root. 
  150. } else { 
  151. bp_core_redirect( bp_get_root_domain() ); 
  152.  
  153. // Finally, let's check the blog details, if the user wants a blog and blog creation is enabled. 
  154. if ( isset( $_POST['signup_with_blog'] ) ) { 
  155. $active_signup = bp_core_get_root_option( 'registration' ); 
  156.  
  157. if ( 'blog' == $active_signup || 'all' == $active_signup ) { 
  158. $blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] ); 
  159.  
  160. // If there are errors with blog details, set them for display. 
  161. if ( !empty( $blog_details['errors']->errors['blogname'] ) ) 
  162. $bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0]; 
  163.  
  164. if ( !empty( $blog_details['errors']->errors['blog_title'] ) ) 
  165. $bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0]; 
  166.  
  167. /** 
  168. * Fires after the validation of a new signup. 
  169. * 
  170. * @since 1.1.0 
  171. */ 
  172. do_action( 'bp_signup_validate' ); 
  173.  
  174. // Add any errors to the action for the field in the template for display. 
  175. if ( !empty( $bp->signup->errors ) ) { 
  176. foreach ( (array) $bp->signup->errors as $fieldname => $error_message ) { 
  177. /** 
  178. * The addslashes() and stripslashes() used to avoid create_function() 
  179. * syntax errors when the $error_message contains quotes. 
  180. */ 
  181.  
  182. /** 
  183. * Filters the error message in the loop. 
  184. * 
  185. * @since 1.5.0 
  186. * 
  187. * @param string $value Error message wrapped in html. 
  188. */ 
  189. add_action( 'bp_' . $fieldname . '_errors', create_function( '', 'echo apply_filters(\'bp_members_signup_error_message\', "<div class=\"error\">" . stripslashes( \'' . addslashes( $error_message ) . '\' ) . "</div>" );' ) ); 
  190. } else { 
  191. $bp->signup->step = 'save-details'; 
  192.  
  193. // No errors! Let's register those deets. 
  194. $active_signup = bp_core_get_root_option( 'registration' ); 
  195.  
  196. if ( 'none' != $active_signup ) { 
  197.  
  198. // Make sure the extended profiles module is enabled. 
  199. if ( bp_is_active( 'xprofile' ) ) { 
  200. // Let's compact any profile field info into usermeta. 
  201. $profile_field_ids = explode( ', ', $_POST['signup_profile_field_ids'] ); 
  202.  
  203. /** 
  204. * Loop through the posted fields, formatting any 
  205. * datebox values, then add to usermeta. 
  206. */ 
  207. foreach ( (array) $profile_field_ids as $field_id ) { 
  208. bp_xprofile_maybe_format_datebox_post_data( $field_id ); 
  209.  
  210. if ( !empty( $_POST['field_' . $field_id] ) ) 
  211. $usermeta['field_' . $field_id] = $_POST['field_' . $field_id]; 
  212.  
  213. if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) ) 
  214. $usermeta['field_' . $field_id . '_visibility'] = $_POST['field_' . $field_id . '_visibility']; 
  215.  
  216. // Store the profile field ID's in usermeta. 
  217. $usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids']; 
  218.  
  219. // Hash and store the password. 
  220. $usermeta['password'] = wp_hash_password( $_POST['signup_password'] ); 
  221.  
  222. // If the user decided to create a blog, save those details to usermeta. 
  223. if ( 'blog' == $active_signup || 'all' == $active_signup ) 
  224. $usermeta['public'] = ( isset( $_POST['signup_blog_privacy'] ) && 'public' == $_POST['signup_blog_privacy'] ) ? true : false; 
  225.  
  226. /** 
  227. * Filters the user meta used for signup. 
  228. * 
  229. * @since 1.1.0 
  230. * 
  231. * @param array $usermeta Array of user meta to add to signup. 
  232. */ 
  233. $usermeta = apply_filters( 'bp_signup_usermeta', $usermeta ); 
  234.  
  235. // Finally, sign up the user and/or blog. 
  236. if ( isset( $_POST['signup_with_blog'] ) && is_multisite() ) 
  237. $wp_user_id = bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta ); 
  238. else 
  239. $wp_user_id = bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta ); 
  240.  
  241. if ( is_wp_error( $wp_user_id ) ) { 
  242. $bp->signup->step = 'request-details'; 
  243. bp_core_add_message( $wp_user_id->get_error_message(), 'error' ); 
  244. } else { 
  245. $bp->signup->step = 'completed-confirmation'; 
  246.  
  247. /** 
  248. * Fires after the completion of a new signup. 
  249. * 
  250. * @since 1.1.0 
  251. */ 
  252. do_action( 'bp_complete_signup' ); 
  253.  
  254.  
  255. /** 
  256. * Fires right before the loading of the Member registration screen template file. 
  257. * 
  258. * @since 1.5.0 
  259. */ 
  260. do_action( 'bp_core_screen_signup' ); 
  261.  
  262. /** 
  263. * Filters the template to load for the Member registration page screen. 
  264. * 
  265. * @since 1.5.0 
  266. * 
  267. * @param string $value Path to the Member registration template to load. 
  268. */ 
  269. bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) ); 
  270. add_action( 'bp_screens', 'bp_core_screen_signup' ); 
  271.  
  272. /** 
  273. * Handle the loading of the Activate screen. 
  274. * 
  275. * @since 1.1.0 
  276. * 
  277. * @todo Move the actual activation process into an action in bp-members-actions.php 
  278. */ 
  279. function bp_core_screen_activation() { 
  280.  
  281. // Bail if not viewing the activation page. 
  282. if ( ! bp_is_current_component( 'activate' ) ) { 
  283. return false; 
  284.  
  285. // If the user is already logged in, redirect away from here. 
  286. if ( is_user_logged_in() ) { 
  287.  
  288. // If activation page is also front page, set to members directory to 
  289. // avoid an infinite loop. Otherwise, set to root domain. 
  290. $redirect_to = bp_is_component_front_page( 'activate' ) 
  291. ? bp_get_members_directory_permalink() 
  292. : bp_get_root_domain(); 
  293.  
  294. // Trailing slash it, as we expect these URL's to be. 
  295. $redirect_to = trailingslashit( $redirect_to ); 
  296.  
  297. /** 
  298. * Filters the URL to redirect logged in users to when visiting activation page. 
  299. * 
  300. * @since 1.9.0 
  301. * 
  302. * @param string $redirect_to URL to redirect user to. 
  303. */ 
  304. $redirect_to = apply_filters( 'bp_loggedin_activate_page_redirect_to', $redirect_to ); 
  305.  
  306. // Redirect away from the activation page. 
  307. bp_core_redirect( $redirect_to ); 
  308.  
  309. // Grab the key (the old way). 
  310. $key = isset( $_GET['key'] ) ? $_GET['key'] : ''; 
  311.  
  312. // Grab the key (the new way). 
  313. if ( empty( $key ) ) { 
  314. $key = bp_current_action(); 
  315.  
  316. // Get BuddyPress. 
  317. $bp = buddypress(); 
  318.  
  319. // We've got a key; let's attempt to activate the signup. 
  320. if ( ! empty( $key ) ) { 
  321.  
  322. /** 
  323. * Filters the activation signup. 
  324. * 
  325. * @since 1.1.0 
  326. * 
  327. * @param bool|int $value Value returned by activation. 
  328. * Integer on success, boolean on failure. 
  329. */ 
  330. $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) ); 
  331.  
  332. // If there were errors, add a message and redirect. 
  333. if ( ! empty( $user->errors ) ) { 
  334. bp_core_add_message( $user->get_error_message(), 'error' ); 
  335. bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) ); 
  336.  
  337. bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) ); 
  338. $bp->activation_complete = true; 
  339.  
  340. /** 
  341. * Filters the template to load for the Member activation page screen. 
  342. * 
  343. * @since 1.1.1 
  344. * 
  345. * @param string $value Path to the Member activation template to load. 
  346. */ 
  347. bp_core_load_template( apply_filters( 'bp_core_template_activate', array( 'activate', 'registration/activate' ) ) ); 
  348. add_action( 'bp_screens', 'bp_core_screen_activation' ); 
  349.  
  350. /** Theme Compatibility *******************************************************/ 
  351.  
  352. new BP_Members_Theme_Compat(); 
  353. new BP_Registration_Theme_Compat(); 
.