redirect attribute.
*
* @param array $atts
* @param string $content
* @return string the rendered form or empty
*/
public static function groups_login( $atts, $content = null ) {
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
extract(
shortcode_atts(
array(
'redirect' => $current_url,
'show_logout' => 'no'
),
$atts
)
);
$redirect = trim( $redirect );
$show_logout = trim( strtolower( $show_logout ) );
$output = '';
if ( !is_user_logged_in() ) {
$output .= wp_login_form(
array(
'echo' => false,
'redirect' => $redirect
)
);
} else {
if ( $show_logout == 'yes' ) {
$output .= self::groups_logout(
array(
'redirect' => $redirect
)
);
}
}
return $output;
}
/**
* Renders the Groups logout link.
*
* The link is rendered if the user is logged in.
* The user is redirected to the current page after logout by default.
* The user can be redirected to a specific URL after logout by
* indicating the redirect attribute.
*
* @param array $atts
* @param string $content not used
* @return string logout link, is empty if not logged in
*/
public static function groups_logout( $atts, $content = null ) {
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
extract(
shortcode_atts(
array(
'redirect' => $current_url
),
$atts
)
);
$redirect = trim( $redirect );
$output = '';
if ( is_user_logged_in() ) {
$output .= sprintf( '', esc_url( wp_logout_url( $redirect ) ) );
$output .= __( 'Log out', GROUPS_PLUGIN_DOMAIN );
$output .= '';
}
return $output;
}
/**
* Renders information about a group.
* Attributes:
* - "group" : group name or id
* - "show" : what to show, can be "name", "description", "count"
* - "format" :
* - "single" : used with show="count", single form, defaults to '1'
* - "plural" : used with show="count", plural form, defaults to '%d', must contain %d to show number
*
* @param array $atts attributes
* @param string $content content to render
* @return rendered information
*/
public static function groups_group_info( $atts, $content = null ) {
global $wpdb;
$output = "";
$options = shortcode_atts(
array(
'group' => '',
'show' => '',
'format' => '',
'single' => '1',
'plural' => '%d'
),
$atts
);
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
switch( $options['show'] ) {
case 'name' :
$output .= wp_filter_nohtml_kses( $current_group->name );
break;
case 'description' :
$output .= wp_filter_nohtml_kses( $current_group->description );
break;
case 'count' :
$user_group_table = _groups_get_tablename( "user_group" );
$count = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
Groups_Utility::id( $current_group->group_id )
) );
if ( $count === null ) {
$count = 0;
} else {
$count = intval( $count );
}
$output .= _n( $options['single'], sprintf( $options['plural'], $count ), $count, GROUPS_PLUGIN_DOMAIN );
break;
// @todo experimental - could use pagination, sorting, link to profile, ...
case 'users' :
$user_group_table = _groups_get_tablename( "user_group" );
$users = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
Groups_Utility::id( $current_group->group_id )
) );
if ( $users ) {
$output .= '
';
foreach( $users as $user ) {
$output .= '- ' . wp_filter_nohtml_kses( $user->user_login ) . '
';
}
$output .= '
';
}
break;
}
}
return $output;
}
/**
* Renders the current or a specific user's groups.
* 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
*/
public static function groups_user_groups( $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 .= '';
break;
case 'ol' :
$output .= '';
break;
default :
$output .= '';
}
foreach( $groups as $group ) {
switch( $options['format'] ) {
case 'list' :
case 'ul' :
case 'ol' :
$output .= '
- ' . $group->name . '
';
break;
default :
$output .= '
' . $group->name . '
';
}
}
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '';
break;
case 'ol' :
$output .= '';
break;
default :
$output .= '
';
}
}
}
return $output;
}
/**
* Group comparison by group_id.
*
* @param Groups_Group $a
* @param Groups_Group $b
* @return int
*/
public static function sort_id( $a, $b ) {
return $a->group_id - $b->group_id;
}
/**
* Group comparison by name.
*
* @param Groups_Group $a
* @param Groups_Group $b
* @return int
*/
public static function sort_name( $a, $b ) {
return strcmp( $a->name, $b->name );
}
/**
* Renders a list of the site's groups.
* Attributes:
* - "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
*/
public static function groups_groups( $atts, $content = null ) {
global $wpdb;
$output = "";
$options = shortcode_atts(
array(
'format' => 'list',
'list_class' => 'groups',
'item_class' => 'name',
'order_by' => 'name',
'order' => 'ASC'
),
$atts
);
switch( $options['order_by'] ) {
case 'group_id' :
case 'name' :
$order_by = $options['order_by'];
break;
default :
$order_by = 'name';
}
switch( $options['order'] ) {
case 'asc' :
case 'ASC' :
case 'desc' :
case 'DESC' :
$order = strtoupper( $options['order'] );
break;
default :
$order = 'ASC';
}
$group_table = _groups_get_tablename( "group" );
if ( $groups = $wpdb->get_results(
"SELECT group_id FROM $group_table ORDER BY $order_by $order"
) ) {
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '';
break;
case 'ol' :
$output .= '';
break;
default :
$output .= '';
}
foreach( $groups as $group ) {
$group = new Groups_Group( $group->group_id );
switch( $options['format'] ) {
case 'list' :
case 'ul' :
case 'ol' :
$output .= '
- ' . $group->name . '
';
break;
default :
$output .= '
' . $group->name . '
';
}
}
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '';
break;
case 'ol' :
$output .= '';
break;
default :
$output .= '
';
}
}
return $output;
}
/**
* Renders a form that lets a user join a group.
* * Attributes:
* - "group" : (required) group name or id
*
* @param array $atts attributes
* @param string $content not used
*/
public static function groups_join( $atts, $content = null ) {
$nonce_action = 'groups_action';
$nonce = 'nonce_join';
$output = "";
$options = shortcode_atts(
array(
'group' => '',
'display_message' => true,
'display_is_member' => false,
'submit_text' => __( 'Join the %s group', GROUPS_PLUGIN_DOMAIN )
),
$atts
);
extract( $options );
if ( $display_message === 'false' ) {
$display_message = false;
}
if ( $display_is_member === 'true' ) {
$display_is_member = true;
}
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( $user_id = get_current_user_id() ) {
$submitted = false;
$invalid_nonce = false;
if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'join' ) {
$submitted = true;
if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
$invalid_nonce = true;
}
}
if ( $submitted && !$invalid_nonce ) {
// add user to group
if ( isset( $_POST['group_id'] ) ) {
$join_group = Groups_Group::read( $_POST['group_id'] );
Groups_User_Group::create(
array(
'group_id' => $join_group->group_id,
'user_id' => $user_id
)
);
}
}
if ( !Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
$submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
$output .= '';
$output .= '';
$output .= '
';
} else if ( $display_message ) {
if ( $submitted && !$invalid_nonce && isset( $join_group ) && $join_group->group_id === $current_group->group_id ) {
$output .= '';
$output .= sprintf( __( 'You have joined the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $join_group->name ) );
$output .= '
';
}
else if ( $display_is_member && isset( $current_group ) && $current_group !== false ) {
$output .= '';
$output .= sprintf( __( 'You are a member of the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $current_group->name ) );
$output .= '
';
}
}
}
}
return $output;
}
/**
* Renders a form that lets a user leave a group.
* * Attributes:
* - "group" : (required) group name or id
*
* @param array $atts attributes
* @param string $content not used
*/
public static function groups_leave( $atts, $content = null ) {
$nonce_action = 'groups_action';
$nonce = 'nonce_leave';
$output = "";
$options = shortcode_atts(
array(
'group' => '',
'display_message' => true,
'submit_text' => __( 'Leave the %s group', GROUPS_PLUGIN_DOMAIN ),
),
$atts
);
extract( $options );
if ( $display_message === 'false' ) {
$display_message = false;
}
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( $user_id = get_current_user_id() ) {
$submitted = false;
$invalid_nonce = false;
if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'leave' ) {
$submitted = true;
if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
$invalid_nonce = true;
}
}
if ( $submitted && !$invalid_nonce ) {
// remove user from group
if ( isset( $_POST['group_id'] ) ) {
$leave_group = Groups_Group::read( $_POST['group_id'] );
Groups_User_Group::delete( $user_id, $leave_group->group_id );
}
}
if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
$submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
$output .= '';
$output .= '';
$output .= '
';
} else if ( $display_message ) {
if ( $submitted && !$invalid_nonce && isset( $leave_group ) && $leave_group->group_id === $current_group->group_id ) {
$output .= '';
$output .= sprintf( __( 'You have left the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $leave_group->name ) );
$output .= '
';
}
}
}
}
return $output;
}
}
Groups_Shortcodes::init();