Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'groups_join_user', 'my_groups_join_user' );
- function my_groups_join_user( $atts, $content = null ) {
- $nonce_action = 'groups_action';
- $nonce = 'nonce_join';
- $output = "";
- $options = shortcode_atts(
- array(
- 'group' => '',
- 'user_id' => '',
- '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 !== '' ) {
- $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 .= '<div class="groups-join">';
- $output .= '<form action="#" method="post">';
- $output .= '<input type="hidden" name="groups_action" value="join" />';
- $output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
- $output .= '<input type="submit" value="' . $submit_text . '" />';
- $output .= wp_nonce_field( $nonce_action, $nonce, true, false );
- $output .= '</form>';
- $output .= '</div>';
- } else if ( $display_message ) {
- if ( $submitted && !$invalid_nonce && isset( $join_group ) && $join_group->group_id === $current_group->group_id ) {
- $output .= '<div class="groups-join joined">';
- $output .= sprintf( __( 'The user has joined the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $join_group->name ) );
- $output .= '</div>';
- }
- else if ( $display_is_member && isset( $current_group ) && $current_group !== false ) {
- $output .= '<div class="groups-join member">';
- $output .= sprintf( __( 'The user is a member of the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $current_group->name ) );
- $output .= '</div>';
- }
- }
- }
- }
- return $output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement