Skip to:
Content

BuddyPress.org

Ticket #5379: bp-members-screens.php

File bp-members-screens.php, 19.7 KB (added by terraling, 12 years ago)
Line 
1<?php
2
3/**
4 * BuddyPress Member Screens
5 *
6 * Handlers for member screens that aren't handled elsewhere
7 *
8 * @package BuddyPress
9 * @subpackage MembersScreens
10 */
11
12// Exit if accessed directly
13if ( !defined( 'ABSPATH' ) ) exit;
14
15/**
16 * Handles the display of the profile page by loading the correct template file.
17 *
18 * @package BuddyPress Members
19 * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
20 */
21function bp_members_screen_display_profile() {
22 do_action( 'bp_members_screen_display_profile' );
23 bp_core_load_template( apply_filters( 'bp_members_screen_display_profile', 'members/single/home' ) );
24}
25
26/**
27 * Handles the display of the members directory index
28 *
29 * @global object $bp
30 *
31 * @uses bp_is_user()
32 * @uses bp_is_current_component()
33 * @uses do_action()
34 * @uses bp_core_load_template()
35 * @uses apply_filters()
36 */
37function bp_members_screen_index() {
38 if ( !bp_is_user() && bp_is_members_component() ) {
39 bp_update_is_directory( true, 'members' );
40
41 do_action( 'bp_members_screen_index' );
42
43 bp_core_load_template( apply_filters( 'bp_members_screen_index', 'members/index' ) );
44 }
45}
46add_action( 'bp_screens', 'bp_members_screen_index' );
47
48
49function bp_core_screen_signup() {
50 global $bp;
51
52 if ( !bp_is_current_component( 'register' ) )
53 return;
54
55 // Not a directory
56 bp_update_is_directory( false, 'register' );
57
58 // If the user is logged in, redirect away from here
59 if ( is_user_logged_in() ) {
60 if ( bp_is_component_front_page( 'register' ) )
61 $redirect_to = trailingslashit( bp_get_root_domain() . '/' . bp_get_members_root_slug() );
62 else
63 $redirect_to = bp_get_root_domain();
64
65 bp_core_redirect( apply_filters( 'bp_loggedin_register_page_redirect_to', $redirect_to ) );
66
67 return;
68 }
69
70 if ( !isset( $bp->signup ) ) {
71 $bp->signup = new stdClass;
72 }
73
74 $bp->signup->step = 'request-details';
75
76 if ( !bp_get_signup_allowed() ) {
77 $bp->signup->step = 'registration-disabled';
78
79 // If the signup page is submitted, validate and save
80 } elseif ( isset( $_POST['signup_submit'] ) && bp_verify_nonce_request( 'bp_new_signup' ) ) {
81
82 do_action( 'bp_signup_prevalidate' );
83
84 // Check the base account details for problems
85 $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );
86
87 // If there are errors with account details, set them for display
88 if ( !empty( $account_details['errors']->errors['user_name'] ) )
89 $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];
90
91 if ( !empty( $account_details['errors']->errors['user_email'] ) )
92 $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];
93
94 // Check that both password fields are filled in
95 if ( empty( $_POST['signup_password'] ) || empty( $_POST['signup_password_confirm'] ) )
96 $bp->signup->errors['signup_password'] = __( 'Please make sure you enter your password twice', 'buddypress' );
97
98 // Check that the passwords match
99 if ( ( !empty( $_POST['signup_password'] ) && !empty( $_POST['signup_password_confirm'] ) ) && $_POST['signup_password'] != $_POST['signup_password_confirm'] )
100 $bp->signup->errors['signup_password'] = __( 'The passwords you entered do not match.', 'buddypress' );
101
102 $bp->signup->username = $_POST['signup_username'];
103 $bp->signup->email = $_POST['signup_email'];
104
105 // Now we've checked account details, we can check profile information
106 if ( bp_is_active( 'xprofile' ) ) {
107
108 // Make sure hidden field is passed and populated
109 if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) {
110
111 // Let's compact any profile field info into an array
112 $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
113
114 // Loop through the posted fields formatting any datebox values then validate the field
115 foreach ( (array) $profile_field_ids as $field_id ) {
116 if ( !isset( $_POST['field_' . $field_id] ) ) {
117 if ( !empty( $_POST['field_' . $field_id . '_day'] ) && !empty( $_POST['field_' . $field_id . '_month'] ) && !empty( $_POST['field_' . $field_id . '_year'] ) )
118 $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $_POST['field_' . $field_id . '_day'] . $_POST['field_' . $field_id . '_month'] . $_POST['field_' . $field_id . '_year'] ) );
119 }
120
121 // Create errors for required fields without values
122 if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST['field_' . $field_id] ) )
123 $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' );
124 }
125
126 // This situation doesn't naturally occur so bounce to website root
127 } else {
128 bp_core_redirect( bp_get_root_domain() );
129 }
130 }
131
132 // Finally, let's check the blog details, if the user wants a blog and blog creation is enabled
133 if ( isset( $_POST['signup_with_blog'] ) ) {
134 $active_signup = $bp->site_options['registration'];
135
136 if ( 'blog' == $active_signup || 'all' == $active_signup ) {
137 $blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] );
138
139 // If there are errors with blog details, set them for display
140 if ( !empty( $blog_details['errors']->errors['blogname'] ) )
141 $bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0];
142
143 if ( !empty( $blog_details['errors']->errors['blog_title'] ) )
144 $bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0];
145 }
146 }
147
148 do_action( 'bp_signup_validate' );
149
150 // Add any errors to the action for the field in the template for display.
151 if ( !empty( $bp->signup->errors ) ) {
152 foreach ( (array) $bp->signup->errors as $fieldname => $error_message ) {
153 // addslashes() and stripslashes() to avoid create_function()
154 // syntax errors when the $error_message contains quotes
155 add_action( 'bp_' . $fieldname . '_errors', create_function( '', 'echo apply_filters(\'bp_members_signup_error_message\', "<div class=\"error\">" . stripslashes( \'' . addslashes( $error_message ) . '\' ) . "</div>" );' ) );
156 }
157 } else {
158 $bp->signup->step = 'save-details';
159
160 // No errors! Let's register those deets.
161 $active_signup = !empty( $bp->site_options['registration'] ) ? $bp->site_options['registration'] : '';
162
163 if ( 'none' != $active_signup ) {
164
165 // Make sure the extended profiles module is enabled
166 if ( bp_is_active( 'xprofile' ) ) {
167 // Let's compact any profile field info into usermeta
168 $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
169
170 // Loop through the posted fields formatting any datebox values then add to usermeta - @todo This logic should be shared with the same in xprofile_screen_edit_profile()
171 foreach ( (array) $profile_field_ids as $field_id ) {
172 if ( ! isset( $_POST['field_' . $field_id] ) ) {
173
174 if ( ! empty( $_POST['field_' . $field_id . '_day'] ) && ! empty( $_POST['field_' . $field_id . '_month'] ) && ! empty( $_POST['field_' . $field_id . '_year'] ) ) {
175 // Concatenate the values
176 $date_value = $_POST['field_' . $field_id . '_day'] . ' ' . $_POST['field_' . $field_id . '_month'] . ' ' . $_POST['field_' . $field_id . '_year'];
177
178 // Turn the concatenated value into a timestamp
179 $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) );
180 }
181 }
182
183 if ( !empty( $_POST['field_' . $field_id] ) )
184 $usermeta['field_' . $field_id] = $_POST['field_' . $field_id];
185
186 if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) )
187 $usermeta['field_' . $field_id . '_visibility'] = $_POST['field_' . $field_id . '_visibility'];
188 }
189
190 // Store the profile field ID's in usermeta
191 $usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids'];
192 }
193
194 // Hash and store the password
195 $usermeta['password'] = wp_hash_password( $_POST['signup_password'] );
196
197 // If the user decided to create a blog, save those details to usermeta
198 if ( 'blog' == $active_signup || 'all' == $active_signup )
199 $usermeta['public'] = ( isset( $_POST['signup_blog_privacy'] ) && 'public' == $_POST['signup_blog_privacy'] ) ? true : false;
200
201 $usermeta = apply_filters( 'bp_signup_usermeta', $usermeta );
202
203 // Finally, sign up the user and/or blog
204 if ( isset( $_POST['signup_with_blog'] ) && is_multisite() )
205 $wp_user_id = bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta );
206 else
207 $wp_user_id = bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta );
208
209 if ( is_wp_error( $wp_user_id ) ) {
210 $bp->signup->step = 'request-details';
211 bp_core_add_message( $wp_user_id->get_error_message(), 'error' );
212 } else {
213 $bp->signup->step = 'completed-confirmation';
214 }
215 }
216
217 do_action( 'bp_complete_signup' );
218 }
219
220 }
221
222 do_action( 'bp_core_screen_signup' );
223 bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) );
224}
225add_action( 'bp_screens', 'bp_core_screen_signup' );
226
227function bp_core_screen_activation() {
228 global $bp;
229
230 if ( !bp_is_current_component( 'activate' ) )
231 return false;
232
233 // If the user is logged in, redirect away from here
234 if ( is_user_logged_in() ) {
235 if ( bp_is_component_front_page( 'activate' ) ) {
236 $redirect_to = trailingslashit( bp_get_root_domain() . '/' . bp_get_members_root_slug() );
237 } else {
238 $redirect_to = trailingslashit( bp_get_root_domain() );
239 }
240
241 bp_core_redirect( apply_filters( 'bp_loggedin_activate_page_redirect_to', $redirect_to ) );
242
243 return;
244 }
245
246 // Check if an activation key has been passed
247 if ( isset( $_GET['key'] ) ) {
248
249 // Activate the signup
250 $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $_GET['key'] ) );
251
252 // If there were errors, add a message and redirect
253 if ( !empty( $user->errors ) ) {
254 bp_core_add_message( $user->get_error_message(), 'error' );
255 bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) );
256 }
257
258 // Check for an uploaded avatar and move that to the correct user folder
259 if ( is_multisite() )
260 $hashed_key = wp_hash( $_GET['key'] );
261 else
262 $hashed_key = wp_hash( $user );
263
264 // Check if the avatar folder exists. If it does, move rename it, move
265 // it and delete the signup avatar dir
266 if ( file_exists( bp_core_avatar_upload_path() . '/avatars/signups/' . $hashed_key ) )
267 @rename( bp_core_avatar_upload_path() . '/avatars/signups/' . $hashed_key, bp_core_avatar_upload_path() . '/avatars/' . $user );
268
269 bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
270
271 $bp->activation_complete = true;
272 }
273
274 bp_core_load_template( apply_filters( 'bp_core_template_activate', array( 'activate', 'registration/activate' ) ) );
275}
276add_action( 'bp_screens', 'bp_core_screen_activation' );
277
278/** Theme Compatability *******************************************************/
279
280/**
281 * The main theme compat class for BuddyPress Members.
282 *
283 * This class sets up the necessary theme compatability actions to safely output
284 * member template parts to the_title and the_content areas of a theme.
285 *
286 * @since BuddyPress (1.7)
287 */
288class BP_Members_Theme_Compat {
289
290 /**
291 * Setup the members component theme compatibility
292 *
293 * @since BuddyPress (1.7)
294 */
295 public function __construct() {
296 add_action( 'bp_setup_theme_compat', array( $this, 'is_members' ) );
297 }
298
299 /**
300 * Are we looking at something that needs members theme compatability?
301 *
302 * @since BuddyPress (1.7)
303 */
304 public function is_members() {
305
306 // Bail if not looking at the members component or a user's page
307 if ( ! bp_is_members_component() && ! bp_is_user() )
308 return;
309
310 // Members Directory
311 if ( ! bp_current_action() && ! bp_current_item() ) {
312 bp_update_is_directory( true, 'members' );
313
314 do_action( 'bp_members_screen_index' );
315
316 add_filter( 'bp_get_buddypress_template', array( $this, 'directory_template_hierarchy' ) );
317 add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'directory_dummy_post' ) );
318 add_filter( 'bp_replace_the_content', array( $this, 'directory_content' ) );
319
320 // User page
321 } elseif ( bp_is_user() ) {
322 // If we're on a single activity permalink page, we shouldn't use the members
323 // template, so stop here!
324 if ( bp_is_active( 'activity' ) && bp_is_single_activity() )
325 return;
326
327 do_action( 'bp_members_screen_display_profile' );
328
329 add_filter( 'bp_get_buddypress_template', array( $this, 'single_template_hierarchy' ) );
330 add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'single_dummy_post' ) );
331 add_filter( 'bp_replace_the_content', array( $this, 'single_dummy_content' ) );
332
333 }
334 }
335
336 /** Directory *************************************************************/
337
338 /**
339 * Add template hierarchy to theme compat for the members directory page.
340 *
341 * This is to mirror how WordPress has {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}.
342 *
343 * @since BuddyPress (1.8)
344 *
345 * @param string $templates The templates from bp_get_theme_compat_templates()
346 * @return array $templates Array of custom templates to look for.
347 */
348 public function directory_template_hierarchy( $templates = array() ) {
349
350 // Setup our templates based on priority
351 $new_templates = apply_filters( 'bp_template_hierarchy_members_directory', array(
352 'members/index-directory.php'
353 ) );
354
355 // Merge new templates with existing stack
356 // @see bp_get_theme_compat_templates()
357 $templates = array_merge( (array) $new_templates, $templates );
358
359 return $templates;
360 }
361
362 /**
363 * Update the global $post with directory data
364 *
365 * @since BuddyPress (1.7)
366 */
367 public function directory_dummy_post() {
368 bp_theme_compat_reset_post( array(
369 'ID' => 0,
370 'post_title' => __( 'Members', 'buddypress' ),
371 'post_author' => 0,
372 'post_date' => 0,
373 'post_content' => '',
374 'post_type' => 'bp_members',
375 'post_status' => 'publish',
376 'is_archive' => true,
377 'comment_status' => 'closed'
378 ) );
379 }
380
381 /**
382 * Filter the_content with the members index template part
383 *
384 * @since BuddyPress (1.7)
385 */
386 public function directory_content() {
387 return bp_buffer_template_part( 'members/index', null, false );
388 }
389
390 /** Single ****************************************************************/
391
392 /**
393 * Add custom template hierarchy to theme compat for member pages.
394 *
395 * This is to mirror how WordPress has {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}.
396 *
397 * @since BuddyPress (1.8)
398 *
399 * @param string $templates The templates from bp_get_theme_compat_templates()
400 * @return array $templates Array of custom templates to look for.
401 */
402 public function single_template_hierarchy( $templates ) {
403 // Setup some variables we're going to reference in our custom templates
404 $user_nicename = buddypress()->displayed_user->userdata->user_nicename;
405
406 // Setup our templates based on priority
407 $new_templates = apply_filters( 'bp_template_hierarchy_members_single_item', array(
408 'members/single/index-id-' . sanitize_file_name( bp_displayed_user_id() ) . '.php',
409 'members/single/index-nicename-' . sanitize_file_name( $user_nicename ) . '.php',
410 'members/single/index-action-' . sanitize_file_name( bp_current_action() ) . '.php',
411 'members/single/index-component-' . sanitize_file_name( bp_current_component() ) . '.php',
412 'members/single/index.php'
413 ) );
414
415 // Merge new templates with existing stack
416 // @see bp_get_theme_compat_templates()
417 $templates = array_merge( (array) $new_templates, $templates );
418
419 return $templates;
420 }
421
422 /**
423 * Update the global $post with the displayed user's data
424 *
425 * @since BuddyPress (1.7)
426 */
427 public function single_dummy_post() {
428 bp_theme_compat_reset_post( array(
429 'ID' => 0,
430 'post_title' => '<a href="' . bp_get_displayed_user_link() . '">' . bp_get_displayed_user_fullname() . '</a>',
431 'post_author' => 0,
432 'post_date' => 0,
433 'post_content' => '',
434 'post_type' => 'bp_members',
435 'post_status' => 'publish',
436 'is_archive' => true,
437 'comment_status' => 'closed'
438 ) );
439 }
440
441 /**
442 * Filter the_content with the members' single home template part
443 *
444 * @since BuddyPress (1.7)
445 */
446 public function single_dummy_content() {
447 return bp_buffer_template_part( 'members/single/home', null, false );
448 }
449}
450new BP_Members_Theme_Compat();
451
452/**
453 * The main theme compat class for BuddyPress Registration.
454 *
455 * This class sets up the necessary theme compatability actions to safely output
456 * registration template parts to the_title and the_content areas of a theme.
457 *
458 * @since BuddyPress (1.7)
459 */
460class BP_Registration_Theme_Compat {
461
462 /**
463 * Setup the groups component theme compatibility
464 *
465 * @since BuddyPress (1.7)
466 */
467 public function __construct() {
468 add_action( 'bp_setup_theme_compat', array( $this, 'is_registration' ) );
469 }
470
471 /**
472 * Are we looking at either the registration or activation pages?
473 *
474 * @since BuddyPress (1.7)
475 */
476 public function is_registration() {
477
478 // Bail if not looking at the registration or activation page
479 if ( ! bp_is_register_page() && ! bp_is_activation_page() ) {
480 return;
481 }
482
483 // Not a directory
484 bp_update_is_directory( false, 'register' );
485
486 // Setup actions
487 add_filter( 'bp_get_buddypress_template', array( $this, 'template_hierarchy' ) );
488 add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'dummy_post' ) );
489 add_filter( 'bp_replace_the_content', array( $this, 'dummy_content' ) );
490 }
491
492 /** Template ***********************************************************/
493
494 /**
495 * Add template hierarchy to theme compat for registration / activation pages.
496 *
497 * This is to mirror how WordPress has {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}.
498 *
499 * @since BuddyPress (1.8)
500 *
501 * @param string $templates The templates from bp_get_theme_compat_templates()
502 * @return array $templates Array of custom templates to look for.
503 */
504 public function template_hierarchy( $templates ) {
505 $component = sanitize_file_name( bp_current_component() );
506
507 // Setup our templates based on priority
508 $new_templates = apply_filters( "bp_template_hierarchy_{$component}", array(
509 "members/index-{$component}.php"
510 ) );
511
512 // Merge new templates with existing stack
513 // @see bp_get_theme_compat_templates()
514 $templates = array_merge( (array) $new_templates, $templates );
515
516 return $templates;
517 }
518
519 /**
520 * Update the global $post with dummy data
521 *
522 * @since BuddyPress (1.7)
523 */
524 public function dummy_post() {
525 // Registration page
526 if ( bp_is_register_page() ) {
527 $title = __( 'Create an Account', 'buddypress' );
528
529 if ( 'completed-confirmation' == bp_get_current_signup_step() ) {
530 $title = __( 'Check Your Email To Activate Your Account!', 'buddypress' );
531 }
532
533 // Activation page
534 } else {
535 $title = __( 'Activate your Account', 'buddypress' );
536
537 if ( bp_account_was_activated() ) {
538 $title = __( 'Account Activated', 'buddypress' );
539 }
540 }
541
542 $post_type = bp_is_register_page() ? 'bp_register' : 'bp_activate';
543
544 bp_theme_compat_reset_post( array(
545 'ID' => 0,
546 'post_title' => $title,
547 'post_author' => 0,
548 'post_date' => 0,
549 'post_content' => '',
550 'post_type' => $post_type,
551 'post_status' => 'publish',
552 'is_archive' => true,
553 'comment_status' => 'closed'
554 ) );
555 }
556
557 /**
558 * Filter the_content with either the register or activate templates.
559 *
560 * @since BuddyPress (1.7)
561 */
562 public function dummy_content() {
563 if ( bp_is_register_page() ) {
564 return bp_buffer_template_part( 'members/register', null, false );
565 } else {
566 return bp_buffer_template_part( 'members/activate', null, false );
567 }
568 }
569}
570new BP_Registration_Theme_Compat();