Skip to:
Content

BuddyPress.org

Changeset 1518 for trunk/bp-core.php


Ignore:
Timestamp:
06/05/2009 09:29:24 PM (17 years ago)
Author:
apeatling
Message:

Streamlined the multiple functions to get the user's name, and brought them under the new "bp_core_get_user_displayname" function.

Added more through commenting to the bp-core.php file.

Allowed greater customization to bp_core_action_search_site()

Added bp_get_buddypress_theme_path() and bp_get_buddypress_theme_uri() functions

Reduced priority on some core BuddyPress functions, so that other plugins can scoot in front if needed.

Added missing filters on some core functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core.php

    r1515 r1518  
    3838require ( BP_PLUGIN_DIR . '/bp-core/bp-core-notifications.php' );
    3939
     40/* If BP_DISABLE_ADMIN_BAR is defined, do not load the global admin bar */
    4041if ( !defined( 'BP_DISABLE_ADMIN_BAR') )
    4142        require ( 'bp-core/bp-core-adminbar.php' );
     
    139140       
    140141        /* Fetch the full name for the logged in and current user */
    141         $bp->loggedin_user->fullname = bp_core_global_user_fullname( $bp->loggedin_user->id );
    142         $bp->displayed_user->fullname = bp_core_global_user_fullname( $bp->displayed_user->id );
     142        $bp->loggedin_user->fullname = bp_core_get_user_displayname( $bp->loggedin_user->id );
     143        $bp->displayed_user->fullname = bp_core_get_user_displayname( $bp->displayed_user->id );
    143144
    144145        /* Used to determine if user has admin rights on current content. If the logged in user is viewing
     
    158159}
    159160add_action( 'plugins_loaded', 'bp_core_setup_globals', 3 );
    160 add_action( '_admin_menu', 'bp_core_setup_globals', 1 ); // must be _admin_menu hook.
    161 
    162 function bp_core_setup_root_components() {
     161add_action( '_admin_menu', 'bp_core_setup_globals', 3 ); // must be _admin_menu hook.
     162
     163
     164/**
     165 * bp_core_setup_root_uris()
     166 *
     167 * Adds the core URIs that should run in the root of the installation.
     168 *
     169 * For example: http://example.org/search or http://example.org/members
     170 *
     171 * @package BuddyPress Core
     172 * @uses bp_core_add_root_component() Adds a slug to the root components global variable.
     173 */
     174function bp_core_setup_root_uris() {
    163175        /* Add core root components */
    164176        bp_core_add_root_component( BP_MEMBERS_SLUG );
     
    168180        bp_core_add_root_component( BP_HOME_BLOG_SLUG );
    169181}
    170 add_action( 'plugins_loaded', 'bp_core_setup_root_components', 1 );
    171 
    172 function bp_core_setup_cookies() {
    173         global $bp_message, $bp_message_type;
    174        
    175         // Render any error/success feedback on the template
    176         if ( $_COOKIE['bp-message'] == '' || !isset( $_COOKIE['bp-message'] ) )
    177                 return false;
    178 
    179         $bp_message = $_COOKIE['bp-message'];
    180         $bp_message_type = $_COOKIE['bp-message-type'];
    181         add_action( 'template_notices', 'bp_core_render_notice' );
    182 
    183         setcookie( 'bp-message', false, time() - 1000, COOKIEPATH );
    184         setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH );
    185 }
    186 add_action( 'wp', 'bp_core_setup_cookies', 3 );
    187 
     182add_action( 'plugins_loaded', 'bp_core_setup_root_uris', 2 );
     183
     184
     185/**
     186 * bp_core_install()
     187 *
     188 * Installs the core DB tables for BuddyPress.
     189 *
     190 * @package BuddyPress Core
     191 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     192 * @global $wpdb WordPress DB access object.
     193 * @uses dbDelta() Performs a table creation, or upgrade based on what already exists in the DB.
     194 * @uses bp_core_add_illegal_names() Adds illegal blog names to the WP settings
     195 */
    188196function bp_core_install() {
    189197        global $wpdb, $bp;
     
    250258add_action( 'admin_menu', 'bp_core_check_installed' );
    251259
     260
     261/**
     262 * bp_core_setup_cookies()
     263 *
     264 * Checks if there is a feedback message in the WP cookie, if so, adds a "template_notices" action
     265 * so that the message can be parsed into the template and displayed to the user.
     266 *
     267 * After the message is displayed, it removes the message vars from the cookie so that the message
     268 * is not shown to the user multiple times.
     269 *
     270 * @package BuddyPress Core
     271 * @global $bp_message The message text
     272 * @global $bp_message_type The type of message (error/success)
     273 * @uses setcookie() Sets a cookie value for the user.
     274 */
     275function bp_core_setup_cookies() {
     276        global $bp_message, $bp_message_type;
     277       
     278        // Render any error/success feedback on the template
     279        if ( $_COOKIE['bp-message'] == '' || !isset( $_COOKIE['bp-message'] ) )
     280                return false;
     281
     282        $bp_message = $_COOKIE['bp-message'];
     283        $bp_message_type = $_COOKIE['bp-message-type'];
     284        add_action( 'template_notices', 'bp_core_render_notice' );
     285
     286        setcookie( 'bp-message', false, time() - 1000, COOKIEPATH );
     287        setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH );
     288}
     289add_action( 'init', 'bp_core_setup_cookies' );
     290
     291
    252292/**
    253293 * bp_core_add_admin_menu()
     
    328368add_action( 'admin_menu', 'bp_core_setup_nav', 2 );
    329369
    330 function bp_core_directory_members() {
     370/**
     371 * bp_core_action_directory_members()
     372 *
     373 * Listens to the $bp component and action variables to determine if the user is viewing the members
     374 * directory page. If they are, it will set up the directory and load the members directory template.
     375 *
     376 * @package BuddyPress Core
     377 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     378 * @uses wp_enqueue_script() Loads a JS script into the header of the page.
     379 * @uses bp_core_load_template() Loads a specific template file.
     380 */
     381function bp_core_action_directory_members() {
    331382        global $bp;
    332383       
     
    339390        }
    340391}
    341 add_action( 'wp', 'bp_core_directory_members', 5 );
     392add_action( 'wp', 'bp_core_action_directory_members', 2 );
    342393
    343394/**
     
    486537       
    487538        if ( function_exists($function) && $user_has_access && $bp->current_action == $slug && $bp->current_component == $parent_id )
    488                 add_action( 'wp', $function, 3 );
     539                add_action( 'wp', $function );
    489540}
    490541
     
    543594        if ( $bp->current_component == $parent_id && !$bp->current_action ) {
    544595                if ( function_exists($function) ) {
    545                         add_action( 'wp', $function, 3 );
     596                        add_action( 'wp', $function );
    546597                }
    547598               
     
    635686        global $wpdb;
    636687       
    637         $sql = $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_login = %s", $username );
    638         return $wpdb->get_var($sql);
    639 }
    640 
    641 /**
    642  * bp_core_get_userid_from_user_login()
    643  *
    644  * Returns the user_id from a user login
    645  * @package BuddyPress Core
    646  * @param $path str Path to check.
    647  * @global $wpdb WordPress DB access object.
    648  * @return false on no match
    649  * @return int the user ID of the matched user.
    650  */
    651 function bp_core_get_userid_from_user_login( $user_login ) {
    652         global $wpdb;
    653 
    654         if ( !empty( $user_login ) )
    655                 return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_login = %s", $user_login ) );
    656 }
     688        if ( !empty( $username ) )
     689                return apply_filters( 'bp_core_get_userid', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . CUSTOM_USER_TABLE . " WHERE user_login = %s", $username ) ) );
     690}
     691        function bp_core_get_userid_from_user_login( $deprecated ) {
     692                return bp_core_get_userid( $deprecated );
     693        }
    657694
    658695/**
     
    672709       
    673710        if ( $uid == $userdata->ID )
    674                 return __( 'You', 'buddypress' );
     711                $username = __( 'You', 'buddypress' );
    675712       
    676713        if ( !$ud = get_userdata($uid) )
    677714                return false;
    678715               
    679         return $ud->user_login;
     716        $username = $ud->user_login;   
     717
     718        return apply_filters( 'bp_core_get_username', $username );
    680719}
    681720
     
    700739        $ud = get_userdata($uid);
    701740               
    702         return $bp->root_domain . '/' . BP_MEMBERS_SLUG . '/' . $ud->user_login . '/';
     741        return apply_filters( 'bp_core_get_userurl', $bp->root_domain . '/' . BP_MEMBERS_SLUG . '/' . $ud->user_login . '/' );
    703742}
    704743
     
    716755function bp_core_get_user_email( $uid ) {
    717756        $ud = get_userdata($uid);
    718         return $ud->user_email;
     757        return apply_filters( 'bp_core_get_user_email', $ud->user_email );
    719758}
    720759
     
    750789
    751790        if ( function_exists('bp_fetch_user_fullname') ) {
    752                 $display_name = bp_fetch_user_fullname( $user_id, false );
     791                $display_name = bp_core_get_user_displayname( $user_id );
    753792               
    754793                if ( $with_s )
     
    759798        }
    760799       
    761         // if ( $user_id == $userdata->ID && !$no_you )
    762         //      $display_name = 'You';
    763        
    764800        if ( $no_anchor )
    765801                return $display_name;
     
    774810}
    775811
    776 /**
    777  * bp_core_global_user_fullname()
    778  *
    779  * Returns the full name for the user, or the display name if Xprofile component is not installed.
    780  *
    781  * @package BuddyPress Core
    782  * @param $user_id string The user ID of the user.
    783  * @param
    784  * @uses bp_fetch_user_fullname() Returns the full name for a user based on user ID.
    785  * @uses get_userdata() Fetches a new userdata object for the user ID passed.
    786  * @return Either the users full name, or the display name.
    787  */
    788 function bp_core_global_user_fullname( $user_id ) {
    789         if ( function_exists('bp_fetch_user_fullname') ) {
    790                 return bp_fetch_user_fullname( $user_id, false );
    791         } else {
    792                 $ud = get_userdata($user_id);
    793                 return $current_user->display_name;
    794         }
    795 }
     812
     813/**
     814 * bp_core_get_user_displayname()
     815 *
     816 * Fetch the display name for a user. This will use the "Name" field in xprofile if it is installed.
     817 * Otherwise, it will fall back to the normal WP display_name, or user_nicename, depending on what has been set.
     818 *
     819 * @package BuddyPress Core
     820 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     821 * @uses wp_cache_get() Will try and fetch the value from the cache, rather than querying the DB again.
     822 * @uses get_userdata() Fetches the WP userdata for a specific user.
     823 * @uses xprofile_set_field_data() Will update the field data for a user based on field name and user id.
     824 * @uses wp_cache_set() Adds a value to the cache.
     825 * @return str The display name for the user in question.
     826 */
     827function bp_core_get_user_displayname( $user_id ) {
     828        global $bp;
     829       
     830        if ( !$user_id )
     831                return false;
     832               
     833        if ( !$fullname = wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ) ) {
     834                if ( function_exists('xprofile_install') ) {
     835                        $fullname = xprofile_get_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $user_id );
     836
     837                        if ( empty($fullname) || !$fullname ) {
     838                                $ud = get_userdata($user_id);
     839
     840                                if ( empty( $ud->display_name ) )
     841                                        $fullname = $ud->user_nicename;
     842                                else
     843                                        $fullname = $ud->display_name;
     844
     845                                xprofile_set_field_data( BP_XPROFILE_FULLNAME_FIELD_NAME, $user_id, $fullname );
     846                        }
     847                } else {
     848                        $ud = get_userdata($user_id);
     849                        $fullname = $ud->display_name;
     850                }
     851
     852                wp_cache_set( 'bp_user_fullname_' . $user_id, $fullname, 'bp' );
     853        }
     854       
     855        return apply_filters( 'bp_core_get_user_displayname', stripslashes( trim( $fullname ) ) );
     856}
     857        /* DEPRECATED Use: bp_core_get_user_displayname */
     858        function bp_core_global_user_fullname( $user_id ) { return bp_core_get_user_displayname( $user_id ); }
     859
    796860
    797861/**
     
    811875}
    812876
     877
    813878/**
    814879 * bp_core_get_userlink_by_username()
     
    827892        return apply_filters( 'bp_core_get_userlink_by_username', bp_core_get_userlink( $user_id, false, false, true ) );
    828893}
     894
    829895
    830896/**
     
    849915}
    850916
     917
     918/**
     919 * bp_core_add_message()
     920 *
     921 * Adds a feedback (error/success) message to the WP cookie so it can be displayed after the page reloads.
     922 *
     923 * @package BuddyPress Core
     924 */
    851925function bp_core_add_message( $message, $type = false ) {
    852926        if ( !$type )
     
    856930        setcookie( 'bp-message-type', $type, time()+60*60*24, COOKIEPATH );
    857931}
     932
    858933
    859934/**
     
    878953}
    879954
     955
    880956/**
    881957 * bp_core_time_since()
     
    9541030        return $output;
    9551031}
     1032
    9561033
    9571034/**
     
    10071084}
    10081085
     1086
    10091087/**
    10101088 * bp_core_get_all_posts_for_user()
     
    10261104}
    10271105
     1106
    10281107/**
    10291108 * bp_core_get_site_path()
     
    10411120}
    10421121
     1122/**
     1123 * bp_core_redirect()
     1124 *
     1125 * Performs a status safe wp_redirect() that is compatible with bp_catch_uri()
     1126 *
     1127 * @package BuddyPress Core
     1128 * @global $bp_no_status_set Makes sure that there are no conflicts with status_header() called in bp_core_do_catch_uri()
     1129 * @uses get_themes()
     1130 * @return An array containing all of the themes.
     1131 */
    10431132function bp_core_redirect( $location, $status = 302 ) {
    10441133        global $bp_no_status_set;
     
    11291218}
    11301219
    1131 function bp_core_get_member_themes() {
    1132         add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
     1220/**
     1221 * bp_core_get_buddypress_themes()
     1222 *
     1223 * Gets an array of all the BuddyPress themes in the /bp-themes/ directory.
     1224 *
     1225 * @package BuddyPress Core
     1226 * @uses get_themes()
     1227 * @return An array containing all of the themes.
     1228 */
     1229function bp_core_get_buddypress_themes() {
     1230        add_filter( 'theme_root', 'bp_core_filter_buddypress_theme_root' );
    11331231        $themes = get_themes();
    11341232
     
    11481246        return $member_themes;
    11491247}
    1150 
    1151 function bp_core_set_member_theme_root() {
    1152         return apply_filters( 'bp_core_set_member_theme_root', WP_CONTENT_DIR . "/bp-themes" );
    1153 }
    1154 
    1155 function bp_core_set_member_theme_root_uri() {
    1156         return apply_filters( 'bp_core_set_member_theme_root_uri', WP_CONTENT_URL . '/bp-themes' );
    1157 }
    1158 
     1248function bp_core_get_member_themes() { return bp_core_get_buddypress_themes(); } // DEPRECATED
     1249
     1250
     1251/**
     1252 * bp_get_buddypress_theme_uri()
     1253 *
     1254 * Get the url of the selected BuddyPress theme.
     1255 *
     1256 * @package BuddyPress Core
     1257 */
     1258function bp_get_buddypress_theme_uri() {
     1259        return apply_filters( 'bp_get_buddypress_theme_uri', WP_CONTENT_URL . '/bp-themes/' . get_site_option( 'active-member-theme' ) );
     1260}
     1261
     1262
     1263/**
     1264 * bp_get_buddypress_theme_path()
     1265 *
     1266 * Get the path of the selected BuddyPress theme.
     1267 *
     1268 * @package BuddyPress Core
     1269 */
     1270function bp_get_buddypress_theme_path() {
     1271        return apply_filters( 'bp_get_buddypress_theme_path', WP_CONTENT_DIR . '/bp-themes/' . get_site_option( 'active-member-theme' ) );
     1272}
     1273
     1274
     1275/**
     1276 * bp_core_filter_buddypress_theme_root()
     1277 *
     1278 * Adds a filter that changes the root path of the theme directory to the bp-themes directory.
     1279 *
     1280 * @package BuddyPress Core
     1281 */
     1282function bp_core_filter_buddypress_theme_root() {
     1283        return apply_filters( 'bp_core_filter_buddypress_theme_root', WP_CONTENT_DIR . "/bp-themes" );
     1284}
     1285
     1286
     1287/**
     1288 * bp_core_filter_buddypress_theme_root_uri()
     1289 *
     1290 * Adds a filter that changes the root URI of the theme directory to the bp-themes directory.
     1291 *
     1292 * @package BuddyPress Core
     1293 */
     1294function bp_core_filter_buddypress_theme_root_uri() {
     1295        return apply_filters( 'bp_core_filter_buddypress_theme_root_uri', WP_CONTENT_URL . '/bp-themes' );
     1296}
     1297
     1298
     1299/**
     1300 * bp_core_add_illegal_names()
     1301 *
     1302 * Adds illegal names to WP so that root components will not conflict with
     1303 * blog names on a subdirectory installation.
     1304 *
     1305 * For example, it would stop someone creating a blog with the slug "groups".
     1306 *
     1307 * @package BuddyPress Core
     1308 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     1309 */
    11591310function bp_core_add_illegal_names() {
    11601311        global $bp;
     
    11921343add_filter( 'wp_mail_from_name', 'bp_core_email_from_name_filter' );
    11931344
     1345
    11941346/**
    11951347 * bp_core_email_from_name_filter()
     
    12081360
    12091361
     1362/**
     1363 * bp_core_delete_account()
     1364 *
     1365 * Allows a user to completely remove their account from the system
     1366 *
     1367 * @package BuddyPress Core
     1368 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     1369 * @uses check_admin_referer() Checks for a valid security nonce.
     1370 * @uses is_site_admin() Checks to see if the user is a site administrator.
     1371 * @uses wpmu_delete_user() Deletes a user from the system.
     1372 */
    12101373function bp_core_delete_account() {
    12111374        global $bp;
    12121375
    12131376        // Be careful with this function!
     1377       
     1378        if ( !check_admin_referer( 'delete-account' ) )
     1379                return false;
     1380       
     1381        /* Site admins should not be allowed to delete their accounts */
     1382        if ( is_site_admin() )
     1383                return false;
    12141384       
    12151385        require_once( ABSPATH . '/wp-admin/includes/mu.php' );
     
    12191389}
    12201390
    1221 function bp_core_search_site() {
    1222         global $bp;
    1223        
     1391
     1392/**
     1393 * bp_core_search_site()
     1394 *
     1395 * A javascript free implementation of the search functions in BuddyPress
     1396 *
     1397 * @package BuddyPress Core
     1398 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     1399 * @param $slug The slug to redirect to for searching.
     1400 */
     1401function bp_core_action_search_site( $slug = false ) {
     1402        global $bp;
     1403
    12241404        if ( $bp->current_component == BP_SEARCH_SLUG ) {
    12251405                $search_terms = $_POST['search-terms'];
    12261406                $search_which = $_POST['search-which'];
    12271407               
    1228                 switch ( $search_which ) {
    1229                         case 'members': default:
    1230                                 $search = BP_MEMBERS_SLUG;
    1231                                 break;
    1232                         case 'groups':
    1233                                 $search = BP_GROUPS_SLUG;
    1234                                 break;
    1235                         case 'blogs':
    1236                                 $search = BP_BLOGS_SLUG;
    1237                                 break;
     1408                if ( !$slug || empty( $slug ) ) {
     1409                        switch ( $search_which ) {
     1410                                case 'members': default:
     1411                                        $slug = BP_MEMBERS_SLUG;
     1412                                        break;
     1413                                case 'groups':
     1414                                        $slug = BP_GROUPS_SLUG;
     1415                                        break;
     1416                                case 'blogs':
     1417                                        $slug = BP_BLOGS_SLUG;
     1418                                        break;
     1419                        }
    12381420                }
    1239                
    1240                 $search_url = apply_filters( 'bp_core_search_site', site_url( $search . '/?s=' . urlencode($search_terms) ), $search_terms );
     1421
     1422                $search_url = apply_filters( 'bp_core_search_site', site_url( $slug . '/?s=' . urlencode($search_terms) ), $search_terms );
    12411423               
    12421424                bp_core_redirect( $search_url );
    12431425        }
    12441426}
    1245 add_action( 'wp', 'bp_core_search_site', 5 );
     1427add_action( 'init', 'bp_core_action_search_site', 5 );
     1428
    12461429
    12471430/**
    12481431 * bp_core_ucfirst()
    12491432 *
    1250  * Localization save ucfirst() support.
     1433 * Localization safe ucfirst() support.
    12511434 *
    12521435 * @package BuddyPress Core
     
    12611444}
    12621445
     1446
    12631447/**
    12641448 * bp_core_strip_username_spaces()
     
    12721456}
    12731457add_action( 'pre_user_login', 'bp_core_strip_username_spaces' );
     1458
    12741459
    12751460/**
     
    12921477}
    12931478
    1294 function bp_core_print_version_numbers() {
    1295         global $bp;
    1296        
    1297         foreach ( $bp->version_numbers as $name => $version ) {
    1298                 echo ucwords($name) . ': <b>' . $version . '</b> / ';
    1299         }
    1300 }
    1301 
     1479/**
     1480 * bp_core_print_generation_time()
     1481 *
     1482 * Prints the generation time in the footer of the site.
     1483 *
     1484 * @package BuddyPress Core
     1485 */
    13021486function bp_core_print_generation_time() {
    13031487        global $wpdb;
     
    13281512add_action( 'delete_user', 'bp_core_remove_data', 1 );
    13291513
     1514
     1515/**
     1516 * bp_core_clear_user_object_cache()
     1517 *
     1518 * Clears all cached objects for a user, or a user is part of.
     1519 *
     1520 * @package BuddyPress Core
     1521 */
    13301522function bp_core_clear_user_object_cache( $user_id ) {
    13311523        wp_cache_delete( 'bp_user_' . $user_id, 'bp' );
Note: See TracChangeset for help on using the changeset viewer.