Skip to:
Content

BuddyPress.org

Changeset 9844


Ignore:
Timestamp:
05/06/2015 11:01:27 PM (4 years ago)
Author:
r-a-y
Message:

Core: Introduce ability for components to register new features.

This commit allows plugin developers to register a feature in the
'BP_Component' class. To BuddyPress, a feature is merely an internal
marker. It is up to the developer to implement the feature.

Developers can check if a feature is registered with this snippet:

bp_is_active( $component, $feature_name )

See #6331 where this idea came about.

Props boonebgorges, imath, r-a-y.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-component.php

    r9819 r9844  
    132132     *
    133133     * @since BuddyPress (1.5.0)
    134      *
    135      * @uses BP_Component::setup_actions() Set up the hooks and actions.
    136      *
    137      * @param string $id Unique ID (for internal identification). Letters,
    138      *        numbers, and underscores only.
    139      * @param string $name Unique name. This should be a translatable name,
    140      *        eg __( 'Groups', 'buddypress' ).
    141      * @param string $path The file path for the component's files. Used by
    142      *        {@link BP_Component::includes()}.
    143      * @param array $params Additional parameters used by the component.
    144      *        The config array supports the following values:
    145      *        - 'adminbar_myaccount_order' Sets the position for our
    146      *          component menu under the WP Toolbar's "My Account" menu.
     134     * @since BuddyPress (1.9.0) Added $params as a parameter.
     135     * @since BuddyPress (2.3.0) Added $params['features'] as a configurable value.
     136     *
     137     * @param string $id   Unique ID. Letters, numbers, and underscores only.
     138     * @param string $name Unique name. This should be a translatable name, eg.
     139     *                     __( 'Groups', 'buddypress' ).
     140     * @param string $path The file path for the component's files. Used by {@link BP_Component::includes()}.
     141     * @param array  $params {
     142     *     Additional parameters used by the component.
     143     *     @type int   $adminbar_myaccount_order Set the position for our menu under the WP Toolbar's "My Account menu"
     144     *     @type array $features                 An array of feature names. This is used to load additional files from your
     145     *                                           component directory and for feature active checks. eg. array( 'awesome' )
     146     *                                           would look for a file called "bp-{$this->id}-awesome.php" and you could use
     147     *                                           bp_is_active( $this->id, 'awesome' ) to determine if the feature is active.
     148     * }
    147149     */
    148150    public function start( $id = '', $name = '', $path = '', $params = array() ) {
     
    162164            if ( ! empty( $params['adminbar_myaccount_order'] ) ) {
    163165                $this->adminbar_myaccount_order = (int) $params['adminbar_myaccount_order'];
     166            }
     167
     168            // Register features
     169            if ( ! empty( $params['features'] ) ) {
     170                $this->features = array_map( 'sanitize_title', (array) $params['features'] );
    164171            }
    165172
  • trunk/src/bp-core/bp-core-template.php

    r9819 r9844  
    18461846
    18471847/**
    1848  * Check whether a given component has been activated by the admin.
     1848 * Check whether a given component (or feature of a component) is active.
     1849 *
     1850 * @since BuddyPress (1.2.0) See r2539.
     1851 * @since BuddyPress (2.3.0) Added $feature as a parameter.
    18491852 *
    18501853 * @param string $component The component name.
    1851  * @return bool True if the component is active, otherwise false.
    1852  */
    1853 function bp_is_active( $component = '' ) {
     1854 * @param string $feature   The feature name.
     1855 * @return bool
     1856 */
     1857function bp_is_active( $component = '', $feature = '' ) {
    18541858    $retval = false;
    18551859
     
    18621866    if ( isset( buddypress()->active_components[ $component ] ) || isset( buddypress()->required_components[ $component ] ) ) {
    18631867        $retval = true;
     1868
     1869        // Is feature active?
     1870        if ( ! empty( $feature ) ) {
     1871            if ( empty( buddypress()->$component->features ) || false === in_array( $feature, buddypress()->$component->features, true ) ) {
     1872                $retval = false;
     1873            }
     1874
     1875            /**
     1876             * Filters whether or not a given feature for a component is active.
     1877             *
     1878             * @since BuddyPress (2.3.0)
     1879             *
     1880             * @param bool $retval
     1881             */
     1882            $retval = apply_filters( "bp_is_{$component}_{$feature}_active", $retval );
     1883        }
    18641884    }
    18651885
  • trunk/tests/phpunit/testcases/core/functions.php

    r9831 r9844  
    621621        $this->assertSame( $expected_upload_dir, $tested_upload_dir );
    622622    }
     623
     624    /**
     625     * @group bp_is_active
     626     */
     627    public function test_bp_is_active_component() {
     628        $bp = buddypress();
     629        $reset_active_components = $bp->active_components;
     630
     631        $this->assertTrue( bp_is_active( 'members' ) );
     632
     633        $this->assertFalse( bp_is_active( 'foo' ) );
     634
     635        // Create and activate the foo component
     636        $bp->foo = new BP_Component;
     637        $bp->foo->id   = 'foo';
     638        $bp->foo->slug = 'foo';
     639        $bp->foo->name = 'Foo';
     640        $bp->active_components[ $bp->foo->id ] = 1;
     641
     642        $this->assertTrue( bp_is_active( 'foo' ) );
     643
     644        add_filter( 'bp_is_active', '__return_false' );
     645
     646        $this->assertFalse( bp_is_active( 'foo' ) );
     647
     648        remove_filter( 'bp_is_active', '__return_false' );
     649
     650        // Reset buddypress() vars
     651        $bp->active_components = $reset_active_components;
     652    }
     653
     654    /**
     655     * @group bp_is_active
     656     */
     657    public function test_bp_is_active_feature() {
     658        $bp = buddypress();
     659        $reset_active_components = $bp->active_components;
     660
     661        // Create and activate the foo component
     662        $bp->foo = new BP_Component;
     663        $bp->foo->id   = 'foo';
     664        $bp->foo->slug = 'foo';
     665        $bp->foo->name = 'Foo';
     666        $bp->active_components[ $bp->foo->id ] = 1;
     667
     668        // foo did not register 'bar' as a feature
     669        $this->assertFalse( bp_is_active( 'foo', 'bar' ) );
     670
     671        // fake registering the 'bar' feature
     672        $bp->foo->features = array( 'bar' );
     673        $this->assertTrue( bp_is_active( 'foo', 'bar' ) );
     674
     675        // test the feature filter
     676        add_filter( 'bp_is_foo_bar_active', '__return_false' );
     677        $this->assertFalse( bp_is_active( 'foo', 'bar' ) );
     678        remove_filter( 'bp_is_foo_bar_active', '__return_false' );
     679
     680        // test the main component filter
     681        add_filter( 'bp_is_active', '__return_false' );
     682        $this->assertFalse( bp_is_active( 'foo', 'bar' ) );
     683        remove_filter( 'bp_is_active', '__return_false' );
     684
     685        // Reset buddypress() vars
     686        $bp->active_components = $reset_active_components;
     687    }
    623688}
Note: See TracChangeset for help on using the changeset viewer.