Skip to:
Content

bbPress.org

Changeset 5370


Ignore:
Timestamp:
06/06/2014 03:56:06 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Introduce bbp_sanitize_displayed_user_field() function to handle the sanitizing of displayed user data, and add it to the bbp_get_displayed_user_field filter. Props mazengamal. See #2610 (2.5 branch).

Location:
branches/2.5/includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.5/includes/core/filters.php

    r5179 r5370  
    183183add_filter( 'bbp_get_topic_post_count',     'bbp_number_format', 10 );
    184184
     185// Sanitize displayed user data
     186add_filter( 'bbp_get_displayed_user_field', 'bbp_sanitize_displayed_user_field', 10, 3 );
     187
    185188// Run wp_kses_data on topic/reply content in admin section
    186189if ( is_admin() ) {
  • branches/2.5/includes/users/functions.php

    r5187 r5370  
    16041604}
    16051605
     1606/** Sanitization **************************************************************/
     1607
     1608/**
     1609 * Sanitize displayed user data, when viewing and editing any user.
     1610 *
     1611 * This somewhat monolithic function handles the escaping and sanitization of
     1612 * user data for a bbPress profile. There are two reasons this all happers here:
     1613 *
     1614 * 1. bbPress took a similar approach to WordPress, and funnels all user profile
     1615 *    data through a central helper. This eventually calls sanitize_user_field()
     1616 *    which applies a few context based filters, which some third party plugins
     1617 *    might be relying on bbPress to play nicely with.
     1618 *
     1619 * 2. Early versions of bbPress 2.x templates did not escape this data meaning
     1620 *    a backwards compatible approach like this one was necessary to protect
     1621 *    existing installations that may have custom template parts.
     1622 *
     1623 * @since bbPress (r5368)
     1624 *
     1625 * @param string $value
     1626 * @param string $field
     1627 * @param string $context
     1628 * @return string
     1629 */
     1630function bbp_sanitize_displayed_user_field( $value = '', $field = '', $context = 'display' ) {
     1631
     1632    // Bail if not editing or displaying (maybe we'll do more here later)
     1633    if ( ! in_array( $context, array( 'edit', 'display' ) ) ) {
     1634        return $value;
     1635    }
     1636
     1637    // By default, no filter set (consider making this an array later)
     1638    $filter = false;
     1639
     1640    // Big switch statement to decide which user field we're sanitizing and how
     1641    switch ( $field ) {
     1642
     1643        // Description is a paragraph
     1644        case 'description' :
     1645            $filter = ( 'edit' === $context ) ? '' : 'wp_kses_data';
     1646            break;
     1647
     1648        // Email addresses are sanitized with a specific function
     1649        case 'user_email'  :
     1650            $filter = 'sanitize_email';
     1651            break;
     1652
     1653        // Name & login fields
     1654        case 'user_login'   :
     1655        case 'display_name' :
     1656        case 'first_name'   :
     1657        case 'last_name'    :
     1658        case 'nick_name'    :
     1659            $filter = ( 'edit' === $context ) ? 'esc_attr' : 'esc_html';
     1660            break;
     1661
     1662        // wp-includes/default-filters.php escapes this for us via esc_url()
     1663        case 'user_url' :
     1664            break;
     1665    }
     1666
     1667    // Run any applicable filters on the value
     1668    if ( ! empty( $filter ) ) {
     1669        $value = call_user_func( $filter, $value );
     1670    }
     1671
     1672    return $value;
     1673}
     1674
    16061675/** Converter *****************************************************************/
    16071676
  • branches/2.5/includes/users/template.php

    r5188 r5370  
    157157
    158158        // Return empty
    159         return apply_filters( 'bbp_get_displayed_user_field', $value, $field );
     159        return apply_filters( 'bbp_get_displayed_user_field', $value, $field, $filter );
    160160    }
    161161
Note: See TracChangeset for help on using the changeset viewer.