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


Generated: Mon Jul 6 01:01:29 2020 Cross-referenced by PHPXref 0.7.1