Skip to:
Content

BuddyPress.org

Changeset 13101


Ignore:
Timestamp:
09/02/2021 04:26:52 PM (3 years ago)
Author:
imath
Message:

Improve the Group Extension API extensibility

  • Use a new global to store the registered Group extensions of the Groups component.
  • Edit the behavior of the bp_register_group_extension() function so that it only registers custom group extensions into the new global.
  • Introduce a new function bp_init_group_extensions() to init the registered Group extensions.

These improvements will make it possible for the BP Rewrites feature as a plugin to allow slug customization of the registered Group extensions.

Props boonebgorges

Fixes #8558

Location:
trunk/src/bp-groups
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r13085 r13101  
    35323532    }
    35333533}
     3534
     3535/**
     3536 * Register a new Group Extension.
     3537 *
     3538 * @since 1.1.0
     3539 * @since 10.0.0 The function was moved from the `/bp-groups/classes/class-bp-group-extension.php` file.
     3540 *               It only registers Group Extensions if their corresponding class name has not been already
     3541 *               registered.
     3542 *
     3543 * @param string $group_extension_class Name of the Extension class.
     3544 * @return bool                         Returns true on success, otherwise false.
     3545 */
     3546function bp_register_group_extension( $group_extension_class = '' ) {
     3547    if ( ! class_exists( $group_extension_class ) ) {
     3548        return false;
     3549    }
     3550
     3551    $bp = buddypress();
     3552
     3553    if ( isset( $bp->groups->group_extensions[ $group_extension_class ] ) ) {
     3554        return false;
     3555    }
     3556
     3557    // Add the new Group extension to the registered ones.
     3558    $bp->groups->group_extensions[ $group_extension_class ] = true;
     3559
     3560    return true;
     3561}
     3562
     3563/**
     3564 * Init Registered Group Extensions.
     3565 *
     3566 * @since 10.0.0
     3567 */
     3568function bp_init_group_extensions() {
     3569    $registered_group_extensions = buddypress()->groups->group_extensions;
     3570
     3571    if ( ! $registered_group_extensions ) {
     3572        return;
     3573    }
     3574
     3575    foreach ( array_keys( $registered_group_extensions ) as $group_extension_class ) {
     3576        $extension = new $group_extension_class;
     3577
     3578        add_action( 'bp_actions', array( &$extension, '_register' ), 8 );
     3579        add_action( 'admin_init', array( &$extension, '_register' ) );
     3580    }
     3581}
     3582add_action( 'bp_init', 'bp_init_group_extensions', 11 );
  • trunk/src/bp-groups/classes/class-bp-group-extension.php

    r11773 r13101  
    1111defined( 'ABSPATH' ) || exit;
    1212
     13if ( ! class_exists( 'BP_Group_Extension', false ) ) :
    1314/**
    1415 * API for creating group extensions without having to hardcode the content into
     
    16961697    }
    16971698}
    1698 
    1699 /**
    1700  * Register a new Group Extension.
    1701  *
    1702  * @since 1.1.0
    1703  *
    1704  * @param string $group_extension_class Name of the Extension class.
    1705  * @return false|null Returns false on failure, otherwise null.
    1706  */
    1707 function bp_register_group_extension( $group_extension_class = '' ) {
    1708 
    1709     if ( ! class_exists( $group_extension_class ) ) {
    1710         return false;
    1711     }
    1712 
    1713     // Register the group extension on the bp_init action so we have access
    1714     // to all plugins.
    1715     add_action( 'bp_init', function() use ( $group_extension_class ) {
    1716         $extension = new $group_extension_class;
    1717         add_action( 'bp_actions', array( &$extension, '_register' ), 8 );
    1718         add_action( 'admin_init', array( &$extension, '_register' ) );
    1719     }, 11 );
    1720 }
     1699endif; // End class_exists check.
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r13093 r13101  
    9494     */
    9595    public $current_directory_type = '';
     96
     97    /**
     98     * List of registered Group extensions.
     99     *
     100     * @see bp_register_group_extension()
     101     *
     102     * @since 10.0.0
     103     * @var array
     104     */
     105    public $group_extensions = array();
    96106
    97107    /**
Note: See TracChangeset for help on using the changeset viewer.