Skip to:
Content

BuddyPress.org

Changeset 8672


Ignore:
Timestamp:
07/23/2014 01:42:48 PM (7 years ago)
Author:
boonebgorges
Message:

Introduce BP_XProfile_Field_Type::pre_validate_filter(), and use in URL field type

The pre_validate_filter() method allows field types to intercept field values,
before they are run through the is_valid() validation routine, and optionally
modify them.

BP_XProfile_Field_Type_URL uses this filter to silently add the 'http://'
protocol to submitted values before run through the validation regex (which
requires the protocol). The result is a smoother UX, as users are not required
to enter the protocol in order to have their field value saved.

To make it possible for protocol-less values to be submitted on the profile
edit form, the 'type' attribute of the URL field was changed from 'url' to
'text' (with a 'url' inputtype). With 'type=url', modern browsers require a
fully-qualified URL before allowing the form to be submitted.

Fixes #5501

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

Legend:

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

    r8671 r8672  
    26462646
    26472647        $r = bp_parse_args( $raw_properties, array(
    2648             'type'  => 'url',
    2649             'value' =>  esc_url( bp_get_the_profile_field_edit_value() ),
     2648            'type'      => 'text',
     2649            'inputmode' => 'url',
     2650            'value'     => esc_url( bp_get_the_profile_field_edit_value() ),
    26502651        ) ); ?>
    26512652
     
    26922693     */
    26932694    public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {}
     2695
     2696    /**
     2697     * Modify submitted URL values before validation.
     2698     *
     2699     * The URL validation regex requires a http(s) protocol, so that all
     2700     * values saved in the database are fully-formed URLs. However, we
     2701     * still want to allow users to enter URLs without a protocol, for a
     2702     * better user experience. So we catch submitted URL values, and if
     2703     * the protocol is missing, we prepend 'http://' before passing to
     2704     * is_valid().
     2705     *
     2706     * @since BuddyPress (2.1.0)
     2707     *
     2708     * @param string $submitted_value Raw value submitted by the user.
     2709     * @return string
     2710     */
     2711    public static function pre_validate_filter( $submitted_value ) {
     2712        if ( false === strpos( $submitted_value, ':'  )
     2713              && substr( $submitted_value, 0, 1 ) !== '/'
     2714                  && substr( $submitted_value, 0, 1 ) !== '#'
     2715          && ! preg_match( '/^[a-z0-9-]+?\.php/i', $submitted_value )
     2716        ) {
     2717            $submitted_value = 'http://' . $submitted_value;
     2718        }
     2719
     2720        return $submitted_value;
     2721    }
    26942722
    26952723    /**
     
    30653093
    30663094    /**
     3095     * Allow field types to modify submitted values before they are validated.
     3096     *
     3097     * In some cases, it may be appropriate for a field type to catch
     3098     * submitted values and modify them before they are passed to the
     3099     * is_valid() method. For example, URL validation requires the
     3100     * 'http://' scheme (so that the value saved in the database is always
     3101     * a fully-formed URL), but in order to allow users to enter a URL
     3102     * without this scheme, BP_XProfile_Field_Type_URL prepends 'http://'
     3103     * when it's not present.
     3104     *
     3105     * By default, this is a pass-through method that does nothing. Only
     3106     * override in your own field type if you need this kind of pre-
     3107     * validation filtering.
     3108     *
     3109     * @since BuddyPress (2.1.0)
     3110     *
     3111     * @param mixed $submitted_value Submitted value.
     3112     * @return mixed
     3113     */
     3114    public static function pre_validate_filter( $field_value ) {
     3115        return $field_value;
     3116    }
     3117
     3118    /**
    30673119     * Allow field types to modify the appearance of their values.
    30683120     *
  • trunk/src/bp-xprofile/bp-xprofile-filters.php

    r8665 r8672  
    5252add_filter( 'xprofile_get_field_data',                  'xprofile_filter_format_field_value_by_field_id', 5, 2 );
    5353
     54add_filter( 'bp_xprofile_set_field_data_pre_validate',  'xprofile_filter_pre_validate_value_by_field_type', 10, 3 );
    5455add_filter( 'xprofile_data_value_before_save',          'xprofile_sanitize_data_value_before_save', 1, 4 );
    5556add_filter( 'xprofile_filtered_data_value_before_save', 'trim', 2 );
     
    176177    $field = new BP_XProfile_Field( $field_id );
    177178    return xprofile_filter_format_field_value_by_type( $field_value, $field->type );
     179}
     180
     181/**
     182 * Apply pre_validate_filter() filters as defined by the BP_XProfile_Field_Type classes before validating.
     183 *
     184 * @since BuddyPress (2.1.0)
     185 *
     186 * @param mixed $value Value passed to the bp_xprofile_set_field_data_pre_validate
     187 *        filter.
     188 * @param BP_XProfile_Field $field Field object.
     189 * @param BP_XProfile_Field_Type Field type object.
     190 * @return mixed
     191 */
     192function xprofile_filter_pre_validate_value_by_field_type( $value, $field, $field_type_obj ) {
     193    if ( method_exists( $field_type_obj, 'pre_validate_filter' ) ) {
     194        $value = call_user_func( array( $field_type_obj, 'pre_validate_filter' ), $value );
     195    }
     196
     197    return $value;
    178198}
    179199
  • trunk/src/bp-xprofile/bp-xprofile-functions.php

    r8670 r8672  
    294294        return false;
    295295
    296     // Special-case support for integer 0 for the number field type
    297     if ( $is_required && ! is_integer( $value ) && $value !== '0' && ( empty( $value ) || ! is_array( $value ) && ! strlen( trim( $value ) ) ) ) {
    298         return false;
    299     }
    300 
    301296    $field          = new BP_XProfile_Field( $field_id );
    302297    $field_type     = BP_XProfile_Field::get_type( $field_id );
    303298    $field_type_obj = bp_xprofile_create_field_type( $field_type );
     299
     300    /**
     301     * Filter the raw submitted profile field value.
     302     *
     303     * Use this filter to modify the values submitted by users before
     304     * doing field-type-specific validation.
     305     *
     306     * @since BuddyPress (2.1.0)
     307     *
     308     * @param mixed $value Value passed to xprofile_set_field_data()
     309     * @param BP_XProfile_Field $field Field object.
     310     * @param BP_XProfile_Field_Type $field_type_obj Field type object.
     311     */
     312    $value = apply_filters( 'bp_xprofile_set_field_data_pre_validate', $value, $field, $field_type_obj );
     313
     314    // Special-case support for integer 0 for the number field type
     315    if ( $is_required && ! is_integer( $value ) && $value !== '0' && ( empty( $value ) || ! is_array( $value ) && ! strlen( trim( $value ) ) ) ) {
     316        return false;
     317    }
    304318
    305319    /**
Note: See TracChangeset for help on using the changeset viewer.