| | 2053 | |
| | 2054 | /** Users List Management ****************************************************/ |
| | 2055 | |
| | 2056 | /** |
| | 2057 | * Display a dropdown to bulk change the member type of selected user(s). |
| | 2058 | * |
| | 2059 | * @since 2.7.0 |
| | 2060 | * |
| | 2061 | * @param string $which Where this dropdown is displayed - top or bottom. |
| | 2062 | */ |
| | 2063 | public function users_table_output_type_change_select( $which ) { |
| | 2064 | $id_name = 'bottom' === $which ? 'bp_change_type2' : 'bp_change_type'; |
| | 2065 | |
| | 2066 | // Bail if current user cannot promote users. |
| | 2067 | if ( ! current_user_can( 'promote_users' ) ) { |
| | 2068 | return; |
| | 2069 | } |
| | 2070 | |
| | 2071 | $types = bp_get_member_types( array(), 'objects' ); ?> |
| | 2072 | |
| | 2073 | <label class="screen-reader-text" for="<?php echo $id_name; ?>"><?php _e( 'Change member type to…', 'buddypress' ) ?></label> |
| | 2074 | <select name="<?php echo $id_name; ?>" id="<?php echo $id_name; ?>" style="display:inline-block;float:none;"> |
| | 2075 | <option value=""><?php _e( 'Change member type to…', 'buddypress' ) ?></option> |
| | 2076 | |
| | 2077 | <?php foreach( $types as $type ) : ?> |
| | 2078 | |
| | 2079 | <option value="<?php echo esc_attr( $type->name ); ?>"><?php echo $type->labels['name']; ?></option> |
| | 2080 | |
| | 2081 | <?php endforeach; ?> |
| | 2082 | |
| | 2083 | <option value="remove_member_type"><?php _e( 'No Member Type', 'buddypress' ) ?></option> |
| | 2084 | |
| | 2085 | </select> |
| | 2086 | <?php |
| | 2087 | wp_nonce_field( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' ); |
| | 2088 | submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_member_type', false ); |
| | 2089 | } |
| | 2090 | |
| | 2091 | /** |
| | 2092 | * Process bulk member type change submission from the WP admin users list table. |
| | 2093 | * |
| | 2094 | * @since 2.7.0 |
| | 2095 | */ |
| | 2096 | public function users_table_process_bulk_type_change() { |
| | 2097 | $this->users_type_change_notice(); |
| | 2098 | |
| | 2099 | // Bail if no users specified. |
| | 2100 | if ( empty( $_REQUEST['users'] ) ) { |
| | 2101 | return; |
| | 2102 | } |
| | 2103 | |
| | 2104 | // Bail if this isn't a BuddyPress action. |
| | 2105 | if ( ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) ) |
| | 2106 | || empty( $_REQUEST['bp_change_member_type'] ) |
| | 2107 | ) { |
| | 2108 | return; |
| | 2109 | } |
| | 2110 | |
| | 2111 | // Bail if nonce check fails. |
| | 2112 | check_admin_referer( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' ); |
| | 2113 | |
| | 2114 | // Bail if current user cannot promote users. |
| | 2115 | if ( ! current_user_can( 'promote_users' ) ) { |
| | 2116 | return; |
| | 2117 | } |
| | 2118 | |
| | 2119 | // Bail if the new type is empty. |
| | 2120 | $new_type = ''; |
| | 2121 | if ( ! empty( $_REQUEST['bp_change_type2'] ) ) { |
| | 2122 | $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] ); |
| | 2123 | } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) { |
| | 2124 | $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] ); |
| | 2125 | } |
| | 2126 | |
| | 2127 | // Check that the selected type actually exists. |
| | 2128 | if ( 'remove_member_type' != $new_type && is_null( bp_get_member_type_object( $new_type ) ) ) { |
| | 2129 | return; |
| | 2130 | } |
| | 2131 | |
| | 2132 | // Run through user ids. |
| | 2133 | $error = false; |
| | 2134 | foreach ( (array) $_REQUEST['users'] as $user_id ) { |
| | 2135 | $user_id = (int) $user_id; |
| | 2136 | |
| | 2137 | // Get the old member type to check against. |
| | 2138 | $member_type = bp_get_member_type( $user_id ); |
| | 2139 | |
| | 2140 | if ( 'remove_member_type' == $new_type ) { |
| | 2141 | // Remove the current member type, if there's one to remove. |
| | 2142 | if ( $member_type ) { |
| | 2143 | $removed = bp_remove_member_type( $user_id, $member_type ); |
| | 2144 | if ( false == $removed || is_wp_error( $removed ) ) { |
| | 2145 | $error = true; |
| | 2146 | } |
| | 2147 | } |
| | 2148 | } else { |
| | 2149 | // Set the new member type. |
| | 2150 | if ( $new_type !== $member_type ) { |
| | 2151 | $set = bp_set_member_type( $user_id, $new_type ); |
| | 2152 | if ( false == $set || is_wp_error( $set ) ) { |
| | 2153 | $error = true; |
| | 2154 | } |
| | 2155 | } |
| | 2156 | } |
| | 2157 | } |
| | 2158 | |
| | 2159 | // If there were any errors, show the error message. |
| | 2160 | if ( $error ) { |
| | 2161 | $redirect = add_query_arg( array( 'updated' => 'member-type-change-error' ), wp_get_referer() ); |
| | 2162 | } else { |
| | 2163 | $redirect = add_query_arg( array( 'updated' => 'member-type-change-success' ), wp_get_referer() ); |
| | 2164 | } |
| | 2165 | |
| | 2166 | wp_redirect( $redirect ); |
| | 2167 | exit(); |
| | 2168 | } |
| | 2169 | |
| | 2170 | /** |
| | 2171 | * Display an admin notice upon member type bulk update. |
| | 2172 | * |
| | 2173 | * @since 2.7.0 |
| | 2174 | */ |
| | 2175 | public function users_type_change_notice() { |
| | 2176 | $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false; |
| | 2177 | |
| | 2178 | // Display feedback. |
| | 2179 | if ( ! empty( $updated ) && in_array( $updated, array( 'member-type-change-error', 'member-type-change-success' ) ) ) { |
| | 2180 | |
| | 2181 | if ( 'member-type-change-error' === $updated ) { |
| | 2182 | $notice = __( 'There was an error while changing member type. Please try again.', 'buddypress' ); |
| | 2183 | } else { |
| | 2184 | $notice = __( 'Member type was successfully changed.', 'buddypress' ); |
| | 2185 | } |
| | 2186 | |
| | 2187 | bp_core_add_admin_notice( $notice ); |
| | 2188 | } |
| | 2189 | } |
| | 2190 | |
| | 2191 | /** |
| | 2192 | * Add member type column to the WordPress admin users list table. |
| | 2193 | * |
| | 2194 | * @since 2.7.0 |
| | 2195 | * |
| | 2196 | * @param array $columns Users table columns. |
| | 2197 | * |
| | 2198 | * @return array $columns |
| | 2199 | */ |
| | 2200 | public function users_table_add_type_column( $columns = array() ) { |
| | 2201 | $columns[ bp_get_member_type_tax_name() ] = _x( 'Member Type', 'Label for the WP users table member type column' , 'buddypress' ); |
| | 2202 | |
| | 2203 | return $columns; |
| | 2204 | } |
| | 2205 | |
| | 2206 | /** |
| | 2207 | * Return member's type for display in the WP admin users list table. |
| | 2208 | * |
| | 2209 | * @since 2.7.0 |
| | 2210 | * |
| | 2211 | * @param string $retval |
| | 2212 | * @param string $column_name |
| | 2213 | * @param int $user_id |
| | 2214 | * |
| | 2215 | * @return string Member type as a link to filter all users. |
| | 2216 | */ |
| | 2217 | public function users_table_populate_type_cell( $retval = '', $column_name = '', $user_id = 0 ) { |
| | 2218 | // Only looking for member type column |
| | 2219 | if ( bp_get_member_type_tax_name() !== $column_name ) { |
| | 2220 | return $retval; |
| | 2221 | } |
| | 2222 | |
| | 2223 | // Get the member type. |
| | 2224 | $type = bp_get_member_type( $user_id ); |
| | 2225 | |
| | 2226 | // Translate user role for display. |
| | 2227 | if ( $type_obj = bp_get_member_type_object( $type ) ) { |
| | 2228 | |
| | 2229 | $url = add_query_arg( array( 'bp-member-type' => urlencode( $type ) ) ); |
| | 2230 | // @TODO: Will `translate_user_role()` do anything here? Or can we assume that the singular name is translated? |
| | 2231 | $retval = '<a href="' . esc_url( $url ) . '">' . translate_user_role( $type_obj->labels['singular_name'] ) . '</a>'; |
| | 2232 | } |
| | 2233 | |
| | 2234 | return $retval; |
| | 2235 | } |
| | 2236 | |
| | 2237 | /** |
| | 2238 | * Filter WP Admin users list table to include users of the specified type. |
| | 2239 | * |
| | 2240 | * @param WP_Query $query |
| | 2241 | * |
| | 2242 | * @since 2.7.0 |
| | 2243 | */ |
| | 2244 | public function users_table_filter_by_type( $query ) { |
| | 2245 | global $pagenow; |
| | 2246 | |
| | 2247 | if ( is_admin() && 'users.php' === $pagenow && ! empty( $_REQUEST['bp-member-type'] ) ) { |
| | 2248 | $type_slug = sanitize_text_field( $_REQUEST['bp-member-type'] ); |
| | 2249 | |
| | 2250 | // Check that the type is registered. |
| | 2251 | if ( is_null( bp_get_member_type_object( $type_slug ) ) ) { |
| | 2252 | return; |
| | 2253 | } |
| | 2254 | |
| | 2255 | // @TODO: Would it be better to use BP_User_Query with the member_type param here? |
| | 2256 | // Get the list of users that are assigned to this member type. |
| | 2257 | $type = get_term_by( 'slug', $type_slug, bp_get_member_type_tax_name() ); |
| | 2258 | |
| | 2259 | if ( ! $type->term_id ) { |
| | 2260 | return; |
| | 2261 | } |
| | 2262 | |
| | 2263 | $user_ids = get_objects_in_term( $type->term_id, bp_get_member_type_tax_name() ); |
| | 2264 | |
| | 2265 | if ( $user_ids && ! is_wp_error( $user_ids ) ) { |
| | 2266 | $query->set( 'include', (array) $user_ids ); |
| | 2267 | } |
| | 2268 | } |
| | 2269 | } |