[ 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          'add_field',
  69          'edit_field',
  70          'delete_field',
  71          'delete_option'
  72      );
  73  
  74      // Is an allowed mode
  75      if ( in_array( $mode, $allowed_modes, true ) ) {
  76  
  77          // All group actions
  78          if ( false !== $group_id ) {
  79  
  80              // Add field to group
  81              if ( 'add_field' == $mode ) {
  82                  xprofile_admin_manage_field( $group_id );
  83  
  84              // Edit field of group
  85              } elseif ( ! empty( $field_id ) && 'edit_field' === $mode ) {
  86                  xprofile_admin_manage_field( $group_id, $field_id );
  87  
  88              // Delete group
  89              } elseif ( 'delete_group' === $mode ) {
  90                  xprofile_admin_delete_group( $group_id );
  91  
  92              // Edit group
  93              } elseif ( 'edit_group' === $mode ) {
  94                  xprofile_admin_manage_group( $group_id );
  95              }
  96  
  97          // Delete field
  98          } elseif ( ( false !== $field_id ) && ( 'delete_field' === $mode ) ) {
  99              xprofile_admin_delete_field( $field_id, 'field');
 100  
 101          // Delete option
 102          } elseif ( ! empty( $option_id ) && 'delete_option' === $mode ) {
 103              xprofile_admin_delete_field( $option_id, 'option' );
 104  
 105          // Add group
 106          } elseif ( 'add_group' == $mode ) {
 107              xprofile_admin_manage_group();
 108          }
 109  
 110      } else {
 111          xprofile_admin_screen( $message, $type );
 112      }
 113  }
 114  
 115  /**
 116   * Output the main XProfile management screen.
 117   *
 118   * @since 2.3.0
 119   *
 120   * @param string $message Feedback message.
 121   * @param string $type    Feedback type.
 122   *
 123   * @todo Improve error message output
 124   */
 125  function xprofile_admin_screen( $message = '', $type = 'error' ) {
 126  
 127      // Users admin URL
 128      $url = bp_get_admin_url( 'users.php' );
 129  
 130      // Add Group
 131      $add_group_url = add_query_arg( array(
 132          'page' => 'bp-profile-setup',
 133          'mode' => 'add_group'
 134      ), $url );
 135  
 136      // Validate type.
 137      $type = preg_replace( '|[^a-z]|i', '', $type );
 138  
 139      // Get all of the profile groups & fields.
 140      $groups = bp_xprofile_get_groups( array(
 141          'fetch_fields' => true
 142      ) ); ?>
 143  
 144      <div class="wrap">
 145          <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 146  
 147              <h1 class="wp-heading-inline"><?php _ex( 'Profile Fields', 'Settings page header', 'buddypress'); ?></h1>
 148  
 149                  <a id="add_group" class="page-title-action" href="<?php echo esc_url( $add_group_url ); ?>"><?php _e( 'Add New Field Group', 'buddypress' ); ?></a>
 150  
 151              <hr class="wp-header-end">
 152  
 153          <?php else : ?>
 154  
 155              <h1>
 156                  <?php _ex( 'Profile Fields', 'Settings page header', 'buddypress'); ?>
 157                  <a id="add_group" class="add-new-h2" href="<?php echo esc_url( $add_group_url ); ?>"><?php _e( 'Add New Field Group', 'buddypress' ); ?></a>
 158              </h1>
 159  
 160          <?php endif; ?>
 161  
 162          <form action="" id="profile-field-form" method="post">
 163  
 164              <?php
 165  
 166              wp_nonce_field( 'bp_reorder_fields', '_wpnonce_reorder_fields'        );
 167              wp_nonce_field( 'bp_reorder_groups', '_wpnonce_reorder_groups', false );
 168  
 169              if ( ! empty( $message ) ) :
 170                  $type = ( $type == 'error' ) ? 'error' : 'updated'; ?>
 171  
 172                  <div id="message" class="<?php echo $type; ?> fade">
 173                      <p><?php echo esc_html( $message ); ?></p>
 174                  </div>
 175  
 176              <?php endif; ?>
 177  
 178              <div id="tabs" aria-live="polite" aria-atomic="true" aria-relevant="all">
 179                  <ul id="field-group-tabs">
 180  
 181                      <?php if ( !empty( $groups ) ) : foreach ( $groups as $group ) : ?>
 182  
 183                          <li id="group_<?php echo esc_attr( $group->id ); ?>">
 184                              <a href="#tabs-<?php echo esc_attr( $group->id ); ?>" class="ui-tab">
 185                                  <?php
 186                                  /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 187                                  echo esc_html( apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 188                                  ?>
 189  
 190                                  <?php if ( !$group->can_delete ) : ?>
 191                                      <?php _e( '(Primary)', 'buddypress'); ?>
 192                                  <?php endif; ?>
 193  
 194                              </a>
 195                          </li>
 196  
 197                      <?php endforeach; 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 );
 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                          <?php if ( empty( $group->can_delete ) ) : ?>
 306  
 307                              <p><?php esc_html_e( '* Fields in this group appear on the signup page.', 'buddypress' ); ?></p>
 308  
 309                          <?php endif; ?>
 310  
 311                      </div>
 312  
 313                  <?php endforeach; else : ?>
 314  
 315                      <div id="message" class="error"><p><?php _ex( 'You have no groups.', 'You have no profile fields groups.', 'buddypress' ); ?></p></div>
 316                      <p><a href="<?php echo esc_url( $add_group_url ); ?>"><?php _ex( 'Add New Group', 'Add New Profile Fields Group', 'buddypress' ); ?></a></p>
 317  
 318                  <?php endif; ?>
 319  
 320              </div>
 321          </form>
 322      </div>
 323  
 324  <?php
 325  }
 326  
 327  /**
 328   * Handles the adding or editing of groups.
 329   *
 330   * @since 1.0.0
 331   *
 332   * @param int|null $group_id Group ID to manage.
 333   */
 334  function xprofile_admin_manage_group( $group_id = null ) {
 335      global $message, $type;
 336  
 337      // Get the field group.
 338      $group = new BP_XProfile_Group( $group_id );
 339  
 340      // Updating.
 341      if ( isset( $_POST['save_group'] ) ) {
 342  
 343          // Check nonce
 344          check_admin_referer( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' );
 345  
 346          // Validate $_POSTed data.
 347          if ( BP_XProfile_Group::admin_validate() ) {
 348  
 349              // Set the group name.
 350              $group->name = $_POST['group_name'];
 351  
 352              // Set the group description.
 353              if ( ! empty( $_POST['group_description'] ) ) {
 354                  $group->description = $_POST['group_description'];
 355              } else {
 356                  $group->description = '';
 357              }
 358  
 359              // Attempt to save the field group.
 360              if ( false === $group->save() ) {
 361                  $message = __( 'There was an error saving the group. Please try again.', 'buddypress' );
 362                  $type    = 'error';
 363  
 364              // Save successful.
 365              } else {
 366                  $message = __( 'The group was saved successfully.', 'buddypress' );
 367                  $type    = 'success';
 368  
 369                  // @todo remove these old options
 370                  if ( 1 == $group_id ) {
 371                      bp_update_option( 'bp-xprofile-base-group-name', $group->name );
 372                  }
 373  
 374                  /**
 375                   * Fires at the end of the group adding/saving process, if successful.
 376                   *
 377                   * @since 1.0.0
 378                   *
 379                   * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 380                   */
 381                  do_action( 'xprofile_groups_saved_group', $group );
 382              }
 383  
 384              xprofile_admin_screen( $message, $type );
 385  
 386          } else {
 387              $group->render_admin_form( $message );
 388          }
 389      } else {
 390          $group->render_admin_form();
 391      }
 392  }
 393  
 394  /**
 395   * Handles the deletion of profile data groups.
 396   *
 397   * @since 1.0.0
 398   *
 399   * @param int $group_id ID of the group to delete.
 400   */
 401  function xprofile_admin_delete_group( $group_id ) {
 402      global $message, $type;
 403  
 404      check_admin_referer( 'bp_xprofile_delete_group' );
 405  
 406      $group = new BP_XProfile_Group( $group_id );
 407  
 408      if ( !$group->delete() ) {
 409          $message = _x( 'There was an error deleting the group. Please try again.', 'Error when deleting profile fields group', 'buddypress' );
 410          $type    = 'error';
 411      } else {
 412          $message = _x( 'The group was deleted successfully.', 'Profile fields group was deleted successfully', 'buddypress' );
 413          $type    = 'success';
 414  
 415          /**
 416           * Fires at the end of group deletion process, if successful.
 417           *
 418           * @since 1.0.0
 419           *
 420           * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 421           */
 422          do_action( 'xprofile_groups_deleted_group', $group );
 423      }
 424  
 425      xprofile_admin_screen( $message, $type );
 426  }
 427  
 428  /**
 429   * Handles the adding or editing of profile field data for a user.
 430   *
 431   * @since 1.0.0
 432   *
 433   * @param int      $group_id ID of the group.
 434   * @param int|null $field_id ID of the field being managed.
 435   */
 436  function xprofile_admin_manage_field( $group_id, $field_id = null ) {
 437      global $wpdb, $message, $groups;
 438  
 439      $bp = buddypress();
 440  
 441      if ( is_null( $field_id ) ) {
 442          $field = new BP_XProfile_Field();
 443      } else {
 444          $field = xprofile_get_field( $field_id );
 445      }
 446  
 447      $field->group_id = $group_id;
 448  
 449      if ( isset( $_POST['saveField'] ) ) {
 450  
 451          // Check nonce
 452          check_admin_referer( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
 453  
 454          if ( BP_XProfile_Field::admin_validate() ) {
 455              $field->is_required = $_POST['required'];
 456              $field->type        = $_POST['fieldtype'];
 457              $field->name        = $_POST['title'];
 458  
 459              if ( ! empty( $_POST['description'] ) ) {
 460                  $field->description = $_POST['description'];
 461              } else {
 462                  $field->description = '';
 463              }
 464  
 465              if ( ! empty( $_POST["sort_order_{$field->type}"] ) ) {
 466                  $field->order_by = $_POST["sort_order_{$field->type}"];
 467              }
 468  
 469              $field->field_order = $wpdb->get_var( $wpdb->prepare( "SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ) );
 470              if ( ! is_numeric( $field->field_order ) || is_wp_error( $field->field_order ) ) {
 471                  $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 ) );
 472                  $field->field_order++;
 473              }
 474  
 475              // For new profile fields, set the $field_id. For existing profile
 476              // fields, this will overwrite $field_id with the same value.
 477              $field_id = $field->save();
 478  
 479              if ( empty( $field_id ) ) {
 480                  $message = __( 'There was an error saving the field. Please try again.', 'buddypress' );
 481                  $type    = 'error';
 482              } else {
 483                  $message = __( 'The field was saved successfully.', 'buddypress' );
 484                  $type    = 'success';
 485  
 486                  // @todo remove these old options
 487                  if ( 1 == $field_id ) {
 488                      bp_update_option( 'bp-xprofile-fullname-field-name', $field->name );
 489                  }
 490  
 491                  // Set member types.
 492                  if ( isset( $_POST['has-member-types'] ) ) {
 493                      $member_types = array();
 494                      if ( isset( $_POST['member-types'] ) ) {
 495                          $member_types = stripslashes_deep( $_POST['member-types'] );
 496                      }
 497  
 498                      $field->set_member_types( $member_types );
 499                  }
 500  
 501                  // Validate default visibility.
 502                  if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) {
 503                      bp_xprofile_update_field_meta( $field_id, 'default_visibility', $_POST['default-visibility'] );
 504                  }
 505  
 506                  // Validate custom visibility.
 507                  if ( ! empty( $_POST['allow-custom-visibility'] ) && in_array( $_POST['allow-custom-visibility'], array( 'allowed', 'disabled' ) ) ) {
 508                      bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility'] );
 509                  }
 510  
 511                  // Validate signup.
 512                  if ( ! empty( $_POST['signup-position'] ) ) {
 513                      bp_xprofile_update_field_meta( $field_id, 'signup_position', (int) $_POST['signup-position'] );
 514                  } else {
 515                      bp_xprofile_delete_meta( $field_id, 'field', 'signup_position' );
 516                  }
 517  
 518                  // Save autolink settings.
 519                  if ( isset( $_POST['do_autolink'] ) && 'on' === wp_unslash( $_POST['do_autolink'] ) ) {
 520                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'on' );
 521                  } else {
 522                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'off' );
 523                  }
 524  
 525                  if ( $field->type_obj->do_settings_section() ) {
 526                      $settings = isset( $_POST['field-settings'] ) ? wp_unslash( $_POST['field-settings'] ) : array();
 527                      $field->admin_save_settings( $settings );
 528                  }
 529  
 530                  /**
 531                   * Fires at the end of the process to save a field for a user, if successful.
 532                   *
 533                   * @since 1.0.0
 534                   *
 535                   * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 536                   */
 537                  do_action( 'xprofile_fields_saved_field', $field );
 538  
 539                  $groups = bp_xprofile_get_groups();
 540              }
 541  
 542              xprofile_admin_screen( $message, $type );
 543  
 544          } else {
 545              $field->render_admin_form( $message );
 546          }
 547      } else {
 548          $field->render_admin_form();
 549      }
 550  }
 551  
 552  /**
 553   * Handles the deletion of a profile field (or field option).
 554   *
 555   * @since 1.0.0
 556   *
 557   * @global string $message The feedback message to show.
 558   * @global string $type The type of feedback message to show.
 559   *
 560   * @param int    $field_id    The field to delete.
 561   * @param string $field_type  The type of field being deleted.
 562   * @param bool   $delete_data Should the field data be deleted too.
 563   */
 564  function xprofile_admin_delete_field( $field_id, $field_type = 'field', $delete_data = false ) {
 565      global $message, $type;
 566  
 567      // Switch type to 'option' if type is not 'field'.
 568      // @todo trust this param.
 569      $field_type  = ( 'field' == $field_type ) ? __( 'field', 'buddypress' ) : __( 'option', 'buddypress' );
 570      $field       = xprofile_get_field( $field_id );
 571  
 572      if ( !$field->delete( (bool) $delete_data ) ) {
 573          $message = sprintf( __( 'There was an error deleting the %s. Please try again.', 'buddypress' ), $field_type );
 574          $type    = 'error';
 575      } else {
 576          $message = sprintf( __( 'The %s was deleted successfully!', 'buddypress' ), $field_type );
 577          $type    = 'success';
 578  
 579          /**
 580           * Fires at the end of the field deletion process, if successful.
 581           *
 582           * @since 1.0.0
 583           *
 584           * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 585           */
 586          do_action( 'xprofile_fields_deleted_field', $field );
 587      }
 588  
 589      xprofile_admin_screen( $message, $type );
 590  }
 591  
 592  /**
 593   * Handles the ajax reordering of fields within a group.
 594   *
 595   * @since 1.0.0
 596   */
 597  function xprofile_ajax_reorder_fields() {
 598  
 599      // Check the nonce.
 600      check_admin_referer( 'bp_reorder_fields', '_wpnonce_reorder_fields' );
 601  
 602      if ( empty( $_POST['field_order'] ) ) {
 603          return false;
 604      }
 605  
 606      parse_str( $_POST['field_order'], $order );
 607  
 608      $field_group_id = $_POST['field_group_id'];
 609  
 610      foreach ( (array) $order['draggable_field'] as $position => $field_id ) {
 611          xprofile_update_field_position( (int) $field_id, (int) $position, (int) $field_group_id );
 612      }
 613  }
 614  add_action( 'wp_ajax_xprofile_reorder_fields', 'xprofile_ajax_reorder_fields' );
 615  
 616  /**
 617   * Handles the reordering of field groups.
 618   *
 619   * @since 1.5.0
 620   */
 621  function xprofile_ajax_reorder_field_groups() {
 622  
 623      // Check the nonce.
 624      check_admin_referer( 'bp_reorder_groups', '_wpnonce_reorder_groups' );
 625  
 626      if ( empty( $_POST['group_order'] ) ) {
 627          return false;
 628      }
 629  
 630      parse_str( $_POST['group_order'], $order );
 631  
 632      foreach ( (array) $order['group'] as $position => $field_group_id ) {
 633          xprofile_update_field_group_position( (int) $field_group_id, (int) $position );
 634      }
 635  }
 636  add_action( 'wp_ajax_xprofile_reorder_groups', 'xprofile_ajax_reorder_field_groups' );
 637  
 638  /**
 639   * Handles the WYSIWYG display of each profile field on the edit screen.
 640   *
 641   * @since 1.5.0
 642   *
 643   * @param BP_XProfile_Field   $admin_field Admin field.
 644   * @param object $admin_group Admin group object.
 645   * @param string $class       Classes to append to output.
 646   */
 647  function xprofile_admin_field( $admin_field, $admin_group, $class = '' ) {
 648      global $field;
 649  
 650      $field = $admin_field;
 651  
 652      // Users admin URL
 653      $url = bp_get_admin_url( 'users.php' );
 654  
 655      // Edit
 656      $field_edit_url = add_query_arg( array(
 657          'page'     => 'bp-profile-setup',
 658          'mode'     => 'edit_field',
 659          'group_id' => (int) $field->group_id,
 660          'field_id' => (int) $field->id
 661      ), $url );
 662  
 663      // Delete
 664      if ( $field->can_delete ) {
 665          $field_delete_url = add_query_arg( array(
 666              'page'     => 'bp-profile-setup',
 667              'mode'     => 'delete_field',
 668              'field_id' => (int) $field->id
 669          ), $url . '#tabs-' . (int) $field->group_id );
 670      } ?>
 671  
 672      <fieldset id="draggable_field_<?php echo esc_attr( $field->id ); ?>" class="sortable<?php echo ' ' . $field->type; if ( !empty( $class ) ) echo ' ' . $class; ?>">
 673          <legend>
 674              <span>
 675                  <?php bp_the_profile_field_name(); ?>
 676  
 677                  <?php if ( empty( $field->can_delete )                                    ) : ?><?php esc_html_e( '(Primary)', 'buddypress' ); endif; ?>
 678                  <?php bp_the_profile_field_required_label(); ?>
 679                  <?php if ( bp_xprofile_get_meta( $field->id, 'field', 'signup_position' ) ) : ?><?php esc_html_e( '(Sign-up)', 'buddypress' ); endif; ?>
 680                  <?php if ( bp_get_member_types() ) : echo $field->get_member_type_label(); endif; ?>
 681  
 682                  <?php
 683  
 684                  /**
 685                   * Fires at end of legend above the name field in base xprofile group.
 686                   *
 687                   * @since 2.2.0
 688                   *
 689                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
 690                   *                                 object being rendered.
 691                   */
 692                  do_action( 'xprofile_admin_field_name_legend', $field ); ?>
 693              </span>
 694          </legend>
 695          <div class="field-wrapper">
 696  
 697              <?php
 698              if ( in_array( $field->type, array_keys( bp_xprofile_get_field_types() ) ) ) {
 699                  $field_type = bp_xprofile_create_field_type( $field->type );
 700                  $field_type->admin_field_html();
 701              } else {
 702  
 703                  /**
 704                   * Fires after the input if the current field is not in default field types.
 705                   *
 706                   * @since 1.5.0
 707                   *
 708                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
 709                   *                                 object being rendered.
 710                   * @param int               $value Integer 1.
 711                   */
 712                  do_action( 'xprofile_admin_field', $field, 1 );
 713              }
 714              ?>
 715  
 716              <?php if ( $field->description ) : ?>
 717  
 718                  <p class="description"><?php echo esc_attr( $field->description ); ?></p>
 719  
 720              <?php endif; ?>
 721  
 722              <div class="actions">
 723                  <a class="button edit" href="<?php echo esc_url( $field_edit_url ); ?>"><?php _ex( 'Edit', 'Edit field link', 'buddypress' ); ?></a>
 724  
 725                  <?php if ( $field->can_delete ) : ?>
 726  
 727                      <div class="delete-button">
 728                          <a class="confirm submit-delete deletion" href="<?php echo esc_url( $field_delete_url ); ?>"><?php _ex( 'Delete', 'Delete field link', 'buddypress' ); ?></a>
 729                      </div>
 730  
 731                  <?php endif; ?>
 732  
 733                  <?php
 734  
 735                  /**
 736                   * Fires at end of field management links in xprofile management admin.
 737                   *
 738                   * @since 2.2.0
 739                   *
 740                   * @param BP_XProfile_Group $group BP_XProfile_Group object
 741                   *                                 for the current group.
 742                   */
 743                  do_action( 'xprofile_admin_field_action', $field ); ?>
 744  
 745              </div>
 746          </div>
 747      </fieldset>
 748  
 749  <?php
 750  }
 751  
 752  /**
 753   * Print <option> elements containing the xprofile field types.
 754   *
 755   * @since 2.0.0
 756   *
 757   * @param string $select_field_type The name of the field type that should be selected.
 758   *                                  Will defaults to "textbox" if NULL is passed.
 759   */
 760  function bp_xprofile_admin_form_field_types( $select_field_type ) {
 761      $categories = array();
 762  
 763      if ( is_null( $select_field_type ) ) {
 764          $select_field_type = 'textbox';
 765      }
 766  
 767      // Sort each field type into its category.
 768      foreach ( bp_xprofile_get_field_types() as $field_name => $field_class ) {
 769          $field_type_obj = new $field_class;
 770          $the_category   = $field_type_obj->category;
 771  
 772          // Fallback to a catch-all if category not set.
 773          if ( ! $the_category ) {
 774              $the_category = _x( 'Other', 'xprofile field type category', 'buddypress' );
 775          }
 776  
 777          if ( isset( $categories[$the_category] ) ) {
 778              $categories[$the_category][] = array( $field_name, $field_type_obj );
 779          } else {
 780              $categories[$the_category] = array( array( $field_name, $field_type_obj ) );
 781          }
 782      }
 783  
 784      // Sort the categories alphabetically. ksort()'s SORT_NATURAL is only in PHP >= 5.4 :((.
 785      uksort( $categories, 'strnatcmp' );
 786  
 787      // Loop through each category and output form <options>.
 788      foreach ( $categories as $category => $fields ) {
 789          printf( '<optgroup label="%1$s">', esc_attr( $category ) );  // Already i18n'd in each profile type class.
 790  
 791          // Sort these fields types alphabetically.
 792          uasort( $fields, function( $a, $b ) { return strnatcmp( $a[1]->name, $b[1]->name ); } );
 793  
 794          foreach ( $fields as $field_type_obj ) {
 795              $field_name     = $field_type_obj[0];
 796              $field_type_obj = $field_type_obj[1];
 797  
 798              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 ) );
 799          }
 800  
 801          printf( '</optgroup>' );
 802      }
 803  }
 804  
 805  // Load the xprofile user admin.
 806  add_action( 'bp_init', array( 'BP_XProfile_User_Admin', 'register_xprofile_user_admin' ), 11 );


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1