Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/16/2021 05:32:04 AM (5 years ago)
Author:
imath
Message:

Use drag and drop into the xProfile Admin UI to to set signup fields

  • Create a new "Signup Fields" tab to list signup fields. By default this tab will include the primary field (Name).
  • The install process has been adapted to make sure this is the case.
  • An upgrade task is migrating fields into the primary group as signup fields.
  • Make it possible to drag fields from any group into the "Signup Fields" one.
  • Make it possible to use drag and drop to reorder signup fields from the corresponding tab.
  • Include a link on each field entry (except the primary one) to remove the field from this "Signup Fields" field group.

Props johnjamesjacoby, boonebgorges, DJPaul, Offereins

See #6347

File:
1 edited

Legend:

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

    r12881 r12886  
    189189                                        <?php endforeach; endif; ?>
    190190
     191                                        <?php if ( bp_get_signup_allowed() ) : ?>
     192                                                <li id="signup-group" class="not-sortable last">
     193                                                        <a href="#tabs-signup-group" class="ui-tab">
     194                                                                <?php esc_html_e( 'Signup Fields', 'buddypress' ); ?>
     195                                                        </a>
     196                                                </li>
     197                                        <?php endif; ?>
     198
    191199                                </ul>
    192200
    … …  
    295303                                                </fieldset>
    296304
    297                                                 <?php if ( empty( $group->can_delete ) ) : ?>
    298 
    299                                                         <p><?php esc_html_e( '* Fields in this group appear on the signup page.', 'buddypress' ); ?></p>
    300 
    301                                                 <?php endif; ?>
    302 
    303305                                        </div>
    304306
    … …  
    310312                                <?php endif; ?>
    311313
     314                                <?php
     315                                // List fields to use into the signup form.
     316                                if ( bp_get_signup_allowed() ) {
     317                                        $signup_groups = bp_xprofile_get_groups(
     318                                                array(
     319                                                        'fetch_fields'       => true,
     320                                                        'signup_fields_only' => true,
     321                                                )
     322                                        );
     323                                        $has_signup_fields   = false;
     324                                        $signup_fields       = array();
     325                                        $signup_fields_order = bp_xprofile_get_signup_field_ids();
     326                                        ?>
     327                                        <div id="tabs-signup-group"" class="tab-wrapper">
     328                                                <div class="tab-toolbar">
     329                                                        <p class="description"><?php esc_html_e( 'Drag fields from other groups and drop them on the above tab to include them into your registration form.', 'buddypress' ); ?></a>
     330                                                </div>
     331                                                <fieldset id="signup-fields" class="connectedSortable field-group" aria-live="polite" aria-atomic="true" aria-relevant="all">
     332                                                        <legend class="screen-reader-text">
     333                                                                <?php esc_html_e( 'Fields to use into the registration form', 'buddypress' );?>
     334                                                        </legend>
     335
     336                                                        <?php
     337                                                        if ( ! empty( $signup_groups ) ) {
     338                                                                foreach ( $signup_groups as $signup_group ) {
     339                                                                        if ( ! empty( $signup_group->fields ) ) {
     340                                                                                $has_signup_fields = true;
     341
     342                                                                                foreach ( $signup_group->fields as $signup_field ) {
     343                                                                                        // Load the field.
     344                                                                                        $_signup_field = xprofile_get_field( $signup_field, null, false );
     345
     346                                                                                        /**
     347                                                                                         * This function handles the WYSIWYG profile field
     348                                                                                         * display for the xprofile admin setup screen.
     349                                                                                         */
     350                                                                                        $signup_fields[ $_signup_field->id ] = bp_xprofile_admin_get_signup_field( $_signup_field, $signup_group, '' );
     351                                                                                }
     352                                                                        }
     353                                                                }
     354
     355                                                                // Output signup fields according to their signup position.
     356                                                                foreach ( $signup_fields_order as $ordered_signup_field_id ) {
     357                                                                        if ( ! isset( $signup_fields[ $ordered_signup_field_id ] ) ) {
     358                                                                                continue;
     359                                                                        }
     360
     361                                                                        echo $signup_fields[ $ordered_signup_field_id ];
     362                                                                }
     363                                                        }
     364
     365                                                        if ( ! $has_signup_fields ) {
     366                                                                ?>
     367                                                                <p class="nodrag nofields"><?php esc_html_e( 'There are no registration fields set. The registration form uses the primary group by default.', 'buddypress' ); ?></p>
     368                                                                <?php
     369                                                        }
     370                                                        ?>
     371                                                </fieldset>
     372
     373                                                <p><?php esc_html_e( '* Fields in this group appear on the registration page.', 'buddypress' ); ?></p>
     374                                        </div>
     375                                        <?php
     376                                }
     377                                ?>
    312378                        </div>
    313379                </form>
    314380        </div>
    315 
    316381<?php
    317382}
    … …  
    581646
    582647                                // Validate signup.
    583                                 if ( ! empty( $_POST['signup-position'] ) ) {
    584                                         bp_xprofile_update_field_meta( $field_id, 'signup_position', (int) $_POST['signup-position'] );
    585                                 } else {
    586                                         bp_xprofile_delete_meta( $field_id, 'field', 'signup_position' );
     648                                if ( $field->field_type_supports( 'signup_position' ) ) {
     649                                        if ( ! empty( $_POST['signup-position'] ) ) {
     650                                                bp_xprofile_update_field_meta( $field_id, 'signup_position', (int) $_POST['signup-position'] );
     651                                        } else {
     652                                                bp_xprofile_delete_meta( $field_id, 'field', 'signup_position' );
     653                                        }
    587654                                }
    588655
    … …  
    736803 *
    737804 * @since 1.0.0
     805 * @since 8.0.0 Returns a JSON object.
    738806 */
    739807function xprofile_ajax_reorder_fields() {
    740 
    741808        // Check the nonce.
    742809        check_admin_referer( 'bp_reorder_fields', '_wpnonce_reorder_fields' );
    743810
    744811        if ( empty( $_POST['field_order'] ) ) {
    745                 return false;
    746         }
    747 
    748         parse_str( $_POST['field_order'], $order );
     812                return wp_send_json_error();
     813        }
    749814
    750815        $field_group_id = $_POST['field_group_id'];
    751 
    752         foreach ( (array) $order['draggable_field'] as $position => $field_id ) {
    753                 xprofile_update_field_position( (int) $field_id, (int) $position, (int) $field_group_id );
     816        $group_tab      = '';
     817
     818        if ( isset( $_POST['group_tab'] ) && $_POST['group_tab'] ) {
     819                $group_tab = wp_unslash( $_POST['group_tab'] );
     820        }
     821
     822        if ( 'signup-fields' === $field_group_id ) {
     823                parse_str( $_POST['field_order'], $order );
     824                $fields = (array) $order['draggable_signup_field'];
     825                $fields = array_map( 'intval', $fields );
     826
     827                if ( isset( $_POST['new_signup_field_id'] ) && $_POST['new_signup_field_id'] ) {
     828                        parse_str( $_POST['new_signup_field_id'], $signup_field );
     829                        $signup_fields = (array) $signup_field['draggable_signup_field'];
     830                }
     831
     832                // Adding a new field to the registration form.
     833                if ( 'signup-group' === $group_tab ) {
     834                        $field_id = (int) reset( $signup_fields );
     835
     836                        // Load the field.
     837                        $field = xprofile_get_field( $field_id, null, false );
     838
     839                        if ( $field instanceof BP_XProfile_Field ) {
     840                                // The field doesn't support the feature, stop right away!
     841                                if ( ! $field->field_type_supports( 'signup_position' ) ) {
     842                                        wp_send_json_error(
     843                                                array(
     844                                                        'message' => __( 'This field cannot be inserted into the registration form.', 'buddypress' ),
     845                                                )
     846                                        );
     847                                }
     848
     849                                $signup_position = bp_xprofile_get_meta( $field->id, 'field', 'signup_position' );
     850
     851                                if ( ! $signup_position ) {
     852                                        $position = array_search( $field->id, $fields, true );
     853                                        if ( false !== $position ) {
     854                                                $position += 1;
     855                                        } else {
     856                                                $position = 1;
     857                                        }
     858
     859                                        // Set the signup position.
     860                                        bp_xprofile_update_field_meta( $field->id, 'signup_position', $position );
     861
     862                                        // Get the real Group object.
     863                                        $group = xprofile_get_field_group( $field->id );
     864
     865                                        // Gets the HTML Output of the signup field.
     866                                        $signup_field = bp_xprofile_admin_get_signup_field( $field, $group );
     867
     868                                        /**
     869                                         * Fires once a signup field has been inserted.
     870                                         *
     871                                         * @since 8.0.0
     872                                         */
     873                                        do_action( 'bp_xprofile_inserted_signup_field' );
     874
     875                                        // Send the signup field to output.
     876                                        wp_send_json_success(
     877                                                array(
     878                                                        'signup_field' => $signup_field,
     879                                                        'field_id'     => $field->id,
     880                                                )
     881                                        );
     882                                } else {
     883                                        wp_send_json_error(
     884                                                array(
     885                                                        'message' => __( 'This field has been already added to the registration form.', 'buddypress' ),
     886                                                )
     887                                        );
     888                                }
     889
     890                        } else {
     891                                wp_send_json_error();
     892                        }
     893                } else {
     894                        // it's a sort operation.
     895                        foreach ( $fields as $position => $field_id ) {
     896                                bp_xprofile_update_field_meta( (int) $field_id, 'signup_position', (int) $position + 1 );
     897                        }
     898
     899                        /**
     900                         * Fires once the signup fields have been reordered.
     901                         *
     902                         * @since 8.0.0
     903                         */
     904                        do_action( 'bp_xprofile_reordered_signup_fields' );
     905
     906                        wp_send_json_success();
     907                }
     908        } else {
     909                /**
     910                 * @todo there's something going wrong here.
     911                 * moving a field to another tab when there's only the fullname field fails.
     912                 */
     913                parse_str( $_POST['field_order'], $order );
     914                $fields = (array) $order['draggable_field'];
     915
     916                foreach ( $fields as $position => $field_id ) {
     917                        xprofile_update_field_position( (int) $field_id, (int) $position, (int) $field_group_id );
     918                }
     919
     920                wp_send_json_success();
    754921        }
    755922}
    756923add_action( 'wp_ajax_xprofile_reorder_fields', 'xprofile_ajax_reorder_fields' );
     924
     925/**
     926 * Removes a field from signup fields.
     927 *
     928 * @since 8.0.0
     929 */
     930function bp_xprofile_ajax_remove_signup_field() {
     931        // Check the nonce.
     932        check_admin_referer( 'bp_reorder_fields', '_wpnonce_reorder_fields' );
     933
     934        if ( ! isset( $_POST['signup_field_id'] ) || ! $_POST['signup_field_id'] ) {
     935                return wp_send_json_error();
     936        }
     937
     938        $signup_field_id = (int) wp_unslash( $_POST['signup_field_id'] );
     939
     940        // Validate the field ID.
     941        $signup_position = bp_xprofile_get_meta( $signup_field_id, 'field', 'signup_position' );
     942
     943        if ( ! $signup_position ) {
     944                wp_send_json_error();
     945        }
     946
     947        bp_xprofile_delete_meta( $signup_field_id, 'field', 'signup_position' );
     948
     949        /**
     950         * Fires when a signup field is removed from the signup form.
     951         *
     952         * @since 8.0.0
     953         */
     954        do_action( 'bp_xprofile_removed_signup_field' );
     955
     956        wp_send_json_success();
     957}
     958add_action( 'wp_ajax_xprofile_remove_signup_field', 'bp_xprofile_ajax_remove_signup_field' );
    757959
    758960/**
    … …  
    782984 *
    783985 * @since 1.5.0
     986 * @since 8.0.0 Adds the `$is_signup` parameter.
    784987 *
    785988 * @param BP_XProfile_Field   $admin_field Admin field.
    786989 * @param object $admin_group Admin group object.
    787990 * @param string $class       Classes to append to output.
    788  */
    789 function xprofile_admin_field( $admin_field, $admin_group, $class = '' ) {
     991 * @param bool   $is_signup   Whether the admin field output is made inside the signup group.
     992 */
     993function xprofile_admin_field( $admin_field, $admin_group, $class = '', $is_signup = false ) {
    790994        global $field;
    791995
    792         $field = $admin_field;
     996        $field       = $admin_field;
     997        $fieldset_id = sprintf( 'draggable_field_%d', $field->id );
    793998
    794999        // Users admin URL.
    … …  
    8101015                        'field_id' => (int) $field->id
    8111016                ), $url . '#tabs-' . (int) $field->group_id );
    812         } ?>
    813 
    814         <fieldset id="draggable_field_<?php echo esc_attr( $field->id ); ?>" class="sortable<?php echo ' ' . $field->type; if ( !empty( $class ) ) echo ' ' . $class; ?>">
     1017        }
     1018
     1019        // Avoid duplicate IDs into the signup group.
     1020        if ( $is_signup ) {
     1021                $fieldset_id = sprintf( 'draggable_signup_field_%d', $field->id );
     1022        }
     1023        ?>
     1024
     1025        <fieldset id="<?php echo esc_attr( $fieldset_id ); ?>" class="sortable<?php echo ' ' . $field->type; if ( !empty( $class ) ) echo ' ' . $class; ?>">
    8151026                <legend>
    8161027                        <span>
    8171028                                <?php bp_the_profile_field_name(); ?>
    8181029
    819                                 <?php if ( empty( $field->can_delete )                                    ) : ?><?php esc_html_e( '(Primary)', 'buddypress' ); endif; ?>
     1030                                <?php if ( empty( $field->can_delete ) ) : ?><?php esc_html_e( '(Primary)', 'buddypress' ); endif; ?>
    8201031                                <?php bp_the_profile_field_required_label(); ?>
    821                                 <?php if ( bp_xprofile_get_meta( $field->id, 'field', 'signup_position' ) ) : ?><?php esc_html_e( '(Sign-up)', 'buddypress' ); endif; ?>
     1032                                <?php if ( bp_get_signup_allowed() && $field->get_signup_position() ) : ?>
     1033                                        <span class="bp-signup-field-label"><?php esc_html_e( '(Sign-up)', 'buddypress' );?></span>
     1034                                <?php endif; ?>
    8221035                                <?php if ( bp_get_member_types() ) : echo $field->get_member_type_label(); endif; ?>
    8231036
    … …  
    8651078                                <a class="button edit" href="<?php echo esc_url( $field_edit_url ); ?>"><?php _ex( 'Edit', 'Edit field link', 'buddypress' ); ?></a>
    8661079
    867                                 <?php if ( $field->can_delete ) : ?>
     1080                                <?php if ( $field->can_delete && ! $is_signup ) : ?>
    8681081
    8691082                                        <div class="delete-button">
    8701083                                                <a class="confirm submit-delete deletion" href="<?php echo esc_url( wp_nonce_url( $field_delete_url, 'bp_xprofile_delete_field-' . $field->id, 'bp_xprofile_delete_field' ) ); ?>"><?php _ex( 'Delete', 'Delete field link', 'buddypress' ); ?></a>
     1084                                        </div>
     1085
     1086                                <?php endif; ?>
     1087
     1088                                <?php if ( $field->can_delete && $is_signup ) : ?>
     1089
     1090                                        <div class="delete-button">
     1091                                                <a class="submit-delete removal" href="<?php echo esc_attr( sprintf( '#remove_field-%d', $field->id ) ); ?>"><?php echo esc_html_x( 'Remove', 'Remove field link', 'buddypress' ); ?></a>
    8711092                                        </div>
    8721093
    … …  
    8881109                </div>
    8891110        </fieldset>
    890 
    8911111<?php
     1112}
     1113
     1114/**
     1115 * Handles the WYSIWYG display of signup profile fields on the edit screen.
     1116 *
     1117 * @since 8.0.0
     1118 *
     1119 * @param BP_XProfile_Field   $signup_field The field to use into the signup form.
     1120 * @param object $field_group The real field group object.
     1121 * @param string $class       Classes to append to output.
     1122 * @param bool   $echo        Whether to return or display the HTML output.
     1123 * @return string The HTML output.
     1124 */
     1125function bp_xprofile_admin_get_signup_field( $signup_field, $field_group = null, $class = '', $echo = false ) {
     1126        add_filter( 'bp_get_the_profile_field_input_name', 'bp_get_the_profile_signup_field_input_name' );
     1127
     1128        if ( ! $echo ) {
     1129                // Set up an output buffer.
     1130                ob_start();
     1131                xprofile_admin_field( $signup_field, $field_group, $class, true );
     1132                $output = ob_get_contents();
     1133                ob_end_clean();
     1134        } else {
     1135                xprofile_admin_field( $signup_field, $field_group, $class, true );
     1136        }
     1137
     1138        remove_filter( 'bp_get_the_profile_field_input_name', 'bp_get_the_profile_signup_field_input_name' );
     1139
     1140        if ( ! $echo ) {
     1141                return $output;
     1142        }
    8921143}
    8931144
Note: See TracChangeset for help on using the changeset viewer.