Skip to:
Content

BuddyPress.org

Ticket #5625: 5625.diff

File 5625.diff, 12.3 KB (added by boonebgorges, 11 years ago)
  • src/bp-xprofile/bp-xprofile-filters.php

    diff --git src/bp-xprofile/bp-xprofile-filters.php src/bp-xprofile/bp-xprofile-filters.php
    index ab0d854..7d3017a 100644
    function xprofile_filter_format_field_value( $field_value, $field_type = '' ) {  
    218218 *
    219219 * @param mixed $field_value Field value.
    220220 * @param string $field_type Field type.
     221 * @param int    $field_id   ID of the field, if available.
    221222 * @return mixed
    222223 */
    223 function xprofile_filter_format_field_value_by_type( $field_value, $field_type = '' ) {
     224function xprofile_filter_format_field_value_by_type( $field_value, $field_type = '', $field_id = '' ) {
    224225        foreach ( bp_xprofile_get_field_types() as $type => $class ) {
    225226                if ( $type !== $field_type ) {
    226227                        continue;
    227228                }
    228229
    229230                if ( method_exists( $class, 'display_filter' ) ) {
    230                         $field_value = call_user_func( array( $class, 'display_filter' ), $field_value );
     231                        $field_value = call_user_func( array( $class, 'display_filter' ), $field_value, $field_id );
    231232                }
    232233        }
    233234
    … … function xprofile_filter_format_field_value_by_type( $field_value, $field_type =  
    244245 */
    245246function xprofile_filter_format_field_value_by_field_id( $field_value, $field_id ) {
    246247        $field = new BP_XProfile_Field( $field_id );
    247         return xprofile_filter_format_field_value_by_type( $field_value, $field->type );
     248        return xprofile_filter_format_field_value_by_type( $field_value, $field->type, $field_id );
    248249}
    249250
    250251/**
  • src/bp-xprofile/bp-xprofile-functions.php

    diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php
    index 71d0649..d92d49b 100644
    function bp_xprofile_fullname_field_name() {  
    983983}
    984984
    985985/**
     986 * Is rich text enabled for this profile field?
     987 *
     988 * @since 2.4.0
     989 *
     990 * @param int $field_id Optional. Default: current field ID.
     991 * @return bool
     992 */
     993function bp_xprofile_is_richtext_enabled_for_field( $field_id = null ) {
     994        if ( ! $field_id ) {
     995                $field_id = bp_get_the_profile_field_id();
     996        }
     997
     998        $field = xprofile_get_field( $field_id );
     999
     1000        $enabled = false;
     1001        if ( isset( $field->type ) && 'textarea' === $field->type ) {
     1002                $enabled = true;
     1003        }
     1004
     1005        /**
     1006         * Filters whether richtext is enabled for the given field.
     1007         *
     1008         * @since 2.4.0
     1009         *
     1010         * @param bool $enabled  True if richtext is enabled for the field, otherwise false.
     1011         * @param int  $field_id ID of the field.
     1012         */
     1013        return apply_filters( 'bp_xprofile_is_richtext_enabled_for_field', $enabled, $field_id );
     1014}
     1015
     1016/**
    9861017 * Get visibility levels out of the $bp global
    9871018 *
    9881019 * @return array
  • src/bp-xprofile/classes/class-bp-xprofile-field-type-textarea.php

    diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type-textarea.php src/bp-xprofile/classes/class-bp-xprofile-field-type-textarea.php
    index 4b5ffc2..cd322ff 100644
    class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {  
    2626
    2727                $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
    2828                $this->name     = _x( 'Multi-line Text Area', 'xprofile field type', 'buddypress' );
     29                $this->type     = 'textare';
    2930
    3031                $this->set_format( '/^.*$/m', 'replace' );
    3132
     33                // Create custom filters that mirror 'the_content' for formatting output
     34                add_filter( 'bp_xprofile_field_type_textarea_content', 'wptexturize'        );
     35                add_filter( 'bp_xprofile_field_type_textarea_content', 'convert_smilies'    );
     36                add_filter( 'bp_xprofile_field_type_textarea_content', 'convert_chars'      );
     37                add_filter( 'bp_xprofile_field_type_textarea_content', 'wpautop'            );
     38                add_filter( 'bp_xprofile_field_type_textarea_content', 'shortcode_unautop'  );
     39
     40                // Override and augment allowed tags
     41                add_filter( 'xprofile_allowed_tags', array( $this, 'allowed_tags' ), 30, 2 );
     42
     43                // Format the value displayed when editing.
     44                add_filter( 'bp_get_the_profile_field_edit_value', array( $this, 'get_field_value' ), 30, 3 );
     45
     46                // Filter for those who use xprofile_get_field_data instead of `get_field_value()`.
     47                add_filter( 'xprofile_get_field_data', array( $this, 'get_field_data' ), 15, 2 );
     48
    3249                /**
    3350                 * Fires inside __construct() method for BP_XProfile_Field_Type_Textarea class.
    3451                 *
    … … class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {  
    5673                        unset( $raw_properties['user_id'] );
    5774                }
    5875
    59                 $r = bp_parse_args( $raw_properties, array(
    60                         'cols' => 40,
    61                         'rows' => 5,
    62                 ) ); ?>
     76                $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field(); ?>
    6377
    6478                <label for="<?php bp_the_profile_field_input_name(); ?>">
    6579                        <?php bp_the_profile_field_name(); ?>
    … … class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {  
    7185                <?php
    7286
    7387                /** This action is documented in bp-xprofile/bp-xprofile-classes */
    74                 do_action( bp_get_the_profile_field_errors_action() ); ?>
     88                do_action( bp_get_the_profile_field_errors_action() );
    7589
    76                 <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>><?php bp_the_profile_field_edit_value(); ?></textarea>
     90                if ( ! $richtext_enabled ) {
     91                        $r = wp_parse_args( $raw_properties, array(
     92                                'cols' => 40,
     93                                'rows' => 5,
     94                        ) );
    7795
    78                 <?php
     96                        ?>
     97
     98                        <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>><?php bp_the_profile_field_edit_value(); ?></textarea>
     99
     100                        <?php
     101
     102                } else {
     103
     104                        /**
     105                         * Filters the arguments passed to `wp_editor()` in richtext xprofile fields.
     106                         *
     107                         * @since 2.4.0
     108                         *
     109                         * @param array $args {
     110                         *     Array of optional arguments. See `wp_editor()`.
     111                         *     @type bool $teeny         Whether to use the teeny version of TinyMCE. Default true.
     112                         *     @type bool $media_buttons Whether to show media buttons. Default false.
     113                         *     @type bool $quicktags     Whether to show the quicktags buttons. Default true.
     114                         * }
     115                         */
     116                        $editor_args = apply_filters( 'bp_xprofile_field_type_textarea_editor_args', array(
     117                                'teeny'         => true,
     118                                'media_buttons' => false,
     119                                'quicktags'     => true,
     120                        ) );
     121
     122                        // Create the editor.
     123                        wp_editor(
     124                                bp_get_the_profile_field_edit_value(),
     125                                bp_get_the_profile_field_input_name(),
     126                                $editor_args
     127                        );
     128                }
    79129        }
    80130
    81131        /**
    … … class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {  
    87137         * @since 2.0.0
    88138         */
    89139        public function admin_field_html( array $raw_properties = array() ) {
    90                 $r = bp_parse_args( $raw_properties, array(
    91                         'cols' => 40,
    92                         'rows' => 5,
    93                 ) ); ?>
     140                $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field();
    94141
    95                 <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>></textarea>
     142                if ( ! $richtext_enabled ) {
    96143
    97                 <?php
     144                        $r = bp_parse_args( $raw_properties, array(
     145                                'cols' => 40,
     146                                'rows' => 5,
     147                        ) ); ?>
     148
     149                        <textarea <?php echo $this->get_edit_field_html_elements( $r ); ?>></textarea>
     150
     151                        <?php
     152                } else {
     153
     154                        /** This filter is documented in bp-xprofile/bp-xprofile-classes */
     155                        $editor_args = apply_filters( 'bp_xprofile_field_type_textarea_editor_args', array(
     156                                'teeny'         => true,
     157                                'media_buttons' => false,
     158                                'quicktags'     => true,
     159                        ) );
     160
     161                        // Create the editor
     162                        wp_editor(
     163                                '',
     164                                'xprofile_textarea_' . bp_get_the_profile_field_id(),
     165                                $editor_args
     166                        );
     167                }
    98168        }
    99169
    100170        /**
    … … class BP_XProfile_Field_Type_Textarea extends BP_XProfile_Field_Type {  
    106176         * @since 2.0.0
    107177         */
    108178        public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     179
     180        /**
     181         * Format Visual Editor content for display.
     182         *
     183         * @since 2.4.0
     184         *
     185         * @param string $field_value The Visual Editor content, as saved in the database.
     186         * @return string Formatted HTML.
     187         */
     188        public static function display_filter( $field_value, $field_id ) {
     189
     190                $richtext_enabled = bp_xprofile_is_richtext_enabled_for_field( $field_id );
     191
     192                if ( $richtext_enabled ) {
     193
     194                        /*
     195                         * This is a bit of a hack because formatted text looks strange with the
     196                         * 'xprofile_filter_link_profile_data' filter applied. If instead, we parse
     197                         * this field type in read mode after all BuddyPress filters have been applied
     198                         * we can avoid this.
     199                         */
     200//                      add_filter( 'bp_get_the_profile_field_value', array( 'BP_XProfile_Field_Type_Textarea', 'get_field_value' ), 30, 3 );
     201
     202                }
     203
     204                return $field_value;
     205
     206        }
     207
     208        /**
     209         * Get the field value.
     210         *
     211         * @param string $value
     212         * @param string $type
     213         * @param integer $id
     214         * @return string
     215         */
     216        public function get_field_value( $value = '', $type = '', $id = '' ) {
     217                global $field;
     218
     219                if ( bp_xprofile_is_richtext_enabled_for_field() ) {
     220
     221                        // Reach directly into the global to get an unfiltered value.
     222                        $value = $field->data->value;
     223
     224                        /**
     225                         * Filters textarea richtext content for display.
     226                         *
     227                         * @since 4.4.0
     228                         *
     229                         * @param string $value Textarea content.
     230                         */
     231                        $value = apply_filters( 'bp_xprofile_field_type_textarea_content', stripslashes( $value ) );
     232
     233                        return apply_filters( 'bp_xprofile_field_type_textarea_value', $value );
     234                }
     235
     236                return $value;
     237
     238        }
     239
     240        /**
     241         * Filter for those who use xprofile_get_field_data instead of get_field_value
     242         *
     243         * @param string $value
     244         * @param integer $field_id
     245         * @param integer $user_id
     246         * @return string
     247         */
     248        public function get_field_data( $value = '', $field_id = '' ) {
     249
     250                if ( bp_xprofile_is_richtext_enabled_for_field( $field_id ) ) {
     251
     252                        // apply content filter
     253                        $value = apply_filters( 'bp_xprofile_field_type_textarea_content', stripslashes( $value ) );
     254
     255                        // return filtered value
     256                        return apply_filters( 'bp_xprofile_field_type_textarea_value', $value );
     257                }
     258
     259                return $value;
     260        }
     261
     262        /**
     263         * Callback to allow tags that teeny TinyMCE enables
     264         *
     265         * @since BuddyPress (2.2.0)
     266         *
     267         * @param array $allowedtags The array of allowed tags
     268         * @param object $data_obj The xProfile data object (BP 2.1+)
     269         * @return array $allowedtags The modified array of allowed tags
     270         */
     271        public function allowed_tags( $allowedtags, $data_obj = null ) {
     272
     273                // test if BP has sent the data object
     274                if ( ! is_null( $data_obj ) ) {
     275
     276                        // get field from data object
     277                        $field = new BP_XProfile_Field( $data_obj->field_id );
     278
     279                        // if this isn't our field, skip amending allowed tags
     280                        if ( $field->type != $this->type ) return $allowedtags;
     281
     282                        // get enabled and default to enabled if not yet present
     283                        $richtext_enabled = bp_xprofile_get_meta( $field->id, 'field', 'richtext_enabled', true );
     284                        if ( $richtext_enabled === false ) $richtext_enabled = 1;
     285                        $richtext_enabled = ( $richtext_enabled == 1 ) ? true : false;
     286
     287                        // bail if not rich text
     288                        if ( ! $richtext_enabled )  return $allowedtags;
     289
     290                }
     291
     292                // default tags we need to enable the teeny TinyMCE editor markup
     293                $richtext_tags = array(
     294                        'img'    => array( 'id' => 1, 'class' => 1, 'src' => 1, 'alt' => 1, 'width' => 1, 'height' => 1 ),
     295                        'ul'     => array( 'id' => 1, 'class' => 1 ),
     296                        'ol'     => array( 'id' => 1, 'class' => 1 ),
     297                        'li'     => array( 'id' => 1, 'class' => 1 ),
     298                        'span'   => array( 'style' => 1 ),
     299                        'p'      => array( 'style' => 1 ),
     300                );
     301
     302                $allowedtags = array_merge( $allowedtags, $richtext_tags );
     303
     304                return apply_filters( 'bp_xprofile_field_type_textarea_allowedtags', $allowedtags, $data_obj );
     305
     306        }
     307
    109308}
  • tests/phpunit/testcases/xprofile/functions.php

    diff --git tests/phpunit/testcases/xprofile/functions.php tests/phpunit/testcases/xprofile/functions.php
    index fc954a2..e624e1c 100644
    Bar!';  
    868868                // assert!
    869869                $this->assertEquals( array( 1, $g1, $g3, $g2 ), wp_list_pluck( $field_groups, 'id' ) );
    870870        }
     871
     872        /**
     873         * @ticket BP5625
     874         */
     875        public function test_bp_xprofie_is_richtext_enabled_for_field_should_default_to_true_for_textareas() {
     876                $g = $this->factory->xprofile_group->create();
     877                $f = $this->factory->xprofile_field->create( array(
     878                        'field_group_id' => $g,
     879                        'type' => 'textarea',
     880                ) );
     881
     882                $this->assertTrue( bp_xprofile_is_richtext_enabled_for_field( $f ) );
     883        }
     884
     885        /**
     886         * @ticket BP5625
     887         */
     888        public function test_bp_xprofie_is_richtext_enabled_for_field_should_default_to_false_for_non_textareas() {
     889                $g = $this->factory->xprofile_group->create();
     890                $f = $this->factory->xprofile_field->create( array(
     891                        'field_group_id' => $g,
     892                        'type' => 'radio',
     893                ) );
     894
     895                $this->assertFalse( bp_xprofile_is_richtext_enabled_for_field( $f ) );
     896        }
    871897}