<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][12885] trunk/src/bp-xprofile: Improve the Field/Group APIs to pick selected fields as signup ones</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/12885">12885</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/12885","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-04-16 05:29:26 +0000 (Fri, 16 Apr 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/6347">#6347</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofilebpxprofilefunctionsphp">trunk/src/bp-xprofile/bp-xprofile-functions.php</a></li>
<li><a href="#trunksrcbpxprofilebpxprofiletemplatephp">trunk/src/bp-xprofile/bp-xprofile-template.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofiledatatemplatephp">trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpresstextboxphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpressphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilegroupphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofilebpxprofilefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-functions.php   2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/bp-xprofile-functions.php     2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1421,3 +1421,50 @@
</span><span class="cx" style="display: block; padding: 0 10px">                array_keys( wp_get_user_contact_methods() )
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Returns the signup field IDs.
+ *
+ * @since 8.0.0
+ *
+ * @return int[] The signup field IDs.
+ */
+function bp_xprofile_get_signup_field_ids() {
+       $signup_field_ids = wp_cache_get( 'signup_fields', 'bp_xprofile' );
+
+       if ( ! $signup_field_ids ) {
+               global $wpdb;
+               $bp = buddypress();
+
+               $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" );
+
+               wp_cache_set( 'signup_fields', $signup_field_ids, 'bp_xprofile' );
+       }
+
+       return array_map( 'intval', $signup_field_ids );
+}
+
+/**
+ * Returns xProfile loop's signup arguments.
+ *
+ * @since 8.0.0
+ *
+ * @param array $extra Optional extra arguments.
+ * @return array The xProfile loop's signup arguments.
+ */
+function bp_xprofile_signup_args( $extra = array() ) {
+       $signup_fields = (array) bp_xprofile_get_signup_field_ids();
+       $default_args  = array(
+               'fetch_fields'     => true,
+               'fetch_field_data' => false,
+       );
+
+       // No signup fields? Let's bring back primary group.
+       if ( ! $signup_fields && bp_is_register_page() ) {
+               $default_args['profile_group_id'] = 1;
+       } else {
+               $default_args['signup_fields_only'] = true;
+       }
+
+       return array_merge( $default_args, $extra );
+}
</ins></span></pre></div>
<a id="trunksrcbpxprofilebpxprofiletemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-template.php    2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/bp-xprofile-template.php      2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -15,7 +15,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.4.0 Introduced `$member_type` argument.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 8.0.0 Introduced `$hide_field_types` argument.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @global object $profile_template
</span><span class="cx" style="display: block; padding: 0 10px">  * @see BP_XProfile_Group::get() for full description of `$args` array.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,6 +36,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type int[]|bool   $exclude_groups         Default: false.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type int[]|bool   $exclude_fields         Default: false.
</span><span class="cx" style="display: block; padding: 0 10px">  *     @type string[]     $hide_field_types       Default: empty array.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *     @type bool         $signup_fields_only     Default: false.
</ins><span class="cx" style="display: block; padding: 0 10px">  *     @type bool         $update_meta_cache      Default: true.
</span><span class="cx" style="display: block; padding: 0 10px">  * }
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,6 +69,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'exclude_groups'         => false, // Comma-separated list of profile field group IDs to exclude.
</span><span class="cx" style="display: block; padding: 0 10px">                'exclude_fields'         => false, // Comma-separated list of profile field IDs to exclude.
</span><span class="cx" style="display: block; padding: 0 10px">                'hide_field_types'       => array(), // List of field types to hide from profile fields loop.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'signup_fields_only'     => false, // Whether to only return signup fields.
</ins><span class="cx" style="display: block; padding: 0 10px">                 'update_meta_cache'      => true,
</span><span class="cx" style="display: block; padding: 0 10px">        ), 'has_profile' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofiledatatemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php 2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php   2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -105,7 +105,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.5.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.4.0 Introduced `$member_type` argument.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 8.0.0 Introduced `$hide_field_types` argument.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array|string $args {
</span><span class="cx" style="display: block; padding: 0 10px">         *     An array of arguments. All items are optional.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,8 +120,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $exclude_fields          Exclude these fields.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int|bool     $hide_empty_fields       Should empty fields be skipped.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int|bool     $fetch_visibility_level  Fetch visibility levels.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
+        *     @type bool         $signup_fields_only      Whether to only return signup fields. Default: false.
</ins><span class="cx" style="display: block; padding: 0 10px">          *     @type int|bool     $update_meta_cache       Should metadata cache be updated.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
</del><span class="cx" style="display: block; padding: 0 10px">          * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $args = '' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -159,10 +160,51 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'exclude_groups'         => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'exclude_fields'         => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'hide_field_types'       => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'signup_fields_only'     => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'update_meta_cache'      => true
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->groups      = bp_xprofile_get_groups( $r );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $groups = bp_xprofile_get_groups( $r );
+
+               if ( true === $r['signup_fields_only'] && bp_get_signup_allowed() ) {
+                       $signup_fields_order       = bp_xprofile_get_signup_field_ids();
+                       $signup_group              = new BP_XProfile_Group();
+                       $signup_group->id          = 0;
+                       $signup_group->name        = __( 'Signup Fields', 'buddypress' );
+                       $signup_group->description = '';
+                       $signup_group->can_delete  = 0;
+                       $signup_group->group_order = 0;
+                       $fields                    = array();
+                       $signup_group->fields      = array();
+
+                       // Get all group fields.
+                       foreach ( $groups as $group ) {
+                               if ( ! $group->fields ) {
+                                       continue;
+                               }
+
+                               // Populate fields using the field ID as key.
+                               foreach ( $group->fields as $signup_field ) {
+                                       $fields[ $signup_field->id ] = $signup_field;
+                               }
+                       }
+
+                       if ( $fields ) {
+                               // Reorder signup fields.
+                               foreach ( $signup_fields_order as $ordered_signup_field_id ) {
+                                       if ( ! isset( $fields[ $ordered_signup_field_id ] ) ) {
+                                               continue;
+                                       }
+
+                                       $signup_group->fields[] = $fields[ $ordered_signup_field_id ];
+                               }
+                       }
+
+                       // Override groups with the signup one.
+                       $groups = array( $signup_group );
+               }
+
+               $this->groups      = $groups;
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->group_count = count( $this->groups );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->user_id     = $r['user_id'];
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpresstextboxphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php  2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress-textbox.php    2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,6 +144,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->wp_user_key = self::get_field_settings( bp_get_the_profile_field_id() );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $field_value = '';
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'user_url' === $this->wp_user_key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( bp_displayed_user_id() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $field_value = bp_get_displayed_user()->userdata->{$this->wp_user_key};
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldtypewordpressphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php  2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-wordpress.php    2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -56,6 +56,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'do_autolink'             => false,
</span><span class="cx" style="display: block; padding: 0 10px">                'allow_custom_visibility' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                'member_types'            => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'signup_position'         => true,
</ins><span class="cx" style="display: block; padding: 0 10px">         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php 2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php   2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,6 +140,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $do_autolink;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * The signup position of the field into the signups form.
+        *
+        * @since 8.0.0
+        * @var int
+        */
+       public $signup_position;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Field type option.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -860,6 +868,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get the field's signup position.
+        *
+        * @since 8.0.0
+        *
+        * @return int the field's signup position.
+        *             0 if the field has not been added to the signup form.
+        */
+       public function get_signup_position() {
+               if ( ! isset( $this->signup_position ) ) {
+                       $this->signup_position = (int) bp_xprofile_get_meta( $this->id, 'field', 'signup_position' );
+               }
+
+               return $this->signup_position;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get whether the field values should be auto-linked to a directory search.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Lazy-loaded to reduce overhead.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1277,6 +1301,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                        // Output the required metabox.
</span><span class="cx" style="display: block; padding: 0 10px">                                                        $this->required_metabox();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                        // Output signup position metabox.
+                                                       $this->signup_position_metabox();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                                         // Output the Member Types metabox.
</span><span class="cx" style="display: block; padding: 0 10px">                                                        $this->member_type_metabox();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1345,10 +1372,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_field_type_supports() {
</span><span class="cx" style="display: block; padding: 0 10px">                $supports = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'switch_fieldtype'        => true,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'allow_required'          => true,
-                       'allow_autolink'          => true,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'required'                => true,
+                       'do_autolink'             => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'allow_custom_visibility' => true,
</span><span class="cx" style="display: block; padding: 0 10px">                        'member_types'            => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'signup_position'         => true,
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $this->type_obj ) && $this->type_obj ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1502,11 +1530,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Private method used to output field Member Type metabox.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @return void If default field or if the field does not support the feature.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        private function member_type_metabox() {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // The primary field is for all, so bail.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 1 === (int) $this->id || ! $this->field_type_supports( 'member_types' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( true === $this->is_default_field() || ! $this->field_type_supports( 'member_types' ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1556,7 +1586,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return void If default field id 1.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return void If default field or if the field does not support the feature.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        private function visibility_metabox() {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1605,7 +1635,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.3.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return void If default field.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return void If default field or if the field does not support the feature.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        private function required_metabox() {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1632,7 +1662,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.5.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return void If default field id 1.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return void If the field does not support the feature.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        private function autolink_metabox() {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1706,6 +1736,46 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Output the metabox for setting the field's position into the signup form.
+        *
+        * @since 8.0.0
+        *
+        * @return void If default field or if the field does not support the feature.
+        */
+       private function signup_position_metabox() {
+               // Field types not supporting the feature cannot be added to signups form.
+               if ( ! $this->field_type_supports( 'signup_position' ) || true === $this->is_default_field() ) {
+                       return;
+               }
+
+               $next_signup_position = 1;
+               $signup_position      = $this->get_signup_position();
+
+               if ( 0 === $signup_position ) {
+                       $signup_fields_order = bp_xprofile_get_signup_field_ids();
+                       $next_signup_position = count( $signup_fields_order ) + 1;
+               } else {
+                       $next_signup_position = $signup_position;
+               }
+               ?>
+
+               <div class="postbox" id="field-signup-position-metabox">
+                       <h2><label for="default-visibility"><?php esc_html_e( 'Signups', 'buddypress' ); ?></label></h2>
+                       <div class="inside">
+                               <div>
+                                       <ul>
+                                               <li>
+                                                       <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 ); ?> />
+                                                       <label for="has-signup-position"><?php esc_html_e( 'Use the field into the registration form.', 'buddypress' ); ?></label>
+                                               </li>
+                                       </ul>
+                               </div>
+                       </div>
+               </div>
+               <?php
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Output hidden fields used by default field.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.3.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1754,6 +1824,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'id'    => 'fieldtype',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'value' => 'textbox',
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                array(
+                                       'name'  => 'signup-position',
+                                       'id'    => 'has-signup-position',
+                                       'value' => $this->get_signup_position(),
+                               ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilegroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php 2021-04-14 04:13:56 UTC (rev 12884)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php   2021-04-16 05:29:26 UTC (rev 12885)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -238,31 +238,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.2.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.4.0 Introduced `$member_type` argument.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 8.0.0 Introduced `$hide_field_types` argument.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @global object $wpdb WordPress DB access object.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args {
</span><span class="cx" style="display: block; padding: 0 10px">         *  Array of optional arguments:
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *      @type int          $profile_group_id  Limit results to a single profile group.
-        *      @type int          $user_id           Required if you want to load a specific user's data.
-        *                                            Default: displayed user's ID.
-        *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
-        *                                            member types. If `$user_id` is provided, the value of `$member_type`
-        *                                            will be overridden by the member types of the provided user. The
-        *                                            special value of 'any' will return only those fields that are
-        *                                            unrestricted by member type - i.e., those applicable to any type.
-        *      @type bool         $hide_empty_groups True to hide groups that don't have any fields. Default: false.
-        *      @type bool         $hide_empty_fields True to hide fields where the user has not provided data.
-        *                                            Default: false.
-        *      @type bool         $fetch_fields      Whether to fetch each group's fields. Default: false.
-        *      @type bool         $fetch_field_data  Whether to fetch data for each field. Requires a $user_id.
-        *                                            Default: false.
-        *      @type int[]|bool   $exclude_groups    Comma-separated list or array of group IDs to exclude.
-        *      @type int[]|bool   $exclude_fields    Comma-separated list or array of field IDs to exclude.
-        *      @type string[]     $hide_field_types  List of field types to hide form loop. Default: empty array.
-        *      @type bool         $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
-        *                                            and data. Default: true.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *      @type int          $profile_group_id   Limit results to a single profile group.
+        *      @type int          $user_id            Required if you want to load a specific user's data.
+        *                                             Default: displayed user's ID.
+        *      @type array|string $member_type        Limit fields by those restricted to a given member type, or array of
+        *                                             member types. If `$user_id` is provided, the value of `$member_type`
+        *                                             will be overridden by the member types of the provided user. The
+        *                                             special value of 'any' will return only those fields that are
+        *                                             unrestricted by member type - i.e., those applicable to any type.
+        *      @type bool         $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
+        *      @type bool         $hide_empty_fields  True to hide fields where the user has not provided data.
+        *                                             Default: false.
+        *      @type bool         $fetch_fields       Whether to fetch each group's fields. Default: false.
+        *      @type bool         $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
+        *                                             Default: false.
+        *      @type int[]|bool   $exclude_groups     Comma-separated list or array of group IDs to exclude.
+        *      @type int[]|bool   $exclude_fields     Comma-separated list or array of field IDs to exclude.
+        *      @type string[]     $hide_field_types   List of field types to hide form loop. Default: empty array.
+        *      @type bool         $signup_fields_only Whether to only return signup fields. Default: false.
+        *      @type bool         $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
+        *                                             and data. Default: true.
</ins><span class="cx" style="display: block; padding: 0 10px">          * }
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array $groups
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,6 +284,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'exclude_fields'         => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'hide_field_types'       => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'update_meta_cache'      => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'signup_fields_only'     => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Keep track of object IDs for cache-priming.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -342,6 +344,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Pull field objects from the cache.
</span><span class="cx" style="display: block; padding: 0 10px">                $fields = array();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $field_ids as $field_id ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( true === $r['signup_fields_only'] && ! in_array( $field_id, bp_xprofile_get_signup_field_ids(), true ) ) {
+                               continue;
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $_field = xprofile_get_field( $field_id, null, false );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( in_array( $_field->type, $r['hide_field_types'], true ) ) {
</span></span></pre>
</div>
</div>

</body>
</html>