| | 2053 | |
| | 2054 | /** Users List Management ****************************************************/ |
| | 2055 | |
| | 2056 | /** |
| | 2057 | * Display a dropdown to 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_type_change_to( $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 = buddypress()->members->types; ?> |
| | 2072 | |
| | 2073 | <label class="screen-reader-text" for="bp_change_type"><?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 | </select> |
| | 2084 | <?php |
| | 2085 | |
| | 2086 | submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_member_type', false ); |
| | 2087 | |
| | 2088 | wp_nonce_field( 'bp-bulk-users-change-type', 'bp-bulk-users-change-type-nonce' ); |
| | 2089 | } |
| | 2090 | |
| | 2091 | /** |
| | 2092 | * Process bulk member type submission from the Users Table. |
| | 2093 | * |
| | 2094 | * @since 2.7.0 |
| | 2095 | */ |
| | 2096 | public function users_type_bulk_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 ( |
| | 2106 | ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) ) || |
| | 2107 | empty( $_REQUEST['bp_change_member_type'] ) |
| | 2108 | ) { |
| | 2109 | return; |
| | 2110 | } |
| | 2111 | |
| | 2112 | // Bail if nonce check fails. |
| | 2113 | check_admin_referer( 'bp-bulk-users-change-type', 'bp-bulk-users-change-type-nonce' ); |
| | 2114 | |
| | 2115 | // Bail if current user cannot promote users. |
| | 2116 | if ( ! current_user_can( 'promote_users' ) ) { |
| | 2117 | return; |
| | 2118 | } |
| | 2119 | |
| | 2120 | // Bail if the new type is empty. |
| | 2121 | $new_type = ''; |
| | 2122 | if ( ! empty( $_REQUEST['bp_change_type2'] ) ) { |
| | 2123 | $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] ); |
| | 2124 | } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) { |
| | 2125 | $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] ); |
| | 2126 | } |
| | 2127 | if ( empty( $new_type ) ) { |
| | 2128 | return; |
| | 2129 | } |
| | 2130 | |
| | 2131 | // Check that the selected type actually exists. |
| | 2132 | $types = buddypress()->members->types; |
| | 2133 | if ( empty( $types[ $new_type ] ) ) { |
| | 2134 | return; |
| | 2135 | } |
| | 2136 | |
| | 2137 | // Run through user ids. |
| | 2138 | foreach ( (array) $_REQUEST['users'] as $user_id ) { |
| | 2139 | $user_id = (int) $user_id; |
| | 2140 | |
| | 2141 | // Get the old member type to check against. |
| | 2142 | $user_type = bp_get_member_type( $user_id ); |
| | 2143 | |
| | 2144 | // Set the new member type. |
| | 2145 | if ( $new_type !== $user_type ) { |
| | 2146 | $set = bp_set_member_type( $user_id, $new_type ); |
| | 2147 | |
| | 2148 | // Lets assume, that everything is fine. |
| | 2149 | if ( ! empty( $set ) && ! is_wp_error( $set ) ) { |
| | 2150 | $redirect = add_query_arg( array( 'updated' => 'member-type-change-success' ), wp_get_referer() ); |
| | 2151 | } else { |
| | 2152 | $redirect = add_query_arg( array( 'updated' => 'member-type-change-error' ), wp_get_referer() ); |
| | 2153 | } |
| | 2154 | |
| | 2155 | wp_redirect( $redirect ); |
| | 2156 | exit(); |
| | 2157 | } |
| | 2158 | } |
| | 2159 | } |
| | 2160 | |
| | 2161 | /** |
| | 2162 | * Display an admin area notice upon member type bulk update. |
| | 2163 | * |
| | 2164 | * @since 2.7.0 |
| | 2165 | */ |
| | 2166 | public function users_type_change_notice() { |
| | 2167 | $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false; |
| | 2168 | |
| | 2169 | // Display feedback. |
| | 2170 | if ( ! empty( $updated ) && in_array( $updated, array( 'member-type-change-error', 'member-type-change-success' ) ) ) { |
| | 2171 | |
| | 2172 | if ( 'member-type-change-error' === $updated ) { |
| | 2173 | $notice = __( 'There was an error while changing member type. Please try again.', 'buddypress' ); |
| | 2174 | } else { |
| | 2175 | $notice = __( 'Member type was successfully changed.', 'buddypress' ); |
| | 2176 | } |
| | 2177 | |
| | 2178 | bp_core_add_admin_notice( $notice ); |
| | 2179 | } |
| | 2180 | } |
| | 2181 | |
| | 2182 | /** |
| | 2183 | * Add Member Type column to the WordPress Users table. |
| | 2184 | * |
| | 2185 | * @since 2.7.0 |
| | 2186 | * |
| | 2187 | * @param array $columns Users table columns. |
| | 2188 | * |
| | 2189 | * @return array $columns |
| | 2190 | */ |
| | 2191 | public function users_type_column( $columns = array() ) { |
| | 2192 | $columns[ bp_get_member_type_tax_type() ] = __( 'Member Type', 'buddypress' ); |
| | 2193 | |
| | 2194 | return $columns; |
| | 2195 | } |
| | 2196 | |
| | 2197 | /** |
| | 2198 | * Return member's type for display in the Users list table. |
| | 2199 | * |
| | 2200 | * @since 2.7.0 |
| | 2201 | * |
| | 2202 | * @param string $retval |
| | 2203 | * @param string $column_name |
| | 2204 | * @param int $user_id |
| | 2205 | * |
| | 2206 | * @return string Displayable member type as a link to filter all users. |
| | 2207 | */ |
| | 2208 | public function users_type_row( $retval = '', $column_name = '', $user_id = 0 ) { |
| | 2209 | // Only looking for member type column |
| | 2210 | if ( bp_get_member_type_tax_type() !== $column_name ) { |
| | 2211 | return $retval; |
| | 2212 | } |
| | 2213 | |
| | 2214 | // Get the member type. |
| | 2215 | $type = bp_get_member_type( $user_id ); |
| | 2216 | $retval = ''; |
| | 2217 | |
| | 2218 | // Translate user role for display. |
| | 2219 | if ( ! empty( $type ) ) { |
| | 2220 | $types = buddypress()->members->types; |
| | 2221 | |
| | 2222 | if ( ! empty( $types[ $type ]->labels['singular_name'] ) ) { |
| | 2223 | $url = add_query_arg( array( 'bp-member-type' => urlencode( $type ) ), wp_get_referer() ); |
| | 2224 | |
| | 2225 | $retval = '<a href="' . esc_url( $url ) . '">' . translate_user_role( $types[ $type ]->labels['singular_name'] ) . '</a>'; |
| | 2226 | } |
| | 2227 | } |
| | 2228 | |
| | 2229 | return $retval; |
| | 2230 | } |
| | 2231 | |
| | 2232 | /*******************************************************************/ |
| | 2233 | |
| | 2234 | /** |
| | 2235 | * Filter in admin area on Users page those with a specified type. |
| | 2236 | * |
| | 2237 | * @param WP_Query $query |
| | 2238 | * |
| | 2239 | * @since 2.7.0 |
| | 2240 | */ |
| | 2241 | public function users_type_process_filter_by( $query ) { |
| | 2242 | global $pagenow; |
| | 2243 | |
| | 2244 | if ( is_admin() && 'users.php' === $pagenow && ! empty( $_REQUEST['bp-member-type'] ) ) { |
| | 2245 | $type_slug = sanitize_text_field( $_REQUEST['bp-member-type'] ); |
| | 2246 | |
| | 2247 | // Check that the type actually exists. |
| | 2248 | $types = buddypress()->members->types; |
| | 2249 | if ( empty( $types[ $type_slug ] ) ) { |
| | 2250 | return; |
| | 2251 | } |
| | 2252 | |
| | 2253 | // Get the list of users, that are assigned to this member type. |
| | 2254 | $type = get_term_by( 'slug', $type_slug, bp_get_member_type_tax_type() ); |
| | 2255 | |
| | 2256 | if ( ! $type->term_id ) { |
| | 2257 | return; |
| | 2258 | } |
| | 2259 | |
| | 2260 | $user_ids = get_objects_in_term( $type->term_id, bp_get_member_type_tax_type() ); |
| | 2261 | |
| | 2262 | if ( $user_ids && ! is_wp_error( $user_ids ) ) { |
| | 2263 | $query->set( 'include', (array) $user_ids ); |
| | 2264 | } |
| | 2265 | } |
| | 2266 | } |