[ 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              $messages[] = sprintf( __( 'The following users could not be added to the group: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $error_new ) ) . '</em>' );
 541          }
 542  
 543          if ( ! empty( $success_new ) ) {
 544              $messages[] = sprintf( __( 'The following users were successfully added to the group: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $success_new ) ) . '</em>' );
 545          }
 546  
 547          if ( ! empty( $error_modified ) ) {
 548              $error_modified = bp_groups_admin_get_usernames_from_ids( $error_modified );
 549              $messages[] = sprintf( __( 'An error occurred when trying to modify the following members: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $error_modified ) ) . '</em>' );
 550          }
 551  
 552          if ( ! empty( $success_modified ) ) {
 553              $success_modified = bp_groups_admin_get_usernames_from_ids( $success_modified );
 554              $messages[] = sprintf( __( 'The following members were successfully modified: %s', 'buddypress' ), '<em>' . esc_html( implode( ', ', $success_modified ) ) . '</em>' );
 555          }
 556      }
 557  
 558      $is_error = ! empty( $no_admins ) || ! empty( $errors ) || ! empty( $error_new ) || ! empty( $error_modified );
 559  
 560      // Get the group from the database.
 561      $group      = groups_get_group( (int) $_GET['gid'] );
 562  
 563      $group_name = isset( $group->name ) ? bp_get_group_name( $group ) : '';
 564  
 565      // Construct URL for form.
 566      $form_url = remove_query_arg( array( 'action', 'deleted', 'no_admins', 'error', 'error_new', 'success_new', 'error_modified', 'success_modified' ), $_SERVER['REQUEST_URI'] );
 567      $form_url = add_query_arg( 'action', 'save', $form_url );
 568  
 569      /**
 570       * Fires before the display of the edit form.
 571       *
 572       * Useful for plugins to modify the group before display.
 573       *
 574       * @since 1.7.0
 575       *
 576       * @param BP_Groups_Group $this Instance of the current group being edited. Passed by reference.
 577       */
 578      do_action_ref_array( 'bp_groups_admin_edit', array( &$group ) ); ?>
 579  
 580      <div class="wrap">
 581          <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 582  
 583              <h1 class="wp-heading-inline"><?php _e( 'Edit Group', 'buddypress' ); ?></h1>
 584  
 585              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 586                  <a class="page-title-action" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 587              <?php endif; ?>
 588  
 589              <hr class="wp-header-end">
 590  
 591          <?php else : ?>
 592  
 593              <h1><?php _e( 'Edit Group', 'buddypress' ); ?>
 594  
 595                  <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 596                      <a class="add-new-h2" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 597                  <?php endif; ?>
 598  
 599              </h1>
 600  
 601          <?php endif; ?>
 602  
 603          <?php // If the user has just made a change to an group, display the status messages. ?>
 604          <?php if ( !empty( $messages ) ) : ?>
 605              <div id="moderated" class="<?php echo ( $is_error ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "</p><p>", $messages ); ?></p></div>
 606          <?php endif; ?>
 607  
 608          <?php if ( $group->id ) : ?>
 609  
 610              <form action="<?php echo esc_url( $form_url ); ?>" id="bp-groups-edit-form" method="post">
 611                  <div id="poststuff">
 612  
 613                      <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
 614                          <div id="post-body-content">
 615                              <div id="postdiv">
 616                                  <div id="bp_groups_name" class="postbox">
 617                                      <h2><?php _e( 'Name and Description', 'buddypress' ); ?></h2>
 618                                      <div class="inside">
 619                                          <label for="bp-groups-name" class="screen-reader-text"><?php
 620                                              /* translators: accessibility text */
 621                                              _e( 'Group Name', 'buddypress' );
 622                                          ?></label>
 623                                          <input type="text" name="bp-groups-name" id="bp-groups-name" value="<?php echo esc_attr( stripslashes( $group_name ) ) ?>" />
 624                                          <div id="bp-groups-permalink-box">
 625                                              <strong><?php esc_html_e( 'Permalink:', 'buddypress' ) ?></strong>
 626                                              <span id="bp-groups-permalink">
 627                                                  <?php bp_groups_directory_permalink(); ?> <input type="text" id="bp-groups-slug" name="bp-groups-slug" value="<?php bp_group_slug( $group ); ?>" autocomplete="off"> /
 628                                              </span>
 629                                              <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>
 630                                          </div>
 631  
 632                                          <label for="bp-groups-description" class="screen-reader-text"><?php
 633                                              /* translators: accessibility text */
 634                                              _e( 'Group Description', 'buddypress' );
 635                                          ?></label>
 636                                          <?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' ) ) ); ?>
 637                                      </div>
 638                                  </div>
 639                              </div>
 640                          </div><!-- #post-body-content -->
 641  
 642                          <div id="postbox-container-1" class="postbox-container">
 643                              <?php do_meta_boxes( get_current_screen()->id, 'side', $group ); ?>
 644                          </div>
 645  
 646                          <div id="postbox-container-2" class="postbox-container">
 647                              <?php do_meta_boxes( get_current_screen()->id, 'normal', $group ); ?>
 648                              <?php do_meta_boxes( get_current_screen()->id, 'advanced', $group ); ?>
 649                          </div>
 650                      </div><!-- #post-body -->
 651  
 652                  </div><!-- #poststuff -->
 653                  <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
 654                  <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
 655                  <?php wp_nonce_field( 'edit-group_' . $group->id ); ?>
 656              </form>
 657  
 658          <?php else : ?>
 659  
 660              <p><?php
 661                  printf(
 662                      '%1$s <a href="%2$s">%3$s</a>',
 663                      __( 'No group found with this ID.', 'buddypress' ),
 664                      esc_url( bp_get_admin_url( 'admin.php?page=bp-groups' ) ),
 665                      __( 'Go back and try again.', 'buddypress' )
 666                  );
 667              ?></p>
 668  
 669          <?php endif; ?>
 670  
 671      </div><!-- .wrap -->
 672  
 673  <?php
 674  }
 675  
 676  /**
 677   * Display the Group delete confirmation screen.
 678   *
 679   * We include a separate confirmation because group deletion is truly
 680   * irreversible.
 681   *
 682   * @since 1.7.0
 683   */
 684  function bp_groups_admin_delete() {
 685  
 686      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
 687          die( '-1' );
 688      }
 689  
 690      $group_ids = isset( $_REQUEST['gid'] ) ? $_REQUEST['gid'] : 0;
 691      if ( ! is_array( $group_ids ) ) {
 692          $group_ids = explode( ',', $group_ids );
 693      }
 694      $group_ids = wp_parse_id_list( $group_ids );
 695      $groups    = groups_get_groups( array(
 696          'include'     => $group_ids,
 697          'show_hidden' => true,
 698          'per_page'    => null, // Return all results.
 699      ) );
 700  
 701      // Create a new list of group ids, based on those that actually exist.
 702      $gids = array();
 703      foreach ( $groups['groups'] as $group ) {
 704          $gids[] = $group->id;
 705      }
 706  
 707      $base_url  = remove_query_arg( array( 'action', 'action2', 'paged', 's', '_wpnonce', 'gid' ), $_SERVER['REQUEST_URI'] ); ?>
 708  
 709      <div class="wrap">
 710          <h1><?php _e( 'Delete Groups', 'buddypress' ) ?></h1>
 711          <p><?php _e( 'You are about to delete the following groups:', 'buddypress' ) ?></p>
 712  
 713          <ul class="bp-group-delete-list">
 714          <?php foreach ( $groups['groups'] as $group ) : ?>
 715              <li><?php echo esc_html( bp_get_group_name( $group ) ); ?></li>
 716          <?php endforeach; ?>
 717          </ul>
 718  
 719          <p><strong><?php _e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>
 720  
 721          <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>
 722          <a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php _e( 'Cancel', 'buddypress' ) ?></a>
 723      </div>
 724  
 725      <?php
 726  }
 727  
 728  /**
 729   * Display the Groups admin index screen.
 730   *
 731   * This screen contains a list of all BuddyPress groups.
 732   *
 733   * @since 1.7.0
 734   *
 735   * @global BP_Groups_List_Table $bp_groups_list_table Group screen list table.
 736   * @global string $plugin_page Currently viewed plugin page.
 737   */
 738  function bp_groups_admin_index() {
 739      global $bp_groups_list_table, $plugin_page;
 740  
 741      $messages = array();
 742  
 743      // If the user has just made a change to a group, build status messages.
 744      if ( ! empty( $_REQUEST['deleted'] ) ) {
 745          $deleted  = ! empty( $_REQUEST['deleted'] ) ? (int) $_REQUEST['deleted'] : 0;
 746  
 747          if ( $deleted > 0 ) {
 748              $messages[] = sprintf( _n( '%s group has been permanently deleted.', '%s groups have been permanently deleted.', $deleted, 'buddypress' ), number_format_i18n( $deleted ) );
 749          }
 750      }
 751  
 752      // Prepare the group items for display.
 753      $bp_groups_list_table->prepare_items();
 754  
 755      /**
 756       * Fires before the display of messages for the edit form.
 757       *
 758       * Useful for plugins to modify the messages before display.
 759       *
 760       * @since 1.7.0
 761       *
 762       * @param array $messages Array of messages to be displayed.
 763       */
 764      do_action( 'bp_groups_admin_index', $messages ); ?>
 765  
 766      <div class="wrap">
 767          <?php if ( version_compare( $GLOBALS['wp_version'], '4.8', '>=' ) ) : ?>
 768  
 769              <h1 class="wp-heading-inline"><?php _e( 'Groups', 'buddypress' ); ?></h1>
 770  
 771              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 772                  <a class="page-title-action" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 773              <?php endif; ?>
 774  
 775              <?php if ( !empty( $_REQUEST['s'] ) ) : ?>
 776                  <span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
 777              <?php endif; ?>
 778  
 779              <hr class="wp-header-end">
 780  
 781          <?php else : ?>
 782  
 783          <h1>
 784              <?php _e( 'Groups', 'buddypress' ); ?>
 785  
 786              <?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
 787                  <a class="add-new-h2" href="<?php echo trailingslashit( bp_get_groups_directory_permalink() . 'create' ); ?>"><?php _e( 'Add New', 'buddypress' ); ?></a>
 788              <?php endif; ?>
 789  
 790              <?php if ( !empty( $_REQUEST['s'] ) ) : ?>
 791                  <span class="subtitle"><?php printf( __( 'Search results for &#8220;%s&#8221;', 'buddypress' ), wp_html_excerpt( esc_html( stripslashes( $_REQUEST['s'] ) ), 50 ) ); ?></span>
 792              <?php endif; ?>
 793          </h1>
 794  
 795          <?php endif; ?>
 796  
 797          <?php // If the user has just made a change to an group, display the status messages. ?>
 798          <?php if ( !empty( $messages ) ) : ?>
 799              <div id="moderated" class="<?php echo ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated'; ?>"><p><?php echo implode( "<br/>\n", $messages ); ?></p></div>
 800          <?php endif; ?>
 801  
 802          <?php // Display each group on its own row. ?>
 803          <?php $bp_groups_list_table->views(); ?>
 804  
 805          <form id="bp-groups-form" action="" method="get">
 806              <?php $bp_groups_list_table->search_box( __( 'Search all Groups', 'buddypress' ), 'bp-groups' ); ?>
 807              <input type="hidden" name="page" value="<?php echo esc_attr( $plugin_page ); ?>" />
 808              <?php $bp_groups_list_table->display(); ?>
 809          </form>
 810  
 811      </div>
 812  
 813  <?php
 814  }
 815  
 816  /**
 817   * Markup for the single group's Settings metabox.
 818   *
 819   * @since 1.7.0
 820   *
 821   * @param object $item Information about the current group.
 822   */
 823  function bp_groups_admin_edit_metabox_settings( $item ) {
 824  
 825      $invite_status = bp_group_get_invite_status( $item->id ); ?>
 826  
 827      <?php if ( bp_is_active( 'forums' ) ) : ?>
 828          <div class="bp-groups-settings-section" id="bp-groups-settings-section-forum">
 829              <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>
 830          </div>
 831      <?php endif; ?>
 832  
 833      <div class="bp-groups-settings-section" id="bp-groups-settings-section-status">
 834          <fieldset>
 835              <legend><?php _e( 'Privacy', 'buddypress' ); ?></legend>
 836  
 837              <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>
 838              <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>
 839              <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>
 840          </fieldset>
 841      </div>
 842  
 843      <div class="bp-groups-settings-section" id="bp-groups-settings-section-invite-status">
 844          <fieldset>
 845              <legend><?php _e( 'Who can invite others to this group?', 'buddypress' ); ?></legend>
 846  
 847              <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>
 848              <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>
 849              <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>
 850          </fieldset>
 851      </div>
 852  
 853  <?php
 854  }
 855  
 856  /**
 857   * Output the markup for a single group's Add New Members metabox.
 858   *
 859   * @since 1.7.0
 860   *
 861   * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 862   */
 863  function bp_groups_admin_edit_metabox_add_new_members( $item ) {
 864      if ( bp_is_large_install() ) {
 865          $class  = '';
 866          $notice = __( 'Enter a comma-separated list of user logins.', 'buddypress' );
 867      } else {
 868          $class  = 'bp-suggest-user';
 869          $notice = '';
 870      }
 871  
 872      ?>
 873  
 874      <label for="bp-groups-new-members" class="screen-reader-text"><?php
 875          /* translators: accessibility text */
 876          _e( 'Add new members', 'buddypress' );
 877      ?></label>
 878      <input name="bp-groups-new-members" type="text" id="bp-groups-new-members" class="<?php echo esc_attr( $class ); ?>" placeholder="" />
 879      <?php if ( $notice ) : ?>
 880          <p class="description"><?php echo esc_html( $notice ); ?></p>
 881      <?php endif; ?>
 882      <ul id="bp-groups-new-members-list"></ul>
 883      <?php
 884  }
 885  
 886  /**
 887   * Renders the Members metabox on single group pages.
 888   *
 889   * @since 1.7.0
 890   *
 891   * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 892   */
 893  function bp_groups_admin_edit_metabox_members( $item ) {
 894      // Use the BP REST API if it supported.
 895      if ( bp_rest_api_is_available() && bp_groups_has_manage_group_members_templates() ) {
 896          wp_enqueue_script( 'bp-group-manage-members' );
 897          wp_localize_script(
 898              'bp-group-manage-members',
 899              'bpGroupManageMembersSettings',
 900              bp_groups_get_group_manage_members_script_data( $item->id )
 901          );
 902  
 903          bp_get_template_part( 'common/js-templates/group-members/index' );
 904  
 905          /**
 906           * Echo out the JavaScript variable.
 907           * This seems to be required by the autocompleter, leaving this here for now...
 908           */
 909          echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';
 910          return;
 911      }
 912  
 913      // Pull up a list of group members, so we can separate out the types
 914      // We'll also keep track of group members here to place them into a
 915      // JavaScript variable, which will help with group member autocomplete.
 916      $members = array(
 917          'admin'  => array(),
 918          'mod'    => array(),
 919          'member' => array(),
 920          'banned' => array(),
 921      );
 922  
 923      $pagination = array(
 924          'admin'  => array(),
 925          'mod'    => array(),
 926          'member' => array(),
 927          'banned' => array(),
 928      );
 929  
 930      foreach ( $members as $type => &$member_type_users ) {
 931          $page_qs_key       = $type . '_page';
 932          $current_type_page = isset( $_GET[ $page_qs_key ] ) ? absint( $_GET[ $page_qs_key ] ) : 1;
 933          $member_type_query = new BP_Group_Member_Query( array(
 934              'group_id'   => $item->id,
 935              'group_role' => array( $type ),
 936              'type'       => 'alphabetical',
 937              /**
 938               * Filters the admin members type per page value.
 939               *
 940               * @since 2.8.0
 941               *
 942               * @param int    $value Member types per page. Default 10.
 943               * @param string $type  Member type.
 944               */
 945              'per_page'   => apply_filters( 'bp_groups_admin_members_type_per_page', 10, $type ),
 946              'page'       => $current_type_page,
 947          ) );
 948  
 949          $member_type_users   = $member_type_query->results;
 950          $pagination[ $type ] = bp_groups_admin_create_pagination_links( $member_type_query, $type );
 951      }
 952  
 953      // Echo out the JavaScript variable.
 954      echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';
 955  
 956      // Loop through each member type.
 957      foreach ( $members as $member_type => $type_users ) : ?>
 958  
 959          <div class="bp-groups-member-type" id="bp-groups-member-type-<?php echo esc_attr( $member_type ) ?>">
 960  
 961              <h3><?php switch ( $member_type ) :
 962                      case 'admin'  : esc_html_e( 'Administrators', 'buddypress' ); break;
 963                      case 'mod'    : esc_html_e( 'Moderators',     'buddypress' ); break;
 964                      case 'member' : esc_html_e( 'Members',        'buddypress' ); break;
 965                      case 'banned' : esc_html_e( 'Banned Members', 'buddypress' ); break;
 966              endswitch; ?></h3>
 967  
 968              <div class="bp-group-admin-pagination table-top">
 969                  <?php echo $pagination[ $member_type ] ?>
 970              </div>
 971  
 972          <?php if ( !empty( $type_users ) ) : ?>
 973  
 974              <table class="widefat bp-group-members">
 975                  <thead>
 976                      <tr>
 977                          <th scope="col" class="uid-column"><?php _ex( 'ID', 'Group member user_id in group admin', 'buddypress' ); ?></th>
 978                          <th scope="col" class="uname-column"><?php _ex( 'Name', 'Group member name in group admin', 'buddypress' ); ?></th>
 979                          <th scope="col" class="urole-column"><?php _ex( 'Group Role', 'Group member role in group admin', 'buddypress' ); ?></th>
 980                      </tr>
 981                  </thead>
 982  
 983                  <tbody>
 984  
 985                  <?php foreach ( $type_users as $type_user ) : ?>
 986                      <tr>
 987                          <th scope="row" class="uid-column"><?php echo esc_html( $type_user->ID ); ?></th>
 988  
 989                          <td class="uname-column">
 990                              <a style="float: left;" href="<?php echo bp_core_get_user_domain( $type_user->ID ); ?>"><?php echo bp_core_fetch_avatar( array(
 991                                  'item_id' => $type_user->ID,
 992                                  'width'   => '32',
 993                                  'height'  => '32'
 994                              ) ); ?></a>
 995  
 996                              <span style="margin: 8px; float: left;"><?php echo bp_core_get_userlink( $type_user->ID ); ?></span>
 997                          </td>
 998  
 999                          <td class="urole-column">
1000                              <label for="bp-groups-role-<?php echo esc_attr( $type_user->ID ); ?>" class="screen-reader-text"><?php
1001                                  /* translators: accessibility text */
1002                                  _e( 'Select group role for member', 'buddypress' );
1003                              ?></label>
1004                              <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 ); ?>]">
1005                                  <optgroup label="<?php esc_attr_e( 'Roles', 'buddypress' ); ?>">
1006                                      <option class="admin"  value="admin"  <?php selected( 'admin',  $member_type ); ?>><?php esc_html_e( 'Administrator', 'buddypress' ); ?></option>
1007                                      <option class="mod"    value="mod"    <?php selected( 'mod',    $member_type ); ?>><?php esc_html_e( 'Moderator',     'buddypress' ); ?></option>
1008                                      <option class="member" value="member" <?php selected( 'member', $member_type ); ?>><?php esc_html_e( 'Member',        'buddypress' ); ?></option>
1009                                      <?php if ( 'banned' === $member_type ) : ?>
1010                                      <option class="banned" value="banned" <?php selected( 'banned', $member_type ); ?>><?php esc_html_e( 'Banned',        'buddypress' ); ?></option>
1011                                      <?php endif; ?>
1012                                  </optgroup>
1013                                  <optgroup label="<?php esc_attr_e( 'Actions', 'buddypress' ); ?>">
1014                                      <option class="remove" value="remove"><?php esc_html_e( 'Remove', 'buddypress' ); ?></option>
1015                                      <?php if ( 'banned' !== $member_type ) : ?>
1016                                          <option class="banned" value="banned"><?php esc_html_e( 'Ban', 'buddypress' ); ?></option>
1017                                      <?php endif; ?>
1018                                  </optgroup>
1019                              </select>
1020  
1021                              <?php
1022                              /**
1023                               * Store the current role for this user,
1024                               * so we can easily detect changes.
1025                               *
1026                               * @todo remove this, and do database detection on save
1027                               */
1028                              ?>
1029                              <input type="hidden" name="bp-groups-existing-role[<?php echo esc_attr( $type_user->ID ); ?>]" value="<?php echo esc_attr( $member_type ); ?>" />
1030                          </td>
1031                      </tr>
1032  
1033                      <?php if ( has_filter( 'bp_groups_admin_manage_member_row' ) ) : ?>
1034                          <tr>
1035                              <td colspan="3">
1036                                  <?php
1037  
1038                                  /**
1039                                   * Fires after the listing of a single row for members in a group on the group edit screen.
1040                                   *
1041                                   * @since 1.8.0
1042                                   *
1043                                   * @param int             $ID   ID of the user being rendered.
1044                                   * @param BP_Groups_Group $item Object for the current group.
1045                                   */
1046                                  do_action( 'bp_groups_admin_manage_member_row', $type_user->ID, $item ); ?>
1047                              </td>
1048                          </tr>
1049                      <?php endif; ?>
1050  
1051                  <?php endforeach; ?>
1052  
1053                  </tbody>
1054              </table>
1055  
1056          <?php else : ?>
1057  
1058              <p class="bp-groups-no-members description"><?php esc_html_e( 'No members of this type', 'buddypress' ); ?></p>
1059  
1060          <?php endif; ?>
1061  
1062          </div><!-- .bp-groups-member-type -->
1063  
1064      <?php endforeach;
1065  }
1066  
1067  /**
1068   * Renders the Status metabox for the Groups admin edit screen.
1069   *
1070   * @since 1.7.0
1071   *
1072   * @param object $item Information about the currently displayed group.
1073   */
1074  function bp_groups_admin_edit_metabox_status( $item ) {
1075      $base_url = add_query_arg( array(
1076          'page' => 'bp-groups',
1077          'gid'  => $item->id
1078      ), bp_get_admin_url( 'admin.php' ) ); ?>
1079  
1080      <div id="submitcomment" class="submitbox">
1081          <div id="major-publishing-actions">
1082              <div id="delete-action">
1083                  <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>
1084              </div>
1085  
1086              <div id="publishing-action">
1087                  <?php submit_button( __( 'Save Changes', 'buddypress' ), 'primary', 'save', false ); ?>
1088              </div>
1089              <div class="clear"></div>
1090          </div><!-- #major-publishing-actions -->
1091      </div><!-- #submitcomment -->
1092  
1093  <?php
1094  }
1095  
1096  /**
1097   * Render the Group Type metabox.
1098   *
1099   * @since 2.6.0
1100   *
1101   * @param BP_Groups_Group|null $group The BP_Groups_Group object corresponding to the group being edited.
1102   */
1103  function bp_groups_admin_edit_metabox_group_type( BP_Groups_Group $group = null ) {
1104  
1105      // Bail if no group ID.
1106      if ( empty( $group->id ) ) {
1107          return;
1108      }
1109  
1110      $types         = bp_groups_get_group_types( array(), 'objects' );
1111      $current_types = (array) bp_groups_get_group_type( $group->id, false );
1112      $backend_only  = bp_groups_get_group_types( array( 'show_in_create_screen' => false ) );
1113      ?>
1114  
1115      <label for="bp-groups-group-type" class="screen-reader-text"><?php
1116          /* translators: accessibility text */
1117          esc_html_e( 'Select group type', 'buddypress' );
1118      ?></label>
1119  
1120      <ul class="categorychecklist form-no-clear">
1121          <?php foreach ( $types as $type ) : ?>
1122              <li>
1123                  <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 ) ); ?>>
1124                      <?php
1125                          echo esc_html( $type->labels['singular_name'] );
1126                          if ( in_array( $type->name, $backend_only ) ) {
1127                              printf( ' <span class="description">%s</span>', esc_html__( '(Not available on the front end)', 'buddypress' ) );
1128                          }
1129                      ?>
1130  
1131                  </label>
1132              </li>
1133  
1134          <?php endforeach; ?>
1135  
1136      </ul>
1137  
1138      <?php
1139  
1140      wp_nonce_field( 'bp-group-type-change-' . $group->id, 'bp-group-type-nonce' );
1141  }
1142  
1143  /**
1144   * Process changes from the Group Type metabox.
1145   *
1146   * @since 2.6.0
1147   *
1148   * @param int $group_id Group ID.
1149   */
1150  function bp_groups_process_group_type_update( $group_id ) {
1151      if ( ! isset( $_POST['bp-group-type-nonce'] ) ) {
1152          return;
1153      }
1154  
1155      check_admin_referer( 'bp-group-type-change-' . $group_id, 'bp-group-type-nonce' );
1156  
1157      // Permission check.
1158      if ( ! bp_current_user_can( 'bp_moderate' ) ) {
1159          return;
1160      }
1161  
1162      $group_types = ! empty( $_POST['bp-groups-group-type'] ) ? wp_unslash( $_POST['bp-groups-group-type'] ) : array();
1163  
1164      /*
1165       * If an invalid group type is passed, someone's doing something
1166       * fishy with the POST request, so we can fail silently.
1167       */
1168      if ( bp_groups_set_group_type( $group_id, $group_types ) ) {
1169          // @todo Success messages can't be posted because other stuff happens on the page load.
1170      }
1171  }
1172  add_action( 'bp_group_admin_edit_after', 'bp_groups_process_group_type_update' );
1173  
1174  /**
1175   * Create pagination links out of a BP_Group_Member_Query.
1176   *
1177   * This function is intended to create pagination links for use under the
1178   * Manage Members section of the Groups Admin Dashboard pages. It is a stopgap
1179   * measure until a more general pagination solution is in place for BuddyPress.
1180   * Plugin authors should not use this function, as it is likely to be
1181   * deprecated soon.
1182   *
1183   * @since 1.8.0
1184   *
1185   * @param BP_Group_Member_Query $query       A BP_Group_Member_Query object.
1186   * @param string                $member_type member|mod|admin|banned.
1187   * @return string Pagination links HTML.
1188   */
1189  function bp_groups_admin_create_pagination_links( BP_Group_Member_Query $query, $member_type ) {
1190      $pagination = '';
1191  
1192      if ( ! in_array( $member_type, array( 'admin', 'mod', 'member', 'banned' ) ) ) {
1193          return $pagination;
1194      }
1195  
1196      // The key used to paginate this member type in the $_GET global.
1197      $qs_key   = $member_type . '_page';
1198      $url_base = remove_query_arg( array( $qs_key, 'updated', 'success_modified' ), $_SERVER['REQUEST_URI'] );
1199  
1200      $page = isset( $_GET[ $qs_key ] ) ? absint( $_GET[ $qs_key ] ) : 1;
1201  
1202      /**
1203       * Filters the number of members per member type that is displayed in group editing admin area.
1204       *
1205       * @since 2.8.0
1206       *
1207       * @param string $member_type Member type, which is a group role (admin, mod etc).
1208       */
1209      $per_page = apply_filters( 'bp_groups_admin_members_type_per_page', 10, $member_type );
1210  
1211      // Don't show anything if there's no pagination.
1212      if ( 1 === $page && $query->total_users <= $per_page ) {
1213          return $pagination;
1214      }
1215  
1216      $current_page_start = ( ( $page - 1 ) * $per_page ) + 1;
1217      $current_page_end   = $page * $per_page > intval( $query->total_users ) ? $query->total_users : $page * $per_page;
1218  
1219      $pag_links = paginate_links( array(
1220          'base'      => add_query_arg( $qs_key, '%#%', $url_base ),
1221          'format'    => '',
1222          'prev_text' => __( '&laquo;', 'buddypress' ),
1223          'next_text' => __( '&raquo;', 'buddypress' ),
1224          'total'     => ceil( $query->total_users / $per_page ),
1225          'current'   => $page,
1226      ) );
1227  
1228      if ( 1 == $query->total_users ) {
1229          $viewing_text = __( 'Viewing 1 member', 'buddypress' );
1230      } else {
1231          $viewing_text = sprintf(
1232              _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' ),
1233              bp_core_number_format( $current_page_start ),
1234              bp_core_number_format( $current_page_end ),
1235              bp_core_number_format( $query->total_users )
1236          );
1237      }
1238  
1239      $pagination .= '<span class="bp-group-admin-pagination-viewing">' . $viewing_text . '</span>';
1240      $pagination .= '<span class="bp-group-admin-pagination-links">' . $pag_links . '</span>';
1241  
1242      return $pagination;
1243  }
1244  
1245  /**
1246   * Get a set of usernames corresponding to a set of user IDs.
1247   *
1248   * @since 1.7.0
1249   *
1250   * @param array $user_ids Array of user IDs.
1251   * @return array Array of user_logins corresponding to $user_ids.
1252   */
1253  function bp_groups_admin_get_usernames_from_ids( $user_ids = array() ) {
1254  
1255      $usernames = array();
1256      $users     = new WP_User_Query( array( 'blog_id' => 0, 'include' => $user_ids ) );
1257  
1258      foreach ( (array) $users->results as $user ) {
1259          $usernames[] = $user->user_login;
1260      }
1261  
1262      return $usernames;
1263  }
1264  
1265  /**
1266   * AJAX handler for group member autocomplete requests.
1267   *
1268   * @since 1.7.0
1269   */
1270  function bp_groups_admin_autocomplete_handler() {
1271  
1272      // Bail if user user shouldn't be here, or is a large network.
1273      if ( ! bp_current_user_can( 'bp_moderate' ) || bp_is_large_install() ) {
1274          wp_die( -1 );
1275      }
1276  
1277      $term     = isset( $_GET['term'] )     ? sanitize_text_field( $_GET['term'] ) : '';
1278      $group_id = isset( $_GET['group_id'] ) ? absint( $_GET['group_id'] )          : 0;
1279  
1280      if ( ! $term || ! $group_id ) {
1281          wp_die( -1 );
1282      }
1283  
1284      $suggestions = bp_core_get_suggestions( array(
1285          'group_id' => -$group_id,  // A negative value will exclude this group's members from the suggestions.
1286          'limit'    => 10,
1287          'term'     => $term,
1288          'type'     => 'members',
1289      ) );
1290  
1291      $matches = array();
1292  
1293      if ( $suggestions && ! is_wp_error( $suggestions ) ) {
1294          foreach ( $suggestions as $user ) {
1295  
1296              $matches[] = array(
1297                  // Translators: 1: user_login, 2: user_email.
1298                  'label' => sprintf( __( '%1$s (%2$s)', 'buddypress' ), $user->name, $user->ID ),
1299                  'value' => $user->ID,
1300              );
1301          }
1302      }
1303  
1304      wp_die( json_encode( $matches ) );
1305  }
1306  add_action( 'wp_ajax_bp_group_admin_member_autocomplete', 'bp_groups_admin_autocomplete_handler' );
1307  
1308  /**
1309   * Process input from the Group Type bulk change select.
1310   *
1311   * @since 2.7.0
1312   *
1313   * @param string $doaction Current $_GET action being performed in admin screen.
1314   */
1315  function bp_groups_admin_process_group_type_bulk_changes( $doaction ) {
1316      // Bail if no groups are specified or if this isn't a relevant action.
1317      if ( empty( $_REQUEST['gid'] )
1318          || ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
1319          || empty( $_REQUEST['bp_change_group_type'] )
1320      ) {
1321          return;
1322      }
1323  
1324      // Bail if nonce check fails.
1325      check_admin_referer( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
1326  
1327      if ( ! bp_current_user_can( 'bp_moderate' )  ) {
1328          return;
1329      }
1330  
1331      $new_type = '';
1332      if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
1333          $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
1334      } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
1335          $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
1336      }
1337  
1338      // Check that the selected type actually exists.
1339      if ( 'remove_group_type' !== $new_type && null === bp_groups_get_group_type_object( $new_type ) ) {
1340          $error = true;
1341      } else {
1342          // Run through group ids.
1343          $error = false;
1344          foreach ( (array) $_REQUEST['gid'] as $group_id ) {
1345              $group_id = (int) $group_id;
1346  
1347              // Get the old group type to check against.
1348              $group_type = bp_groups_get_group_type( $group_id );
1349  
1350              if ( 'remove_group_type' === $new_type ) {
1351                  // Remove the current group type, if there's one to remove.
1352                  if ( $group_type ) {
1353                      $removed = bp_groups_remove_group_type( $group_id, $group_type );
1354                      if ( false === $removed || is_wp_error( $removed ) ) {
1355                          $error = true;
1356                      }
1357                  }
1358              } else {
1359                  // Set the new group type.
1360                  if ( $new_type !== $group_type ) {
1361                      $set = bp_groups_set_group_type( $group_id, $new_type );
1362                      if ( false === $set || is_wp_error( $set ) ) {
1363                          $error = true;
1364                      }
1365                  }
1366              }
1367          }
1368      }
1369  
1370      // If there were any errors, show the error message.
1371      if ( $error ) {
1372          $redirect = add_query_arg( array( 'updated' => 'group-type-change-error' ), wp_get_referer() );
1373      } else {
1374          $redirect = add_query_arg( array( 'updated' => 'group-type-change-success' ), wp_get_referer() );
1375      }
1376  
1377      wp_redirect( $redirect );
1378      exit();
1379  }
1380  add_action( 'bp_groups_admin_load', 'bp_groups_admin_process_group_type_bulk_changes' );
1381  
1382  /**
1383   * Display an admin notice upon group type bulk update.
1384   *
1385   * @since 2.7.0
1386   */
1387  function bp_groups_admin_groups_type_change_notice() {
1388      $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
1389  
1390      // Display feedback.
1391      if ( $updated && in_array( $updated, array( 'group-type-change-error', 'group-type-change-success' ), true ) ) {
1392  
1393          if ( 'group-type-change-error' === $updated ) {
1394              $notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
1395              $type   = 'error';
1396          } else {
1397              $notice = __( 'Group type was changed successfully.', 'buddypress' );
1398              $type   = 'updated';
1399          }
1400  
1401          bp_core_add_admin_notice( $notice, $type );
1402      }
1403  }
1404  add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1