Skip to:
Content

BuddyPress.org

Changeset 8547


Ignore:
Timestamp:
06/25/2014 01:36:44 PM (5 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

Location:
trunk/src/bp-xprofile
Files:
2 edited

Legend:

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

    r8535 r8547  
    16961696     */
    16971697    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     1698
     1699    /**
     1700     * Format Date values for display.
     1701     *
     1702     * @since BuddyPress (2.1.0)
     1703     *
     1704     * @param string $field_value The date value, as saved in the database.
     1705     *        Typically, this is a MySQL-formatted date string (Y-m-d H:i:s).
     1706     * @return string Date formatted by bp_format_time().
     1707     */
     1708    public static function display_filter( $field_value ) {
     1709        // If Unix timestamp
     1710        if ( is_numeric( $field_value ) ) {
     1711            $field_value = bp_format_time( $field_value, true, false );
     1712
     1713        // If MySQL timestamp
     1714        } else {
     1715            $field_value = bp_format_time( strtotime( $field_value ), true, false );
     1716        }
     1717
     1718        return $field_value;
     1719    }
    16981720}
    16991721
     
    28522874    }
    28532875
     2876    /**
     2877     * Allow field types to modify the appearance of their values.
     2878     *
     2879     * By default, this is a pass-through method that does nothing. Only
     2880     * override in your own field type if you need to provide custom
     2881     * filtering for output values.
     2882     *
     2883     * @since BuddyPress (2.1.0)
     2884     *
     2885     * @param mixed $field_value Field value.
     2886     * @return mixed
     2887     */
     2888    public static function display_filter( $field_value ) {
     2889        return $field_value;
     2890    }
    28542891
    28552892    /**
  • 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.