Skip to:
Content

BuddyPress.org

Changeset 12885


Ignore:
Timestamp:
04/16/2021 05:29:26 AM (4 years ago)
Author:
imath
Message:

Improve the Field/Group APIs to pick selected fields as signup ones

  • Introduce the bp_xprofile_get_signup_field_ids() function to get an ordered list of field IDs to use as signup fields.
  • Introduce the bp_xprofile_signup_args() function to get the signup arguments to use into the registration page xProfile loop. If no signup fields are available, it falls back to the primary fields group.
  • Introduce the $signup_fields_only xProfile loop argument to restrict fetched fields to signup ones if set to true.
  • Adapt the BP_XProfile_Data_Template class to build a unique xProfile field group out of any possible field groups containing one or more signup fields when the $signup_fields_only argument is set to true and registration is allowed on the site.
  • Adapt the BP_XProfile_Field class to get the possible field's signup position and add a new metabox to add a field to signup ones from the xProfile Field Administration screen.
  • Adapt the BP_XProfile_Group class to ignore non signup fields if the $signup_fields_only argument is set to true.

Props johnjamesjacoby, boonebgorges, DJPaul, Offereins

See #6347

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

Legend:

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

    r12882 r12885  
    14221422    );
    14231423}
     1424
     1425/**
     1426 * Returns the signup field IDs.
     1427 *
     1428 * @since 8.0.0
     1429 *
     1430 * @return int[] The signup field IDs.
     1431 */
     1432function bp_xprofile_get_signup_field_ids() {
     1433    $signup_field_ids = wp_cache_get( 'signup_fields', 'bp_xprofile' );
     1434
     1435    if ( ! $signup_field_ids ) {
     1436        global $wpdb;
     1437        $bp = buddypress();
     1438
     1439        $signup_field_ids = $wpdb->get_col( "SELECT object_id FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND meta_key = 'signup_position' ORDER BY meta_value ASC" );
     1440
     1441        wp_cache_set( 'signup_fields', $signup_field_ids, 'bp_xprofile' );
     1442    }
     1443
     1444    return array_map( 'intval', $signup_field_ids );
     1445}
     1446
     1447/**
     1448 * Returns xProfile loop's signup arguments.
     1449 *
     1450 * @since 8.0.0
     1451 *
     1452 * @param array $extra Optional extra arguments.
     1453 * @return array The xProfile loop's signup arguments.
     1454 */
     1455function bp_xprofile_signup_args( $extra = array() ) {
     1456    $signup_fields = (array) bp_xprofile_get_signup_field_ids();
     1457    $default_args  = array(
     1458        'fetch_fields'     => true,
     1459        'fetch_field_data' => false,
     1460    );
     1461
     1462    // No signup fields? Let's bring back primary group.
     1463    if ( ! $signup_fields && bp_is_register_page() ) {
     1464        $default_args['profile_group_id'] = 1;
     1465    } else {
     1466        $default_args['signup_fields_only'] = true;
     1467    }
     1468
     1469    return array_merge( $default_args, $extra );
     1470}
  • trunk/src/bp-xprofile/bp-xprofile-template.php

    r12868 r12885  
    1616 * @since 1.0.0
    1717 * @since 2.4.0 Introduced `$member_type` argument.
    18  * @since 8.0.0 Introduced `$hide_field_types` argument.
     18 * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
    1919 *
    2020 * @global object $profile_template
     
    3737 *     @type int[]|bool   $exclude_fields         Default: false.
    3838 *     @type string[]     $hide_field_types       Default: empty array.
     39 *     @type bool         $signup_fields_only     Default: false.
    3940 *     @type bool         $update_meta_cache      Default: true.
    4041 * }
     
    6970        'exclude_fields'         => false, // Comma-separated list of profile field IDs to exclude.
    7071        'hide_field_types'       => array(), // List of field types to hide from profile fields loop.
     72        'signup_fields_only'     => false, // Whether to only return signup fields.
    7173        'update_meta_cache'      => true,
    7274    ), 'has_profile' );
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php

    r12868 r12885  
    106106     * @since 1.5.0
    107107     * @since 2.4.0 Introduced `$member_type` argument.
    108      * @since 8.0.0 Introduced `$hide_field_types` argument.
     108     * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
    109109     *
    110110     * @param array|string $args {
     
    121121     *     @type int|bool     $hide_empty_fields       Should empty fields be skipped.
    122122     *     @type int|bool     $fetch_visibility_level  Fetch visibility levels.
     123     *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
     124     *     @type bool         $signup_fields_only      Whether to only return signup fields. Default: false.
    123125     *     @type int|bool     $update_meta_cache       Should metadata cache be updated.
    124      *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
    125126     * }
    126127     */
     
    160161            'exclude_fields'         => false,
    161162            'hide_field_types'       => array(),
     163            'signup_fields_only'     => false,
    162164            'update_meta_cache'      => true
    163165        ) );
    164166
    165         $this->groups      = bp_xprofile_get_groups( $r );
     167        $groups = bp_xprofile_get_groups( $r );
     168
     169        if ( true === $r['signup_fields_only'] && bp_get_signup_allowed() ) {
     170            $signup_fields_order       = bp_xprofile_get_signup_field_ids();
     171            $signup_group              = new BP_XProfile_Group();
     172            $signup_group->id          = 0;
     173            $signup_group->name        = __( 'Signup Fields', 'buddypress' );
     174            $signup_group->description = '';
     175            $signup_group->can_delete  = 0;
     176            $signup_group->group_order = 0;
     177            $fields                    = array();
     178            $signup_group->fields      = array();
     179
     180            // Get all group fields.
     181            foreach ( $groups as $group ) {
     182                if ( ! $group->fields ) {
     183                    continue;
     184                }
     185
     186                // Populate fields using the field ID as key.
     187                foreach ( $group->fields as $signup_field ) {
     188                    $fields[ $signup_field->id ] = $signup_field;
     189                }
     190            }
     191
     192            if ( $fields ) {
     193                // Reorder signup fields.
     194                foreach ( $signup_fields_order as $ordered_signup_field_id ) {
     195                    if ( ! isset( $fields[ $ordered_signup_field_id ] ) ) {
     196                        continue;
     197                    }
     198
     199                    $signup_group->fields[] = $fields[ $ordered_signup_field_id ];
     200                }
     201            }
     202
     203            // Override groups with the signup one.
     204            $groups = array( $signup_group );
     205        }
     206
     207        $this->groups      = $groups;
    166208        $this->group_count = count( $this->groups );
    167209        $this->user_id     = $r['user_id'];
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php

    r12871 r12885  
    145145        }
    146146
     147        $field_value = '';
    147148        if ( 'user_url' === $this->wp_user_key ) {
    148149            if ( bp_displayed_user_id() ) {
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php

    r12870 r12885  
    5757        'allow_custom_visibility' => false,
    5858        'member_types'            => false,
     59        'signup_position'         => true,
    5960    );
    6061
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php

    r12868 r12885  
    139139     */
    140140    public $do_autolink;
     141
     142    /**
     143     * The signup position of the field into the signups form.
     144     *
     145     * @since 8.0.0
     146     * @var int
     147     */
     148    public $signup_position;
    141149
    142150    /**
     
    861869
    862870    /**
     871     * Get the field's signup position.
     872     *
     873     * @since 8.0.0
     874     *
     875     * @return int the field's signup position.
     876     *             0 if the field has not been added to the signup form.
     877     */
     878    public function get_signup_position() {
     879        if ( ! isset( $this->signup_position ) ) {
     880            $this->signup_position = (int) bp_xprofile_get_meta( $this->id, 'field', 'signup_position' );
     881        }
     882
     883        return $this->signup_position;
     884    }
     885
     886    /**
    863887     * Get whether the field values should be auto-linked to a directory search.
    864888     *
     
    12781302                            $this->required_metabox();
    12791303
     1304                            // Output signup position metabox.
     1305                            $this->signup_position_metabox();
     1306
    12801307                            // Output the Member Types metabox.
    12811308                            $this->member_type_metabox();
     
    13461373        $supports = array(
    13471374            'switch_fieldtype'        => true,
    1348             'allow_required'          => true,
    1349             'allow_autolink'          => true,
     1375            'required'                => true,
     1376            'do_autolink'             => true,
    13501377            'allow_custom_visibility' => true,
    13511378            'member_types'            => true,
     1379            'signup_position'         => true,
    13521380        );
    13531381
     
    15031531     *
    15041532     * @since 2.4.0
     1533     *
     1534     * @return void If default field or if the field does not support the feature.
    15051535     */
    15061536    private function member_type_metabox() {
    15071537
    15081538        // The primary field is for all, so bail.
    1509         if ( 1 === (int) $this->id || ! $this->field_type_supports( 'member_types' ) ) {
     1539        if ( true === $this->is_default_field() || ! $this->field_type_supports( 'member_types' ) ) {
    15101540            return;
    15111541        }
     
    15571587     * @since 2.3.0
    15581588     *
    1559      * @return void If default field id 1.
     1589     * @return void If default field or if the field does not support the feature.
    15601590     */
    15611591    private function visibility_metabox() {
     
    16061636     * @since 2.3.0
    16071637     *
    1608      * @return void If default field.
     1638     * @return void If default field or if the field does not support the feature.
    16091639     */
    16101640    private function required_metabox() {
     
    16331663     * @since 2.5.0
    16341664     *
    1635      * @return void If default field id 1.
     1665     * @return void If the field does not support the feature.
    16361666     */
    16371667    private function autolink_metabox() {
     
    17041734
    17051735    <?php
     1736    }
     1737
     1738    /**
     1739     * Output the metabox for setting the field's position into the signup form.
     1740     *
     1741     * @since 8.0.0
     1742     *
     1743     * @return void If default field or if the field does not support the feature.
     1744     */
     1745    private function signup_position_metabox() {
     1746        // Field types not supporting the feature cannot be added to signups form.
     1747        if ( ! $this->field_type_supports( 'signup_position' ) || true === $this->is_default_field() ) {
     1748            return;
     1749        }
     1750
     1751        $next_signup_position = 1;
     1752        $signup_position      = $this->get_signup_position();
     1753
     1754        if ( 0 === $signup_position ) {
     1755            $signup_fields_order = bp_xprofile_get_signup_field_ids();
     1756            $next_signup_position = count( $signup_fields_order ) + 1;
     1757        } else {
     1758            $next_signup_position = $signup_position;
     1759        }
     1760        ?>
     1761
     1762        <div class="postbox" id="field-signup-position-metabox">
     1763            <h2><label for="default-visibility"><?php esc_html_e( 'Signups', 'buddypress' ); ?></label></h2>
     1764            <div class="inside">
     1765                <div>
     1766                    <ul>
     1767                        <li>
     1768                            <input type="checkbox" id="has-signup-position" name="signup-position" value="<?php echo esc_attr( $next_signup_position ); ?>" <?php checked( $signup_position, $next_signup_position ); ?> />
     1769                            <label for="has-signup-position"><?php esc_html_e( 'Use the field into the registration form.', 'buddypress' ); ?></label>
     1770                        </li>
     1771                    </ul>
     1772                </div>
     1773            </div>
     1774        </div>
     1775        <?php
    17061776    }
    17071777
     
    17551825                    'value' => 'textbox',
    17561826                ),
     1827                array(
     1828                    'name'  => 'signup-position',
     1829                    'id'    => 'has-signup-position',
     1830                    'value' => $this->get_signup_position(),
     1831                ),
    17571832            );
    17581833        }
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r12868 r12885  
    239239     * @since 1.2.0
    240240     * @since 2.4.0 Introduced `$member_type` argument.
    241      * @since 8.0.0 Introduced `$hide_field_types` argument.
     241     * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
    242242     *
    243243     * @global object $wpdb WordPress DB access object.
     
    245245     * @param array $args {
    246246     *  Array of optional arguments:
    247      *      @type int          $profile_group_id  Limit results to a single profile group.
    248      *      @type int          $user_id           Required if you want to load a specific user's data.
    249      *                                            Default: displayed user's ID.
    250      *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
    251      *                                            member types. If `$user_id` is provided, the value of `$member_type`
    252      *                                            will be overridden by the member types of the provided user. The
    253      *                                            special value of 'any' will return only those fields that are
    254      *                                            unrestricted by member type - i.e., those applicable to any type.
    255      *      @type bool         $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    256      *      @type bool         $hide_empty_fields True to hide fields where the user has not provided data.
    257      *                                            Default: false.
    258      *      @type bool         $fetch_fields      Whether to fetch each group's fields. Default: false.
    259      *      @type bool         $fetch_field_data  Whether to fetch data for each field. Requires a $user_id.
    260      *                                            Default: false.
    261      *      @type int[]|bool   $exclude_groups    Comma-separated list or array of group IDs to exclude.
    262      *      @type int[]|bool   $exclude_fields    Comma-separated list or array of field IDs to exclude.
    263      *      @type string[]     $hide_field_types  List of field types to hide form loop. Default: empty array.
    264      *      @type bool         $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
    265      *                                            and data. Default: true.
     247     *      @type int          $profile_group_id   Limit results to a single profile group.
     248     *      @type int          $user_id            Required if you want to load a specific user's data.
     249     *                                             Default: displayed user's ID.
     250     *      @type array|string $member_type        Limit fields by those restricted to a given member type, or array of
     251     *                                             member types. If `$user_id` is provided, the value of `$member_type`
     252     *                                             will be overridden by the member types of the provided user. The
     253     *                                             special value of 'any' will return only those fields that are
     254     *                                             unrestricted by member type - i.e., those applicable to any type.
     255     *      @type bool         $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
     256     *      @type bool         $hide_empty_fields  True to hide fields where the user has not provided data.
     257     *                                             Default: false.
     258     *      @type bool         $fetch_fields       Whether to fetch each group's fields. Default: false.
     259     *      @type bool         $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
     260     *                                             Default: false.
     261     *      @type int[]|bool   $exclude_groups     Comma-separated list or array of group IDs to exclude.
     262     *      @type int[]|bool   $exclude_fields     Comma-separated list or array of field IDs to exclude.
     263     *      @type string[]     $hide_field_types   List of field types to hide form loop. Default: empty array.
     264     *      @type bool         $signup_fields_only Whether to only return signup fields. Default: false.
     265     *      @type bool         $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
     266     *                                             and data. Default: true.
    266267     * }
    267268     * @return array $groups
     
    284285            'hide_field_types'       => array(),
    285286            'update_meta_cache'      => true,
     287            'signup_fields_only'     => false,
    286288        ) );
    287289
     
    343345        $fields = array();
    344346        foreach ( $field_ids as $field_id ) {
     347            if ( true === $r['signup_fields_only'] && ! in_array( $field_id, bp_xprofile_get_signup_field_ids(), true ) ) {
     348                continue;
     349            }
     350
    345351            $_field = xprofile_get_field( $field_id, null, false );
    346352
Note: See TracChangeset for help on using the changeset viewer.