Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/29/2015 10:12:48 PM (9 years ago)
Author:
imath
Message:

Rely on the BP Theme Compat API to trigger the Cover Image feature

BuddyPress Cover Images are first targeting themes using the BP Theme Compat API. We want to make sure Cover images will look awesome in most WordPress themes. We also want to make sure standalone BuddyPress themes (eg: BP Default) will have time to implement the feature peacefully.

The BP Theme Compat API now includes 3 new functions :

  • bp_set_theme_compat_feature() to register the feature settings for targeted components (eg: xProfile, Groups..).
  • bp_get_theme_compat_feature() to get the requested feature settings (eg: cover_image)
  • bp_register_theme_compat_default_features() is automatically registering the feature for themes using the BP Theme Compat API and the BP Legacy template pack.

Props johnjamesjacoby, boone, djpaul, modemlooper, BuddyBoss

See #6570

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-theme-compatibility.php

    r10108 r10153  
    434434
    435435    return buddypress()->theme_compat->original_template;
     436}
     437
     438/**
     439 * Set a theme compat feature
     440 *
     441 * @since 2.4.0
     442 *
     443 * @param  string $theme_id the theme id (eg: legacy)
     444 * @param  array  $feature  an associative array (eg: array( name => 'feature_name', 'settings' => array() ))
     445 */
     446function bp_set_theme_compat_feature( $theme_id, $feature = array() ) {
     447    if ( empty( $theme_id ) || empty( $feature['name'] ) ) {
     448        return;
     449    }
     450
     451    // Get BuddyPress instance
     452    $bp = buddypress();
     453
     454    // Get current theme compat theme
     455    $theme_compat_theme = $bp->theme_compat->theme;
     456
     457    // Bail if the Theme Compat theme is not in use
     458    if ( $theme_id !== bp_get_theme_compat_id() ) {
     459        return;
     460    }
     461
     462    $features = $theme_compat_theme->__get( 'features' );
     463    if ( empty( $features ) ) {
     464        $features = array();
     465    }
     466
     467    // Bail if the feature is already registered or no settings were provided
     468    if ( isset( $features[ $feature['name'] ] ) || empty( $feature['settings'] ) ) {
     469        return;
     470    }
     471
     472    // Add the feature
     473    $features[ $feature['name'] ] = (object) $feature['settings'];
     474
     475    // The feature is attached to components
     476    if ( isset( $features[ $feature['name'] ]->components ) ) {
     477        // Set the feature for each concerned component
     478        foreach ( (array) $features[ $feature['name'] ]->components as $component ) {
     479            // The xProfile component is specific
     480            if ( 'xprofile' === $component ) {
     481                $component = 'profile';
     482            }
     483
     484            if ( isset( $bp->{$component} ) ) {
     485                if ( isset( $bp->{$component}->features ) ) {
     486                    $bp->{$component}->features[] = $feature['name'];
     487                } else {
     488                    $bp->{$component}->features = array( $feature['name'] );
     489                }
     490            }
     491        }
     492    }
     493
     494    // Finally update the theme compat features
     495    $theme_compat_theme->__set( 'features', $features );
     496}
     497
     498/**
     499 * Get a theme compat feature
     500 *
     501 * @since 2.4.0
     502 *
     503 * @param  string $feature the feature (eg: cover_image)
     504 * @return object          the feature settings.
     505 */
     506function bp_get_theme_compat_feature( $feature = '' ) {
     507    // Get current theme compat theme
     508    $theme_compat_theme = buddypress()->theme_compat->theme;
     509
     510    // Get features
     511    $features = $theme_compat_theme->__get( 'features' );
     512
     513    if ( ! isset( $features[ $feature ] ) ) {
     514        return false;
     515    }
     516
     517    return $features[ $feature ];
     518}
     519
     520/**
     521 * Setup the theme's features
     522 *
     523 * Note: BP Legacy's buddypress-functions.php is not loaded in WP Administration
     524 * as it's loaded using bp_locate_template(). That's why this function is here.
     525 *
     526 * @since 2.4.0
     527 *
     528 * @global $content_width the content width of the theme
     529 */
     530function bp_register_theme_compat_default_features() {
     531    global $content_width;
     532
     533    // If the current theme doesn't need theme compat, bail at this point.
     534    if ( ! bp_use_theme_compat_with_current_theme() ) {
     535        return;
     536    }
     537
     538    // Make sure BP Legacy is the Theme Compat in use.
     539    if ( 'legacy' !== bp_get_theme_compat_id() ) {
     540        return;
     541    }
     542
     543    // Get the theme
     544    $current_theme = wp_get_theme();
     545    $theme_handle  = $current_theme->get_stylesheet();
     546    $parent        = $current_theme->parent();
     547
     548    if ( $parent ) {
     549        $theme_handle = $parent->get_stylesheet();
     550    }
     551
     552    /**
     553     * Since Companion stylesheets, the $content_width is smaller
     554     * than the width used by BuddyPress, so we need to manually set the
     555     * content width for the concerned themes.
     556     *
     557     * array( stylesheet => content width used by BuddyPress )
     558     */
     559    $bp_content_widths = array(
     560        'twentyfifteen'  => 1300,
     561        'twentyfourteen' => 955,
     562        'twentythirteen' => 890,
     563    );
     564
     565    // Default values
     566    $bp_content_width = (int) $content_width;
     567    $bp_handle        = 'bp-legacy-css';
     568
     569    // Specific to themes having companion stylesheets
     570    if ( isset( $bp_content_widths[ $theme_handle ] ) ) {
     571        $bp_content_width = $bp_content_widths[ $theme_handle ];
     572        $bp_handle        = 'bp-' . $theme_handle;
     573    }
     574
     575    if ( is_rtl() ) {
     576        $bp_handle .= '-rtl';
     577    }
     578
     579    $top_offset    = 150;
     580    $avatar_height = apply_filters( 'bp_core_avatar_full_height', $top_offset );
     581
     582    if ( $avatar_height > $top_offset ) {
     583        $top_offset = $avatar_height;
     584    }
     585
     586    bp_set_theme_compat_feature( 'legacy', array(
     587        'name'     => 'cover_image',
     588        'settings' => array(
     589            'components'   => array( 'xprofile', 'groups' ),
     590            'width'        => $bp_content_width,
     591            'height'       => $top_offset + round( $avatar_height / 2 ),
     592            'callback'     => 'bp_legacy_theme_cover_image',
     593            'theme_handle' => $bp_handle,
     594        ),
     595    ) );
    436596}
    437597
Note: See TracChangeset for help on using the changeset viewer.