Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/23/2014 01:42:48 PM (12 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

File:
1 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         *
Note: See TracChangeset for help on using the changeset viewer.