Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the way the themes directory is registered #24

Merged
merged 2 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 1 addition & 28 deletions 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 All @@ -72,33 +72,6 @@ function bp_classic_default_theme_root_uri( $theme_root_uri, $siteurl, $styleshe
}
add_filter( 'theme_root_uri', 'bp_classic_default_theme_root_uri', 10, 3 );

/**
* Once BuddyPress 12.0.0 is updated, eventually updateds active theme options.
*
* In case the BP Default theme was active in BuddyPress < 12.0.0, it's required
* to update template and eventually stylesheet root options to keep BP Default
* as the active theme safely.
*
* @since 1.0.0
*/
function bp_classic_update_bp_default_theme_options() {
// If the current active template theme is not BP Default from the BuddyPress plugin, stop.
if ( false === strpos( get_template_directory(), 'plugins/buddypress/bp-themes/bp-default' ) ) {
return;
}

$theme_root = str_replace( content_url(), '', bp_classic_get_themes_url() );

if ( 'bp-default' === get_template() ) {
update_option( 'template_root', $theme_root );
}

if ( 'bp-default' === get_stylesheet() ) {
update_option( 'stylesheet_root', $theme_root );
}
}
add_action( 'bp_updated_to_12_0', 'bp_classic_update_bp_default_theme_options' );

/**
* Inits Legacy navigation to preserve backward compatibility with BP < 2.6 code.
*
Expand Down
73 changes: 72 additions & 1 deletion inc/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,5 +587,76 @@ 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.
*/
$register_theme = apply_filters( 'bp_do_register_theme_directory', $register );

/*
* In case the BP Default theme was active in BuddyPress < 12.0.0, it's required
* to update template and eventually stylesheet root options to keep BP Default
* as the active theme safely.
*/
if ( $register_theme && false === strpos( get_option( 'template_root', '' ), '/plugins/bp-classic/themes' ) ) {
$theme_root = str_replace( content_url(), '', bp_classic_get_themes_url() );

if ( 'bp-default' === get_template() ) {
update_option( 'template_root', $theme_root );
}

if ( 'bp-default' === get_stylesheet() ) {
update_option( 'stylesheet_root', $theme_root );
}
}

return $register_theme;
}

/**
* 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 );