Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/25/2014 01:36:44 PM (7 years ago)
Author:
boonebgorges
Message:

Introduce BP_XProfile_Field_Type::display_filter(), and implement in BP_XProfile_Field_Type_Datebox.

The new display_filter() method makes it easy for field types to customize the
appearance of its values. In the case of Datebox fields, this means formatting
the MySQL- or UNIX-formatted date in the format of bp_format_time().

Fixes #5630

File:
1 edited

Legend:

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

    r8163 r8547  
    5050add_filter( 'bp_get_the_profile_field_value',           'xprofile_filter_format_field_value', 1, 2 );
    5151add_filter( 'bp_get_the_site_member_profile_data',      'xprofile_filter_format_field_value', 1, 2 );
     52add_filter( 'xprofile_get_field_data',                  'xprofile_filter_format_field_value_by_field_id', 5, 2 );
     53add_filter( 'bp_get_the_profile_field_value',           'xprofile_filter_format_field_value_by_type', 5, 2 );
    5254add_filter( 'bp_get_the_profile_field_value',           'xprofile_filter_link_profile_data',  9, 2 );
    5355
     
    117119
    118120/**
    119  * xprofile_filter_format_field_value()
    120  *
    121  * Runs stripslashes on XProfile fields. If is field_type is 'datebox'
    122  * then the date will be formatted by bp_format_time().
    123  *
    124  * @since BuddyPress (1.0)
     121 * Runs stripslashes on XProfile fields.
     122 *
     123 * @since BuddyPress (1.0.0)
    125124 *
    126125 * @param string $field_value XProfile field_value to be filtered.
    127126 * @param string $field_type XProfile field_type to be filtered.
    128  *
    129  * @uses bp_format_time()
    130  *
    131127 * @return string $field_value Filtered XProfile field_value. False on failure.
    132128 */
     
    135131        return false;
    136132
    137     if ( 'datebox' == $field_type ) {
    138 
    139         // If Unix timestamp
    140         if ( is_numeric( $field_value ) ) {
    141             $field_value = bp_format_time( $field_value, true, false );
    142 
    143         // If MySQL timestamp
    144         } else {
    145             $field_value = bp_format_time( strtotime( $field_value ), true, false );
    146         }
    147 
    148     } else {
     133    if ( 'datebox' != $field_type ) {
    149134        $field_value = str_replace(']]>', ']]>', $field_value );
    150135    }
    151136
    152137    return stripslashes( $field_value );
     138}
     139
     140/**
     141 * Apply display_filter() filters as defined by the BP_XProfile_Field_Type classes, when fetched inside a bp_has_profile() loop.
     142 *
     143 * @since BuddyPress (2.1.0)
     144 *
     145 * @param mixed $field_value Field value.
     146 * @param string $field_type Field type.
     147 * @return mixed
     148 */
     149function xprofile_filter_format_field_value_by_type( $field_value, $field_type = '' ) {
     150    foreach ( bp_xprofile_get_field_types() as $type => $class ) {
     151        if ( $type !== $field_type ) {
     152            continue;
     153        }
     154
     155        if ( method_exists( $class, 'display_filter' ) ) {
     156            $field_value = call_user_func( array( $class, 'display_filter' ), $field_value );
     157        }
     158    }
     159
     160    return $field_value;
     161}
     162
     163/**
     164 * Apply display_filter() filters as defined by the BP_XProfile_Field_Type classes, when fetched by xprofile_get_field_data().
     165 *
     166 * @since BuddyPress (2.1.0)
     167 *
     168 * @param mixed $field_value Field value.
     169 * @param int $field_id Field type.
     170 */
     171function xprofile_filter_format_field_value_by_field_id( $field_value, $field_id ) {
     172    $field = new BP_XProfile_Field( $field_id );
     173    return xprofile_filter_format_field_value_by_type( $field_value, $field->type );
    153174}
    154175
Note: See TracChangeset for help on using the changeset viewer.