Skip to content

Instantly share code, notes, and snippets.

@imath

imath/1.02.patch Secret

Created Jul 3, 2015
Embed
What would you like to do?
bp-xprofile-field-for-member-types : Alternative patch to get along with 5192.bp_xprofile_get_hidden_fields_for_user.patch one. see https://github.com/lmoffereins/bp-xprofile-field-for-member-types/issues/1 & https://buddypress.trac.wordpress.org/ticket/5192
diff --git bp-xprofile-field-for-member-types.php bp-xprofile-field-for-member-types.php
index 6926728..c129368 100644
--- bp-xprofile-field-for-member-types.php
+++ bp-xprofile-field-for-member-types.php
@@ -115,6 +115,12 @@ final class BP_XProfile_Field_For_Member_Types {
// Admin: Profile Fields
add_action( 'xprofile_admin_field_name_legend', array( $this, 'admin_field_legend' ) );
+ // Custom registration page ?
+ add_action( 'bp_screens', array( $this, 'signup_for_member_type' ), 9 );
+
+ // Save the member type for a signup if needed
+ add_action( 'bp_core_activated_user', array( $this, 'signup_set_member_type' ), 10, 3 );
+
// Fire plugin loaded hook
do_action( 'bp_xprofile_field_for_member_types_loaded' );
}
@@ -169,6 +175,18 @@ final class BP_XProfile_Field_For_Member_Types {
public function filter_hidden_fields( $hidden_fields, $displayed_user_id, $current_user_id ) {
global $wpdb, $bp;
+ $current_screen = false;
+
+ // get_current_screen() is only available in Admin
+ if ( is_admin() ) {
+ $current_screen = get_current_screen();
+ }
+
+ // Make sure all fields are listed within the xProfile fields administration screen
+ if ( isset( $current_screen->id ) && false !== strpos( $current_screen->id, 'users_page_bp-profile-setup' ) ) {
+ return $hidden_fields;
+ }
+
// Hidden = All - Visible for displayed user AND current user
$all_fields = array_map( 'intval', (array) $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" ) );
@@ -207,8 +225,9 @@ final class BP_XProfile_Field_For_Member_Types {
}
// The primary field is for all, so bail
- if ( 1 === (int) $field_id )
+ if ( 1 === (int) $field_id ) {
return true;
+ }
// Default to displayed user
if ( ! is_numeric( $user_id ) ) {
@@ -218,8 +237,12 @@ final class BP_XProfile_Field_For_Member_Types {
// Get the field's member types
if ( $member_types = $this->get_xprofile_member_types( $field_id, 'field' ) ) {
+ // Specific Registration page eg: site.url/register/member_type_slug
+ if ( empty( $user_id ) && bp_get_current_member_type() ) {
+ $u_member_types = array( bp_get_current_member_type() );
+
// Default to 'none' when the user has no member type(s)
- if ( ! $u_member_types = bp_get_member_type( $user_id, false ) ) {
+ } elseif ( ! $u_member_types = bp_get_member_type( $user_id, false ) ) {
$u_member_types = array( 'none' );
}
@@ -416,6 +439,65 @@ final class BP_XProfile_Field_For_Member_Types {
// Output legend <span>
echo '<span class="member-types">(' . $legend . ')</span>';
}
+
+ /**
+ * Checks if the registration form is a specific one
+ * eg: site.url/register/member_type_slug
+ *
+ * @since ?
+ */
+ public function signup_for_member_type() {
+ if ( ! bp_is_register_page() || ! bp_current_action() ) {
+ return;
+ }
+
+ // Get BuddyPress instance
+ $bp = buddypress();
+
+ // Is the current action a member type slug ?
+ $matched_types = bp_get_member_types( array(
+ 'has_directory' => true,
+ 'directory_slug' => bp_current_action(),
+ ) );
+
+ if ( ! empty( $matched_types ) ) {
+ // Set the current member type
+ $bp->current_member_type = reset( $matched_types );
+
+ // Reset the current action to make sure the form is showing
+ $bp->current_action = '';
+
+ // Filter the signup usermeta to set the member type once the
+ // User has activated his account
+ add_filter( 'bp_signup_usermeta', array( $this, 'signup_usermeta' ), 10, 1 );
+ }
+ }
+
+ /**
+ * Add a signup usermeta containing the member type for the future user
+ *
+ * @since ?
+ */
+ public function signup_usermeta( $usermeta = array() ) {
+ if ( bp_get_current_member_type() ) {
+ $usermeta['bp_member_type'] = bp_get_current_member_type();
+ }
+
+ return $usermeta;
+ }
+
+ /**
+ * Set the member type for the activated account
+ *
+ * @since ?
+ */
+ public function signup_set_member_type( $user_id = 0, $key = '', $user = array() ) {
+ if ( empty( $user_id ) || empty( $user['meta']['bp_member_type'] ) ) {
+ return;
+ }
+
+ bp_set_member_type( $user_id, sanitize_key( $user['meta']['bp_member_type'] ) );
+ }
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment