[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress Groups component admin screen.
   4   *
   5   * Props to WordPress core for the Comments admin screen, and its contextual
   6   * help text, on which this implementation is heavily based.
   7   *
   8   * @package BuddyPress
   9   * @subpackage Groups
  10   * @since 1.7.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  // Include WP's list table class.
  17  if ( !class_exists( 'WP_List_Table' ) ) require( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  18  
  19  // The per_page screen option. Has to be hooked in extremely early.
  20  if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] )
  21      add_filter( 'set-screen-option', 'bp_groups_admin_screen_options', 10, 3 );
  22  
  23  /**
  24   * Register the Groups component admin screen.
  25   *
  26   * @since 1.7.0
  27   */
  28  function bp_groups_add_admin_menu() {
  29  
  30      // Add our screen.
  31      $hook = add_menu_page(
  32          _x( 'Groups', 'Admin Groups page title', 'buddypress' ),
  33          _x( 'Groups', 'Admin Groups menu', 'buddypress' ),
  34          'bp_moderate',
  35          'bp-groups',
  36          'bp_groups_admin',
  37          'div'
  38      );
  39  
  40      // Hook into early actions to load custom CSS and our init handler.
  41      add_action( "load-$hook", 'bp_groups_admin_load' );
  42  }
  43  add_action( bp_core_admin_hook(), 'bp_groups_add_admin_menu' );
  44  
  45  /**
  46   * Add groups component to custom menus array.
  47   *
  48   * This ensures that the Groups menu item appears in the proper order on the
  49   * main Dashboard menu.
  50   *
  51   * @since 1.7.0
  52   *
  53   * @param array $custom_menus Array of BP top-level menu items.
  54   * @return array Menu item array, with Groups added.
  55   */
  56  function bp_groups_admin_menu_order( $custom_menus = array() ) {
  57      array_push( $custom_menus, 'bp-groups' );
  58      return $custom_menus;
  59  }
  60  add_filter( 'bp_admin_menu_order', 'bp_groups_admin_menu_order' );
  61  
  62  /**
  63   * Set up the Groups admin page.
  64   *
  65   * Loaded before the page is rendered, this function does all initial setup,
  66   * including: processing form requests, registering contextual help, and
  67   * setting up screen options.
  68   *
  69   * @since 1.7.0
  70   *
  71   * @global BP_Groups_List_Table $bp_groups_list_table Groups screen list table.
  72   */
  73  function bp_groups_admin_load() {
  74      global $bp_groups_list_table;
  75  
  76      // Build redirection URL.
  77      $redirect_to = remove_query_arg( array( 'action', 'action2', 'gid', 'deleted', 'error', 'updated', 'success_new', 'error_new', 'success_modified', 'error_modified' ), $_SERVER['REQUEST_URI'] );
  78  
  79      $doaction   = bp_admin_list_table_current_bulk_action();
  80      $min        = bp_core_get_minified_asset_suffix();
  81  
  82      /**
  83       * Fires at top of groups admin page.
  84       *
  85       * @since 1.7.0
  86       *
  87       * @param string $doaction Current $_GET action being performed in admin screen.
  88       */
  89      do_action( 'bp_groups_admin_load', $doaction );
  90  
  91      // Edit screen.
  92      if ( 'do_delete' == $doaction && ! empty( $_GET['gid'] ) ) {
  93  
  94          check_admin_referer( 'bp-groups-delete' );
  95  
  96          $group_ids = wp_parse_id_list( $_GET['gid'] );
  97  
  98          $count = 0;
  99          foreach ( $group_ids as $group_id ) {
 100              if ( groups_delete_group( $group_id ) ) {
 101                  $count++;
 102              }
 103          }
 104  
 105          $redirect_to = add_query_arg( 'deleted', $count, $redirect_to );
 106  
 107          bp_core_redirect( $redirect_to );
 108  
 109      } elseif ( 'edit' == $doaction && ! empty( $_GET['gid'] ) ) {
 110          // Columns screen option.
 111          add_screen_option( 'layout_columns', array( 'default' => 2, 'max' => 2, ) );
 112  
 113          get_current_screen()->add_help_tab( array(
 114              'id'      => 'bp-group-edit-overview',
 115              'title'   => __( 'Overview', 'buddypress' ),
 116              'content' =>
 117                  '<p>' . __( 'This page is a convenient way to edit the details associated with one of your groups.', 'buddypress' ) . '</p>' .
 118                  '<p>' . __( 'The Name and Description box is fixed in place, but you can reposition all the other boxes using drag and drop, and can minimize or expand them by clicking the title bar of each box. Use the Screen Options tab to hide or unhide, or to choose a 1- or 2-column layout for this screen.', 'buddypress' ) . '</p>'
 119          ) );
 120  
 121          // Help panel - sidebar links.
 122          get_current_screen()->set_help_sidebar(
 123              '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 124              '<p><a href="https://buddypress.org/support">' . __( 'Support Forums', 'buddypress' ) . '</a></p>'
 125          );
 126  
 127          // Register metaboxes for the edit screen.
 128          add_meta_box( 'submitdiv', _x( 'Save', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_status', get_current_screen()->id, 'side', 'high' );
 129          add_meta_box( 'bp_group_settings', _x( 'Settings', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_settings', get_current_screen()->id, 'side', 'core' );
 130          add_meta_box( 'bp_group_add_members', _x( 'Add New Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_add_new_members', get_current_screen()->id, 'normal', 'core' );
 131          add_meta_box( 'bp_group_members', _x( 'Manage Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_members', get_current_screen()->id, 'normal', 'core' );
 132  
 133          // Group Type metabox. Only added if group types have been registered.
 134          $group_types = bp_groups_get_group_types();
 135          if ( ! empty( $group_types ) ) {
 136              add_meta_box(
 137                  'bp_groups_admin_group_type',
 138                  _x( 'Group Type', 'groups admin edit screen', 'buddypress' ),
 139                  'bp_groups_admin_edit_metabox_group_type',
 140                  get_current_screen()->id,
 141                  'side',
 142                  'core'
 143              );
 144          }
 145  
 146          /**
 147           * Fires after the registration of all of the default group meta boxes.
 148           *
 149           * @since 1.7.0
 150           */
 151          do_action( 'bp_groups_admin_meta_boxes' );
 152  
 153          // Enqueue JavaScript files.
 154          wp_enqueue_script( 'postbox' );
 155          wp_enqueue_script( 'dashboard' );
 156  
 157      // Index screen.
 158      } else {
 159          // Create the Groups screen list table.
 160          $bp_groups_list_table = new BP_Groups_List_Table();
 161  
 162          // The per_page screen option.
 163          add_screen_option( 'per_page', array( 'label' => _x( 'Groups', 'Groups per page (screen options)', 'buddypress' )) );
 164  
 165          // Help panel - overview text.
 166          get_current_screen()->add_help_tab( array(
 167              'id'      => 'bp-groups-overview',
 168              'title'   => __( 'Overview', 'buddypress' ),
 169              'content' =>
 170                  '<p>' . __( 'You can manage groups much like you can manage comments and other content. This screen is customizable in the same ways as other management screens, and you can act on groups by using the on-hover action links or the Bulk Actions.', 'buddypress' ) . '</p>',
 171          ) );
 172  
 173          get_current_screen()->add_help_tab( array(
 174              'id'      => 'bp-groups-overview-actions',
 175              'title'   => __( 'Group Actions', 'buddypress' ),
 176              'content' =>
 177                  '<p>' . __( 'Clicking "Visit" will take you to the group&#8217;s public page. Use this link to see what the group looks like on the front end of your site.', 'buddypress' ) . '</p>' .
 178                  '<p>' . __( 'Clicking "Edit" will take you to a Dashboard panel where you can manage various details about the group, such as its name and description, its members, and other settings.', 'buddypress' ) . '</p>' .
 179                  '<p>' . __( 'If you click "Delete" under a specific group, or select a number of groups and then choose Delete from the Bulk Actions menu, you will be led to a page where you&#8217;ll be asked to confirm the permanent deletion of the group(s).', 'buddypress' ) . '</p>',
 180          ) );
 181  
 182          // Help panel - sidebar links.
 183          get_current_screen()->set_help_sidebar(
 184              '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
 185              '<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
 186          );
 187  
 188          // Add accessible hidden heading and text for Groups screen pagination.
 189          get_current_screen()->set_screen_reader_content( array(
 190              /* translators: accessibility text */
 191              'heading_pagination' => __( 'Groups list navigation', 'buddypress' ),
 192          ) );
 193      }
 194  
 195      $bp = buddypress();
 196  
 197      // Enqueue CSS and JavaScript.
 198      wp_enqueue_script( 'bp_groups_admin_js', $bp->plugin_url . "bp-groups/admin/js/admin{$min}.js", array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), bp_get_version(), true );
 199      wp_localize_script( 'bp_groups_admin_js', 'BP_Group_Admin', array(
 200          'add_member_placeholder' => __( 'Start typing a username to add a new member.', 'buddypress' ),
 201          'warn_on_leave'          => __( 'If you leave this page, you will lose any unsaved changes you have made to the group.', 'buddypress' ),
 202      ) );
 203      wp_enqueue_style( 'bp_groups_admin_css', $bp->plugin_url . "bp-groups/admin/css/admin{$min}.css", array(), bp_get_version() );
 204  
 205      wp_style_add_data( 'bp_groups_admin_css', 'rtl', 'replace' );
 206      if ( $min ) {
 207          wp_style_add_data( 'bp_groups_admin_css', 'suffix', $min );
 208      }
 209  
 210  
 211      if ( $doaction && 'save' == $doaction ) {
 212          // Get group ID.
 213          $group_id = isset( $_REQUEST['gid'] ) ? (int) $_REQUEST['gid'] : '';
 214  
 215          $redirect_to = add_query_arg( array(
 216              'gid'    => (int) $group_id,
 217              'action' => 'edit'
 218          ), $redirect_to );
 219  
 220          // Check this is a valid form submission.
 221          check_admin_referer( 'edit-group_' . $group_id );
 222  
 223          // Get the group from the database.
 224          $group = groups_get_group( $group_id );
 225  
 226          // If the group doesn't exist, just redirect back to the index.
 227          if ( empty( $group->slug ) ) {
 228              wp_redirect( $redirect_to );
 229              exit;
 230          }
 231  
 232          // Check the form for the updated properties.
 233          // Store errors.
 234          $error = 0;
 235          $success_new = $error_new = $success_modified = $error_modified = array();
 236  
 237          // Name, description and slug must not be empty.
 238          if ( empty( $_POST['bp-groups-name'] ) ) {
 239              $error = $error - 1;
 240          }
 241          if ( empty( $_POST['bp-groups-description'] ) ) {
 242              $error = $error - 2;
 243          }
 244          if ( empty( $_POST['bp-groups-slug'] ) ) {
 245              $error = $error - 4;
 246          }
 247  
 248          /*
 249           * Group name, slug, and description are handled with
 250           * groups_edit_base_group_details().
 251           */
 252          if ( ! $error && ! groups_edit_base_group_details( array(
 253                  'group_id'       => $group_id,
 254                  'name'           => $_POST['bp-groups-name'],
 255                  'slug'           => $_POST['bp-groups-slug'],
 256                  'description'    => $_POST['bp-groups-description'],
 257                  'notify_members' => false,
 258              ) ) ) {
 259              $error = $group_id;
 260          }
 261  
 262          // Enable discussion forum.
 263          $enable_forum   = ( isset( $_POST['group-show-forum'] ) ) ? 1 : 0;
 264  
 265          /**
 266           * Filters the allowed status values for the group.
 267           *
 268           * @since 1.0.2
 269           *
 270           * @param array $value Array of allowed group statuses.
 271           */
 272          $allowed_status = apply_filters( 'groups_allowed_status', array( 'public', 'private', 'hidden' ) );
 273          $status         = ( in_array( $_POST['group-status'], (array) $allowed_status ) ) ? $_POST['group-status'] : 'public';
 274  
 275          /**
 276           * Filters the allowed invite status values for the group.
 277           *
 278           * @since 1.5.0
 279           *
 280           * @param array $value Array of allowed invite statuses.
 281           */
 282          $allowed_invite_status = apply_filters( 'groups_allowed_invite_status', array( 'members', 'mods', 'admins' ) );
 283          $invite_status           = in_array( $_POST['group-invite-status'], (array) $allowed_invite_status ) ? $_POST['group-invite-status'] : 'members';
 284  
 285          if ( !groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status ) ) {
 286              $error = $group_id;
 287          }
 288  
 289          // Process new members.
 290          $user_names = array();
 291  
 292          if ( ! empty( $_POST['bp-groups-new-members'] ) ) {
 293              $user_names = array_merge( $user_names, explode( ',', $_POST['bp-groups-new-members'] ) );
 294          }
 295  
 296          if ( ! empty( $user_names ) ) {
 297  
 298              foreach( array_values( $user_names ) as $user_name ) {
 299                  $un = trim( $user_name );
 300  
 301                  // Make sure the user exists before attempting
 302                  // to add to the group.
 303                  $user = get_user_by( 'slug', $un );
 304  
 305                  if ( empty( $user ) ) {
 306                      $error_new[] = $un;
 307                  } else {
 308                      if ( ! groups_join_group( $group_id, $user->ID ) ) {
 309                          $error_new[]   = $un;
 310                      } else {
 311                          $success_new[] = $un;
 312                      }
 313                  }
 314              }
 315          }
 316  
 317          // Process member role changes.
 318          if ( ! empty( $_POST['bp-groups-role'] ) && ! empty( $_POST['bp-groups-existing-role'] ) ) {
 319  
 320              // Before processing anything, make sure you're not
 321              // attempting to remove the all user admins.
 322              $admin_count = 0;
 323              foreach ( (array) $_POST['bp-groups-role'] as $new_role ) {
 324                  if ( 'admin' == $new_role ) {
 325                      $admin_count++;
 326                      break;
 327                  }
 328              }
 329  
 330              if ( ! $admin_count ) {
 331  
 332                  $redirect_to = add_query_arg( 'no_admins', 1, $redirect_to );
 333                  $error = $group_id;
 334  
 335              } else {
 336  
 337                  // Process only those users who have had their roles changed.
 338                  foreach ( (array) $_POST['bp-groups-role'] as $user_id => $new_role ) {
 339                      $user_id = (int) $user_id;
 340  
 341                      $existing_role = isset( $_POST['bp-groups-existing-role'][$user_id] ) ? $_POST['bp-groups-existing-role'][$user_id] : '';
 342  
 343                      if ( $existing_role != $new_role ) {
 344                          $result = false;
 345  
 346                          switch ( $new_role ) {
 347                              case 'mod' :
 348                                  // Admin to mod is a demotion. Demote to
 349                                  // member, then fall through.
 350                                  if ( 'admin' == $existing_role ) {
 351                                      groups_demote_member( $user_id, $group_id );
 352                                  }
 353  
 354                              case 'admin' :
 355                                  // If the user was banned, we must
 356                                  // unban first.
 357                                  if ( 'banned' == $existing_role ) {
 358                                      groups_unban_member( $user_id, $group_id );
 359                                  }
 360  
 361                                  // At this point, each existing_role
 362                                  // is a member, so promote.
 363                                  $result = groups_promote_member( $user_id, $group_id, $new_role );
 364  
 365                                  break;
 366  
 367                              case 'member' :
 368  
 369                                  if ( 'admin' == $existing_role || 'mod' == $existing_role ) {
 370                                      $result = groups_demote_member( $user_id, $group_id );
 371                                  } elseif ( 'banned' == $existing_role ) {
 372                                      $result = groups_unban_member( $user_id, $group_id );
 373                                  }
 374  
 375                                  break;
 376  
 377                              case 'banned' :
 378  
 379                                  $result = groups_ban_member( $user_id, $group_id );
 380  
 381                                  break;
 382  
 383                              case 'remove' :
 384  
 385                                  $result = groups_remove_member( $user_id, $group_id );
 386  
 387                                  break;
 388                          }
 389  
 390                          // Store the success or failure.
 391                          if ( $result ) {
 392                              $success_modified[] = $user_id;
 393                          } else {
 394                              $error_modified[]   = $user_id;
 395                          }
 396                      }
 397                  }
 398              }
 399          }
 400  
 401          /**
 402           * Fires before redirect so plugins can do something first on save action.
 403           *
 404           * @since 1.6.0
 405           *
 406           * @param int $group_id ID of the group being edited.
 407           */
 408          do_action( 'bp_group_admin_edit_after', $group_id );
 409  
 410          // Create the redirect URL.
 411          if ( $error ) {
 412              // This means there was an error updating group details.
 413              $redirect_to = add_query_arg( 'error', (int) $error, $redirect_to );
 414          } else {
 415              // Group details were update successfully.
 416              $redirect_to = add_query_arg( 'updated', 1, $redirect_to );
 417          }
 418  
 419          if ( !empty( $success_new ) ) {
 420              $success_new = implode( ',', array_filter( $success_new, 'urlencode' ) );
 421              $redirect_to = add_query_arg( 'success_new', $success_new, $redirect_to );
 422          }
 423  
 424          if ( !empty( $error_new ) ) {
 425              $error_new = implode( ',', array_filter( $error_new, 'urlencode' ) );
 426              $redirect_to = add_query_arg( 'error_new', $error_new, $redirect_to );
 427          }
 428  
 429          if ( !empty( $success_modified ) ) {
 430              $success_modified = implode( ',', array_filter( $success_modified, 'urlencode' ) );
 431              $redirect_to = add_query_arg( 'success_modified', $success_modified, $redirect_to );
 432          }
 433  
 434          if ( !empty( $error_modified ) ) {
 435              $error_modified = implode( ',', array_filter( $error_modified, 'urlencode' ) );
 436              $redirect_to = add_query_arg( 'error_modified', $error_modified, $redirect_to );
 437          }
 438  
 439          /**
 440           * Filters the URL to redirect to after successfully editing a group.
 441           *
 442           * @since 1.7.0
 443           *
 444           * @param string $redirect_to URL to redirect user to.
 445           */
 446          wp_redirect( apply_filters( 'bp_group_admin_edit_redirect', $redirect_to ) );
 447          exit;
 448  
 449  
 450      // If a referrer and a nonce is supplied, but no action, redirect back.
 451      } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
 452          wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) );
 453          exit;
 454      }
 455  }
 456  
 457  /**
 458   * Handle save/update of screen options for the Groups component admin screen.
 459   *
 460   * @since 1.7.0
 461   *
 462   * @param string $value     Will always be false unless another plugin filters it first.
 463   * @param string $option    Screen option name.
 464   * @param string $new_value Screen option form value.
 465   * @return string|int Option value. False to abandon update.
 466   */
 467  function bp_groups_admin_screen_options( $value, $option, $new_value ) {
 468      if ( 'toplevel_page_bp_groups_per_page' != $option && 'toplevel_page_bp_groups_network_per_page' != $option )
 469          return $value;
 470  
 471      // Per page.
 472      $new_value = (int) $new_value;
 473      if ( $new_value < 1 || $new_value > 999 )
 474          return $value;
 475  
 476      return $new_value;
 477  }
 478  
 479  /**
 480   * Select the appropriate Groups admin screen, and output it.
 481   *
 482   * @since 1.7.0
 483   */
 484  function bp_groups_admin() {
 485      // Decide whether to load the index or edit screen.
 486      $doaction = bp_admin_list_table_current_bulk_action();
 487  
 488      // Display the single group edit screen.
 489      if ( 'edit' == $doaction && ! empty( $_GET['gid'] ) ) {
 490          bp_groups_admin_edit();
 491  
 492      // Display the group deletion confirmation screen.
 493      } elseif ( 'delete' == $doaction && ! empty( $_GET['gid'] ) ) {
 494          bp_groups_admin_delete();
 495  
 496      // Otherwise, display the groups index screen.
 497      } else {
 498          bp_groups_admin_index();
 499      }
 500  }
 501  
 502  /**
 503   * Display the single groups edit screen.
 504   *
 505   * @since 1.7.0
 506   */
 507  function bp_groups_admin_edit() {
 508  
 509      if ( ! bp_current_user_can( 'bp_moderate' ) )
 510          die( '-1' );
 511  
 512      $messages = array();
 513  
 514      // If the user has just made a change to a group, build status messages.
 515      if ( !empty( $_REQUEST['no_admins'] ) || ! empty( $_REQUEST['error'] ) || ! empty( $_REQUEST['updated'] ) || ! empty( $_REQUEST['error_new'] ) || ! empty( $_REQUEST['success_new'] ) || ! empty( $_REQUEST['error_modified'] ) || ! empty( $_REQUEST['success_modified'] ) ) {
 516          $no_admins        = ! empty( $_REQUEST['no_admins']        ) ? 1                                             : 0;
 517          $errors           = ! empty( $_REQUEST['error']            ) ? $_REQUEST['error']                            : '';
 518          $updated          = ! empty( $_REQUEST['updated']          ) ? $_REQUEST['updated']                          : '';
 519          $error_new        = ! empty( $_REQUEST['error_new']        ) ? explode( ',', $_REQUEST['error_new'] )        : array();
 520          $success_new      = ! empty( $_REQUEST['success_new']      ) ? explode( ',', $_REQUEST['success_new'] )      : array();
 521          $error_modified   = ! empty( $_REQUEST['error_modified']   ) ? explode( ',', $_REQUEST['error_modified'] )   : array();
 522          $success_modified = ! empty( $_REQUEST['success_modified'] ) ? explode( ',', $_REQUEST['success_modified'] ) : array();
 523  
 524          if ( ! empty( $no_admins ) ) {
 525              $messages[] = __( 'You cannot remove all administrators from a group.', 'buddypress' );
 526          }
 527  
 528          if ( ! empty( $errors ) ) {
 529              if ( $errors < 0 ) {
 530                  $messages[] = __( 'Group name, slug, and description are all required fields.', 'buddypress' );
 531              } else {
 532                  $messages[] = __( 'An error occurred when trying to update your group details.', 'buddypress' );
 533              }
 534  
 535          } elseif ( ! empty( $updated ) ) {
 536              $messages[] = __( 'The group has been updated successfully.', 'buddypress' );
 537          }
 538  
 539          if ( ! empty( $error_new ) ) {
 540              /* translators: %s: comma separated list of usernames */
 541              $messages[] = sprintf( __( 'The following users could not be added to the group: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $error_new ) ) . '</em>' );
 542          }
 543  
 544          if ( ! empty( $success_new ) ) {
 545              /* translators: %s: comma separated list of usernames */
 546              $messages[] = sprintf( __( 'The following users were successfully added to the group: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $success_new ) ) . '</em>' );
 547          }
 548  
 549          if ( ! empty( $error_modified ) ) {
 550              $error_modified = bp_groups_admin_get_usernames_from_ids( $error_modified );
 551              /* translators: %s: comma separated list of usernames */
 552              $messages[] = sprintf( __( 'An error occurred when trying to modify the following members: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $error_modified ) ) . '</em>' );
 553          }
 554  
 555          if ( ! empty( $success_modified ) ) {
 556              $success_modified = bp_groups_admin_get_usernames_from_ids( $success_modified );
 557              /* translators: %s: comma separated list of usernames */
 558              $messages[] = sprintf( __( 'The following members were successfully modified: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $success_modified ) ) . '</em>' );
 559          }
 560      }
 561  
 562      $is_error = ! empty( $no_admins ) || ! empty( $errors ) || ! empty( $error_new ) || ! empty( $error_modified );
 563  
 564      // Get the group from the database.
 565      $group = groups_get_group( (int) $_GET['gid'] );
 566  
 567      $group_name = isset( $group->name ) ? bp_get_group_name( $group ) : '';
 568  
 569      // Construct URL for form.
 570      $form_url = remove_query_arg( array( 'action', 'deleted', 'no_admins', 'error', 'error_new', 'success_new', 'error_modified', 'success_modified' ), $_SERVER['REQUEST_URI'] );
 571      $form_url = add_query_arg( 'action', 'save', $form_url );
 572  
 573      /**
 574       * Fires before the display of the edit form.
 575       *
 576       * Useful for plugins to modify the group before display.
 577       *
 578       * @since 1.7.0
 579       *
 580       * @param BP_Groups_Group $this Instance of the current group being edited. Passed by reference.
 581       */
 582      do_action_ref_array( 'bp_groups_admin_edit', array( &$group ) ); ?>
 583  
 584      <div class="wrap">
 585          <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 586  
 587              <h1 class="wp-heading-inline"><?php _e( 'Edit Group', 'buddypress' ); ?></h1>
 588  
 589              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 590                  <a class="page-title-action" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 591              <?php endif; ?>
 592  
 593              <hr class="wp-header-end">
 594  
 595          <?php else : ?>
 596  
 597              <h1><?php _e( 'Edit Group', 'buddypress' ); ?>
 598  
 599                  <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 600                      <a class="add-new-h2" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 601                  <?php endif; ?>
 602  
 603              </h1>
 604  
 605          <?php endif; ?>
 606  
 607          <?php // If the user has just made a change to an group, display the status messages. ?>
 608          <?php if ( !empty( $messages ) ) : ?>
 609              <div id="moderated" class="<?php echo ( $is_error ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "</p><p>", $messages ); ?></p></div>
 610          <?php endif; ?>
 611  
 612          <?php if ( $group->id ) : ?>
 613  
 614              <form action="<?php echo esc_url( $form_url ); ?>" id="bp-groups-edit-form" method="post">
 615                  <div id="poststuff">
 616  
 617                      <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
 618                          <div id="post-body-content">
 619                              <div id="postdiv">
 620                                  <div id="bp_groups_name" class="postbox">
 621                                      <h2><?php _e( 'Name and Description', 'buddypress' ); ?></h2>
 622                                      <div class="inside">
 623                                          <label for="bp-groups-name" class="screen-reader-text"><?php
 624                                              /* translators: accessibility text */
 625                                              _e( 'Group Name', 'buddypress' );
 626                                          ?></label>
 627                                          <input type="text" name="bp-groups-name" id="bp-groups-name" value="<?php echo esc_attr( stripslashes( $group_name ) ) ?>" />
 628                                          <div id="bp-groups-permalink-box">
 629                                              <strong><?php esc_html_e( 'Permalink:', 'buddypress' ) ?></strong>
 630                                              <span id="bp-groups-permalink">
 631                                                  <?php bp_groups_directory_permalink(); ?> <input type="text" id="bp-groups-slug" name="bp-groups-slug" value="<?php bp_group_slug( $group ); ?>" autocomplete="off"> /
 632                                              </span>
 633                                              <a href="<?php echo bp_group_permalink( $group ) ?>" class="button button-small" id="bp-groups-visit-group"><?php esc_html_e( 'Visit Group', 'buddypress' ) ?></a>
 634                                          </div>
 635  
 636                                          <label for="bp-groups-description" class="screen-reader-text"><?php
 637                                              /* translators: accessibility text */
 638                                              _e( 'Group Description', 'buddypress' );
 639                                          ?></label>
 640                                          <?php wp_editor( stripslashes( $group->description ), 'bp-groups-description', array( 'media_buttons' => false, 'teeny' => true, 'textarea_rows' => 5, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
 641                                      </div>
 642                                  </div>
 643                              </div>
 644                          </div><!-- #post-body-content -->
 645  
 646                          <div id="postbox-container-1" class="postbox-container">
 647                              <?php do_meta_boxes( get_current_screen()->id, 'side', $group ); ?>
 648                          </div>
 649  
 650                          <div id="postbox-container-2" class="postbox-container">
 651                              <?php do_meta_boxes( get_current_screen()->id, 'normal', $group ); ?>
 652                              <?php do_meta_boxes( get_current_screen()->id, 'advanced', $group ); ?>
 653                          </div>
 654                      </div><!-- #post-body -->
 655  
 656                  </div><!-- #poststuff -->
 657                  <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
 658                  <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
 659                  <?php wp_nonce_field( 'edit-group_' . $group->id ); ?>
 660              </form>
 661  
 662          <?php else : ?>
 663  
 664              <p><?php
 665                  printf(
 666                      '%1$s <a href="%2$s">%3$s</a>',
 667                      __( 'No group found with this ID.', 'buddypress' ),
 668                      esc_url( bp_get_admin_url( 'admin.php?page=bp-groups' ) ),
 669                      __( 'Go back and try again.', 'buddypress' )
 670                  );
 671              ?></p>
 672  
 673          <?php endif; ?>
 674  
 675      </div><!-- .wrap -->
 676  
 677  <?php
 678  }
 679  
 680  /**
 681   * Display the Group delete confirmation screen.
 682   *
 683   * We include a separate confirmation because group deletion is truly
 684   * irreversible.
 685   *
 686   * @since 1.7.0
 687   */
 688  function bp_groups_admin_delete() {
 689  
 690      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 691          die( '-1' );
 692      }
 693  
 694      $group_ids = isset( $_REQUEST['gid'] ) ? $_REQUEST['gid'] : 0;
 695      if ( ! is_array( $group_ids ) ) {
 696          $group_ids = explode( ',', $group_ids );
 697      }
 698      $group_ids = wp_parse_id_list( $group_ids );
 699      $groups    = groups_get_groups( array(
 700          'include'     => $group_ids,
 701          'show_hidden' => true,
 702          'per_page'    => null, // Return all results.
 703      ) );
 704  
 705      // Create a new list of group ids, based on those that actually exist.
 706      $gids = array();
 707      foreach ( $groups['groups'] as $group ) {
 708          $gids[] = $group->id;
 709      }
 710  
 711      $base_url  = remove_query_arg( array( 'action', 'action2', 'paged', 's', '_wpnonce', 'gid' ), $_SERVER['REQUEST_URI'] ); ?>
 712  
 713      <div class="wrap">
 714          <h1><?php _e( 'Delete Groups', 'buddypress' ) ?></h1>
 715          <p><?php _e( 'You are about to delete the following groups:', 'buddypress' ) ?></p>
 716  
 717          <ul class="bp-group-delete-list">
 718          <?php foreach ( $groups['groups'] as $group ) : ?>
 719              <li><?php echo esc_html( bp_get_group_name( $group ) ); ?></li>
 720          <?php endforeach; ?>
 721          </ul>
 722  
 723          <p><strong><?php _e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>
 724  
 725          <a class="button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'do_delete', 'gid' => implode( ',', $gids ) ), $base_url ), 'bp-groups-delete' ) ); ?>"><?php _e( 'Delete Permanently', 'buddypress' ) ?></a>
 726          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php _e( 'Cancel', 'buddypress' ) ?></a>
 727      </div>
 728  
 729      <?php
 730  }
 731  
 732  /**
 733   * Display the Groups admin index screen.
 734   *
 735   * This screen contains a list of all BuddyPress groups.
 736   *
 737   * @since 1.7.0
 738   *
 739   * @global BP_Groups_List_Table $bp_groups_list_table Group screen list table.
 740   * @global string $plugin_page Currently viewed plugin page.
 741   */
 742  function bp_groups_admin_index() {
 743      global $bp_groups_list_table, $plugin_page;
 744  
 745      $messages = array();
 746  
 747      // If the user has just made a change to a group, build status messages.
 748      if ( ! empty( $_REQUEST['deleted'] ) ) {
 749          $deleted  = ! empty( $_REQUEST['deleted'] ) ? (int) $_REQUEST['deleted'] : 0;
 750  
 751          if ( $deleted > 0 ) {
 752              /* translators: %s: number of deleted groups */
 753              $messages[] = sprintf( _n( '%s group has been permanently deleted.', '%s groups have been permanently deleted.', $deleted, 'buddypress' ), number_format_i18n( $deleted ) );
 754          }
 755      }
 756  
 757      // Prepare the group items for display.
 758      $bp_groups_list_table->prepare_items();
 759  
 760      /**
 761       * Fires before the display of messages for the edit form.
 762       *
 763       * Useful for plugins to modify the messages before display.
 764       *
 765       * @since 1.7.0
 766       *
 767       * @param array $messages Array of messages to be displayed.
 768       */
 769      do_action( 'bp_groups_admin_index', $messages ); ?>
 770  
 771      <div class="wrap">
 772          <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 773  
 774              <h1 class="wp-heading-inline"><?php _e( 'Groups', 'buddypress' ); ?></h1>
 775  
 776              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 777                  <a class="page-title-action" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 778              <?php endif; ?>
 779  
 780              <?php if ( !empty( $_REQUEST['s'] ) ) : ?>
 781                  <span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
 782              <?php endif; ?>
 783  
 784              <hr class="wp-header-end">
 785  
 786          <?php else : ?>
 787  
 788          <h1>
 789              <?php _e( 'Groups', 'buddypress' ); ?>
 790  
 791              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 792                  <a class="add-new-h2" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 793              <?php endif; ?>
 794  
 795              <?php if ( !empty( $_REQUEST['s'] ) ) : ?>
 796                  <span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
 797              <?php endif; ?>
 798          </h1>
 799  
 800          <?php endif; ?>
 801  
 802          <?php // If the user has just made a change to an group, display the status messages. ?>
 803          <?php if ( !empty( $messages ) ) : ?>
 804              <div id="moderated" class="<?php echo ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
 805          <?php endif; ?>
 806  
 807          <?php // Display each group on its own row. ?>
 808          <?php $bp_groups_list_table->views(); ?>
 809  
 810          <form id="bp-groups-form" action="" method="get">
 811              <?php $bp_groups_list_table->search_box( __( 'Search all Groups', 'buddypress' ), 'bp-groups' ); ?>
 812              <input type="hidden" name="page" value="<?php echo esc_attr( $plugin_page ); ?>" />
 813              <?php $bp_groups_list_table->display(); ?>
 814          </form>
 815  
 816      </div>
 817  
 818  <?php
 819  }
 820  
 821  /**
 822   * Markup for the single group's Settings metabox.
 823   *
 824   * @since 1.7.0
 825   *
 826   * @param object $item Information about the current group.
 827   */
 828  function bp_groups_admin_edit_metabox_settings( $item ) {
 829  
 830      $invite_status = bp_group_get_invite_status( $item->id ); ?>
 831  
 832      <?php if ( bp_is_active( 'forums' ) ) : ?>
 833          <div class="bp-groups-settings-section" id="bp-groups-settings-section-forum">
 834              <label for="group-show-forum"><input type="checkbox" name="group-show-forum" id="group-show-forum" <?php checked( $item->enable_forum ) ?> /> <?php _e( 'Enable discussion forum', 'buddypress' ) ?></label>
 835          </div>
 836      <?php endif; ?>
 837  
 838      <div class="bp-groups-settings-section" id="bp-groups-settings-section-status">
 839          <fieldset>
 840              <legend><?php _e( 'Privacy', 'buddypress' ); ?></legend>
 841  
 842              <label for="bp-group-status-public"><input type="radio" name="group-status" id="bp-group-status-public" value="public" <?php checked( $item->status, 'public' ) ?> /><?php _e( 'Public', 'buddypress' ) ?></label>
 843              <label for="bp-group-status-private"><input type="radio" name="group-status" id="bp-group-status-private" value="private" <?php checked( $item->status, 'private' ) ?> /><?php _e( 'Private', 'buddypress' ) ?></label>
 844              <label for="bp-group-status-hidden"><input type="radio" name="group-status" id="bp-group-status-hidden" value="hidden" <?php checked( $item->status, 'hidden' ) ?> /><?php _e( 'Hidden', 'buddypress' ) ?></label>
 845          </fieldset>
 846      </div>
 847  
 848      <div class="bp-groups-settings-section" id="bp-groups-settings-section-invite-status">
 849          <fieldset>
 850              <legend><?php _e( 'Who can invite others to this group?', 'buddypress' ); ?></legend>
 851  
 852              <label for="bp-group-invite-status-members"><input type="radio" name="group-invite-status" id="bp-group-invite-status-members" value="members" <?php checked( $invite_status, 'members' ) ?> /><?php _e( 'All group members', 'buddypress' ) ?></label>
 853              <label for="bp-group-invite-status-mods"><input type="radio" name="group-invite-status" id="bp-group-invite-status-mods" value="mods" <?php checked( $invite_status, 'mods' ) ?> /><?php _e( 'Group admins and mods only', 'buddypress' ) ?></label>
 854              <label for="bp-group-invite-status-admins"><input type="radio" name="group-invite-status" id="bp-group-invite-status-admins" value="admins" <?php checked( $invite_status, 'admins' ) ?> /><?php _e( 'Group admins only', 'buddypress' ) ?></label>
 855          </fieldset>
 856      </div>
 857  
 858  <?php
 859  }
 860  
 861  /**
 862   * Output the markup for a single group's Add New Members metabox.
 863   *
 864   * @since 1.7.0
 865   *
 866   * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 867   */
 868  function bp_groups_admin_edit_metabox_add_new_members( $item ) {
 869      if ( bp_is_large_install() ) {
 870          $class  = '';
 871          $notice = __( 'Enter a comma-separated list of user logins.', 'buddypress' );
 872      } else {
 873          $class  = 'bp-suggest-user';
 874          $notice = '';
 875      }
 876  
 877      ?>
 878  
 879      <label for="bp-groups-new-members" class="screen-reader-text"><?php
 880          /* translators: accessibility text */
 881          _e( 'Add new members', 'buddypress' );
 882      ?></label>
 883      <input name="bp-groups-new-members" type="text" id="bp-groups-new-members" class="<?php echo esc_attr( $class ); ?>" placeholder="" />
 884      <?php if ( $notice ) : ?>
 885          <p class="description"><?php echo esc_html( $notice ); ?></p>
 886      <?php endif; ?>
 887      <ul id="bp-groups-new-members-list"></ul>
 888      <?php
 889  }
 890  
 891  /**
 892   * Renders the Members metabox on single group pages.
 893   *
 894   * @since 1.7.0
 895   *
 896   * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 897   */
 898  function bp_groups_admin_edit_metabox_members( $item ) {
 899      // Use the BP REST API if it supported.
 900      if ( bp_rest_api_is_available() && bp_groups_has_manage_group_members_templates() ) {
 901          wp_enqueue_script( 'bp-group-manage-members' );
 902          wp_localize_script(
 903              'bp-group-manage-members',
 904              'bpGroupManageMembersSettings',
 905              bp_groups_get_group_manage_members_script_data( $item->id )
 906          );
 907  
 908          bp_get_template_part( 'common/js-templates/group-members/index' );
 909  
 910          /**
 911           * Echo out the JavaScript variable.
 912           * This seems to be required by the autocompleter, leaving this here for now...
 913           */
 914          echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';
 915          return;
 916      }
 917  
 918      // Pull up a list of group members, so we can separate out the types
 919      // We'll also keep track of group members here to place them into a
 920      // JavaScript variable, which will help with group member autocomplete.
 921      $members = array(
 922          'admin'  => array(),
 923          'mod'    => array(),
 924          'member' => array(),
 925          'banned' => array(),
 926      );
 927  
 928      $pagination = array(
 929          'admin'  => array(),
 930          'mod'    => array(),
 931          'member' => array(),
 932          'banned' => array(),
 933      );
 934  
 935      foreach ( $members as $type => &$member_type_users ) {
 936          $page_qs_key       = $type . '_page';
 937          $current_type_page = isset( $_GET[ $page_qs_key ] ) ? absint( $_GET[ $page_qs_key ] ) : 1;
 938          $member_type_query = new BP_Group_Member_Query( array(
 939              'group_id'   => $item->id,
 940              'group_role' => array( $type ),
 941              'type'       => 'alphabetical',
 942              /**
 943               * Filters the admin members type per page value.
 944               *
 945               * @since 2.8.0
 946               *
 947               * @param int    $value Member types per page. Default 10.
 948               * @param string $type  Member type.
 949               */
 950              'per_page'   => apply_filters( 'bp_groups_admin_members_type_per_page', 10, $type ),
 951              'page'       => $current_type_page,
 952          ) );
 953  
 954          $member_type_users   = $member_type_query->results;
 955          $pagination[ $type ] = bp_groups_admin_create_pagination_links( $member_type_query, $type );
 956      }
 957  
 958      // Echo out the JavaScript variable.
 959      echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';
 960  
 961      // Loop through each member type.
 962      foreach ( $members as $member_type => $type_users ) : ?>
 963  
 964          <div class="bp-groups-member-type" id="bp-groups-member-type-<?php echo esc_attr( $member_type ) ?>">
 965  
 966              <h3><?php switch ( $member_type ) :
 967                      case 'admin'  : esc_html_e( 'Administrators', 'buddypress' ); break;
 968                      case 'mod'    : esc_html_e( 'Moderators',     'buddypress' ); break;
 969                      case 'member' : esc_html_e( 'Members',        'buddypress' ); break;
 970                      case 'banned' : esc_html_e( 'Banned Members', 'buddypress' ); break;
 971              endswitch; ?></h3>
 972  
 973              <div class="bp-group-admin-pagination table-top">
 974                  <?php echo $pagination[ $member_type ] ?>
 975              </div>
 976  
 977          <?php if ( !empty( $type_users ) ) : ?>
 978  
 979              <table class="widefat bp-group-members">
 980                  <thead>
 981                      <tr>
 982                          <th scope="col" class="uid-column"><?php _ex( 'ID', 'Group member user_id in group admin', 'buddypress' ); ?></th>
 983                          <th scope="col" class="uname-column"><?php _ex( 'Name', 'Group member name in group admin', 'buddypress' ); ?></th>
 984                          <th scope="col" class="urole-column"><?php _ex( 'Group Role', 'Group member role in group admin', 'buddypress' ); ?></th>
 985                      </tr>
 986                  </thead>
 987  
 988                  <tbody>
 989  
 990                  <?php foreach ( $type_users as $type_user ) : ?>
 991                      <tr>
 992                          <th scope="row" class="uid-column"><?php echo esc_html( $type_user->ID ); ?></th>
 993  
 994                          <td class="uname-column">
 995                              <a style="float: left;" href="<?php echo bp_core_get_user_domain( $type_user->ID ); ?>"><?php echo bp_core_fetch_avatar( array(
 996                                  'item_id' => $type_user->ID,
 997                                  'width'   => '32',
 998                                  'height'  => '32'
 999                              ) ); ?></a>
1000  
1001                              <span style="margin: 8px; float: left;"><?php echo bp_core_get_userlink( $type_user->ID ); ?></span>
1002                          </td>
1003  
1004                          <td class="urole-column">
1005                              <label for="bp-groups-role-<?php echo esc_attr( $type_user->ID ); ?>" class="screen-reader-text"><?php
1006                                  /* translators: accessibility text */
1007                                  _e( 'Select group role for member', 'buddypress' );
1008                              ?></label>
1009                              <select class="bp-groups-role" id="bp-groups-role-<?php echo esc_attr( $type_user->ID ); ?>" name="bp-groups-role[<?php echo esc_attr( $type_user->ID ); ?>]">
1010                                  <optgroup label="<?php esc_attr_e( 'Roles', 'buddypress' ); ?>">
1011                                      <option class="admin"  value="admin"  <?php selected( 'admin',  $member_type ); ?>><?php esc_html_e( 'Administrator', 'buddypress' ); ?></option>
1012                                      <option class="mod"    value="mod"    <?php selected( 'mod',    $member_type ); ?>><?php esc_html_e( 'Moderator',     'buddypress' ); ?></option>
1013                                      <option class="member" value="member" <?php selected( 'member', $member_type ); ?>><?php esc_html_e( 'Member',        'buddypress' ); ?></option>
1014                                      <?php if ( 'banned' === $member_type ) : ?>
1015                                      <option class="banned" value="banned" <?php selected( 'banned', $member_type ); ?>><?php esc_html_e( 'Banned',        'buddypress' ); ?></option>
1016                                      <?php endif; ?>
1017                                  </optgroup>
1018                                  <optgroup label="<?php esc_attr_e( 'Actions', 'buddypress' ); ?>">
1019                                      <option class="remove" value="remove"><?php esc_html_e( 'Remove', 'buddypress' ); ?></option>
1020                                      <?php if ( 'banned' !== $member_type ) : ?>
1021                                          <option class="banned" value="banned"><?php esc_html_e( 'Ban', 'buddypress' ); ?></option>
1022                                      <?php endif; ?>
1023                                  </optgroup>
1024                              </select>
1025  
1026                              <?php
1027                              /**
1028                               * Store the current role for this user,
1029                               * so we can easily detect changes.
1030                               *
1031                               * @todo remove this, and do database detection on save
1032                               */
1033                              ?>
1034                              <input type="hidden" name="bp-groups-existing-role[<?php echo esc_attr( $type_user->ID ); ?>]" value="<?php echo esc_attr( $member_type ); ?>" />
1035                          </td>
1036                      </tr>
1037  
1038                      <?php if ( has_filter( 'bp_groups_admin_manage_member_row' ) ) : ?>
1039                          <tr>
1040                              <td colspan="3">
1041                                  <?php
1042  
1043                                  /**
1044                                   * Fires after the listing of a single row for members in a group on the group edit screen.
1045                                   *
1046                                   * @since 1.8.0
1047                                   *
1048                                   * @param int             $ID   ID of the user being rendered.
1049                                   * @param BP_Groups_Group $item Object for the current group.
1050                                   */
1051                                  do_action( 'bp_groups_admin_manage_member_row', $type_user->ID, $item ); ?>
1052                              </td>
1053                          </tr>
1054                      <?php endif; ?>
1055  
1056                  <?php endforeach; ?>
1057  
1058                  </tbody>
1059              </table>
1060  
1061          <?php else : ?>
1062  
1063              <p class="bp-groups-no-members description"><?php esc_html_e( 'No members of this type', 'buddypress' ); ?></p>
1064  
1065          <?php endif; ?>
1066  
1067          </div><!-- .bp-groups-member-type -->
1068  
1069      <?php endforeach;
1070  }
1071  
1072  /**
1073   * Renders the Status metabox for the Groups admin edit screen.
1074   *
1075   * @since 1.7.0
1076   *
1077   * @param object $item Information about the currently displayed group.
1078   */
1079  function bp_groups_admin_edit_metabox_status( $item ) {
1080      $base_url = add_query_arg( array(
1081          'page' => 'bp-groups',
1082          'gid'  => $item->id
1083      ), bp_get_admin_url( 'admin.php' ) ); ?>
1084  
1085      <div id="submitcomment" class="submitbox">
1086          <div id="major-publishing-actions">
1087              <div id="delete-action">
1088                  <a class="submitdelete deletion" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'action', 'delete', $base_url ), 'bp-groups-delete' ) ); ?>"><?php _e( 'Delete Group', 'buddypress' ) ?></a>
1089              </div>
1090  
1091              <div id="publishing-action">
1092                  <?php submit_button( __( 'Save Changes', 'buddypress' ), 'primary', 'save', false ); ?>
1093              </div>
1094              <div class="clear"></div>
1095          </div><!-- #major-publishing-actions -->
1096      </div><!-- #submitcomment -->
1097  
1098  <?php
1099  }
1100  
1101  /**
1102   * Render the Group Type metabox.
1103   *
1104   * @since 2.6.0
1105   *
1106   * @param BP_Groups_Group|null $group The BP_Groups_Group object corresponding to the group being edited.
1107   */
1108  function bp_groups_admin_edit_metabox_group_type( BP_Groups_Group $group = null ) {
1109  
1110      // Bail if no group ID.
1111      if ( empty( $group->id ) ) {
1112          return;
1113      }
1114  
1115      $types         = bp_groups_get_group_types( array(), 'objects' );
1116      $current_types = (array) bp_groups_get_group_type( $group->id, false );
1117      $backend_only  = bp_groups_get_group_types( array( 'show_in_create_screen' => false ) );
1118      ?>
1119  
1120      <label for="bp-groups-group-type" class="screen-reader-text"><?php
1121          /* translators: accessibility text */
1122          esc_html_e( 'Select group type', 'buddypress' );
1123      ?></label>
1124  
1125      <ul class="categorychecklist form-no-clear">
1126          <?php foreach ( $types as $type ) : ?>
1127              <li>
1128                  <label class="selectit"><input value="<?php echo esc_attr( $type->name ) ?>" name="bp-groups-group-type[]" type="checkbox" <?php checked( true, in_array( $type->name, $current_types ) ); ?>>
1129                      <?php
1130                          echo esc_html( $type->labels['singular_name'] );
1131                          if ( in_array( $type->name, $backend_only ) ) {
1132                              printf( ' <span class="description">%s</span>', esc_html__( '(Not available on the front end)', 'buddypress' ) );
1133                          }
1134                      ?>
1135  
1136                  </label>
1137              </li>
1138  
1139          <?php endforeach; ?>
1140  
1141      </ul>
1142  
1143      <?php
1144  
1145      wp_nonce_field( 'bp-group-type-change-' . $group->id, 'bp-group-type-nonce' );
1146  }
1147  
1148  /**
1149   * Process changes from the Group Type metabox.
1150   *
1151   * @since 2.6.0
1152   *
1153   * @param int $group_id Group ID.
1154   */
1155  function bp_groups_process_group_type_update( $group_id ) {
1156      if ( ! isset( $_POST['bp-group-type-nonce'] ) ) {
1157          return;
1158      }
1159  
1160      check_admin_referer( 'bp-group-type-change-' . $group_id, 'bp-group-type-nonce' );
1161  
1162      // Permission check.
1163      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
1164          return;
1165      }
1166  
1167      $group_types = ! empty( $_POST['bp-groups-group-type'] ) ? wp_unslash( $_POST['bp-groups-group-type'] ) : array();
1168  
1169      /*
1170       * If an invalid group type is passed, someone's doing something
1171       * fishy with the POST request, so we can fail silently.
1172       */
1173      if ( bp_groups_set_group_type( $group_id, $group_types ) ) {
1174          // @todo Success messages can't be posted because other stuff happens on the page load.
1175      }
1176  }
1177  add_action( 'bp_group_admin_edit_after', 'bp_groups_process_group_type_update' );
1178  
1179  /**
1180   * Create pagination links out of a BP_Group_Member_Query.
1181   *
1182   * This function is intended to create pagination links for use under the
1183   * Manage Members section of the Groups Admin Dashboard pages. It is a stopgap
1184   * measure until a more general pagination solution is in place for BuddyPress.
1185   * Plugin authors should not use this function, as it is likely to be
1186   * deprecated soon.
1187   *
1188   * @since 1.8.0
1189   *
1190   * @param BP_Group_Member_Query $query       A BP_Group_Member_Query object.
1191   * @param string                $member_type member|mod|admin|banned.
1192   * @return string Pagination links HTML.
1193   */
1194  function bp_groups_admin_create_pagination_links( BP_Group_Member_Query $query, $member_type ) {
1195      $pagination = '';
1196  
1197      if ( ! in_array( $member_type, array( 'admin', 'mod', 'member', 'banned' ) ) ) {
1198          return $pagination;
1199      }
1200  
1201      // The key used to paginate this member type in the $_GET global.
1202      $qs_key   = $member_type . '_page';
1203      $url_base = remove_query_arg( array( $qs_key, 'updated', 'success_modified' ), $_SERVER['REQUEST_URI'] );
1204  
1205      $page = isset( $_GET[ $qs_key ] ) ? absint( $_GET[ $qs_key ] ) : 1;
1206  
1207      /**
1208       * Filters the number of members per member type that is displayed in group editing admin area.
1209       *
1210       * @since 2.8.0
1211       *
1212       * @param string $member_type Member type, which is a group role (admin, mod etc).
1213       */
1214      $per_page = apply_filters( 'bp_groups_admin_members_type_per_page', 10, $member_type );
1215  
1216      // Don't show anything if there's no pagination.
1217      if ( 1 === $page && $query->total_users <= $per_page ) {
1218          return $pagination;
1219      }
1220  
1221      $current_page_start = ( ( $page - 1 ) * $per_page ) + 1;
1222      $current_page_end   = $page * $per_page > intval( $query->total_users ) ? $query->total_users : $page * $per_page;
1223  
1224      $pag_links = paginate_links( array(
1225          'base'      => add_query_arg( $qs_key, '%#%', $url_base ),
1226          'format'    => '',
1227          'prev_text' => __( '&laquo;', 'buddypress' ),
1228          'next_text' => __( '&raquo;', 'buddypress' ),
1229          'total'     => ceil( $query->total_users / $per_page ),
1230          'current'   => $page,
1231      ) );
1232  
1233      if ( 1 == $query->total_users ) {
1234          $viewing_text = __( 'Viewing 1 member', 'buddypress' );
1235      } else {
1236          $viewing_text = sprintf(
1237              /* translators: 1: group member from number. 2: group member to number. 3: total group members. */
1238              _nx( 'Viewing %1$s - %2$s of %3$s member', 'Viewing %1$s - %2$s of %3$s members', $query->total_users, 'Group members pagination in group admin', 'buddypress' ),
1239              bp_core_number_format( $current_page_start ),
1240              bp_core_number_format( $current_page_end ),
1241              bp_core_number_format( $query->total_users )
1242          );
1243      }
1244  
1245      $pagination .= '<span class="bp-group-admin-pagination-viewing">' . $viewing_text . '</span>';
1246      $pagination .= '<span class="bp-group-admin-pagination-links">' . $pag_links . '</span>';
1247  
1248      return $pagination;
1249  }
1250  
1251  /**
1252   * Get a set of usernames corresponding to a set of user IDs.
1253   *
1254   * @since 1.7.0
1255   *
1256   * @param array $user_ids Array of user IDs.
1257   * @return array Array of user_logins corresponding to $user_ids.
1258   */
1259  function bp_groups_admin_get_usernames_from_ids( $user_ids = array() ) {
1260  
1261      $usernames = array();
1262      $users     = new WP_User_Query( array( 'blog_id' => 0, 'include' => $user_ids ) );
1263  
1264      foreach ( (array) $users->results as $user ) {
1265          $usernames[] = $user->user_login;
1266      }
1267  
1268      return $usernames;
1269  }
1270  
1271  /**
1272   * AJAX handler for group member autocomplete requests.
1273   *
1274   * @since 1.7.0
1275   */
1276  function bp_groups_admin_autocomplete_handler() {
1277  
1278      // Bail if user user shouldn't be here, or is a large network.
1279      if ( ! bp_current_user_can( 'bp_moderate' ) || bp_is_large_install() ) {
1280          wp_die( -1 );
1281      }
1282  
1283      $term     = isset( $_GET['term'] )     ? sanitize_text_field( $_GET['term'] ) : '';
1284      $group_id = isset( $_GET['group_id'] ) ? absint( $_GET['group_id'] )          : 0;
1285  
1286      if ( ! $term || ! $group_id ) {
1287          wp_die( -1 );
1288      }
1289  
1290      $suggestions = bp_core_get_suggestions( array(
1291          'group_id' => -$group_id,  // A negative value will exclude this group's members from the suggestions.
1292          'limit'    => 10,
1293          'term'     => $term,
1294          'type'     => 'members',
1295      ) );
1296  
1297      $matches = array();
1298  
1299      if ( $suggestions && ! is_wp_error( $suggestions ) ) {
1300          foreach ( $suggestions as $user ) {
1301  
1302              $matches[] = array(
1303                  // Translators: 1: user_login, 2: user_email.
1304                  'label' => sprintf( __( '%1$s (%2$s)', 'buddypress' ), $user->name, $user->ID ),
1305                  'value' => $user->ID,
1306              );
1307          }
1308      }
1309  
1310      wp_die( json_encode( $matches ) );
1311  }
1312  add_action( 'wp_ajax_bp_group_admin_member_autocomplete', 'bp_groups_admin_autocomplete_handler' );
1313  
1314  /**
1315   * Process input from the Group Type bulk change select.
1316   *
1317   * @since 2.7.0
1318   *
1319   * @param string $doaction Current $_GET action being performed in admin screen.
1320   */
1321  function bp_groups_admin_process_group_type_bulk_changes( $doaction ) {
1322      // Bail if no groups are specified or if this isn't a relevant action.
1323      if ( empty( $_REQUEST['gid'] )
1324          || ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
1325          || empty( $_REQUEST['bp_change_group_type'] )
1326      ) {
1327          return;
1328      }
1329  
1330      // Bail if nonce check fails.
1331      check_admin_referer( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
1332  
1333      if ( ! bp_current_user_can( 'bp_moderate' )  ) {
1334          return;
1335      }
1336  
1337      $new_type = '';
1338      if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
1339          $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
1340      } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
1341          $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
1342      }
1343  
1344      // Check that the selected type actually exists.
1345      if ( 'remove_group_type' !== $new_type && null === bp_groups_get_group_type_object( $new_type ) ) {
1346          $error = true;
1347      } else {
1348          // Run through group ids.
1349          $error = false;
1350          foreach ( (array) $_REQUEST['gid'] as $group_id ) {
1351              $group_id = (int) $group_id;
1352  
1353              // Get the old group type to check against.
1354              $group_type = bp_groups_get_group_type( $group_id );
1355  
1356              if ( 'remove_group_type' === $new_type ) {
1357                  // Remove the current group type, if there's one to remove.
1358                  if ( $group_type ) {
1359                      $removed = bp_groups_remove_group_type( $group_id, $group_type );
1360                      if ( false === $removed || is_wp_error( $removed ) ) {
1361                          $error = true;
1362                      }
1363                  }
1364              } else {
1365                  // Set the new group type.
1366                  if ( $new_type !== $group_type ) {
1367                      $set = bp_groups_set_group_type( $group_id, $new_type );
1368                      if ( false === $set || is_wp_error( $set ) ) {
1369                          $error = true;
1370                      }
1371                  }
1372              }
1373          }
1374      }
1375  
1376      // If there were any errors, show the error message.
1377      if ( $error ) {
1378          $redirect = add_query_arg( array( 'updated' => 'group-type-change-error' ), wp_get_referer() );
1379      } else {
1380          $redirect = add_query_arg( array( 'updated' => 'group-type-change-success' ), wp_get_referer() );
1381      }
1382  
1383      wp_redirect( $redirect );
1384      exit();
1385  }
1386  add_action( 'bp_groups_admin_load', 'bp_groups_admin_process_group_type_bulk_changes' );
1387  
1388  /**
1389   * Display an admin notice upon group type bulk update.
1390   *
1391   * @since 2.7.0
1392   */
1393  function bp_groups_admin_groups_type_change_notice() {
1394      $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
1395  
1396      // Display feedback.
1397      if ( $updated && in_array( $updated, array( 'group-type-change-error', 'group-type-change-success' ), true ) ) {
1398  
1399          if ( 'group-type-change-error' === $updated ) {
1400              $notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
1401              $type   = 'error';
1402          } else {
1403              $notice = __( 'Group type was changed successfully.', 'buddypress' );
1404              $type   = 'updated';
1405          }
1406  
1407          bp_core_add_admin_notice( $notice, $type );
1408      }
1409  }
1410  add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );


Generated: Tue Apr 7 01:01:33 2020 Cross-referenced by PHPXref 0.7.1