Skip to:
Content

BuddyPress.org

Changeset 10460


Ignore:
Timestamp:
01/25/2016 01:49:30 AM (9 years ago)
Author:
dcavins
Message:

Allow bp_field_css_class() to accept multiple classes.

Allow bp_field_css_class() to accept more than one class
name passed as a space-separated string or array.

Fixes #6679.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/bp-xprofile-template.php

    r10434 r10460  
    444444 * Output the class attribute for a field.
    445445 *
    446  * @param string|bool $class Extra classes to append to class attribute.
     446 * @since 1.0.0
     447 *
     448 * @param array|string $class Extra classes to append to class attribute.
     449 *                            Pass mutiple class names as an array or
     450 *                            space-delimited string.
     451 *
     452 * @return string
    447453 */
    448454function bp_field_css_class( $class = false ) {
     
    462468
    463469        if ( ! empty( $class ) ) {
    464             $css_classes[] = sanitize_title( esc_attr( $class ) );
     470            if ( ! is_array( $class ) ) {
     471                $class = preg_split( '#\s+#', $class );
     472            }
     473            $css_classes = array_map( 'sanitize_html_class', $class );
    465474        }
    466475
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r10254 r10460  
    940940        $this->assertFalse( bp_xprofile_is_richtext_enabled_for_field( $f ) );
    941941    }
     942
     943    /**
     944     * @group bp_get_field_css_class
     945     */
     946    public function test_bp_get_field_css_class_empty_param() {
     947        // Fake the global
     948        global $profile_template;
     949        $reset_profile_template = $profile_template;
     950
     951        $profile_template = new stdClass;
     952        // Avoid the 'alt' class being added
     953        $profile_template->current_field = 2;
     954        $profile_template->field = new stdClass;
     955        $profile_template->field->id = 145;
     956        $profile_template->field->name = 'Pie';
     957        $profile_template->field->type = 'textbox';
     958
     959        $expected_classes = array(
     960            'optional-field',
     961            'field_' . $profile_template->field->id,
     962            'field_' . sanitize_title( $profile_template->field->name ),
     963            'field_type_' . sanitize_title( $profile_template->field->type ),
     964            'visibility-public'
     965            );
     966
     967        $classes = bp_get_field_css_class();
     968        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $classes, $matches );
     969        $ret_classes = explode( ' ', $matches[1] );
     970        $this->assertEqualSets( $expected_classes, $ret_classes );
     971
     972        // Clean up!
     973        $profile_template = $reset_profile_template;
     974    }
     975
     976    /**
     977     * @group bp_get_field_css_class
     978     */
     979    public function test_bp_get_field_css_class_space_delimited_string() {
     980        // Fake the global
     981        global $profile_template;
     982        $reset_profile_template = $profile_template;
     983
     984        $profile_template = new stdClass;
     985        // Avoid the 'alt' class being added
     986        $profile_template->current_field = 2;
     987        $profile_template->field = new stdClass;
     988        $profile_template->field->id = 145;
     989        $profile_template->field->name = 'Pie';
     990        $profile_template->field->type = 'textbox';
     991
     992        $expected_classes = array(
     993            'optional-field',
     994            'field_' . $profile_template->field->id,
     995            'field_' . sanitize_title( $profile_template->field->name ),
     996            'field_type_' . sanitize_title( $profile_template->field->type ),
     997            'visibility-public',
     998            'rhubarb',
     999            'apple'
     1000            );
     1001
     1002        $classes = bp_get_field_css_class( 'rhubarb apple' );
     1003        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $classes, $matches );
     1004        $ret_classes = explode( ' ', $matches[1] );
     1005        $this->assertEqualSets( $expected_classes, $ret_classes );
     1006
     1007        // Clean up!
     1008        $profile_template = $reset_profile_template;
     1009    }
     1010
     1011    /**
     1012     * @group bp_get_field_css_class
     1013     */
     1014    public function test_bp_get_field_css_class_array() {
     1015        // Fake the global
     1016        global $profile_template;
     1017        $reset_profile_template = $profile_template;
     1018
     1019        $profile_template = new stdClass;
     1020        // Avoid the 'alt' class being added
     1021        $profile_template->current_field = 2;
     1022        $profile_template->field = new stdClass;
     1023        $profile_template->field->id = 145;
     1024        $profile_template->field->name = 'Pie';
     1025        $profile_template->field->type = 'textbox';
     1026
     1027        $expected_classes = array(
     1028            'optional-field',
     1029            'field_' . $profile_template->field->id,
     1030            'field_' . sanitize_title( $profile_template->field->name ),
     1031            'field_type_' . sanitize_title( $profile_template->field->type ),
     1032            'visibility-public',
     1033            'blueberry',
     1034            'gooseberry'
     1035            );
     1036
     1037        $classes = bp_get_field_css_class( array( 'blueberry', 'gooseberry' ) );
     1038        preg_match( '/class=["\']?([^"\']*)["\' ]/is', $classes, $matches );
     1039        $ret_classes = explode( ' ', $matches[1] );
     1040        $this->assertEqualSets( $expected_classes, $ret_classes );
     1041
     1042        // Clean up!
     1043        $profile_template = $reset_profile_template;
     1044    }
    9421045}
Note: See TracChangeset for help on using the changeset viewer.