Extends the [groups_user_groups] shortcode to display the ids. This new shortcode is [groups_user_groups_id]
add_shortcode( 'groups_user_groups_id', 'itx_groups_user_groups_id' ); | |
/** | |
* Renders the current or a specific user's groups id. | |
* Attributes: | |
* - "user_id" OR "user_login" OR "user_email" to identify the user, if none given assumes the current user | |
* - "format" : one of "list" "div" "ul" or "ol" - "list" and "ul" are equivalent | |
* - "list_class" : defaults to "groups" | |
* - "item_class" : defaults to "name" | |
* - "order_by" : defaults to "name", also accepts "group_id" | |
* - "order" : default to "ASC", also accepts "asc", "desc" and "DESC" | |
* | |
* @param array $atts attributes | |
* @param string $content not used | |
* @return rendered groups for current user | |
*/ | |
function itx_groups_user_groups_id( $atts, $content = null ) { | |
$output = ''; | |
$options = shortcode_atts( | |
array( | |
'user_id' => null, | |
'user_login' => null, | |
'user_email' => null, | |
'format' => 'list', | |
'list_class' => 'groups', | |
'item_class' => 'name', | |
'order_by' => 'name', | |
'order' => 'ASC', | |
'group' => null, | |
'exclude_group' => null | |
), | |
$atts | |
); | |
$user_id = null; | |
if ( $options['user_id'] !== null ) { | |
if ( $user = get_user_by( 'id', $options['user_id'] ) ) { | |
$user_id = $user->ID; | |
} | |
} else if ( $options['user_id'] !== null ) { | |
if ( $user = get_user_by( 'login', $options['user_login'] ) ) { | |
$user_id = $user->ID; | |
} | |
} else if ( $options['user_email'] !== null ) { | |
if ( $user = get_user_by( 'email', $options['user_login'] ) ) { | |
$user_id = $user->ID; | |
} | |
} | |
if ( $user_id === null ) { | |
$user_id = get_current_user_id(); | |
} | |
if ( $user_id !== null ) { | |
$user = new Groups_User( $user_id ); | |
$groups = $user->groups; | |
if ( !empty( $groups ) ) { | |
// group attr | |
if ( $options['group'] !== null ) { | |
$groups = array(); | |
$groups_incl = explode( ',', $options['group'] ); | |
foreach ( $groups_incl as $group_incl ) { | |
$group = trim( $group_incl ); | |
$current_group = Groups_Group::read( $group ); | |
if ( !$current_group ) { | |
$current_group = Groups_Group::read_by_name( $group ); | |
} | |
if ( $current_group ) { | |
if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) { | |
$groups[] = $current_group; | |
} | |
} | |
} | |
} | |
// exclude_group attr | |
if ( $options['exclude_group'] !== null ) { | |
$groups_excl = explode( ',', $options['exclude_group'] ); | |
foreach ( $groups_excl as $key => $group_excl ) { | |
$group = trim( $group_excl ); | |
$current_group = Groups_Group::read( $group ); | |
if ( !$current_group ) { | |
$current_group = Groups_Group::read_by_name( $group ); | |
} | |
if ( $current_group ) { | |
$groups_excl[$key] = $current_group->group_id; | |
} else { | |
unset( $groups_excl[$key] ); | |
} | |
} | |
foreach ( $groups as $key => $group ) { | |
if ( in_array( $group->group_id, $groups_excl ) ) { | |
unset( $groups[$key] ); | |
} | |
} | |
} | |
switch( $options['order_by'] ) { | |
case 'group_id' : | |
usort( $groups, array( __CLASS__, 'sort_id' ) ); | |
break; | |
default : | |
usort( $groups, array( __CLASS__, 'sort_name' ) ); | |
} | |
switch( $options['order'] ) { | |
case 'desc' : | |
case 'DESC' : | |
$groups = array_reverse( $groups ); | |
break; | |
} | |
switch( $options['format'] ) { | |
case 'list' : | |
case 'ul' : | |
$output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">'; | |
break; | |
case 'ol' : | |
$output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">'; | |
break; | |
default : | |
$output .= '<div class="' . esc_attr( $options['list_class'] ) . '">'; | |
} | |
foreach( $groups as $group ) { | |
switch( $options['format'] ) { | |
case 'list' : | |
case 'ul' : | |
case 'ol' : | |
$output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . ' (' . $group->group_id . ')' . '</li>'; | |
break; | |
default : | |
$output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . ' (' . $group->group_id . ')' . '</div>'; | |
} | |
} | |
switch( $options['format'] ) { | |
case 'list' : | |
case 'ul' : | |
$output .= '</ul>'; | |
break; | |
case 'ol' : | |
$output .= '</ol>'; | |
break; | |
default : | |
$output .= '</div>'; | |
} | |
} | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment