bp_core_activation_notice()

Verify that some BP prerequisites are set up properly, and notify the admin if not.

Description

On every Dashboard page, this function checks the following:

  • that pretty permalinks are enabled.
  • that every BP component that needs a WP page for a directory has one.
  • that no WP page has multiple BP components associated with it. The administrator will be shown a notice for each check that fails.

Source

File: bp-core/admin/bp-core-admin-functions.php

function bp_core_activation_notice() {
	global $wp_rewrite, $wpdb;

	// Only the super admin gets warnings.
	if ( ! bp_current_user_can( 'bp_moderate' ) ) {
		return;
	}

	// Bail in user admin.
	if ( is_user_admin() ) {
		return;
	}

	// On multisite installs, don't load on a non-root blog, unless do_network_admin is overridden.
	if ( is_multisite() && bp_core_do_network_admin() && ! bp_is_root_blog() ) {
		return;
	}

	// Bail if in network admin, and BuddyPress is not network activated.
	if ( is_network_admin() && ! bp_is_network_activated() ) {
		return;
	}

	/**
	 * Check to make sure that the blog setup routine has run. This can't
	 * happen during the wizard because of the order which the components
	 * are loaded.
	 */
	if ( bp_is_active( 'blogs' ) ) {
		$bp    = buddypress();
		$count = $wpdb->get_var( "SELECT COUNT(*) FROM {$bp->blogs->table_name}" );

		if ( empty( $count ) ) {
			bp_blogs_record_existing_blogs();
		}
	}

	// Add notice if no rewrite rules are enabled.
	if ( empty( $wp_rewrite->permalink_structure ) ) {
		bp_core_add_admin_notice( sprintf( __( '<strong>BuddyBoss Platform is almost ready</strong>. You must <a href="%s">update your permalink structure</a> to something other than the default for it to work.', 'buddyboss' ), admin_url( 'options-permalink.php' ) ), 'error' );
	}

	// Get BuddyPress instance.
	$bp = buddypress();

	/**
	 * Check for orphaned BP components (BP component is enabled, no WP page exists).
	 */
	$orphaned_components = array();
	$wp_page_components  = array();

	// Only components with 'has_directory' require a WP page to function.
	foreach( array_keys( $bp->loaded_components ) as $component_id ) {
		if ( !empty( $bp->{$component_id}->has_directory ) ) {
			$wp_page_components[] = array(
				'id'   => $component_id,
				'name' => isset( $bp->{$component_id}->name ) ? $bp->{$component_id}->name : ucwords( $bp->{$component_id}->id )
			);
		}
	}

	// Activate and Register are special cases. They are not components but they need WP pages.
	// If user registration is disabled, we can skip this step.
	if ( bp_get_signup_allowed() ) {
		$wp_page_components[] = array(
			'id'   => 'activate',
			'name' => __( 'Activate', 'buddyboss' )
		);

		$wp_page_components[] = array(
			'id'   => 'register',
			'name' => __( 'Register', 'buddyboss' )
		);
	}

	// On the first admin screen after a new installation, this isn't set, so grab it to suppress
	// a misleading error message.
	if ( empty( $bp->pages->members ) ) {
		$bp->pages = bp_core_get_directory_pages();
	}

	foreach( $wp_page_components as $component ) {
		if ( !isset( $bp->pages->{$component['id']} ) ) {
			$orphaned_components[] = $component['name'];
		}
	}

	if ( function_exists( 'bp_nouveau_get_appearance_settings' ) ) {
		if ( bp_nouveau_get_appearance_settings( 'user_front_page' ) ) {
			if ( ! isset( $bp->pages->profile_dashboard ) ) {
				$orphaned_components[] = 'Profile Dashboard';
			}
		}
	}

	// If forum enabled and forum page is not set then add to forums to $orphaned_components components to show the notice.
	if ( bp_is_active( 'forums' ) ) {

		$id = (int) bp_get_option( '_bbp_root_slug_custom_slug');

		// Check the status of current set value.
		$status = get_post_status( $id );

		// Set the page id if page exists and in publish otherwise set blank.
		$id = ( '' !== $status && 'publish' === $status ) ? $id : '';

		if ( empty( $id ) ) {
			$orphaned_components[] = 'Forums';
		}
	}

	if ( !empty( $orphaned_components ) ) {
		$admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-pages' ), 'admin.php' ) );

		if ( isset( $_GET['page'] ) && 'bp-pages' === $_GET['page'] ) {
			$notice = sprintf( '%1$s',
				sprintf( __( 'The following active BuddyBoss Components do not have associated WordPress Pages: %s.',
					'buddyboss' ),
					'<strong>' . implode( '</strong>, <strong>',
						array_map( 'esc_html', $orphaned_components ) ) . '</strong>' )
				);
		} else {
			$notice = sprintf( '%1$s <a href="%2$s">%3$s</a>',
				sprintf( __( 'The following active BuddyBoss Components do not have associated WordPress Pages: %s.',
					'buddyboss' ),
					'<strong>' . implode( '</strong>, <strong>',
						array_map( 'esc_html', $orphaned_components ) ) . '</strong>' ),
				esc_url( $admin_url ),
				__( 'Repair', 'buddyboss' ) );
		}

		bp_core_add_admin_notice( $notice );
	}

	// BP components cannot share a single WP page. Check for duplicate assignments, and post a message if found.
	$dupe_names = array();
	$page_ids   = bp_core_get_directory_page_ids();
	$dupes      = array_diff_assoc( $page_ids, array_unique( $page_ids ) );

	if ( !empty( $dupes ) ) {
		foreach( array_keys( $dupes ) as $dupe_component ) {
			$dupe_names[] = $bp->pages->{$dupe_component}->title;
		}

		// Make sure that there are no duplicate duplicates :).
		$dupe_names = array_unique( $dupe_names );
	}

	// If there are duplicates, post a message about them.
	if ( !empty( $dupe_names ) ) {
		$admin_url = bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) );
		if ( isset( $_GET['page'] ) && 'bp-pages' === $_GET['page'] ) {
			$notice = sprintf( '%1$s',
				sprintf( __( 'Each BuddyBoss Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %s.',
					'buddyboss' ),
					'<strong>' . implode( '</strong>, <strong>', array_map( 'esc_html', $dupe_names ) ) . '</strong>' ) );
		} else {
			$notice = sprintf( '%1$s <a href="%2$s">%3$s</a>',
				sprintf( __( 'Each BuddyBoss Component needs its own WordPress page. The following WordPress Pages have more than one component associated with them: %s.',
					'buddyboss' ),
					'<strong>' . implode( '</strong>, <strong>', array_map( 'esc_html', $dupe_names ) ) . '</strong>' ),
				esc_url( $admin_url ),
				__( 'Repair', 'buddyboss' ) );
		}

		bp_core_add_admin_notice( $notice );
	}

	do_action( 'bp_core_activation_notice' );
}

Changelog

Changelog
Version Description
BuddyPress 1.2.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.