BuddyPress::autoload( string $class )

Autoload classes.

Description

Parameters

$class

(Required)

Source

File: class-buddypress.php

	public function autoload( $class ) {
		$class_parts = explode( '_', strtolower( $class ) );

		if ( 'bp' !== $class_parts[0] ) {
			return;
		}

		$components = array(
			'activity',
			'blogs',
			'core',
			'friends',
			'groups',
			'members',
			'messages',
			'notifications',
			'settings',
			'xprofile',
			'profiletype',
			'forums',
			'search',
			'media',
			'gdpr',
			'invites',
		);

		// These classes don't have a name that matches their component.
		$irregular_map = array(
			'BP_Akismet'                     => 'activity',
			'BP_Admin'                       => 'core',
			'BP_Attachment_Avatar'           => 'core',
			'BP_Attachment_Cover_Image'      => 'core',
			'BP_Attachment'                  => 'core',
			'BP_Button'                      => 'core',
			'BP_Component'                   => 'core',
			'BP_Integration'                 => 'core',
			'BP_Customizer_Control_Range'    => 'core',
			'BP_Date_Query'                  => 'core',
			'BP_Email_Tokens'                => 'core',
			'BP_Email_Delivery'              => 'core',
			'BP_Email_Recipient'             => 'core',
			'BP_Email'                       => 'core',
			'BP_Embed'                       => 'core',
			'BP_Media_Extractor'             => 'core',
			'BP_Members_Suggestions'         => 'core',
			'BP_PHPMailer'                   => 'core',
			'BP_Recursive_Query'             => 'core',
			'BP_Suggestions'                 => 'core',
			'BP_Theme_Compat'                => 'core',
			'BP_User_Query'                  => 'core',
			'BP_Walker_Category_Checklist'   => 'core',
			'BP_Walker_Nav_Menu_Checklist'   => 'core',
			'BP_Walker_Nav_Menu'             => 'core',
			'BP_Core_Gdpr'                   => 'gdpr',
			'BP_Activity_Export'             => 'gdpr',
			'BP_Export'                      => 'gdpr',
			'BP_Friendship_Export'           => 'gdpr',
			'BP_Group_Export'                => 'gdpr',
			'BP_Group_Membership_Export'     => 'gdpr',
			'BP_Message_Export'              => 'gdpr',
			'BP_Notification_Export'         => 'gdpr',
			'BP_Settings_Export'             => 'gdpr',
			'BP_Xprofile_Export'             => 'gdpr',
			'BP_Bbp_Gdpr_Forums'             => 'gdpr',
			'BP_Bbp_Gdpr_Replies'            => 'gdpr',
			'BP_Bbp_Gdpr_Topics'             => 'gdpr',
			'BP_Core_Friends_Widget'         => 'friends',
            'BP_Core_Network_Posts_Widget'   => 'core',
            'BP_Core_Follow_Following_Widget'=> 'core',
			'BP_Group_Extension'             => 'groups',
			'BP_Group_Member_Query'          => 'groups',
			'BP_Core_Members_Template'       => 'members',
			'BP_Core_Members_Widget'         => 'members',
			'BP_Core_Recently_Active_Widget' => 'members',
			'BP_Core_Whos_Online_Widget'     => 'members',
			'BP_Registration_Theme_Compat'   => 'members',
			'BP_Signup'                      => 'members',
			'BP_BuddyBoss_Platform_Updater'  => 'core',
		);

		$component = null;

		// First check to see if the class is one without a properly namespaced name.
		if ( isset( $irregular_map[ $class ] ) ) {
			$component = $irregular_map[ $class ];

		// Next chunk is usually the component name.
		} elseif ( in_array( $class_parts[1], $components, true ) ) {
			$component = $class_parts[1];
		}

		if ( ! $component ) {
			return;
		}

		// Sanitize class name.
		$class = strtolower( str_replace( '_', '-', $class ) );
		if ( 'gdpr' === $component ) {
			$path = dirname( __FILE__ ) . "/bp-core/gdpr/class-{$class}.php";
		} else {
			$path = dirname( __FILE__ ) . "/bp-{$component}/classes/class-{$class}.php";
		}

		// Sanity check.
		if ( ! file_exists( $path ) ) {
			return;
		}

		/*
		 * Sanity check 2 - Check if component is active before loading class.
		 * Skip if PHPUnit is running, or BuddyPress is installing for the first time.
		 */
		if (
			! in_array( $component, array( 'core', 'members', 'xprofile', 'gdpr', 'profiletype' ), true ) &&
			! bp_is_active( $component ) &&
			! function_exists( 'tests_add_filter' )
		) {
			return;
		}

		require $path;
	}

Changelog

Changelog
Version Description
BuddyPress 2.5.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.