Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2016 02:40:27 AM (9 years ago)
Author:
r-a-y
Message:

Core: Refactor BP_Button class to use an easier syntax.

Previously, to use the BP_Button class, a dev would have to define
several parameters in order to render the HTML element for a button:

array(

'wrapper' => 'div',
'wrapper_id' => 'my-wrapper',
'wrapper_class' => 'my-wrapper-class',
'link_href' => 'hxxp://example.com',
'link_class' => 'my-link-class',
'link_id' => 'my-link-id',
'link_rel' => 'nofollow',
'link_title' => 'my-link-title'

)

This commit simplifies the syntax to:

array(

'parent_element' => 'div',
'parent_attr' => array(

'id' => 'my-wrapper',
'class' => 'my-wrapper-class'

),
'button_attr' => array(

'href' => 'hxxp://example.com',
'class' => 'my-link-class',
'id' => 'my-link-id',
'rel' => 'nofollow',
'title' => 'my-link-title'

)

)

The 'parent_attr' and 'button_attr' parameters can use any arbitrary
HTML attribute set as the array key.

This commit also means we are deprecating the older parameters listed in
the first example. However, we still support these parameters via backward
compatibility.

Fixes #7226.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/core/class-bp-button.php

    r9819 r11114  
    163163        $GLOBALS['members_template'] = null;
    164164    }
     165
     166    /**
     167     * @ticket BP7226
     168     */
     169    public function test_bp_button_new_args() {
     170        $b = new BP_Button( array(
     171            'id' => 'foo',
     172            'component' => 'members',
     173            'block_self' => false,
     174            'must_be_logged_in' => false,
     175            'parent_element' => 'section',
     176            'parent_attr' => array(
     177                'class' => 'section-class',
     178                'id' => 'section-id',
     179                'data-parent' => 'foo',
     180            ),
     181            'button_element' => 'button',
     182            'button_attr' => array(
     183                'autofocus' => 'autofocus',
     184                'type' => 'submit',
     185                'name' => 'my-button'
     186            )
     187        ) );
     188
     189        $this->assertNotFalse( strpos( $b->contents, '<section ' ) );
     190        $this->assertNotFalse( strpos( $b->contents, 'class="section-class ' ) );
     191        $this->assertNotFalse( strpos( $b->contents, 'id="section-id"' ) );
     192        $this->assertNotFalse( strpos( $b->contents, 'data-parent="foo"' ) );
     193        $this->assertNotFalse( strpos( $b->contents, '<button ' ) );
     194        $this->assertNotFalse( strpos( $b->contents, 'autofocus="autofocus"' ) );
     195        $this->assertNotFalse( strpos( $b->contents, 'type="submit"' ) );
     196        $this->assertNotFalse( strpos( $b->contents, 'name="my-button"' ) );
     197    }
     198
     199    /**
     200     * @ticket BP7226
     201     */
     202    public function test_bp_button_deprecated_args_should_still_render() {
     203        $b = new BP_Button( array(
     204            'id' => 'foo',
     205            'component' => 'members',
     206            'block_self' => false,
     207            'must_be_logged_in' => false,
     208            'wrapper' => 'section',
     209            'wrapper_class' => 'section-class',
     210            'wrapper_id' => 'section-id',
     211            'link_href' => 'http://example.com',
     212            'link_class' => 'link-class',
     213            'link_id' => 'link-id',
     214            'link_rel' => 'nofollow',
     215            'link_title' => 'link-title'
     216        ) );
     217
     218        $this->assertNotFalse( strpos( $b->contents, '<section ' ) );
     219        $this->assertNotFalse( strpos( $b->contents, 'class="section-class ' ) );
     220        $this->assertNotFalse( strpos( $b->contents, 'id="section-id"' ) );
     221        $this->assertNotFalse( strpos( $b->contents, 'href="http://example.com"' ) );
     222        $this->assertNotFalse( strpos( $b->contents, 'class="link-class"' ) );
     223        $this->assertNotFalse( strpos( $b->contents, 'id="link-id"' ) );
     224        $this->assertNotFalse( strpos( $b->contents, 'rel="nofollow"' ) );
     225        $this->assertNotFalse( strpos( $b->contents, 'title="link-title"' ) );
     226    }
     227
     228    /**
     229     * @ticket BP7226
     230     */
     231    public function test_bp_button_new_element_attrs_have_precedence_over_deprecated_element_attrs() {
     232        $b = new BP_Button( array(
     233            'id' => 'foo',
     234            'component' => 'members',
     235            'block_self' => false,
     236            'must_be_logged_in' => false,
     237            'button_element' => 'button',
     238            'button_attr' => array(
     239                'class' => 'new-class',
     240            ),
     241            'link_class' => 'old-class'
     242        ) );
     243
     244        $this->assertNotFalse( strpos( $b->contents, '<button class="new-class"' ) );
     245    }
     246
     247    /**
     248     * @ticket BP7226
     249     */
     250    public function test_bp_button_new_element_attrs_should_not_render_for_empty_attrs() {
     251        $b = new BP_Button( array(
     252            'id' => 'foo',
     253            'component' => 'members',
     254            'block_self' => false,
     255            'must_be_logged_in' => false,
     256            'button_element' => 'button',
     257            'button_attr' => array(
     258                'class' => '',
     259            ),
     260        ) );
     261
     262        $this->assertFalse( strpos( $b->contents, '<button class=""' ) );
     263    }
    165264}
Note: See TracChangeset for help on using the changeset viewer.