Skip to:
Content

BuddyPress.org

Changeset 10770


Ignore:
Timestamp:
05/15/2016 02:52:11 AM (4 years ago)
Author:
boonebgorges
Message:

Add Group Type metabox to Group admin panel.

When group types have been registered, the Group Type metabox allows site
administrators to select the group type for a given group.

Props dcavins.
See #6784.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-admin.php

    r10739 r10770  
    134134        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' );
    135135        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' );
     136
     137        // Group Type metabox. Only added if group types have been registered.
     138        $group_types = bp_groups_get_group_types();
     139        if ( ! empty( $group_types ) ) {
     140            add_meta_box(
     141                'bp_groups_admin_group_type',
     142                _x( 'Group Type', 'groups admin edit screen', 'buddypress' ),
     143                'bp_groups_admin_edit_metabox_group_type',
     144                get_current_screen()->id,
     145                'side',
     146                'core'
     147            );
     148        }
    136149
    137150        /**
     
    10141027
    10151028/**
     1029 * Render the Group Type metabox.
     1030 *
     1031 * @since 2.6.0
     1032 *
     1033 * @param BP_Groups_Group|null $user The BP_Groups_Group object corresponding to the group being edited.
     1034 */
     1035function bp_groups_admin_edit_metabox_group_type( BP_Groups_Group $group = null ) {
     1036
     1037    // Bail if no group ID.
     1038    if ( empty( $group->id ) ) {
     1039        return;
     1040    }
     1041
     1042    $types = bp_groups_get_group_types( array(), 'objects' );
     1043    $current_type = bp_groups_get_group_type( $group->id );
     1044    ?>
     1045
     1046    <label for="bp-groups-group-type" class="screen-reader-text"><?php esc_html_e( 'Select group type', 'buddypress' ); ?></label>
     1047    <select name="bp-groups-group-type" id="bp-groups-group-type">
     1048        <option value="" <?php selected( '', $current_type ); ?>><?php /* translators: no option picked in select box */ esc_attr_e( '----', 'buddypress' ) ?></option>
     1049        <?php foreach ( $types as $type ) : ?>
     1050            <option value="<?php echo esc_attr( $type->name ) ?>" <?php selected( $type->name, $current_type ) ?>><?php echo esc_html( $type->labels['singular_name'] ) ?></option>
     1051        <?php endforeach; ?>
     1052    </select>
     1053
     1054    <?php
     1055
     1056    wp_nonce_field( 'bp-group-type-change-' . $group->id, 'bp-group-type-nonce' );
     1057}
     1058
     1059/**
     1060 * Process changes from the Group Type metabox.
     1061 *
     1062 * @since 2.6.0
     1063 */
     1064function bp_groups_process_group_type_update( $group_id ) {
     1065    if ( ! isset( $_POST['bp-group-type-nonce'] ) || ! isset( $_POST['bp-groups-group-type'] ) ) {
     1066        return;
     1067    }
     1068
     1069    check_admin_referer( 'bp-group-type-change-' . $group_id, 'bp-group-type-nonce' );
     1070
     1071    // Permission check.
     1072    if ( ! current_user_can( 'bp_moderate' ) ) {
     1073        return;
     1074    }
     1075
     1076    // Group type string must either reference a valid group type, or be empty.
     1077    $group_type = wp_unslash( $_POST['bp-groups-group-type'] );
     1078    if ( $group_type && ! bp_groups_get_group_type_object( $group_type ) ) {
     1079        return;
     1080    }
     1081
     1082    /*
     1083     * If an invalid group type is passed, someone's doing something
     1084     * fishy with the POST request, so we can fail silently.
     1085     */
     1086    if ( bp_groups_set_group_type( $group_id, $group_type ) ) {
     1087        // @todo Success messages can't be posted because other stuff happens on the page load.
     1088    }
     1089}
     1090add_action( 'bp_group_admin_edit_after', 'bp_groups_process_group_type_update' );
     1091
     1092/**
    10161093 * Create pagination links out of a BP_Group_Member_Query.
    10171094 *
Note: See TracChangeset for help on using the changeset viewer.