Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/21/2021 02:17:21 PM (4 years ago)
Author:
imath
Message:

Field Types can now declare supported features & field visibility

  • Edit the JavaScript file used by the xProfile Create field Administration screen to handle Field types requirements by showing/hiding screen metaboxes according to feature supports and to get ride of some jQuery deprecated methods.
  • Improve the xProfile Field API to take in account xProfile Field Types declared feature supports by adding two new methods to get (BP_XProfile_Field->get_field_type_supports()) & check (BP_XProfile_Field->field_type_supports( $feature_name )) the field type supported features.
  • The xProfile Create field Administration Screen Metaboxes displayed to set the field properties can now be disabled by the Field Type using the static variable $supported_features. See tests/phpunit/assets/bptest-xprofile-field-type.php for an example of use.
  • Improve the xProfile Field API to take in account the xProfile Field Types visibility property to use as default field visibility. NB: setting this Field Type visibility and its allow_custom_visibility feature support to false, a Field Type can now enforce the visibility to use for a field.
  • Introduce a new xProfile Fields loop argument $hide_field_types to avoid displaying fields according to an array of Field types. To customize this new argument you can use the bp_before_has_profile_parse_args filter for existing xProfile loop. For instance you can avoid to list xProfile fields according to their type from the WP-Admin/Extended profile screen checking the corresponding Administration Screen ID.
  • Add PHP unit tests to verify these improvements are working the right way.

Props DJPaul, Offereins, needle, netweb, vapvarun

See #7162

File:
1 edited

Legend:

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

    r12768 r12868  
    494494            $field->name        = $_POST['title'];
    495495
     496            /*
     497             * By default a Textbox field is created. To run field type's feature
     498             * checks we need to set it to what it really is early.
     499             */
     500            if ( is_null( $field_id ) ) {
     501                $field_type = bp_xprofile_create_field_type( $field->type );
     502
     503                // If it's a placeholder, then the field type is not registered.
     504                if ( ! $field_type instanceof BP_XProfile_Field_Type_Placeholder ) {
     505                    $field->type_obj = $field_type;
     506                }
     507            }
     508
     509            if ( ! $field->field_type_supports( 'required' ) ) {
     510                $field->is_required = "0";
     511            }
     512
    496513            if ( ! empty( $_POST['description'] ) ) {
    497514                $field->description = $_POST['description'];
     
    538555                // Validate default visibility.
    539556                if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) {
    540                     bp_xprofile_update_field_meta( $field_id, 'default_visibility', $_POST['default-visibility'] );
     557                    $default_visibility = $_POST['default-visibility'];
     558
     559                    if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) {
     560                        $default_visibility = 'public';
     561                    }
     562
     563                    bp_xprofile_update_field_meta( $field_id, 'default_visibility', $default_visibility );
    541564                }
    542565
    543566                // Validate custom visibility.
    544567                if ( ! empty( $_POST['allow-custom-visibility'] ) && in_array( $_POST['allow-custom-visibility'], array( 'allowed', 'disabled' ) ) ) {
    545                     bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility'] );
     568                    $allow_custom_visibility = $_POST['allow-custom-visibility'];
     569
     570                    if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) {
     571                        $allow_custom_visibility = 'disabled';
     572                    }
     573
     574                    bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $allow_custom_visibility );
    546575                }
    547576
     
    553582                }
    554583
     584                $do_autolink = '';
     585                if ( $field->field_type_supports( 'do_autolink' ) && isset( $_POST['do_autolink'] ) && $_POST['do_autolink'] ) {
     586                    $do_autolink = wp_unslash( $_POST['do_autolink'] );
     587                }
     588
    555589                // Save autolink settings.
    556                 if ( isset( $_POST['do_autolink'] ) && 'on' === wp_unslash( $_POST['do_autolink'] ) ) {
     590                if ( 'on' === $do_autolink ) {
    557591                    bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'on' );
    558592                } else {
Note: See TracChangeset for help on using the changeset viewer.