[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/ -> bp-xprofile-admin.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Admin.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileAdmin
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Creates the administration interface menus and checks to see if the DB
  15   * tables are set up.
  16   *
  17   * @since 1.0.0
  18   *
  19   * @return bool
  20   */
  21  function xprofile_add_admin_menu() {
  22  
  23      // Bail if current user cannot moderate community.
  24      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
  25          return false;
  26      }
  27  
  28      add_users_page( _x( 'Profile Fields', 'xProfile admin page title', 'buddypress' ), _x( 'Profile Fields', 'Admin Users menu', 'buddypress' ), 'manage_options', 'bp-profile-setup', 'xprofile_admin' );
  29  }
  30  add_action( bp_core_admin_hook(), 'xprofile_add_admin_menu' );
  31  
  32  /**
  33   * Handles all actions for the admin area for creating, editing and deleting
  34   * profile groups and fields.
  35   *
  36   * @since 1.0.0
  37   *
  38   * @param string $message Message to display.
  39   * @param string $type    Type of action to be displayed.
  40   */
  41  function xprofile_admin( $message = '', $type = 'error' ) {
  42  
  43      // What mode?
  44      $mode = ! empty( $_GET['mode'] )
  45          ? sanitize_key( $_GET['mode'] )
  46          : false;
  47  
  48      // Group ID.
  49      $group_id = ! empty( $_GET['group_id'] )
  50          ? intval( $_GET['group_id'] )
  51          : false;
  52  
  53      // Field ID.
  54      $field_id = ! empty( $_GET['field_id'] )
  55          ? intval( $_GET['field_id'] )
  56          : false;
  57  
  58      // Option ID.
  59      $option_id = ! empty( $_GET['option_id'] )
  60          ? intval( $_GET['option_id'] )
  61          : false;
  62  
  63      // Allowed modes.
  64      $allowed_modes = array(
  65          'add_group',
  66          'edit_group',
  67          'delete_group',
  68          'do_delete_group',
  69          'add_field',
  70          'edit_field',
  71          'delete_field',
  72          'do_delete_field',
  73          'delete_option',
  74          'do_delete_option'
  75      );
  76  
  77      // Is an allowed mode.
  78      if ( in_array( $mode, $allowed_modes, true ) ) {
  79  
  80          // All group actions.
  81          if ( false !== $group_id ) {
  82  
  83              // Add field to group.
  84              if ( 'add_field' == $mode ) {
  85                  xprofile_admin_manage_field( $group_id );
  86  
  87              // Edit field of group.
  88              } elseif ( ! empty( $field_id ) && 'edit_field' === $mode ) {
  89                  xprofile_admin_manage_field( $group_id, $field_id );
  90  
  91              // Delete group.
  92              } elseif ( in_array( $mode, array( 'delete_group', 'do_delete_group' ), true ) ) {
  93                  xprofile_admin_delete_group( $group_id );
  94  
  95              // Edit group.
  96              } elseif ( 'edit_group' === $mode ) {
  97                  xprofile_admin_manage_group( $group_id );
  98              }
  99  
 100          // Delete field.
 101          } elseif ( ( false !== $field_id ) && ( in_array( $mode, array( 'delete_field', 'do_delete_field' ), true ) ) ) {
 102              xprofile_admin_delete_field( $field_id, 'field' );
 103  
 104          // Delete option.
 105          } elseif ( ! empty( $option_id ) && in_array( $mode, array( 'delete_option', 'do_delete_option' ), true ) ) {
 106              xprofile_admin_delete_field( $option_id, 'option' );
 107  
 108          // Add group.
 109          } elseif ( 'add_group' == $mode ) {
 110              xprofile_admin_manage_group();
 111          }
 112  
 113      } else {
 114          xprofile_admin_screen( $message, $type );
 115      }
 116  }
 117  
 118  /**
 119   * Output the main XProfile management screen.
 120   *
 121   * @since 2.3.0
 122   *
 123   * @param string $message Feedback message.
 124   * @param string $type    Feedback type.
 125   *
 126   * @todo Improve error message output
 127   */
 128  function xprofile_admin_screen( $message = '', $type = 'error' ) {
 129  
 130      // Users admin URL.
 131      $url = bp_get_admin_url( 'users.php' );
 132  
 133      // Add Group.
 134      $add_group_url = add_query_arg( array(
 135          'page' => 'bp-profile-setup',
 136          'mode' => 'add_group'
 137      ), $url );
 138  
 139      // Validate type.
 140      $type = preg_replace( '|[^a-z]|i', '', $type );
 141  
 142      // Get all of the profile groups & fields.
 143      $groups = bp_xprofile_get_groups( array(
 144          'fetch_fields' => true
 145      ) ); ?>
 146  
 147      <div class="wrap">
 148          <h1 class="wp-heading-inline"><?php _ex( 'Profile Fields', 'Settings page header', 'buddypress'); ?></h1>
 149  
 150              <a id="add_group" class="page-title-action" href="<?php echo esc_url( $add_group_url ); ?>"><?php _e( 'Add New Field Group', 'buddypress' ); ?></a>
 151  
 152          <hr class="wp-header-end">
 153  
 154          <form action="" id="profile-field-form" method="post">
 155  
 156              <?php
 157  
 158              wp_nonce_field( 'bp_reorder_fields', '_wpnonce_reorder_fields'        );
 159              wp_nonce_field( 'bp_reorder_groups', '_wpnonce_reorder_groups', false );
 160  
 161              if ( ! empty( $message ) ) :
 162                  $type = ( $type == 'error' ) ? 'error' : 'updated'; ?>
 163  
 164                  <div id="message" class="<?php echo $type; ?> fade notice is-dismissible">
 165                      <p><?php echo esc_html( $message ); ?></p>
 166                  </div>
 167  
 168              <?php endif; ?>
 169  
 170              <div id="tabs" aria-live="polite" aria-atomic="true" aria-relevant="all">
 171                  <ul id="field-group-tabs">
 172  
 173                      <?php if ( !empty( $groups ) ) : foreach ( $groups as $group ) : ?>
 174  
 175                          <li id="group_<?php echo esc_attr( $group->id ); ?>">
 176                              <a href="#tabs-<?php echo esc_attr( $group->id ); ?>" class="ui-tab">
 177                                  <?php
 178                                  /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 179                                  echo esc_html( apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 180                                  ?>
 181  
 182                                  <?php if ( !$group->can_delete ) : ?>
 183                                      <?php _e( '(Primary)', 'buddypress'); ?>
 184                                  <?php endif; ?>
 185  
 186                              </a>
 187                          </li>
 188  
 189                      <?php endforeach; endif; ?>
 190  
 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  
 199                  </ul>
 200  
 201                  <?php if ( !empty( $groups ) ) : foreach ( $groups as $group ) :
 202  
 203                      // Add Field to Group URL.
 204                      $add_field_url = add_query_arg( array(
 205                          'page'     => 'bp-profile-setup',
 206                          'mode'     => 'add_field',
 207                          'group_id' => (int) $group->id
 208                      ), $url );
 209  
 210                      // Edit Group URL.
 211                      $edit_group_url = add_query_arg( array(
 212                          'page'     => 'bp-profile-setup',
 213                          'mode'     => 'edit_group',
 214                          'group_id' => (int) $group->id
 215                      ), $url );
 216  
 217                      // Delete Group URL.
 218                      $delete_group_url = wp_nonce_url( add_query_arg( array(
 219                          'page'     => 'bp-profile-setup',
 220                          'mode'     => 'delete_group',
 221                          'group_id' => (int) $group->id
 222                      ), $url ), 'bp_xprofile_delete_group' ); ?>
 223  
 224                      <noscript>
 225                          <h3><?php
 226                          /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 227                          echo esc_html( apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 228                          ?></h3>
 229                      </noscript>
 230  
 231                      <div id="tabs-<?php echo esc_attr( $group->id ); ?>" class="tab-wrapper">
 232                          <div class="tab-toolbar">
 233                              <div class="tab-toolbar-left">
 234                                  <a class="button-primary" href="<?php echo esc_url( $add_field_url ); ?>"><?php _e( 'Add New Field', 'buddypress' ); ?></a>
 235                                  <a class="button edit" href="<?php echo esc_url( $edit_group_url ); ?>"><?php _ex( 'Edit Group', 'Edit Profile Fields Group', 'buddypress' ); ?></a>
 236  
 237                                  <?php if ( $group->can_delete ) : ?>
 238  
 239                                      <div class="delete-button">
 240                                          <a class="confirm submitdelete deletion ajax-option-delete" href="<?php echo esc_url( $delete_group_url ); ?>"><?php _ex( 'Delete Group', 'Delete Profile Fields Group', 'buddypress' ); ?></a>
 241                                      </div>
 242  
 243                                  <?php endif; ?>
 244  
 245                                  <?php
 246  
 247                                  /**
 248                                   * Fires at end of action buttons in xprofile management admin.
 249                                   *
 250                                   * @since 2.2.0
 251                                   *
 252                                   * @param BP_XProfile_Group $group BP_XProfile_Group object
 253                                   *                                 for the current group.
 254                                   */
 255                                  do_action( 'xprofile_admin_group_action', $group ); ?>
 256  
 257                              </div>
 258                          </div>
 259  
 260                          <?php if ( ! empty( $group->description ) ) : ?>
 261  
 262                              <p><?php
 263                              /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 264                              echo esc_html( apply_filters( 'bp_get_the_profile_group_description', $group->description ) );
 265                              ?></p>
 266  
 267                          <?php endif; ?>
 268  
 269                          <fieldset id="<?php echo esc_attr( $group->id ); ?>" class="connectedSortable field-group" aria-live="polite" aria-atomic="true" aria-relevant="all">
 270                              <legend class="screen-reader-text"><?php
 271                              /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 272                              /* translators: accessibility text */
 273                              printf( esc_html__( 'Fields for "%s" Group', 'buddypress' ), apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 274                              ?></legend>
 275  
 276                              <?php
 277  
 278                              if ( !empty( $group->fields ) ) :
 279                                  foreach ( $group->fields as $field ) {
 280  
 281                                      // Load the field.
 282                                      $field = xprofile_get_field( $field->id, null, false );
 283  
 284                                      $class = '';
 285                                      if ( empty( $field->can_delete ) ) {
 286                                          $class = ' core nodrag';
 287                                      }
 288  
 289                                      /**
 290                                       * This function handles the WYSIWYG profile field
 291                                       * display for the xprofile admin setup screen.
 292                                       */
 293                                      xprofile_admin_field( $field, $group, $class );
 294  
 295                                  } // end for
 296  
 297                              else : // !$group->fields ?>
 298  
 299                                  <p class="nodrag nofields"><?php _e( 'There are no fields in this group.', 'buddypress' ); ?></p>
 300  
 301                              <?php endif; // End $group->fields. ?>
 302  
 303                          </fieldset>
 304  
 305                      </div>
 306  
 307                  <?php endforeach; else : ?>
 308  
 309                      <div id="message" class="error notice is-dismissible"><p><?php _ex( 'You have no groups.', 'You have no profile fields groups.', 'buddypress' ); ?></p></div>
 310                      <p><a href="<?php echo esc_url( $add_group_url ); ?>"><?php _ex( 'Add New Group', 'Add New Profile Fields Group', 'buddypress' ); ?></a></p>
 311  
 312                  <?php endif; ?>
 313  
 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                  ?>
 378              </div>
 379          </form>
 380      </div>
 381  <?php
 382  }
 383  
 384  /**
 385   * Handles the adding or editing of groups.
 386   *
 387   * @since 1.0.0
 388   *
 389   * @param int|null $group_id Group ID to manage.
 390   */
 391  function xprofile_admin_manage_group( $group_id = null ) {
 392      global $message, $type;
 393  
 394      // Get the field group.
 395      $group = new BP_XProfile_Group( $group_id );
 396  
 397      // Updating.
 398      if ( isset( $_POST['save_group'] ) ) {
 399  
 400          // Check nonce.
 401          check_admin_referer( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' );
 402  
 403          // Validate $_POSTed data.
 404          if ( BP_XProfile_Group::admin_validate() ) {
 405  
 406              // Set the group name.
 407              $group->name = $_POST['group_name'];
 408  
 409              // Set the group description.
 410              if ( ! empty( $_POST['group_description'] ) ) {
 411                  $group->description = $_POST['group_description'];
 412              } else {
 413                  $group->description = '';
 414              }
 415  
 416              // Attempt to save the field group.
 417              if ( false === $group->save() ) {
 418                  $message = __( 'There was an error saving the group. Please try again.', 'buddypress' );
 419                  $type    = 'error';
 420  
 421              // Save successful.
 422              } else {
 423                  $message = __( 'The group was saved successfully.', 'buddypress' );
 424                  $type    = 'success';
 425  
 426                  // @todo remove these old options.
 427                  if ( 1 == $group_id ) {
 428                      bp_update_option( 'bp-xprofile-base-group-name', $group->name );
 429                  }
 430  
 431                  /**
 432                   * Fires at the end of the group adding/saving process, if successful.
 433                   *
 434                   * @since 1.0.0
 435                   *
 436                   * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 437                   */
 438                  do_action( 'xprofile_groups_saved_group', $group );
 439              }
 440  
 441              xprofile_admin_screen( $message, $type );
 442  
 443          } else {
 444              $group->render_admin_form( $message );
 445          }
 446      } else {
 447          $group->render_admin_form();
 448      }
 449  }
 450  
 451  /**
 452   * Handles the deletion of profile data groups.
 453   *
 454   * @since 1.0.0
 455   *
 456   * @param int $group_id ID of the group to delete.
 457   */
 458  function xprofile_admin_delete_group( $group_id ) {
 459      global $message, $type;
 460  
 461      check_admin_referer( 'bp_xprofile_delete_group' );
 462  
 463      $mode = ! empty( $_GET['mode'] )
 464            ? sanitize_key( $_GET['mode'] )
 465            : false;
 466  
 467      // Display the group delete confirmation screen.
 468      if ( 'delete_group' === $mode ) {
 469          xprofile_admin_delete_group_screen( $group_id );
 470  
 471      // Handle the deletion of group.
 472      } else {
 473          $group = new BP_XProfile_Group( $group_id );
 474  
 475          if ( ! $group->delete() ) {
 476              $message = _x( 'There was an error deleting the group. Please try again.', 'Error when deleting profile fields group', 'buddypress' );
 477              $type    = 'error';
 478          } else {
 479              $message = _x( 'The group was deleted successfully.', 'Profile fields group was deleted successfully', 'buddypress' );
 480              $type    = 'success';
 481  
 482              /**
 483               * Fires at the end of group deletion process, if successful.
 484               *
 485               * @since 1.0.0
 486               *
 487               * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 488               */
 489              do_action( 'xprofile_groups_deleted_group', $group );
 490          }
 491  
 492          xprofile_admin_screen( $message, $type );
 493      }
 494  }
 495  
 496  /**
 497   * Display the delete confirmation screen of profile data groups.
 498   *
 499   * @since 7.0.0
 500   */
 501  function xprofile_admin_delete_group_screen( $group_id ) {
 502  
 503      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 504          die( '-1' );
 505      }
 506  
 507      $group = new BP_XProfile_Group( $group_id );
 508  
 509      $base_url = remove_query_arg( array( 'mode', 'group_id', '_wpnonce' ), $_SERVER['REQUEST_URI'] ); ?>
 510  
 511      <div class="wrap">
 512          <h1 class="wp-heading-inline"><?php esc_html_e( 'Delete Field Group', 'buddypress' ) ?></h1>
 513          <hr class="wp-header-end">
 514  
 515          <p><?php esc_html_e( 'You are about to delete the following field group:', 'buddypress' ) ?></p>
 516  
 517          <ul class="bp-xprofile-delete-group-list">
 518              <li><?php echo esc_html( $group->name ); ?></li>
 519          </ul>
 520  
 521          <p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>
 522  
 523          <a class="button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'mode' => 'do_delete_group', 'group_id' => $group_id ), $base_url ), 'bp_xprofile_delete_group' ) ); ?>"><?php esc_html_e( 'Delete Permanently', 'buddypress' ) ?></a>
 524          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ) ?></a>
 525      </div>
 526  
 527      <?php
 528  }
 529  
 530  /**
 531   * Handles the adding or editing of profile field data for a user.
 532   *
 533   * @since 1.0.0
 534   *
 535   * @param int      $group_id ID of the group.
 536   * @param int|null $field_id ID of the field being managed.
 537   */
 538  function xprofile_admin_manage_field( $group_id, $field_id = null ) {
 539      global $wpdb, $message, $groups;
 540  
 541      $bp = buddypress();
 542  
 543      if ( is_null( $field_id ) ) {
 544          $field = new BP_XProfile_Field();
 545      } else {
 546          $field = xprofile_get_field( $field_id, null, false );
 547      }
 548  
 549      $field->group_id = $group_id;
 550  
 551      if ( isset( $_POST['saveField'] ) ) {
 552  
 553          // Check nonce.
 554          check_admin_referer( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
 555  
 556          if ( BP_XProfile_Field::admin_validate() ) {
 557              $field->is_required = $_POST['required'];
 558              $field->type        = $_POST['fieldtype'];
 559              $field->name        = $_POST['title'];
 560  
 561              /*
 562               * By default a Textbox field is created. To run field type's feature
 563               * checks we need to set it to what it really is early.
 564               */
 565              if ( is_null( $field_id ) ) {
 566                  $field_type = bp_xprofile_create_field_type( $field->type );
 567  
 568                  // If it's a placeholder, then the field type is not registered.
 569                  if ( ! $field_type instanceof BP_XProfile_Field_Type_Placeholder ) {
 570                      $field->type_obj = $field_type;
 571                  }
 572              }
 573  
 574              if ( ! $field->field_type_supports( 'required' ) ) {
 575                  $field->is_required = "0";
 576              }
 577  
 578              if ( ! empty( $_POST['description'] ) ) {
 579                  $field->description = $_POST['description'];
 580              } else {
 581                  $field->description = '';
 582              }
 583  
 584              if ( ! empty( $_POST["sort_order_{$field->type}"] ) ) {
 585                  $field->order_by = $_POST["sort_order_{$field->type}"];
 586              }
 587  
 588              $field->field_order = $wpdb->get_var( $wpdb->prepare( "SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ) );
 589              if ( ! is_numeric( $field->field_order ) || is_wp_error( $field->field_order ) ) {
 590                  $field->field_order = (int) $wpdb->get_var( $wpdb->prepare( "SELECT max(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id ) );
 591                  $field->field_order++;
 592              }
 593  
 594              // For new profile fields, set the $field_id. For existing profile
 595              // fields, this will overwrite $field_id with the same value.
 596              $field_id = $field->save();
 597  
 598              if ( empty( $field_id ) ) {
 599                  $message = __( 'There was an error saving the field. Please try again.', 'buddypress' );
 600                  $type    = 'error';
 601              } else {
 602                  $message = __( 'The field was saved successfully.', 'buddypress' );
 603                  $type    = 'success';
 604  
 605                  // @todo remove these old options.
 606                  if ( 1 == $field_id ) {
 607                      bp_update_option( 'bp-xprofile-fullname-field-name', $field->name );
 608                  }
 609  
 610                  // Set member types.
 611                  if ( isset( $_POST['has-member-types'] ) ) {
 612                      $member_types = array();
 613                      if ( isset( $_POST['member-types'] ) ) {
 614                          $member_types = stripslashes_deep( $_POST['member-types'] );
 615                      }
 616  
 617                      $field->set_member_types( $member_types );
 618                  }
 619  
 620                  // Validate default visibility.
 621                  if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) {
 622                      $default_visibility = $_POST['default-visibility'];
 623  
 624                      if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) {
 625                          $default_visibility          = 'public';
 626                          $available_visibility_levels = bp_xprofile_get_visibility_levels();
 627  
 628                          if ( isset( $field->type_obj->visibility ) && in_array( $field->type_obj->visibility, array_keys( $available_visibility_levels ), true ) ) {
 629                              $default_visibility = $field->type_obj->visibility;
 630                          }
 631                      }
 632  
 633                      bp_xprofile_update_field_meta( $field_id, 'default_visibility', $default_visibility );
 634                  }
 635  
 636                  // Validate custom visibility.
 637                  if ( ! empty( $_POST['allow-custom-visibility'] ) && in_array( $_POST['allow-custom-visibility'], array( 'allowed', 'disabled' ) ) ) {
 638                      $allow_custom_visibility = $_POST['allow-custom-visibility'];
 639  
 640                      if ( ! $field->field_type_supports( 'allow_custom_visibility' ) ) {
 641                          $allow_custom_visibility = 'disabled';
 642                      }
 643  
 644                      bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $allow_custom_visibility );
 645                  }
 646  
 647                  // Validate signup.
 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                      }
 654                  }
 655  
 656                  $do_autolink = '';
 657                  if ( $field->field_type_supports( 'do_autolink' ) && isset( $_POST['do_autolink'] ) && $_POST['do_autolink'] ) {
 658                      $do_autolink = wp_unslash( $_POST['do_autolink'] );
 659                  }
 660  
 661                  // Save autolink settings.
 662                  if ( 'on' === $do_autolink ) {
 663                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'on' );
 664                  } else {
 665                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'off' );
 666                  }
 667  
 668                  if ( $field->type_obj->do_settings_section() ) {
 669                      $settings = isset( $_POST['field-settings'] ) ? wp_unslash( $_POST['field-settings'] ) : array();
 670                      $field->admin_save_settings( $settings );
 671                  }
 672  
 673                  /**
 674                   * Fires at the end of the process to save a field for a user, if successful.
 675                   *
 676                   * @since 1.0.0
 677                   *
 678                   * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 679                   */
 680                  do_action( 'xprofile_fields_saved_field', $field );
 681  
 682                  $groups = bp_xprofile_get_groups();
 683              }
 684  
 685              xprofile_admin_screen( $message, $type );
 686  
 687          } else {
 688              $field->render_admin_form( $message );
 689          }
 690      } else {
 691          $field->render_admin_form();
 692      }
 693  }
 694  
 695  /**
 696   * Handles the deletion of a profile field (or field option).
 697   *
 698   * @since 1.0.0
 699   *
 700   * @global string $message The feedback message to show.
 701   * @global string $type The type of feedback message to show.
 702   *
 703   * @param int    $field_id    The field to delete.
 704   * @param string $field_type  The type of field being deleted.
 705   * @param bool   $delete_data Should the field data be deleted too.
 706   */
 707  function xprofile_admin_delete_field( $field_id, $field_type = 'field', $delete_data = false ) {
 708      global $message, $type;
 709  
 710      check_admin_referer( 'bp_xprofile_delete_field-' . $field_id, 'bp_xprofile_delete_field' );
 711  
 712      $mode = ! empty( $_GET['mode'] ) ? sanitize_key( $_GET['mode'] ) : false;
 713  
 714      // Switch type to 'option' if type is not 'field'.
 715      // @todo trust this param.
 716      $field_type  = ( 'field' == $field_type ) ? __( 'field', 'buddypress' ) : __( 'option', 'buddypress' );
 717  
 718      // Display the field/option delete confirmation screen.
 719      if ( in_array( $mode, array( 'delete_field', 'delete_option' ) ) ) {
 720          xprofile_admin_delete_field_screen( $field_id, $field_type );
 721  
 722      // Handle the deletion of field
 723      } else {
 724          $field = xprofile_get_field( $field_id, null, false );
 725  
 726          if ( !$field->delete( (bool) $delete_data ) ) {
 727              /* translators: %s: the field type */
 728              $message = sprintf( __( 'There was an error deleting the %s. Please try again.', 'buddypress' ), $field_type );
 729              $type    = 'error';
 730          } else {
 731              /* translators: %s: the field type */
 732              $message = sprintf( __( 'The %s was deleted successfully!', 'buddypress' ), $field_type );
 733              $type    = 'success';
 734  
 735              /**
 736               * Fires at the end of the field deletion process, if successful.
 737               *
 738               * @since 1.0.0
 739               *
 740               * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 741               */
 742              do_action( 'xprofile_fields_deleted_field', $field );
 743          }
 744  
 745          xprofile_admin_screen( $message, $type );
 746      }
 747  }
 748  
 749  /**
 750   * Display the delete confirmation screen of xprofile field/option.
 751   *
 752   * @since 7.0.0
 753   */
 754  function xprofile_admin_delete_field_screen( $field_id, $field_type ) {
 755      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 756          die( '-1' );
 757      }
 758  
 759      $field = xprofile_get_field( $field_id, null, false );
 760  
 761      $base_url = remove_query_arg( array( 'mode', 'field_id', 'bp_xprofile_delete_field' ), $_SERVER['REQUEST_URI'] ); ?>
 762  
 763      <div class="wrap">
 764          <h1 class="wp-heading-inline">
 765              <?php
 766              printf(
 767                  /* translators: %s is the field type name. */
 768                  esc_html__( 'Delete %s', 'buddypress' ),
 769                  $field_type
 770              );
 771              ?>
 772          </h1>
 773  
 774          <hr class="wp-header-end">
 775  
 776          <p>
 777              <?php
 778              printf(
 779                  /* translators: %s is the field type name. */
 780                  esc_html__( 'You are about to delete the following %s:', 'buddypress' ),
 781                  $field_type
 782              );
 783              ?>
 784          </p>
 785  
 786          <ul class="bp-xprofile-delete-group-list">
 787              <li><?php echo esc_html( $field->name ); ?></li>
 788          </ul>
 789  
 790          <p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ); ?></strong></p>
 791  
 792          <a class="button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'mode' => 'do_delete_field', 'field_id' => $field_id ), $base_url ), 'bp_xprofile_delete_field-' . $field_id, 'bp_xprofile_delete_field' ) ); ?>"><?php esc_html_e( 'Delete Permanently', 'buddypress' ); ?></a>
 793          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a>
 794      </div>
 795  
 796      <?php
 797  }
 798  
 799  
 800  
 801  /**
 802   * Handles the ajax reordering of fields within a group.
 803   *
 804   * @since 1.0.0
 805   * @since 8.0.0 Returns a JSON object.
 806   */
 807  function xprofile_ajax_reorder_fields() {
 808      // Check the nonce.
 809      check_admin_referer( 'bp_reorder_fields', '_wpnonce_reorder_fields' );
 810  
 811      if ( empty( $_POST['field_order'] ) ) {
 812          return wp_send_json_error();
 813      }
 814  
 815      $field_group_id = $_POST['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();
 921      }
 922  }
 923  add_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   */
 930  function 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  }
 958  add_action( 'wp_ajax_xprofile_remove_signup_field', 'bp_xprofile_ajax_remove_signup_field' );
 959  
 960  /**
 961   * Handles the reordering of field groups.
 962   *
 963   * @since 1.5.0
 964   */
 965  function xprofile_ajax_reorder_field_groups() {
 966  
 967      // Check the nonce.
 968      check_admin_referer( 'bp_reorder_groups', '_wpnonce_reorder_groups' );
 969  
 970      if ( empty( $_POST['group_order'] ) ) {
 971          return false;
 972      }
 973  
 974      parse_str( $_POST['group_order'], $order );
 975  
 976      foreach ( (array) $order['group'] as $position => $field_group_id ) {
 977          xprofile_update_field_group_position( (int) $field_group_id, (int) $position );
 978      }
 979  }
 980  add_action( 'wp_ajax_xprofile_reorder_groups', 'xprofile_ajax_reorder_field_groups' );
 981  
 982  /**
 983   * Handles the WYSIWYG display of each profile field on the edit screen.
 984   *
 985   * @since 1.5.0
 986   * @since 8.0.0 Adds the `$is_signup` parameter.
 987   *
 988   * @param BP_XProfile_Field   $admin_field Admin field.
 989   * @param object $admin_group Admin group object.
 990   * @param string $class       Classes to append to output.
 991   * @param bool   $is_signup   Whether the admin field output is made inside the signup group.
 992   */
 993  function xprofile_admin_field( $admin_field, $admin_group, $class = '', $is_signup = false ) {
 994      global $field;
 995  
 996      $field       = $admin_field;
 997      $fieldset_id = sprintf( 'draggable_field_%d', $field->id );
 998  
 999      // Users admin URL.
1000      $url = bp_get_admin_url( 'users.php' );
1001  
1002      // Edit.
1003      $field_edit_url = add_query_arg( array(
1004          'page'     => 'bp-profile-setup',
1005          'mode'     => 'edit_field',
1006          'group_id' => (int) $field->group_id,
1007          'field_id' => (int) $field->id
1008      ), $url );
1009  
1010      // Delete.
1011      if ( $field->can_delete ) {
1012          $field_delete_url = add_query_arg( array(
1013              'page'     => 'bp-profile-setup',
1014              'mode'     => 'delete_field',
1015              'field_id' => (int) $field->id
1016          ), $url . '#tabs-' . (int) $field->group_id );
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; ?>">
1026          <legend>
1027              <span>
1028                  <?php bp_the_profile_field_name(); ?>
1029  
1030                  <?php if ( empty( $field->can_delete ) ) : ?><?php esc_html_e( '(Primary)', 'buddypress' ); endif; ?>
1031                  <?php bp_the_profile_field_required_label(); ?>
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; ?>
1035                  <?php if ( bp_get_member_types() ) : echo $field->get_member_type_label(); endif; ?>
1036  
1037                  <?php
1038  
1039                  /**
1040                   * Fires at end of legend above the name field in base xprofile group.
1041                   *
1042                   * @since 2.2.0
1043                   *
1044                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
1045                   *                                 object being rendered.
1046                   */
1047                  do_action( 'xprofile_admin_field_name_legend', $field ); ?>
1048              </span>
1049          </legend>
1050          <div class="field-wrapper">
1051  
1052              <?php
1053              if ( in_array( $field->type, array_keys( bp_xprofile_get_field_types() ) ) ) {
1054                  $field_type = bp_xprofile_create_field_type( $field->type );
1055                  $field_type->admin_field_html();
1056              } else {
1057  
1058                  /**
1059                   * Fires after the input if the current field is not in default field types.
1060                   *
1061                   * @since 1.5.0
1062                   *
1063                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
1064                   *                                 object being rendered.
1065                   * @param int               $value Integer 1.
1066                   */
1067                  do_action( 'xprofile_admin_field', $field, 1 );
1068              }
1069              ?>
1070  
1071              <?php if ( $field->description ) : ?>
1072  
1073                  <p class="description"><?php echo esc_attr( $field->description ); ?></p>
1074  
1075              <?php endif; ?>
1076  
1077              <div class="actions">
1078                  <a class="button edit" href="<?php echo esc_url( $field_edit_url ); ?>"><?php _ex( 'Edit', 'Edit field link', 'buddypress' ); ?></a>
1079  
1080                  <?php if ( $field->can_delete && ! $is_signup ) : ?>
1081  
1082                      <div class="delete-button">
1083                          <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>
1092                      </div>
1093  
1094                  <?php endif; ?>
1095  
1096                  <?php
1097  
1098                  /**
1099                   * Fires at end of field management links in xprofile management admin.
1100                   *
1101                   * @since 2.2.0
1102                   *
1103                   * @param BP_XProfile_Group $group BP_XProfile_Group object
1104                   *                                 for the current group.
1105                   */
1106                  do_action( 'xprofile_admin_field_action', $field ); ?>
1107  
1108              </div>
1109          </div>
1110      </fieldset>
1111  <?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   */
1125  function 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      }
1143  }
1144  
1145  /**
1146   * Print <option> elements containing the xprofile field types.
1147   *
1148   * @since 2.0.0
1149   *
1150   * @param string $select_field_type The name of the field type that should be selected.
1151   *                                  Will defaults to "textbox" if NULL is passed.
1152   */
1153  function bp_xprofile_admin_form_field_types( $select_field_type ) {
1154      $categories = array();
1155  
1156      if ( is_null( $select_field_type ) ) {
1157          $select_field_type = 'textbox';
1158      }
1159  
1160      // Sort each field type into its category.
1161      foreach ( bp_xprofile_get_field_types() as $field_name => $field_class ) {
1162          $field_type_obj = new $field_class;
1163          $the_category   = $field_type_obj->category;
1164  
1165          // Fallback to a catch-all if category not set.
1166          if ( ! $the_category ) {
1167              $the_category = _x( 'Other', 'xprofile field type category', 'buddypress' );
1168          }
1169  
1170          if ( isset( $categories[$the_category] ) ) {
1171              $categories[$the_category][] = array( $field_name, $field_type_obj );
1172          } else {
1173              $categories[$the_category] = array( array( $field_name, $field_type_obj ) );
1174          }
1175      }
1176  
1177      // Sort the categories alphabetically. ksort()'s SORT_NATURAL is only in PHP >= 5.4 :((.
1178      uksort( $categories, 'strnatcmp' );
1179  
1180      // Loop through each category and output form <options>.
1181      foreach ( $categories as $category => $fields ) {
1182          printf( '<optgroup label="%1$s">', esc_attr( $category ) );  // Already i18n'd in each profile type class.
1183  
1184          // Sort these fields types alphabetically.
1185          uasort( $fields, function( $a, $b ) { return strnatcmp( $a[1]->name, $b[1]->name ); } );
1186  
1187          foreach ( $fields as $field_type_obj ) {
1188              $field_name     = $field_type_obj[0];
1189              $field_type_obj = $field_type_obj[1];
1190  
1191              printf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $field_name ), selected( $select_field_type, $field_name, false ), esc_html( $field_type_obj->name ) );
1192          }
1193  
1194          printf( '</optgroup>' );
1195      }
1196  }
1197  
1198  // Load the xprofile user admin.
1199  add_action( 'bp_init', array( 'BP_XProfile_User_Admin', 'register_xprofile_user_admin' ), 11 );


Generated: Sat May 15 01:01:41 2021 Cross-referenced by PHPXref 0.7.1