Skip to content

Commit

Permalink
Improve the way the themes directory is registered
Browse files Browse the repository at this point in the history
This should fix #23
  • Loading branch information
imath committed Aug 12, 2023
1 parent 8c4a50a commit 7d76ff2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 72 deletions.
70 changes: 0 additions & 70 deletions class-bp-classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,73 +268,6 @@ public static function switch_directory_post_type() {
bp_classic_switch_directory_post_type( $post_type );
}

/**
* Determine whether BuddyPress should register the `themes` directory.
*
* @since 1.0.0
*
* @return boolean True if the `themes` directory should be registered.
* False otherwise.
*/
public static function do_register_theme_directory() {
$register = false;

if ( ! self::is_buddypress_supported() ) {
return $register;
}

/*
* If bp-default exists in another theme directory, bail.
* This ensures that the version of bp-default in the regular themes
* directory will always take precedence, as part of a migration away
* from the version packaged with BuddyPress.
*/
foreach ( array_values( (array) $GLOBALS['wp_theme_directories'] ) as $directory ) {
if ( is_dir( $directory . '/bp-default' ) ) {
return $register;
}
}

// If the current theme is bp-default (or a bp-default child), BP should register its directory.
$register = 'bp-default' === get_stylesheet() || 'bp-default' === get_template();

// Legacy sites continue to have the theme registered.
if ( empty( $register ) && ( 1 === (int) get_site_option( '_bp_retain_bp_default' ) ) ) {
$register = true;
}

/**
* Filters whether BuddyPress should register the bp-themes directory.
*
* @since 1.0.0
*
* @param bool $register If bp-themes should be registered.
*/
return apply_filters( 'bp_do_register_theme_directory', $register );
}

/**
* Set up BuddyPress's legacy theme directory.
*
* BuddyPress is no more including BP Default. This plugin
* is there to provide backward compatibility to BuddyPress
* setups still using this deprecated theme.
*
* @since 1.0.0
*
* @param BuddyPress $bp The main BuddyPress instance.
*/
public static function register_theme_directory( $bp ) {
if ( ! self::do_register_theme_directory() ) {
return;
}

$bp->old_themes_dir = plugin_dir_path( __FILE__ ) . 'themes';
$bp->old_themes_url = plugins_url( 'themes', __FILE__ );

register_theme_directory( $bp->old_themes_dir );
}

/**
* Return an instance of this class.
*
Expand Down Expand Up @@ -375,9 +308,6 @@ function bp_classic() {
add_action( 'network_admin_notices', array( 'BP_Classic', 'admin_notice' ) );
add_action( 'admin_notices', array( 'BP_Classic', 'admin_notice' ) );

// Eventually registers the BP Default theme directory.
add_action( 'bp_after_setup_actions', array( 'BP_Classic', 'register_theme_directory' ) );

/*
* Use Activation and Deactivation to switch directory pages post type between WP pages
* and BuddyPress one.
Expand Down
2 changes: 1 addition & 1 deletion inc/core/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function bp_register_theme_directory() {
*/
do_action( 'bp_register_theme_directory' );
}
add_action( 'bp_loaded', 'bp_register_theme_directory', 14 );
add_action( 'bp_loaded', 'bp_register_theme_directory', 4 );

/**
* If the BP Classic is symlinked the Theme Root URI might not be set the right way.
Expand Down
54 changes: 53 additions & 1 deletion inc/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,5 +587,57 @@ function bp_core_get_user_domain( $user_id = 0, $user_nicename = false, $user_lo
* False otherwise.
*/
function bp_do_register_theme_directory() {
return BP_Classic::do_register_theme_directory();
$register = false;

/*
* If bp-default exists in another theme directory, bail.
* This ensures that the version of bp-default in the regular themes
* directory will always take precedence, as part of a migration away
* from the version packaged with BuddyPress.
*/
foreach ( array_values( (array) $GLOBALS['wp_theme_directories'] ) as $directory ) {
if ( is_dir( $directory . '/bp-default' ) ) {
return $register;
}
}

// If the current theme is bp-default (or a bp-default child), BP should register its directory.
$register = 'bp-default' === get_stylesheet() || 'bp-default' === get_template();

// Legacy sites continue to have the theme registered.
if ( empty( $register ) && ( 1 === (int) get_site_option( '_bp_retain_bp_default' ) ) ) {
$register = true;
}

/**
* Filters whether BuddyPress should register the bp-themes directory.
*
* @since 1.0.0
*
* @param bool $register If bp-themes should be registered.
*/
return apply_filters( 'bp_do_register_theme_directory', $register );
}

/**
* Set up BuddyPress's legacy theme directory.
*
* BuddyPress is no more including BP Default. This plugin
* is there to provide backward compatibility to BuddyPress
* setups still using this deprecated theme.
*
* @since 1.1.0
*/
function bp_classic_register_themes_directory() {
if ( ! bp_do_register_theme_directory() ) {
return;
}

$bp = buddypress();

$bp->old_themes_dir = bp_classic_get_themes_dir();
$bp->old_themes_url = bp_classic_get_themes_url();

register_theme_directory( $bp->old_themes_dir );
}
add_action( 'bp_register_theme_directory', 'bp_classic_register_themes_directory', 1 );

0 comments on commit 7d76ff2

Please sign in to comment.