[ 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                  </ul>
 192  
 193                  <?php if ( !empty( $groups ) ) : foreach ( $groups as $group ) :
 194  
 195                      // Add Field to Group URL.
 196                      $add_field_url = add_query_arg( array(
 197                          'page'     => 'bp-profile-setup',
 198                          'mode'     => 'add_field',
 199                          'group_id' => (int) $group->id
 200                      ), $url );
 201  
 202                      // Edit Group URL.
 203                      $edit_group_url = add_query_arg( array(
 204                          'page'     => 'bp-profile-setup',
 205                          'mode'     => 'edit_group',
 206                          'group_id' => (int) $group->id
 207                      ), $url );
 208  
 209                      // Delete Group URL.
 210                      $delete_group_url = wp_nonce_url( add_query_arg( array(
 211                          'page'     => 'bp-profile-setup',
 212                          'mode'     => 'delete_group',
 213                          'group_id' => (int) $group->id
 214                      ), $url ), 'bp_xprofile_delete_group' ); ?>
 215  
 216                      <noscript>
 217                          <h3><?php
 218                          /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 219                          echo esc_html( apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 220                          ?></h3>
 221                      </noscript>
 222  
 223                      <div id="tabs-<?php echo esc_attr( $group->id ); ?>" class="tab-wrapper">
 224                          <div class="tab-toolbar">
 225                              <div class="tab-toolbar-left">
 226                                  <a class="button-primary" href="<?php echo esc_url( $add_field_url ); ?>"><?php _e( 'Add New Field', 'buddypress' ); ?></a>
 227                                  <a class="button edit" href="<?php echo esc_url( $edit_group_url ); ?>"><?php _ex( 'Edit Group', 'Edit Profile Fields Group', 'buddypress' ); ?></a>
 228  
 229                                  <?php if ( $group->can_delete ) : ?>
 230  
 231                                      <div class="delete-button">
 232                                          <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>
 233                                      </div>
 234  
 235                                  <?php endif; ?>
 236  
 237                                  <?php
 238  
 239                                  /**
 240                                   * Fires at end of action buttons in xprofile management admin.
 241                                   *
 242                                   * @since 2.2.0
 243                                   *
 244                                   * @param BP_XProfile_Group $group BP_XProfile_Group object
 245                                   *                                 for the current group.
 246                                   */
 247                                  do_action( 'xprofile_admin_group_action', $group ); ?>
 248  
 249                              </div>
 250                          </div>
 251  
 252                          <?php if ( ! empty( $group->description ) ) : ?>
 253  
 254                              <p><?php
 255                              /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 256                              echo esc_html( apply_filters( 'bp_get_the_profile_group_description', $group->description ) );
 257                              ?></p>
 258  
 259                          <?php endif; ?>
 260  
 261                          <fieldset id="<?php echo esc_attr( $group->id ); ?>" class="connectedSortable field-group" aria-live="polite" aria-atomic="true" aria-relevant="all">
 262                              <legend class="screen-reader-text"><?php
 263                              /** This filter is documented in bp-xprofile/bp-xprofile-template.php */
 264                              /* translators: accessibility text */
 265                              printf( esc_html__( 'Fields for "%s" Group', 'buddypress' ), apply_filters( 'bp_get_the_profile_group_name', $group->name ) );
 266                              ?></legend>
 267  
 268                              <?php
 269  
 270                              if ( !empty( $group->fields ) ) :
 271                                  foreach ( $group->fields as $field ) {
 272  
 273                                      // Load the field.
 274                                      $field = xprofile_get_field( $field->id, null, false );
 275  
 276                                      $class = '';
 277                                      if ( empty( $field->can_delete ) ) {
 278                                          $class = ' core nodrag';
 279                                      }
 280  
 281                                      /**
 282                                       * This function handles the WYSIWYG profile field
 283                                       * display for the xprofile admin setup screen.
 284                                       */
 285                                      xprofile_admin_field( $field, $group, $class );
 286  
 287                                  } // end for
 288  
 289                              else : // !$group->fields ?>
 290  
 291                                  <p class="nodrag nofields"><?php _e( 'There are no fields in this group.', 'buddypress' ); ?></p>
 292  
 293                              <?php endif; // End $group->fields. ?>
 294  
 295                          </fieldset>
 296  
 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  
 303                      </div>
 304  
 305                  <?php endforeach; else : ?>
 306  
 307                      <div id="message" class="error notice is-dismissible"><p><?php _ex( 'You have no groups.', 'You have no profile fields groups.', 'buddypress' ); ?></p></div>
 308                      <p><a href="<?php echo esc_url( $add_group_url ); ?>"><?php _ex( 'Add New Group', 'Add New Profile Fields Group', 'buddypress' ); ?></a></p>
 309  
 310                  <?php endif; ?>
 311  
 312              </div>
 313          </form>
 314      </div>
 315  
 316  <?php
 317  }
 318  
 319  /**
 320   * Handles the adding or editing of groups.
 321   *
 322   * @since 1.0.0
 323   *
 324   * @param int|null $group_id Group ID to manage.
 325   */
 326  function xprofile_admin_manage_group( $group_id = null ) {
 327      global $message, $type;
 328  
 329      // Get the field group.
 330      $group = new BP_XProfile_Group( $group_id );
 331  
 332      // Updating.
 333      if ( isset( $_POST['save_group'] ) ) {
 334  
 335          // Check nonce.
 336          check_admin_referer( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' );
 337  
 338          // Validate $_POSTed data.
 339          if ( BP_XProfile_Group::admin_validate() ) {
 340  
 341              // Set the group name.
 342              $group->name = $_POST['group_name'];
 343  
 344              // Set the group description.
 345              if ( ! empty( $_POST['group_description'] ) ) {
 346                  $group->description = $_POST['group_description'];
 347              } else {
 348                  $group->description = '';
 349              }
 350  
 351              // Attempt to save the field group.
 352              if ( false === $group->save() ) {
 353                  $message = __( 'There was an error saving the group. Please try again.', 'buddypress' );
 354                  $type    = 'error';
 355  
 356              // Save successful.
 357              } else {
 358                  $message = __( 'The group was saved successfully.', 'buddypress' );
 359                  $type    = 'success';
 360  
 361                  // @todo remove these old options.
 362                  if ( 1 == $group_id ) {
 363                      bp_update_option( 'bp-xprofile-base-group-name', $group->name );
 364                  }
 365  
 366                  /**
 367                   * Fires at the end of the group adding/saving process, if successful.
 368                   *
 369                   * @since 1.0.0
 370                   *
 371                   * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 372                   */
 373                  do_action( 'xprofile_groups_saved_group', $group );
 374              }
 375  
 376              xprofile_admin_screen( $message, $type );
 377  
 378          } else {
 379              $group->render_admin_form( $message );
 380          }
 381      } else {
 382          $group->render_admin_form();
 383      }
 384  }
 385  
 386  /**
 387   * Handles the deletion of profile data groups.
 388   *
 389   * @since 1.0.0
 390   *
 391   * @param int $group_id ID of the group to delete.
 392   */
 393  function xprofile_admin_delete_group( $group_id ) {
 394      global $message, $type;
 395  
 396      check_admin_referer( 'bp_xprofile_delete_group' );
 397  
 398      $mode = ! empty( $_GET['mode'] )
 399            ? sanitize_key( $_GET['mode'] )
 400            : false;
 401  
 402      // Display the group delete confirmation screen.
 403      if ( 'delete_group' === $mode ) {
 404          xprofile_admin_delete_group_screen( $group_id );
 405  
 406      // Handle the deletion of group.
 407      } else {
 408          $group = new BP_XProfile_Group( $group_id );
 409  
 410          if ( ! $group->delete() ) {
 411              $message = _x( 'There was an error deleting the group. Please try again.', 'Error when deleting profile fields group', 'buddypress' );
 412              $type    = 'error';
 413          } else {
 414              $message = _x( 'The group was deleted successfully.', 'Profile fields group was deleted successfully', 'buddypress' );
 415              $type    = 'success';
 416  
 417              /**
 418               * Fires at the end of group deletion process, if successful.
 419               *
 420               * @since 1.0.0
 421               *
 422               * @param BP_XProfile_Group $group Current BP_XProfile_Group object.
 423               */
 424              do_action( 'xprofile_groups_deleted_group', $group );
 425          }
 426  
 427          xprofile_admin_screen( $message, $type );
 428      }
 429  }
 430  
 431  /**
 432   * Display the delete confirmation screen of profile data groups.
 433   *
 434   * @since 7.0.0
 435   */
 436  function xprofile_admin_delete_group_screen( $group_id ) {
 437  
 438      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 439          die( '-1' );
 440      }
 441  
 442      $group = new BP_XProfile_Group( $group_id );
 443  
 444      $base_url = remove_query_arg( array( 'mode', 'group_id', '_wpnonce' ), $_SERVER['REQUEST_URI'] ); ?>
 445  
 446      <div class="wrap">
 447          <h1 class="wp-heading-inline"><?php esc_html_e( 'Delete Field Group', 'buddypress' ) ?></h1>
 448          <hr class="wp-header-end">
 449  
 450          <p><?php esc_html_e( 'You are about to delete the following field group:', 'buddypress' ) ?></p>
 451  
 452          <ul class="bp-xprofile-delete-group-list">
 453              <li><?php echo esc_html( $group->name ); ?></li>
 454          </ul>
 455  
 456          <p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>
 457  
 458          <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>
 459          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ) ?></a>
 460      </div>
 461  
 462      <?php
 463  }
 464  
 465  /**
 466   * Handles the adding or editing of profile field data for a user.
 467   *
 468   * @since 1.0.0
 469   *
 470   * @param int      $group_id ID of the group.
 471   * @param int|null $field_id ID of the field being managed.
 472   */
 473  function xprofile_admin_manage_field( $group_id, $field_id = null ) {
 474      global $wpdb, $message, $groups;
 475  
 476      $bp = buddypress();
 477  
 478      if ( is_null( $field_id ) ) {
 479          $field = new BP_XProfile_Field();
 480      } else {
 481          $field = xprofile_get_field( $field_id, null, false );
 482      }
 483  
 484      $field->group_id = $group_id;
 485  
 486      if ( isset( $_POST['saveField'] ) ) {
 487  
 488          // Check nonce.
 489          check_admin_referer( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
 490  
 491          if ( BP_XProfile_Field::admin_validate() ) {
 492              $field->is_required = $_POST['required'];
 493              $field->type        = $_POST['fieldtype'];
 494              $field->name        = $_POST['title'];
 495  
 496              if ( ! empty( $_POST['description'] ) ) {
 497                  $field->description = $_POST['description'];
 498              } else {
 499                  $field->description = '';
 500              }
 501  
 502              if ( ! empty( $_POST["sort_order_{$field->type}"] ) ) {
 503                  $field->order_by = $_POST["sort_order_{$field->type}"];
 504              }
 505  
 506              $field->field_order = $wpdb->get_var( $wpdb->prepare( "SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ) );
 507              if ( ! is_numeric( $field->field_order ) || is_wp_error( $field->field_order ) ) {
 508                  $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 ) );
 509                  $field->field_order++;
 510              }
 511  
 512              // For new profile fields, set the $field_id. For existing profile
 513              // fields, this will overwrite $field_id with the same value.
 514              $field_id = $field->save();
 515  
 516              if ( empty( $field_id ) ) {
 517                  $message = __( 'There was an error saving the field. Please try again.', 'buddypress' );
 518                  $type    = 'error';
 519              } else {
 520                  $message = __( 'The field was saved successfully.', 'buddypress' );
 521                  $type    = 'success';
 522  
 523                  // @todo remove these old options.
 524                  if ( 1 == $field_id ) {
 525                      bp_update_option( 'bp-xprofile-fullname-field-name', $field->name );
 526                  }
 527  
 528                  // Set member types.
 529                  if ( isset( $_POST['has-member-types'] ) ) {
 530                      $member_types = array();
 531                      if ( isset( $_POST['member-types'] ) ) {
 532                          $member_types = stripslashes_deep( $_POST['member-types'] );
 533                      }
 534  
 535                      $field->set_member_types( $member_types );
 536                  }
 537  
 538                  // Validate default visibility.
 539                  if ( ! empty( $_POST['default-visibility'] ) && in_array( $_POST['default-visibility'], wp_list_pluck( bp_xprofile_get_visibility_levels(), 'id' ) ) ) {
 540                      bp_xprofile_update_field_meta( $field_id, 'default_visibility', $_POST['default-visibility'] );
 541                  }
 542  
 543                  // Validate custom visibility.
 544                  if ( ! empty( $_POST['allow-custom-visibility'] ) && in_array( $_POST['allow-custom-visibility'], array( 'allowed', 'disabled' ) ) ) {
 545                      bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility'] );
 546                  }
 547  
 548                  // Validate signup.
 549                  if ( ! empty( $_POST['signup-position'] ) ) {
 550                      bp_xprofile_update_field_meta( $field_id, 'signup_position', (int) $_POST['signup-position'] );
 551                  } else {
 552                      bp_xprofile_delete_meta( $field_id, 'field', 'signup_position' );
 553                  }
 554  
 555                  // Save autolink settings.
 556                  if ( isset( $_POST['do_autolink'] ) && 'on' === wp_unslash( $_POST['do_autolink'] ) ) {
 557                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'on' );
 558                  } else {
 559                      bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'off' );
 560                  }
 561  
 562                  if ( $field->type_obj->do_settings_section() ) {
 563                      $settings = isset( $_POST['field-settings'] ) ? wp_unslash( $_POST['field-settings'] ) : array();
 564                      $field->admin_save_settings( $settings );
 565                  }
 566  
 567                  /**
 568                   * Fires at the end of the process to save a field for a user, if successful.
 569                   *
 570                   * @since 1.0.0
 571                   *
 572                   * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 573                   */
 574                  do_action( 'xprofile_fields_saved_field', $field );
 575  
 576                  $groups = bp_xprofile_get_groups();
 577              }
 578  
 579              xprofile_admin_screen( $message, $type );
 580  
 581          } else {
 582              $field->render_admin_form( $message );
 583          }
 584      } else {
 585          $field->render_admin_form();
 586      }
 587  }
 588  
 589  /**
 590   * Handles the deletion of a profile field (or field option).
 591   *
 592   * @since 1.0.0
 593   *
 594   * @global string $message The feedback message to show.
 595   * @global string $type The type of feedback message to show.
 596   *
 597   * @param int    $field_id    The field to delete.
 598   * @param string $field_type  The type of field being deleted.
 599   * @param bool   $delete_data Should the field data be deleted too.
 600   */
 601  function xprofile_admin_delete_field( $field_id, $field_type = 'field', $delete_data = false ) {
 602      global $message, $type;
 603  
 604      check_admin_referer( 'bp_xprofile_delete_field-' . $field_id, 'bp_xprofile_delete_field' );
 605  
 606      $mode = ! empty( $_GET['mode'] ) ? sanitize_key( $_GET['mode'] ) : false;
 607  
 608      // Switch type to 'option' if type is not 'field'.
 609      // @todo trust this param.
 610      $field_type  = ( 'field' == $field_type ) ? __( 'field', 'buddypress' ) : __( 'option', 'buddypress' );
 611  
 612      // Display the field/option delete confirmation screen.
 613      if ( in_array( $mode, array( 'delete_field', 'delete_option' ) ) ) {
 614          xprofile_admin_delete_field_screen( $field_id, $field_type );
 615  
 616      // Handle the deletion of field
 617      } else {
 618          $field = xprofile_get_field( $field_id, null, false );
 619  
 620          if ( !$field->delete( (bool) $delete_data ) ) {
 621              /* translators: %s: the field type */
 622              $message = sprintf( __( 'There was an error deleting the %s. Please try again.', 'buddypress' ), $field_type );
 623              $type    = 'error';
 624          } else {
 625              /* translators: %s: the field type */
 626              $message = sprintf( __( 'The %s was deleted successfully!', 'buddypress' ), $field_type );
 627              $type    = 'success';
 628  
 629              /**
 630               * Fires at the end of the field deletion process, if successful.
 631               *
 632               * @since 1.0.0
 633               *
 634               * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
 635               */
 636              do_action( 'xprofile_fields_deleted_field', $field );
 637          }
 638  
 639          xprofile_admin_screen( $message, $type );
 640      }
 641  }
 642  
 643  /**
 644   * Display the delete confirmation screen of xprofile field/option.
 645   *
 646   * @since 7.0.0
 647   */
 648  function xprofile_admin_delete_field_screen( $field_id, $field_type ) {
 649      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 650          die( '-1' );
 651      }
 652  
 653      $field = xprofile_get_field( $field_id, null, false );
 654  
 655      $base_url = remove_query_arg( array( 'mode', 'field_id', 'bp_xprofile_delete_field' ), $_SERVER['REQUEST_URI'] ); ?>
 656  
 657      <div class="wrap">
 658          <h1 class="wp-heading-inline">
 659              <?php
 660              printf(
 661                  /* translators: %s is the field type name. */
 662                  esc_html__( 'Delete %s', 'buddypress' ),
 663                  $field_type
 664              );
 665              ?>
 666          </h1>
 667  
 668          <hr class="wp-header-end">
 669  
 670          <p>
 671              <?php
 672              printf(
 673                  /* translators: %s is the field type name. */
 674                  esc_html__( 'You are about to delete the following %s:', 'buddypress' ),
 675                  $field_type
 676              );
 677              ?>
 678          </p>
 679  
 680          <ul class="bp-xprofile-delete-group-list">
 681              <li><?php echo esc_html( $field->name ); ?></li>
 682          </ul>
 683  
 684          <p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ); ?></strong></p>
 685  
 686          <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>
 687          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a>
 688      </div>
 689  
 690      <?php
 691  }
 692  
 693  
 694  
 695  /**
 696   * Handles the ajax reordering of fields within a group.
 697   *
 698   * @since 1.0.0
 699   */
 700  function xprofile_ajax_reorder_fields() {
 701  
 702      // Check the nonce.
 703      check_admin_referer( 'bp_reorder_fields', '_wpnonce_reorder_fields' );
 704  
 705      if ( empty( $_POST['field_order'] ) ) {
 706          return false;
 707      }
 708  
 709      parse_str( $_POST['field_order'], $order );
 710  
 711      $field_group_id = $_POST['field_group_id'];
 712  
 713      foreach ( (array) $order['draggable_field'] as $position => $field_id ) {
 714          xprofile_update_field_position( (int) $field_id, (int) $position, (int) $field_group_id );
 715      }
 716  }
 717  add_action( 'wp_ajax_xprofile_reorder_fields', 'xprofile_ajax_reorder_fields' );
 718  
 719  /**
 720   * Handles the reordering of field groups.
 721   *
 722   * @since 1.5.0
 723   */
 724  function xprofile_ajax_reorder_field_groups() {
 725  
 726      // Check the nonce.
 727      check_admin_referer( 'bp_reorder_groups', '_wpnonce_reorder_groups' );
 728  
 729      if ( empty( $_POST['group_order'] ) ) {
 730          return false;
 731      }
 732  
 733      parse_str( $_POST['group_order'], $order );
 734  
 735      foreach ( (array) $order['group'] as $position => $field_group_id ) {
 736          xprofile_update_field_group_position( (int) $field_group_id, (int) $position );
 737      }
 738  }
 739  add_action( 'wp_ajax_xprofile_reorder_groups', 'xprofile_ajax_reorder_field_groups' );
 740  
 741  /**
 742   * Handles the WYSIWYG display of each profile field on the edit screen.
 743   *
 744   * @since 1.5.0
 745   *
 746   * @param BP_XProfile_Field   $admin_field Admin field.
 747   * @param object $admin_group Admin group object.
 748   * @param string $class       Classes to append to output.
 749   */
 750  function xprofile_admin_field( $admin_field, $admin_group, $class = '' ) {
 751      global $field;
 752  
 753      $field = $admin_field;
 754  
 755      // Users admin URL.
 756      $url = bp_get_admin_url( 'users.php' );
 757  
 758      // Edit.
 759      $field_edit_url = add_query_arg( array(
 760          'page'     => 'bp-profile-setup',
 761          'mode'     => 'edit_field',
 762          'group_id' => (int) $field->group_id,
 763          'field_id' => (int) $field->id
 764      ), $url );
 765  
 766      // Delete.
 767      if ( $field->can_delete ) {
 768          $field_delete_url = add_query_arg( array(
 769              'page'     => 'bp-profile-setup',
 770              'mode'     => 'delete_field',
 771              'field_id' => (int) $field->id
 772          ), $url . '#tabs-' . (int) $field->group_id );
 773      } ?>
 774  
 775      <fieldset id="draggable_field_<?php echo esc_attr( $field->id ); ?>" class="sortable<?php echo ' ' . $field->type; if ( !empty( $class ) ) echo ' ' . $class; ?>">
 776          <legend>
 777              <span>
 778                  <?php bp_the_profile_field_name(); ?>
 779  
 780                  <?php if ( empty( $field->can_delete )                                    ) : ?><?php esc_html_e( '(Primary)', 'buddypress' ); endif; ?>
 781                  <?php bp_the_profile_field_required_label(); ?>
 782                  <?php if ( bp_xprofile_get_meta( $field->id, 'field', 'signup_position' ) ) : ?><?php esc_html_e( '(Sign-up)', 'buddypress' ); endif; ?>
 783                  <?php if ( bp_get_member_types() ) : echo $field->get_member_type_label(); endif; ?>
 784  
 785                  <?php
 786  
 787                  /**
 788                   * Fires at end of legend above the name field in base xprofile group.
 789                   *
 790                   * @since 2.2.0
 791                   *
 792                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
 793                   *                                 object being rendered.
 794                   */
 795                  do_action( 'xprofile_admin_field_name_legend', $field ); ?>
 796              </span>
 797          </legend>
 798          <div class="field-wrapper">
 799  
 800              <?php
 801              if ( in_array( $field->type, array_keys( bp_xprofile_get_field_types() ) ) ) {
 802                  $field_type = bp_xprofile_create_field_type( $field->type );
 803                  $field_type->admin_field_html();
 804              } else {
 805  
 806                  /**
 807                   * Fires after the input if the current field is not in default field types.
 808                   *
 809                   * @since 1.5.0
 810                   *
 811                   * @param BP_XProfile_Field $field Current BP_XProfile_Field
 812                   *                                 object being rendered.
 813                   * @param int               $value Integer 1.
 814                   */
 815                  do_action( 'xprofile_admin_field', $field, 1 );
 816              }
 817              ?>
 818  
 819              <?php if ( $field->description ) : ?>
 820  
 821                  <p class="description"><?php echo esc_attr( $field->description ); ?></p>
 822  
 823              <?php endif; ?>
 824  
 825              <div class="actions">
 826                  <a class="button edit" href="<?php echo esc_url( $field_edit_url ); ?>"><?php _ex( 'Edit', 'Edit field link', 'buddypress' ); ?></a>
 827  
 828                  <?php if ( $field->can_delete ) : ?>
 829  
 830                      <div class="delete-button">
 831                          <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>
 832                      </div>
 833  
 834                  <?php endif; ?>
 835  
 836                  <?php
 837  
 838                  /**
 839                   * Fires at end of field management links in xprofile management admin.
 840                   *
 841                   * @since 2.2.0
 842                   *
 843                   * @param BP_XProfile_Group $group BP_XProfile_Group object
 844                   *                                 for the current group.
 845                   */
 846                  do_action( 'xprofile_admin_field_action', $field ); ?>
 847  
 848              </div>
 849          </div>
 850      </fieldset>
 851  
 852  <?php
 853  }
 854  
 855  /**
 856   * Print <option> elements containing the xprofile field types.
 857   *
 858   * @since 2.0.0
 859   *
 860   * @param string $select_field_type The name of the field type that should be selected.
 861   *                                  Will defaults to "textbox" if NULL is passed.
 862   */
 863  function bp_xprofile_admin_form_field_types( $select_field_type ) {
 864      $categories = array();
 865  
 866      if ( is_null( $select_field_type ) ) {
 867          $select_field_type = 'textbox';
 868      }
 869  
 870      // Sort each field type into its category.
 871      foreach ( bp_xprofile_get_field_types() as $field_name => $field_class ) {
 872          $field_type_obj = new $field_class;
 873          $the_category   = $field_type_obj->category;
 874  
 875          // Fallback to a catch-all if category not set.
 876          if ( ! $the_category ) {
 877              $the_category = _x( 'Other', 'xprofile field type category', 'buddypress' );
 878          }
 879  
 880          if ( isset( $categories[$the_category] ) ) {
 881              $categories[$the_category][] = array( $field_name, $field_type_obj );
 882          } else {
 883              $categories[$the_category] = array( array( $field_name, $field_type_obj ) );
 884          }
 885      }
 886  
 887      // Sort the categories alphabetically. ksort()'s SORT_NATURAL is only in PHP >= 5.4 :((.
 888      uksort( $categories, 'strnatcmp' );
 889  
 890      // Loop through each category and output form <options>.
 891      foreach ( $categories as $category => $fields ) {
 892          printf( '<optgroup label="%1$s">', esc_attr( $category ) );  // Already i18n'd in each profile type class.
 893  
 894          // Sort these fields types alphabetically.
 895          uasort( $fields, function( $a, $b ) { return strnatcmp( $a[1]->name, $b[1]->name ); } );
 896  
 897          foreach ( $fields as $field_type_obj ) {
 898              $field_name     = $field_type_obj[0];
 899              $field_type_obj = $field_type_obj[1];
 900  
 901              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 ) );
 902          }
 903  
 904          printf( '</optgroup>' );
 905      }
 906  }
 907  
 908  // Load the xprofile user admin.
 909  add_action( 'bp_init', array( 'BP_XProfile_User_Admin', 'register_xprofile_user_admin' ), 11 );


Generated: Tue Dec 1 01:01:37 2020 Cross-referenced by PHPXref 0.7.1