Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/20/2013 08:39:05 PM (11 years ago)
Author:
r-a-y
Message:

Organize bp-core-functions.php into sections.

Todo:

  • Move bp_is_username_compatibility_mode() to Members component
  • Deprecate bp_core_add_illegal_names()?
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r7054 r7093  
    8686
    8787/**
     88 * Format numbers the BuddyPress way
     89 *
     90 * @param str $number
     91 * @param bool $decimals
     92 * @return str
     93 */
     94function bp_core_number_format( $number, $decimals = false ) {
     95
     96    // Force number to 0 if needed
     97    if ( empty( $number ) )
     98        $number = 0;
     99
     100    return apply_filters( 'bp_core_number_format', number_format_i18n( $number, $decimals ), $number, $decimals );
     101}
     102
     103/**
     104 * A utility for parsing individual function arguments into an array.
     105 *
     106 * The purpose of this function is to help with backward compatibility in cases where
     107 *
     108 *   function foo( $bar = 1, $baz = false, $barry = array(), $blip = false ) { // ...
     109 *
     110 * is deprecated in favor of
     111 *
     112 *   function foo( $args = array() ) {
     113 *       $defaults = array(
     114 *           'bar'  => 1,
     115 *           'arg2' => false,
     116 *           'arg3' => array(),
     117 *           'arg4' => false,
     118 *       );
     119 *       $r = wp_parse_args( $args, $defaults ); // ...
     120 *
     121 * The first argument, $old_args_keys, is an array that matches the parameter positions (keys) to
     122 * the new $args keys (values):
     123 *
     124 *   $old_args_keys = array(
     125 *       0 => 'bar', // because $bar was the 0th parameter for foo()
     126 *       1 => 'baz', // because $baz was the 1st parameter for foo()
     127 *       2 => 'barry', // etc
     128 *       3 => 'blip'
     129 *   );
     130 *
     131 * For the second argument, $func_args, you should just pass the value of func_get_args().
     132 *
     133 * @since BuddyPress (1.6)
     134 * @param array $old_args_keys
     135 * @param array $func_args
     136 * @return array $new_args
     137 */
     138function bp_core_parse_args_array( $old_args_keys, $func_args ) {
     139    $new_args = array();
     140
     141    foreach( $old_args_keys as $arg_num => $arg_key ) {
     142        if ( isset( $func_args[$arg_num] ) ) {
     143            $new_args[$arg_key] = $func_args[$arg_num];
     144        }
     145    }
     146
     147    return $new_args;
     148}
     149
     150/**
     151 * Sanitize an 'order' parameter for use in building SQL queries
     152 *
     153 * Strings like 'DESC', 'desc', ' desc' will be interpreted into 'DESC'.
     154 * Everything else becomes 'ASC'.
     155 *
     156 * @since BuddyPress (1.8)
     157 * @param string $order The 'order' string, as passed to the SQL constructor
     158 * @return string The sanitized value 'DESC' or 'ASC'
     159 */
     160function bp_esc_sql_order( $order = '' ) {
     161    $order = strtoupper( trim( $order ) );
     162    return 'DESC' === $order ? 'DESC' : 'ASC';
     163}
     164
     165/**
     166 * Are we running username compatibility mode?
     167 *
     168 * @package BuddyPress
     169 * @since BuddyPress (1.5)
     170 *
     171 * @uses apply_filters() Filter 'bp_is_username_compatibility_mode' to alter
     172 * @return bool False when compatibility mode is disabled (default); true when enabled
     173 * @todo Move to members component?
     174 */
     175function bp_is_username_compatibility_mode() {
     176    return apply_filters( 'bp_is_username_compatibility_mode', defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) && BP_ENABLE_USERNAME_COMPATIBILITY_MODE );
     177}
     178
     179/**
     180 * Should we use the WP Toolbar?
     181 *
     182 * The WP Toolbar, introduced in WP 3.1, is fully supported in BuddyPress as of BP 1.5.
     183 * For BP 1.6, the WP Toolbar is the default.
     184 *
     185 * @return bool False when WP Toolbar support is disabled; true when enabled (default)
     186 * @since BuddyPress (1.5)
     187 * @uses apply_filters() Filter 'bp_use_wp_admin_bar' to alter
     188 */
     189function bp_use_wp_admin_bar() {
     190    $use_admin_bar = true;
     191
     192    // Has the WP Toolbar constant been explicity set?
     193    if ( defined( 'BP_USE_WP_ADMIN_BAR' ) && ! BP_USE_WP_ADMIN_BAR )
     194        $use_admin_bar = false;
     195
     196    // Has the admin chosen to use the BuddyBar during an upgrade?
     197    elseif ( (bool) bp_get_option( '_bp_force_buddybar', false ) )
     198        $use_admin_bar = false;
     199
     200    return apply_filters( 'bp_use_wp_admin_bar', $use_admin_bar );
     201}
     202
     203/** Directory *****************************************************************/
     204
     205/**
    88206 * Fetches BP pages from the meta table, depending on setup
    89207 *
     
    287405
    288406/**
     407 * This function originally let plugins add support for pages in the root of the install.
     408 * These root level pages are now handled by actual WordPress pages and this function is now
     409 * a convenience for compatibility with the new method.
     410 *
     411 * @global $bp BuddyPress global settings
     412 * @param $slug str The slug of the component
     413 */
     414function bp_core_add_root_component( $slug ) {
     415    global $bp;
     416
     417    if ( empty( $bp->pages ) )
     418        $bp->pages = bp_core_get_directory_pages();
     419
     420    $match = false;
     421
     422    // Check if the slug is registered in the $bp->pages global
     423    foreach ( (array) $bp->pages as $key => $page ) {
     424        if ( $key == $slug || $page->slug == $slug )
     425            $match = true;
     426    }
     427
     428    // If there was no match, add a page for this root component
     429    if ( empty( $match ) ) {
     430        $bp->add_root[] = $slug;
     431    }
     432
     433    // Make sure that this component is registered as requiring a top-level directory
     434    if ( isset( $bp->{$slug} ) ) {
     435        $bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;
     436        $bp->{$slug}->has_directory = true;
     437    }
     438}
     439
     440function bp_core_create_root_component_page() {
     441    global $bp;
     442
     443    $new_page_ids = array();
     444
     445    foreach ( (array) $bp->add_root as $slug )
     446        $new_page_ids[$slug] = wp_insert_post( array( 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_title' => ucwords( $slug ), 'post_status' => 'publish', 'post_type' => 'page' ) );
     447
     448    $page_ids = array_merge( (array) $new_page_ids, (array) bp_core_get_directory_page_ids() );
     449    bp_core_update_directory_page_ids( $page_ids );
     450}
     451
     452/**
     453 * Adds illegal names to WP so that root components will not conflict with
     454 * blog names on a subdirectory installation.
     455 *
     456 * For example, it would stop someone creating a blog with the slug "groups".
     457 *
     458 * @todo Deprecate?
     459 */
     460function bp_core_add_illegal_names() {
     461    update_site_option( 'illegal_names', get_site_option( 'illegal_names' ), array() );
     462}
     463
     464/** URI ***********************************************************************/
     465
     466/**
    289467 * Returns the domain for the root blog.
    290468 * eg: http://domain.com/ OR https://domain.com
     
    300478    return apply_filters( 'bp_core_get_root_domain', $domain );
    301479}
     480
     481/**
     482 * Performs a status safe wp_redirect() that is compatible with bp_catch_uri()
     483 *
     484 * @package BuddyPress Core
     485 * @uses wp_safe_redirect()
     486 */
     487function bp_core_redirect( $location, $status = 302 ) {
     488
     489    // On some setups, passing the value of wp_get_referer() may result in an
     490    // empty value for $location, which results in an error. Ensure that we
     491    // have a valid URL.
     492    if ( empty( $location ) )
     493        $location = bp_get_root_domain();
     494
     495    // Make sure we don't call status_header() in bp_core_do_catch_uri() as this
     496    // conflicts with wp_redirect() and wp_safe_redirect().
     497    buddypress()->no_status_set = true;
     498
     499    wp_safe_redirect( $location, $status );
     500    die;
     501}
     502
     503/**
     504 * Returns the referrer URL without the http(s)://
     505 *
     506 * @package BuddyPress Core
     507 * @return The referrer URL
     508 */
     509function bp_core_referrer() {
     510    $referer = explode( '/', wp_get_referer() );
     511    unset( $referer[0], $referer[1], $referer[2] );
     512    return implode( '/', $referer );
     513}
     514
     515/**
     516 * Get the path of of the current site.
     517 *
     518 * @package BuddyPress Core
     519 *
     520 * @global object $current_site
     521 * @return string
     522 */
     523function bp_core_get_site_path() {
     524    global $current_site;
     525
     526    if ( is_multisite() )
     527        $site_path = $current_site->path;
     528    else {
     529        $site_path = (array) explode( '/', home_url() );
     530
     531        if ( count( $site_path ) < 2 )
     532            $site_path = '/';
     533        else {
     534            // Unset the first three segments (http(s)://domain.com part)
     535            unset( $site_path[0] );
     536            unset( $site_path[1] );
     537            unset( $site_path[2] );
     538
     539            if ( !count( $site_path ) )
     540                $site_path = '/';
     541            else
     542                $site_path = '/' . implode( '/', $site_path ) . '/';
     543        }
     544    }
     545
     546    return apply_filters( 'bp_core_get_site_path', $site_path );
     547}
     548
     549/** Time **********************************************************************/
    302550
    303551/**
     
    312560
    313561    return apply_filters( 'bp_core_current_time', $current_time );
    314 }
    315 
    316 /**
    317  * Adds a feedback (error/success) message to the WP cookie so it can be
    318  * displayed after the page reloads.
    319  *
    320  * @package BuddyPress Core
    321  *
    322  * @global BuddyPress $bp The one true BuddyPress instance
    323  * @param str $message Feedback to give to user
    324  * @param str $type updated|success|error|warning
    325  */
    326 function bp_core_add_message( $message, $type = '' ) {
    327     global $bp;
    328 
    329     // Success is the default
    330     if ( empty( $type ) )
    331         $type = 'success';
    332 
    333     // Send the values to the cookie for page reload display
    334     @setcookie( 'bp-message',      $message, time() + 60 * 60 * 24, COOKIEPATH );
    335     @setcookie( 'bp-message-type', $type,    time() + 60 * 60 * 24, COOKIEPATH );
    336 
    337     /***
    338      * Send the values to the $bp global so we can still output messages
    339      * without a page reload
    340      */
    341     $bp->template_message      = $message;
    342     $bp->template_message_type = $type;
    343 }
    344 
    345 /**
    346  * Checks if there is a feedback message in the WP cookie, if so, adds a
    347  * "template_notices" action so that the message can be parsed into the template
    348  * and displayed to the user.
    349  *
    350  * After the message is displayed, it removes the message vars from the cookie
    351  * so that the message is not shown to the user multiple times.
    352  *
    353  * @package BuddyPress Core
    354  * @global $bp_message The message text
    355  * @global $bp_message_type The type of message (error/success)
    356  * @uses setcookie() Sets a cookie value for the user.
    357  */
    358 function bp_core_setup_message() {
    359     global $bp;
    360 
    361     if ( empty( $bp->template_message ) && isset( $_COOKIE['bp-message'] ) )
    362         $bp->template_message = $_COOKIE['bp-message'];
    363 
    364     if ( empty( $bp->template_message_type ) && isset( $_COOKIE['bp-message-type'] ) )
    365         $bp->template_message_type = $_COOKIE['bp-message-type'];
    366 
    367     add_action( 'template_notices', 'bp_core_render_message' );
    368 
    369     @setcookie( 'bp-message',      false, time() - 1000, COOKIEPATH );
    370     @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH );
    371 }
    372 add_action( 'bp_actions', 'bp_core_setup_message', 5 );
    373 
    374 /**
    375  * Renders a feedback message (either error or success message) to the theme template.
    376  * The hook action 'template_notices' is used to call this function, it is not called directly.
    377  *
    378  * @package BuddyPress Core
    379  * @global BuddyPress $bp The one true BuddyPress instance
    380  */
    381 function bp_core_render_message() {
    382     global $bp;
    383 
    384     if ( !empty( $bp->template_message ) ) :
    385         $type    = ( 'success' == $bp->template_message_type ) ? 'updated' : 'error';
    386         $content = apply_filters( 'bp_core_render_message_content', $bp->template_message, $type ); ?>
    387 
    388         <div id="message" class="bp-template-notice <?php echo $type; ?>">
    389 
    390             <?php echo $content; ?>
    391 
    392         </div>
    393 
    394     <?php
    395 
    396         do_action( 'bp_core_render_message' );
    397 
    398     endif;
    399 }
    400 
    401 /**
    402  * Format numbers the BuddyPress way
    403  *
    404  * @param str $number
    405  * @param bool $decimals
    406  * @return str
    407  */
    408 function bp_core_number_format( $number, $decimals = false ) {
    409 
    410     // Force number to 0 if needed
    411     if ( empty( $number ) )
    412         $number = 0;
    413 
    414     return apply_filters( 'bp_core_number_format', number_format_i18n( $number, $decimals ), $number, $decimals );
    415562}
    416563
     
    529676}
    530677
     678/** Messages ******************************************************************/
     679
     680/**
     681 * Adds a feedback (error/success) message to the WP cookie so it can be
     682 * displayed after the page reloads.
     683 *
     684 * @package BuddyPress Core
     685 *
     686 * @global BuddyPress $bp The one true BuddyPress instance
     687 * @param str $message Feedback to give to user
     688 * @param str $type updated|success|error|warning
     689 */
     690function bp_core_add_message( $message, $type = '' ) {
     691    global $bp;
     692
     693    // Success is the default
     694    if ( empty( $type ) )
     695        $type = 'success';
     696
     697    // Send the values to the cookie for page reload display
     698    @setcookie( 'bp-message',      $message, time() + 60 * 60 * 24, COOKIEPATH );
     699    @setcookie( 'bp-message-type', $type,    time() + 60 * 60 * 24, COOKIEPATH );
     700
     701    /***
     702     * Send the values to the $bp global so we can still output messages
     703     * without a page reload
     704     */
     705    $bp->template_message      = $message;
     706    $bp->template_message_type = $type;
     707}
     708
     709/**
     710 * Checks if there is a feedback message in the WP cookie, if so, adds a
     711 * "template_notices" action so that the message can be parsed into the template
     712 * and displayed to the user.
     713 *
     714 * After the message is displayed, it removes the message vars from the cookie
     715 * so that the message is not shown to the user multiple times.
     716 *
     717 * @package BuddyPress Core
     718 * @global $bp_message The message text
     719 * @global $bp_message_type The type of message (error/success)
     720 * @uses setcookie() Sets a cookie value for the user.
     721 */
     722function bp_core_setup_message() {
     723    global $bp;
     724
     725    if ( empty( $bp->template_message ) && isset( $_COOKIE['bp-message'] ) )
     726        $bp->template_message = $_COOKIE['bp-message'];
     727
     728    if ( empty( $bp->template_message_type ) && isset( $_COOKIE['bp-message-type'] ) )
     729        $bp->template_message_type = $_COOKIE['bp-message-type'];
     730
     731    add_action( 'template_notices', 'bp_core_render_message' );
     732
     733    @setcookie( 'bp-message',      false, time() - 1000, COOKIEPATH );
     734    @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH );
     735}
     736add_action( 'bp_actions', 'bp_core_setup_message', 5 );
     737
     738/**
     739 * Renders a feedback message (either error or success message) to the theme template.
     740 * The hook action 'template_notices' is used to call this function, it is not called directly.
     741 *
     742 * @package BuddyPress Core
     743 * @global BuddyPress $bp The one true BuddyPress instance
     744 */
     745function bp_core_render_message() {
     746    global $bp;
     747
     748    if ( !empty( $bp->template_message ) ) :
     749        $type    = ( 'success' == $bp->template_message_type ) ? 'updated' : 'error';
     750        $content = apply_filters( 'bp_core_render_message_content', $bp->template_message, $type ); ?>
     751
     752        <div id="message" class="bp-template-notice <?php echo $type; ?>">
     753
     754            <?php echo $content; ?>
     755
     756        </div>
     757
     758    <?php
     759
     760        do_action( 'bp_core_render_message' );
     761
     762    endif;
     763}
     764
     765/** Last active ***************************************************************/
     766
    531767/**
    532768 * Record user activity to the database. Many functions use a "last active" feature to
     
    585821}
    586822
    587 /**
    588  * Get the path of of the current site.
    589  *
    590  * @package BuddyPress Core
    591  *
    592  * @global object $current_site
    593  * @return string
    594  */
    595 function bp_core_get_site_path() {
    596     global $current_site;
    597 
    598     if ( is_multisite() )
    599         $site_path = $current_site->path;
    600     else {
    601         $site_path = (array) explode( '/', home_url() );
    602 
    603         if ( count( $site_path ) < 2 )
    604             $site_path = '/';
    605         else {
    606             // Unset the first three segments (http(s)://domain.com part)
    607             unset( $site_path[0] );
    608             unset( $site_path[1] );
    609             unset( $site_path[2] );
    610 
    611             if ( !count( $site_path ) )
    612                 $site_path = '/';
    613             else
    614                 $site_path = '/' . implode( '/', $site_path ) . '/';
    615         }
    616     }
    617 
    618     return apply_filters( 'bp_core_get_site_path', $site_path );
    619 }
    620 
    621 /**
    622  * Performs a status safe wp_redirect() that is compatible with bp_catch_uri()
    623  *
    624  * @package BuddyPress Core
    625  * @uses wp_safe_redirect()
    626  */
    627 function bp_core_redirect( $location, $status = 302 ) {
    628 
    629     // On some setups, passing the value of wp_get_referer() may result in an
    630     // empty value for $location, which results in an error. Ensure that we
    631     // have a valid URL.
    632     if ( empty( $location ) )
    633         $location = bp_get_root_domain();
    634 
    635     // Make sure we don't call status_header() in bp_core_do_catch_uri() as this
    636     // conflicts with wp_redirect() and wp_safe_redirect().
    637     buddypress()->no_status_set = true;
    638 
    639     wp_safe_redirect( $location, $status );
    640     die;
    641 }
    642 
    643 /**
    644  * Returns the referrer URL without the http(s)://
    645  *
    646  * @package BuddyPress Core
    647  * @return The referrer URL
    648  */
    649 function bp_core_referrer() {
    650     $referer = explode( '/', wp_get_referer() );
    651     unset( $referer[0], $referer[1], $referer[2] );
    652     return implode( '/', $referer );
    653 }
    654 
    655 /**
    656  * Adds illegal names to WP so that root components will not conflict with
    657  * blog names on a subdirectory installation.
    658  *
    659  * For example, it would stop someone creating a blog with the slug "groups".
    660  */
    661 function bp_core_add_illegal_names() {
    662     update_site_option( 'illegal_names', get_site_option( 'illegal_names' ), array() );
    663 }
    664 
    665 /**
    666  * A javascript free implementation of the search functions in BuddyPress
    667  *
    668  * @package BuddyPress Core
    669  * @param string $slug The slug to redirect to for searching.
    670  */
    671 function bp_core_action_search_site( $slug = '' ) {
    672 
    673     if ( !bp_is_current_component( bp_get_search_slug() ) )
    674         return;
    675 
    676     if ( empty( $_POST['search-terms'] ) ) {
    677         bp_core_redirect( bp_get_root_domain() );
    678         return;
    679     }
    680 
    681     $search_terms = stripslashes( $_POST['search-terms'] );
    682     $search_which = !empty( $_POST['search-which'] ) ? $_POST['search-which'] : '';
    683     $query_string = '/?s=';
    684 
    685     if ( empty( $slug ) ) {
    686         switch ( $search_which ) {
    687             case 'posts':
    688                 $slug = '';
    689                 $var  = '/?s=';
    690 
    691                 // If posts aren't displayed on the front page, find the post page's slug.
    692                 if ( 'page' == get_option( 'show_on_front' ) ) {
    693                     $page = get_post( get_option( 'page_for_posts' ) );
    694 
    695                     if ( !is_wp_error( $page ) && !empty( $page->post_name ) ) {
    696                         $slug = $page->post_name;
    697                         $var  = '?s=';
    698                     }
    699                 }
    700                 break;
    701 
    702             case 'blogs':
    703                 $slug = bp_is_active( 'blogs' )  ? bp_get_blogs_root_slug()  : '';
    704                 break;
    705 
    706             case 'forums':
    707                 $slug = bp_is_active( 'forums' ) ? bp_get_forums_root_slug() : '';
    708                 $query_string = '/?fs=';
    709                 break;
    710 
    711             case 'groups':
    712                 $slug = bp_is_active( 'groups' ) ? bp_get_groups_root_slug() : '';
    713                 break;
    714 
    715             case 'members':
    716             default:
    717                 $slug = bp_get_members_root_slug();
    718                 break;
    719         }
    720 
    721         if ( empty( $slug ) && 'posts' != $search_which ) {
    722             bp_core_redirect( bp_get_root_domain() );
    723             return;
    724         }
    725     }
    726 
    727     bp_core_redirect( apply_filters( 'bp_core_search_site', home_url( $slug . $query_string . urlencode( $search_terms ) ), $search_terms ) );
    728 }
    729 add_action( 'bp_init', 'bp_core_action_search_site', 7 );
    730 
    731 /**
    732  * Prints the generation time in the footer of the site.
    733  *
    734  * @package BuddyPress Core
    735  */
    736 function bp_core_print_generation_time() {
    737 ?>
    738 
    739 <!-- Generated in <?php timer_stop(1); ?> seconds. (<?php echo get_num_queries(); ?> q) -->
    740 
    741     <?php
    742 }
    743 add_action( 'wp_footer', 'bp_core_print_generation_time' );
    744 
    745 /**
    746  * Load the buddypress translation file for current language
    747  *
    748  * @package BuddyPress Core
    749  */
    750 function bp_core_load_buddypress_textdomain() {
    751     $locale        = apply_filters( 'buddypress_locale', get_locale() );
    752     $mofile        = sprintf( 'buddypress-%s.mo', $locale );
    753     $mofile_global = WP_LANG_DIR . '/' . $mofile;
    754     $mofile_local  = BP_PLUGIN_DIR . 'bp-languages/' . $mofile;
    755 
    756     if ( file_exists( $mofile_global ) )
    757         return load_textdomain( 'buddypress', $mofile_global );
    758     elseif ( file_exists( $mofile_local ) )
    759         return load_textdomain( 'buddypress', $mofile_local );
    760     else
    761         return false;
    762 }
    763 add_action ( 'bp_core_loaded', 'bp_core_load_buddypress_textdomain' );
    764 
    765 /**
    766  * Initializes {@link BP_Embed} after everything is loaded.
    767  *
    768  * @global object $bp BuddyPress global settings
    769  * @package BuddyPress Core
    770  * @since BuddyPress (1.5)
    771  */
    772 function bp_embed_init() {
    773     global $bp;
    774 
    775     if ( empty( $bp->embed ) )
    776         $bp->embed = new BP_Embed();
    777 }
    778 add_action( 'bp_init', 'bp_embed_init', 9 );
    779 
    780 /**
    781  * This function originally let plugins add support for pages in the root of the install.
    782  * These root level pages are now handled by actual WordPress pages and this function is now
    783  * a convenience for compatibility with the new method.
    784  *
    785  * @global $bp BuddyPress global settings
    786  * @param $slug str The slug of the component
    787  */
    788 function bp_core_add_root_component( $slug ) {
    789     global $bp;
    790 
    791     if ( empty( $bp->pages ) )
    792         $bp->pages = bp_core_get_directory_pages();
    793 
    794     $match = false;
    795 
    796     // Check if the slug is registered in the $bp->pages global
    797     foreach ( (array) $bp->pages as $key => $page ) {
    798         if ( $key == $slug || $page->slug == $slug )
    799             $match = true;
    800     }
    801 
    802     // If there was no match, add a page for this root component
    803     if ( empty( $match ) ) {
    804         $bp->add_root[] = $slug;
    805     }
    806 
    807     // Make sure that this component is registered as requiring a top-level directory
    808     if ( isset( $bp->{$slug} ) ) {
    809         $bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;
    810         $bp->{$slug}->has_directory = true;
    811     }
    812 }
    813 
    814 function bp_core_create_root_component_page() {
    815     global $bp;
    816 
    817     $new_page_ids = array();
    818 
    819     foreach ( (array) $bp->add_root as $slug )
    820         $new_page_ids[$slug] = wp_insert_post( array( 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_title' => ucwords( $slug ), 'post_status' => 'publish', 'post_type' => 'page' ) );
    821 
    822     $page_ids = array_merge( (array) $new_page_ids, (array) bp_core_get_directory_page_ids() );
    823     bp_core_update_directory_page_ids( $page_ids );
    824 }
    825 
    826 /**
    827  * Is this the root blog ID?
    828  *
    829  * @package BuddyPress
    830  * @since BuddyPress (1.5)
    831  *
    832  * @param int $blog_id Optional. Defaults to the current blog id.
    833  * @return bool $is_root_blog Returns true if this is bp_get_root_blog_id().
    834  */
    835 function bp_is_root_blog( $blog_id = 0 ) {
    836 
    837     // Assume false
    838     $is_root_blog = false;
    839 
    840     // Use current blog if no ID is passed
    841     if ( empty( $blog_id ) )
    842         $blog_id = get_current_blog_id();
    843 
    844     // Compare to root blog ID
    845     if ( $blog_id == bp_get_root_blog_id() )
    846         $is_root_blog = true;
    847 
    848     return (bool) apply_filters( 'bp_is_root_blog', (bool) $is_root_blog );
    849 }
    850 
    851 /**
    852  * Is this bp_get_root_blog_id()?
    853  *
    854  * @package BuddyPress
    855  * @since BuddyPress (1.5)
    856  *
    857  * @return int Return the root site ID
    858  */
    859 function bp_get_root_blog_id() {
    860     global $bp;
    861 
    862     return (int) apply_filters( 'bp_get_root_blog_id', (int) $bp->root_blog_id );
    863 }
     823/** Meta **********************************************************************/
    864824
    865825/**
     
    949909}
    950910
    951 /**
    952  * Are we running username compatibility mode?
    953  *
    954  * @package BuddyPress
    955  * @since BuddyPress (1.5)
    956  *
    957  * @uses apply_filters() Filter 'bp_is_username_compatibility_mode' to alter
    958  * @return bool False when compatibility mode is disabled (default); true when enabled
    959  */
    960 function bp_is_username_compatibility_mode() {
    961     return apply_filters( 'bp_is_username_compatibility_mode', defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) && BP_ENABLE_USERNAME_COMPATIBILITY_MODE );
    962 }
    963 
    964 /**
    965  * Are we running multiblog mode?
    966  *
    967  * Note that BP_ENABLE_MULTIBLOG is different from (but dependent on) WordPress
    968  * Multisite. "Multiblog" is BuddyPress setup that allows BuddyPress components
    969  * to be viewed on every blog on the network, each with their own settings.
    970  *
    971  * Thus, instead of having all 'boonebgorges' links go to
    972  *   http://example.com/members/boonebgorges
    973  * on the root blog, each blog will have its own version of the same content, eg
    974  *   http://site2.example.com/members/boonebgorges (for subdomains)
    975  *   http://example.com/site2/members/boonebgorges (for subdirectories)
    976  *
    977  * Multiblog mode is disabled by default, meaning that all BuddyPress content
    978  * must be viewed on the root blog. It's also recommended not to use the
    979  * BP_ENABLE_MULTIBLOG constant beyond 1.7, as BuddyPress can now be activated
    980  * on individual sites.
    981  *
    982  * Why would you want to use this? Originally it was intended to allow
    983  * BuddyPress to live in mu-plugins and be visible on mapped domains. This is
    984  * a very small use-case with large architectural shortcomings, so do not go
    985  * down this road unless you specifically need to.
    986  *
    987  * @package BuddyPress
    988  * @since BuddyPress (1.5)
    989  *
    990  * @uses apply_filters() Filter 'bp_is_multiblog_mode' to alter
    991  * @return bool False when multiblog mode is disabled (default); true when enabled
    992  */
    993 function bp_is_multiblog_mode() {
    994 
    995     // Setup some default values
    996     $retval         = false;
    997     $is_multisite   = is_multisite();
    998     $network_active = bp_is_network_activated();
    999     $is_multiblog   = defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG;
    1000 
    1001     // Multisite, Network Activated, and Specifically Multiblog
    1002     if ( $is_multisite && $network_active && $is_multiblog ) {
    1003         $retval = true;
    1004 
    1005     // Multisite, but not network activated
    1006     } elseif ( $is_multisite && ! $network_active ) {
    1007         $retval = true;
    1008     }
    1009 
    1010     return apply_filters( 'bp_is_multiblog_mode', $retval );
    1011 }
    1012 
    1013 /**
    1014  * Should we use the WP Toolbar?
    1015  *
    1016  * The WP Toolbar, introduced in WP 3.1, is fully supported in BuddyPress as of BP 1.5.
    1017  * For BP 1.6, the WP Toolbar is the default.
    1018  *
    1019  * @return bool False when WP Toolbar support is disabled; true when enabled (default)
    1020  * @since BuddyPress (1.5)
    1021  * @uses apply_filters() Filter 'bp_use_wp_admin_bar' to alter
    1022  */
    1023 function bp_use_wp_admin_bar() {
    1024     $use_admin_bar = true;
    1025 
    1026     // Has the WP Toolbar constant been explicity set?
    1027     if ( defined( 'BP_USE_WP_ADMIN_BAR' ) && ! BP_USE_WP_ADMIN_BAR )
    1028         $use_admin_bar = false;
    1029 
    1030     // Has the admin chosen to use the BuddyBar during an upgrade?
    1031     elseif ( (bool) bp_get_option( '_bp_force_buddybar', false ) )
    1032         $use_admin_bar = false;
    1033 
    1034     return apply_filters( 'bp_use_wp_admin_bar', $use_admin_bar );
    1035 }
    1036 
    1037 /**
    1038  * A utility for parsing individual function arguments into an array.
    1039  *
    1040  * The purpose of this function is to help with backward compatibility in cases where
    1041  *
    1042  *   function foo( $bar = 1, $baz = false, $barry = array(), $blip = false ) { // ...
    1043  *
    1044  * is deprecated in favor of
    1045  *
    1046  *   function foo( $args = array() ) {
    1047  *       $defaults = array(
    1048  *           'bar'  => 1,
    1049  *           'arg2' => false,
    1050  *           'arg3' => array(),
    1051  *           'arg4' => false,
    1052  *       );
    1053  *       $r = wp_parse_args( $args, $defaults ); // ...
    1054  *
    1055  * The first argument, $old_args_keys, is an array that matches the parameter positions (keys) to
    1056  * the new $args keys (values):
    1057  *
    1058  *   $old_args_keys = array(
    1059  *       0 => 'bar', // because $bar was the 0th parameter for foo()
    1060  *       1 => 'baz', // because $baz was the 1st parameter for foo()
    1061  *       2 => 'barry', // etc
    1062  *       3 => 'blip'
    1063  *   );
    1064  *
    1065  * For the second argument, $func_args, you should just pass the value of func_get_args().
    1066  *
    1067  * @since BuddyPress (1.6)
    1068  * @param array $old_args_keys
    1069  * @param array $func_args
    1070  * @return array $new_args
    1071  */
    1072 function bp_core_parse_args_array( $old_args_keys, $func_args ) {
    1073     $new_args = array();
    1074 
    1075     foreach( $old_args_keys as $arg_num => $arg_key ) {
    1076         if ( isset( $func_args[$arg_num] ) ) {
    1077             $new_args[$arg_key] = $func_args[$arg_num];
    1078         }
    1079     }
    1080 
    1081     return $new_args;
    1082 }
    1083 
    1084 /**
    1085  * Sanitize an 'order' parameter for use in building SQL queries
    1086  *
    1087  * Strings like 'DESC', 'desc', ' desc' will be interpreted into 'DESC'.
    1088  * Everything else becomes 'ASC'.
    1089  *
    1090  * @since BuddyPress (1.8)
    1091  * @param string $order The 'order' string, as passed to the SQL constructor
    1092  * @return string The sanitized value 'DESC' or 'ASC'
    1093  */
    1094 function bp_esc_sql_order( $order = '' ) {
    1095     $order = strtoupper( trim( $order ) );
    1096     return 'DESC' === $order ? 'DESC' : 'ASC';
    1097 }
    1098 
    1099911/** Embeds ********************************************************************/
     912
     913/**
     914 * Initializes {@link BP_Embed} after everything is loaded.
     915 *
     916 * @global object $bp BuddyPress global settings
     917 * @package BuddyPress Core
     918 * @since BuddyPress (1.5)
     919 */
     920function bp_embed_init() {
     921    global $bp;
     922
     923    if ( empty( $bp->embed ) )
     924        $bp->embed = new BP_Embed();
     925}
     926add_action( 'bp_init', 'bp_embed_init', 9 );
    1100927
    1101928/**
     
    12131040}
    12141041
     1042/** Multisite *****************************************************************/
     1043
     1044/**
     1045 * Is this the root blog ID?
     1046 *
     1047 * @package BuddyPress
     1048 * @since BuddyPress (1.5)
     1049 *
     1050 * @param int $blog_id Optional. Defaults to the current blog id.
     1051 * @return bool $is_root_blog Returns true if this is bp_get_root_blog_id().
     1052 */
     1053function bp_is_root_blog( $blog_id = 0 ) {
     1054
     1055    // Assume false
     1056    $is_root_blog = false;
     1057
     1058    // Use current blog if no ID is passed
     1059    if ( empty( $blog_id ) )
     1060        $blog_id = get_current_blog_id();
     1061
     1062    // Compare to root blog ID
     1063    if ( $blog_id == bp_get_root_blog_id() )
     1064        $is_root_blog = true;
     1065
     1066    return (bool) apply_filters( 'bp_is_root_blog', (bool) $is_root_blog );
     1067}
     1068
     1069/**
     1070 * Is this bp_get_root_blog_id()?
     1071 *
     1072 * @package BuddyPress
     1073 * @since BuddyPress (1.5)
     1074 *
     1075 * @return int Return the root site ID
     1076 */
     1077function bp_get_root_blog_id() {
     1078    global $bp;
     1079
     1080    return (int) apply_filters( 'bp_get_root_blog_id', (int) $bp->root_blog_id );
     1081}
     1082
     1083/**
     1084 * Are we running multiblog mode?
     1085 *
     1086 * Note that BP_ENABLE_MULTIBLOG is different from (but dependent on) WordPress
     1087 * Multisite. "Multiblog" is BuddyPress setup that allows BuddyPress components
     1088 * to be viewed on every blog on the network, each with their own settings.
     1089 *
     1090 * Thus, instead of having all 'boonebgorges' links go to
     1091 *   http://example.com/members/boonebgorges
     1092 * on the root blog, each blog will have its own version of the same content, eg
     1093 *   http://site2.example.com/members/boonebgorges (for subdomains)
     1094 *   http://example.com/site2/members/boonebgorges (for subdirectories)
     1095 *
     1096 * Multiblog mode is disabled by default, meaning that all BuddyPress content
     1097 * must be viewed on the root blog. It's also recommended not to use the
     1098 * BP_ENABLE_MULTIBLOG constant beyond 1.7, as BuddyPress can now be activated
     1099 * on individual sites.
     1100 *
     1101 * Why would you want to use this? Originally it was intended to allow
     1102 * BuddyPress to live in mu-plugins and be visible on mapped domains. This is
     1103 * a very small use-case with large architectural shortcomings, so do not go
     1104 * down this road unless you specifically need to.
     1105 *
     1106 * @package BuddyPress
     1107 * @since BuddyPress (1.5)
     1108 *
     1109 * @uses apply_filters() Filter 'bp_is_multiblog_mode' to alter
     1110 * @return bool False when multiblog mode is disabled (default); true when enabled
     1111 */
     1112function bp_is_multiblog_mode() {
     1113
     1114    // Setup some default values
     1115    $retval         = false;
     1116    $is_multisite   = is_multisite();
     1117    $network_active = bp_is_network_activated();
     1118    $is_multiblog   = defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG;
     1119
     1120    // Multisite, Network Activated, and Specifically Multiblog
     1121    if ( $is_multisite && $network_active && $is_multiblog ) {
     1122        $retval = true;
     1123
     1124    // Multisite, but not network activated
     1125    } elseif ( $is_multisite && ! $network_active ) {
     1126        $retval = true;
     1127    }
     1128
     1129    return apply_filters( 'bp_is_multiblog_mode', $retval );
     1130}
     1131
    12151132/**
    12161133 * Is BuddyPress active at the network level for this network?
     
    13421259    return $result;
    13431260}
     1261
     1262/** Miscellaneous hooks *******************************************************/
     1263
     1264/**
     1265 * Load the buddypress translation file for current language
     1266 *
     1267 * @package BuddyPress Core
     1268 */
     1269function bp_core_load_buddypress_textdomain() {
     1270    $locale        = apply_filters( 'buddypress_locale', get_locale() );
     1271    $mofile        = sprintf( 'buddypress-%s.mo', $locale );
     1272    $mofile_global = WP_LANG_DIR . '/' . $mofile;
     1273    $mofile_local  = BP_PLUGIN_DIR . 'bp-languages/' . $mofile;
     1274
     1275    if ( file_exists( $mofile_global ) )
     1276        return load_textdomain( 'buddypress', $mofile_global );
     1277    elseif ( file_exists( $mofile_local ) )
     1278        return load_textdomain( 'buddypress', $mofile_local );
     1279    else
     1280        return false;
     1281}
     1282add_action ( 'bp_core_loaded', 'bp_core_load_buddypress_textdomain' );
     1283
     1284/**
     1285 * A javascript free implementation of the search functions in BuddyPress
     1286 *
     1287 * @package BuddyPress Core
     1288 * @param string $slug The slug to redirect to for searching.
     1289 */
     1290function bp_core_action_search_site( $slug = '' ) {
     1291
     1292    if ( !bp_is_current_component( bp_get_search_slug() ) )
     1293        return;
     1294
     1295    if ( empty( $_POST['search-terms'] ) ) {
     1296        bp_core_redirect( bp_get_root_domain() );
     1297        return;
     1298    }
     1299
     1300    $search_terms = stripslashes( $_POST['search-terms'] );
     1301    $search_which = !empty( $_POST['search-which'] ) ? $_POST['search-which'] : '';
     1302    $query_string = '/?s=';
     1303
     1304    if ( empty( $slug ) ) {
     1305        switch ( $search_which ) {
     1306            case 'posts':
     1307                $slug = '';
     1308                $var  = '/?s=';
     1309
     1310                // If posts aren't displayed on the front page, find the post page's slug.
     1311                if ( 'page' == get_option( 'show_on_front' ) ) {
     1312                    $page = get_post( get_option( 'page_for_posts' ) );
     1313
     1314                    if ( !is_wp_error( $page ) && !empty( $page->post_name ) ) {
     1315                        $slug = $page->post_name;
     1316                        $var  = '?s=';
     1317                    }
     1318                }
     1319                break;
     1320
     1321            case 'blogs':
     1322                $slug = bp_is_active( 'blogs' )  ? bp_get_blogs_root_slug()  : '';
     1323                break;
     1324
     1325            case 'forums':
     1326                $slug = bp_is_active( 'forums' ) ? bp_get_forums_root_slug() : '';
     1327                $query_string = '/?fs=';
     1328                break;
     1329
     1330            case 'groups':
     1331                $slug = bp_is_active( 'groups' ) ? bp_get_groups_root_slug() : '';
     1332                break;
     1333
     1334            case 'members':
     1335            default:
     1336                $slug = bp_get_members_root_slug();
     1337                break;
     1338        }
     1339
     1340        if ( empty( $slug ) && 'posts' != $search_which ) {
     1341            bp_core_redirect( bp_get_root_domain() );
     1342            return;
     1343        }
     1344    }
     1345
     1346    bp_core_redirect( apply_filters( 'bp_core_search_site', home_url( $slug . $query_string . urlencode( $search_terms ) ), $search_terms ) );
     1347}
     1348add_action( 'bp_init', 'bp_core_action_search_site', 7 );
     1349
     1350/**
     1351 * Prints the generation time in the footer of the site.
     1352 *
     1353 * @package BuddyPress Core
     1354 */
     1355function bp_core_print_generation_time() {
     1356?>
     1357
     1358<!-- Generated in <?php timer_stop(1); ?> seconds. (<?php echo get_num_queries(); ?> q) -->
     1359
     1360    <?php
     1361}
     1362add_action( 'wp_footer', 'bp_core_print_generation_time' );
Note: See TracChangeset for help on using the changeset viewer.