/** * @group xprofile_set_field_data * @ticket BP5836 */ public function test_xprofile_sync_bp_profile_new_user() { $post_vars = $_POST; $_POST = array('user_login' => 'foobar', 'pass1' => 'password', 'pass2' => 'password', 'role' => 'subscriber', 'email' => '*****@*****.**', 'first_name' => 'Foo', 'last_name' => 'Bar'); $id = add_user(); $display_name = 'Bar Foo'; $_POST = array('display_name' => $display_name, 'email' => '*****@*****.**', 'nickname' => 'foobar'); $id = edit_user($id); // clean up post vars $_POST = $post_vars; $this->assertEquals($display_name, xprofile_get_field_data(bp_xprofile_fullname_field_id(), $id)); }
public static function get_fullname($user_id = 0) { if (empty($user_id)) { $user_id = bp_displayed_user_id(); } $data = xprofile_get_field_data(bp_xprofile_fullname_field_id(), $user_id); return $data[$field_name]; }
/** * Syncs the standard built in WordPress profile data to XProfile. * * @since BuddyPress (1.2.4) * @package BuddyPress Core */ function xprofile_sync_bp_profile(&$errors, $update, &$user) { // Bail if profile syncing is disabled if (bp_disable_profile_sync() || !$update || $errors->get_error_codes()) { return; } xprofile_set_field_data(bp_xprofile_fullname_field_id(), $user->ID, $user->display_name); }
/** * Prepare the query for user_ids. * * @since 1.7.0 */ public function prepare_user_ids_query() { global $wpdb; $bp = buddypress(); // Default query variables used here. $type = ''; $per_page = 0; $page = 1; $user_id = 0; $include = false; $search_terms = false; $exclude = false; $meta_key = false; $meta_value = false; extract($this->query_vars); // Setup the main SQL query container. $sql = array('select' => '', 'where' => array(), 'orderby' => '', 'order' => '', 'limit' => ''); /* TYPE **************************************************************/ // Determines the sort order, which means it also determines where the // user IDs are drawn from (the SELECT and WHERE statements). switch ($type) { // 'online' query happens against the last_activity usermeta key // Filter 'bp_user_query_online_interval' to modify the // number of minutes used as an interval. case 'online': $this->uid_name = 'user_id'; $this->uid_table = $bp->members->table_name_last_activity; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare("u.component = %s AND u.type = 'last_activity'", buddypress()->members->id); /** * Filters the threshold for activity timestamp minutes since to indicate online status. * * @since 1.8.0 * * @param int $value Amount of minutes for threshold. Default 15. */ $sql['where'][] = $wpdb->prepare("u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters('bp_user_query_online_interval', 15)); $sql['orderby'] = "ORDER BY u.date_recorded"; $sql['order'] = "DESC"; break; // 'active', 'newest', and 'random' queries // all happen against the last_activity usermeta key. // 'active', 'newest', and 'random' queries // all happen against the last_activity usermeta key. case 'active': case 'newest': case 'random': $this->uid_name = 'user_id'; $this->uid_table = $bp->members->table_name_last_activity; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare("u.component = %s AND u.type = 'last_activity'", buddypress()->members->id); if ('newest' == $type) { $sql['orderby'] = "ORDER BY u.user_id"; $sql['order'] = "DESC"; } elseif ('random' == $type) { $sql['orderby'] = "ORDER BY rand()"; } else { $sql['orderby'] = "ORDER BY u.date_recorded"; $sql['order'] = "DESC"; } break; // 'popular' sorts by the 'total_friend_count' usermeta. // 'popular' sorts by the 'total_friend_count' usermeta. case 'popular': $this->uid_name = 'user_id'; $this->uid_table = $wpdb->usermeta; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare("u.meta_key = %s", bp_get_user_meta_key('total_friend_count')); $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)"; $sql['order'] = "DESC"; break; // 'alphabetical' sorts depend on the xprofile setup. // 'alphabetical' sorts depend on the xprofile setup. case 'alphabetical': // We prefer to do alphabetical sorts against the display_name field // of wp_users, because the table is smaller and better indexed. We // can do so if xprofile sync is enabled, or if xprofile is inactive. // // @todo remove need for bp_is_active() check. if (!bp_disable_profile_sync() || !bp_is_active('xprofile')) { $this->uid_name = 'ID'; $this->uid_table = $wpdb->users; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['orderby'] = "ORDER BY u.display_name"; $sql['order'] = "ASC"; // When profile sync is disabled, alphabetical sorts must happen against // the xprofile table. } else { $this->uid_name = 'user_id'; $this->uid_table = $bp->profile->table_name_data; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare("u.field_id = %d", bp_xprofile_fullname_field_id()); $sql['orderby'] = "ORDER BY u.value"; $sql['order'] = "ASC"; } // Alphabetical queries ignore last_activity, while BP uses last_activity // to infer spam/deleted/non-activated users. To ensure that these users // are filtered out, we add an appropriate sub-query. $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql('') . " )"; break; // Any other 'type' falls through. // Any other 'type' falls through. default: $this->uid_name = 'ID'; $this->uid_table = $wpdb->users; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; // In this case, we assume that a plugin is // handling order, so we leave those clauses // blank. break; } /* WHERE *************************************************************/ // 'include' - User ids to include in the results. $include = false !== $include ? wp_parse_id_list($include) : array(); $include_ids = $this->get_include_ids($include); if (!empty($include_ids)) { $include_ids = implode(',', wp_parse_id_list($include_ids)); $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})"; } // 'exclude' - User ids to exclude from the results. if (false !== $exclude) { $exclude_ids = implode(',', wp_parse_id_list($exclude)); $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})"; } // 'user_id' - When a user id is passed, limit to the friends of the user // @todo remove need for bp_is_active() check. if (!empty($user_id) && bp_is_active('friends')) { $friend_ids = friends_get_friend_user_ids($user_id); $friend_ids = implode(',', wp_parse_id_list($friend_ids)); if (!empty($friend_ids)) { $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})"; // If the user has no friends, the query should always // return no users. } else { $sql['where'][] = $this->no_results['where']; } } /* Search Terms ******************************************************/ // 'search_terms' searches user_login and user_nicename // xprofile field matches happen in bp_xprofile_bp_user_query_search(). if (false !== $search_terms) { $search_terms = bp_esc_like(wp_kses_normalize_entities($search_terms)); if ($search_wildcard === 'left') { $search_terms_nospace = '%' . $search_terms; $search_terms_space = '%' . $search_terms . ' %'; } elseif ($search_wildcard === 'right') { $search_terms_nospace = $search_terms . '%'; $search_terms_space = '% ' . $search_terms . '%'; } else { $search_terms_nospace = '%' . $search_terms . '%'; $search_terms_space = '%' . $search_terms . '%'; } $sql['where']['search'] = $wpdb->prepare("u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )", $search_terms_nospace, $search_terms_space, $search_terms_nospace, $search_terms_space); } // Only use $member_type__in if $member_type is not set. if (empty($member_type) && !empty($member_type__in)) { $member_type = $member_type__in; } // Member types to exclude. Note that this takes precedence over inclusions. if (!empty($member_type__not_in)) { $member_type_clause = $this->get_sql_clause_for_member_types($member_type__not_in, 'NOT IN'); // Member types to include. } elseif (!empty($member_type)) { $member_type_clause = $this->get_sql_clause_for_member_types($member_type, 'IN'); } if (!empty($member_type_clause)) { $sql['where']['member_type'] = $member_type_clause; } // 'meta_key', 'meta_value' allow usermeta search // To avoid global joins, do a separate query. if (false !== $meta_key) { $meta_sql = $wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key); if (false !== $meta_value) { $meta_sql .= $wpdb->prepare(" AND meta_value = %s", $meta_value); } $found_user_ids = $wpdb->get_col($meta_sql); if (!empty($found_user_ids)) { $sql['where'][] = "u.{$this->uid_name} IN (" . implode(',', wp_parse_id_list($found_user_ids)) . ")"; } else { $sql['where'][] = '1 = 0'; } } // 'per_page', 'page' - handles LIMIT. if (!empty($per_page) && !empty($page)) { $sql['limit'] = $wpdb->prepare("LIMIT %d, %d", intval(($page - 1) * $per_page), intval($per_page)); } else { $sql['limit'] = ''; } /** * Filters the clauses for the user query. * * @since 2.0.0 * * @param array $sql Array of SQL clauses to be used in the query. * @param BP_User_Query $this Current BP_User_Query instance. */ $sql = apply_filters_ref_array('bp_user_query_uid_clauses', array($sql, &$this)); // Assemble the query chunks. $this->uid_clauses['select'] = $sql['select']; $this->uid_clauses['where'] = !empty($sql['where']) ? 'WHERE ' . implode(' AND ', $sql['where']) : ''; $this->uid_clauses['orderby'] = $sql['orderby']; $this->uid_clauses['order'] = $sql['order']; $this->uid_clauses['limit'] = $sql['limit']; /** * Fires before the BP_User_Query query is made. * * @since 1.7.0 * * @param BP_User_Query $this Current BP_User_Query instance. Passed by reference. */ do_action_ref_array('bp_pre_user_query', array(&$this)); }
/** * Prepare the query for user_ids. * * @since BuddyPress (1.7.0) */ public function prepare_user_ids_query() { global $wpdb, $bp; // Default query variables used here $type = ''; $per_page = 0; $page = 1; $user_id = 0; $include = false; $search_terms = false; $exclude = false; $meta_key = false; $meta_value = false; extract( $this->query_vars ); // Setup the main SQL query container $sql = array( 'select' => '', 'where' => array(), 'orderby' => '', 'order' => '', 'limit' => '' ); /** TYPE **************************************************************/ // Determines the sort order, which means it also determines where the // user IDs are drawn from (the SELECT and WHERE statements) switch ( $type ) { // 'online' query happens against the last_activity usermeta key // Filter 'bp_user_query_online_interval' to modify the // number of minutes used as an interval case 'online' : $this->uid_name = 'user_id'; $this->uid_table = $bp->members->table_name_last_activity; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); $sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) ); $sql['orderby'] = "ORDER BY u.date_recorded"; $sql['order'] = "DESC"; break; // 'active', 'newest', and 'random' queries // all happen against the last_activity usermeta key case 'active' : case 'newest' : case 'random' : $this->uid_name = 'user_id'; $this->uid_table = $bp->members->table_name_last_activity; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); if ( 'newest' == $type ) { $sql['orderby'] = "ORDER BY u.user_id"; $sql['order'] = "DESC"; } elseif ( 'random' == $type ) { $sql['orderby'] = "ORDER BY rand()"; } else { $sql['orderby'] = "ORDER BY u.date_recorded"; $sql['order'] = "DESC"; } break; // 'popular' sorts by the 'total_friend_count' usermeta case 'popular' : $this->uid_name = 'user_id'; $this->uid_table = $wpdb->usermeta; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) ); $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)"; $sql['order'] = "DESC"; break; // 'alphabetical' sorts depend on the xprofile setup case 'alphabetical' : // We prefer to do alphabetical sorts against the display_name field // of wp_users, because the table is smaller and better indexed. We // can do so if xprofile sync is enabled, or if xprofile is inactive. // // @todo remove need for bp_is_active() check if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) { $this->uid_name = 'ID'; $this->uid_table = $wpdb->users; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['orderby'] = "ORDER BY u.display_name"; $sql['order'] = "ASC"; // When profile sync is disabled, alphabetical sorts must happen against // the xprofile table } else { $this->uid_name = 'user_id'; $this->uid_table = $bp->profile->table_name_data; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; $sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() ); $sql['orderby'] = "ORDER BY u.value"; $sql['order'] = "ASC"; } // Alphabetical queries ignore last_activity, while BP uses last_activity // to infer spam/deleted/non-activated users. To ensure that these users // are filtered out, we add an appropriate sub-query. $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . " )"; break; // Any other 'type' falls through default : $this->uid_name = 'ID'; $this->uid_table = $wpdb->users; $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; // In this case, we assume that a plugin is // handling order, so we leave those clauses // blank break; } /** WHERE *************************************************************/ // 'include' - User ids to include in the results $include = false !== $include ? wp_parse_id_list( $include ) : array(); $include_ids = $this->get_include_ids( $include ); if ( ! empty( $include_ids ) ) { $include_ids = implode( ',', wp_parse_id_list( $include_ids ) ); $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})"; } // 'exclude' - User ids to exclude from the results if ( false !== $exclude ) { $exclude_ids = implode( ',', wp_parse_id_list( $exclude ) ); $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})"; } // 'user_id' - When a user id is passed, limit to the friends of the user // @todo remove need for bp_is_active() check if ( ! empty( $user_id ) && bp_is_active( 'friends' ) ) { $friend_ids = friends_get_friend_user_ids( $user_id ); $friend_ids = implode( ',', wp_parse_id_list( $friend_ids ) ); if ( ! empty( $friend_ids ) ) { $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})"; // If the user has no friends, the query should always // return no users } else { $sql['where'][] = $this->no_results['where']; } } /** Search Terms ******************************************************/ // 'search_terms' searches user_login and user_nicename // xprofile field matches happen in bp_xprofile_bp_user_query_search() if ( false !== $search_terms ) { $search_terms = bp_esc_like( wp_kses_normalize_entities( $search_terms ) ); if ( $search_wildcard === 'left' ) { $search_terms_nospace = '%' . $search_terms; $search_terms_space = '%' . $search_terms . ' %'; } elseif ( $search_wildcard === 'right' ) { $search_terms_nospace = $search_terms . '%'; $search_terms_space = '% ' . $search_terms . '%'; } else { $search_terms_nospace = '%' . $search_terms . '%'; $search_terms_space = '%' . $search_terms . '%'; } $sql['where']['search'] = $wpdb->prepare( "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )", $search_terms_nospace, $search_terms_space, $search_terms_nospace, $search_terms_space ); } // Member type. if ( ! empty( $member_type ) ) { $member_types = array(); if ( ! is_array( $member_type ) ) { $member_type = preg_split( '/[,\s+]/', $member_type ); } foreach ( $member_type as $mt ) { if ( ! bp_get_member_type_object( $mt ) ) { continue; } $member_types[] = $mt; } if ( ! empty( $member_types ) ) { $member_type_tq = new WP_Tax_Query( array( array( 'taxonomy' => 'bp_member_type', 'field' => 'name', 'operator' => 'IN', 'terms' => $member_types, ), ) ); // Switch to the root blog, where member type taxonomies live. switch_to_blog( bp_get_root_blog_id() ); $member_type_sql_clauses = $member_type_tq->get_sql( 'u', $this->uid_name ); restore_current_blog(); // Grab the first term_relationships clause and convert to a subquery. if ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $member_type_sql_clauses['where'], $matches ) ) { $sql['where']['member_type'] = "u.{$this->uid_name} IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )"; } } } // 'meta_key', 'meta_value' allow usermeta search // To avoid global joins, do a separate query if ( false !== $meta_key ) { $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ); if ( false !== $meta_value ) { $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value ); } $found_user_ids = $wpdb->get_col( $meta_sql ); if ( ! empty( $found_user_ids ) ) { $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")"; } else { $sql['where'][] = '1 = 0'; } } // 'per_page', 'page' - handles LIMIT if ( !empty( $per_page ) && !empty( $page ) ) { $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) ); } else { $sql['limit'] = ''; } // Allow custom filters $sql = apply_filters_ref_array( 'bp_user_query_uid_clauses', array( $sql, &$this ) ); // Assemble the query chunks $this->uid_clauses['select'] = $sql['select']; $this->uid_clauses['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; $this->uid_clauses['orderby'] = $sql['orderby']; $this->uid_clauses['order'] = $sql['order']; $this->uid_clauses['limit'] = $sql['limit']; do_action_ref_array( 'bp_pre_user_query', array( &$this ) ); }
/** * Syncs the standard built in WordPress profile data to XProfile. * * @since BuddyPress (1.2.4) * @package BuddyPress Core */ function xprofile_sync_bp_profile(&$errors, $update, &$user) { global $bp; if (!empty($bp->site_options['bp-disable-profile-sync']) && (int) $bp->site_options['bp-disable-profile-sync'] || !$update || $errors->get_error_codes()) { return; } xprofile_set_field_data(bp_xprofile_fullname_field_id(), $user->ID, $user->display_name); }