Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. add_shortcode( 'groups_join_user', 'my_groups_join_user' );
  2. function my_groups_join_user( $atts, $content = null ) {
  3.     $nonce_action = 'groups_action';
  4.     $nonce        = 'nonce_join';
  5.     $output       = "";
  6.  
  7.     $options = shortcode_atts(
  8.             array(
  9.                     'group'             => '',
  10.                     'user_id'             => '',
  11.                     'display_message'   => true,
  12.                     'display_is_member' => false,
  13.                     'submit_text'       => __( 'Join the %s group', GROUPS_PLUGIN_DOMAIN )
  14.             ),
  15.             $atts
  16.             );
  17.     extract( $options );
  18.  
  19.     if ( $display_message === 'false' ) {
  20.         $display_message = false;
  21.     }
  22.     if ( $display_is_member === 'true' ) {
  23.         $display_is_member = true;
  24.     }
  25.  
  26.     $group = trim( $options['group'] );
  27.     $current_group = Groups_Group::read( $group );
  28.     if ( !$current_group ) {
  29.         $current_group = Groups_Group::read_by_name( $group );
  30.     }
  31.     if ( $current_group ) {
  32.         if ( $user_id !== '' ) {
  33.             $submitted     = false;
  34.             $invalid_nonce = false;
  35.             if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'join' ) {
  36.                 $submitted = true;
  37.                 if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
  38.                     $invalid_nonce = true;
  39.                 }
  40.             }
  41.             if ( $submitted && !$invalid_nonce ) {
  42.                 // add user to group
  43.                 if ( isset( $_POST['group_id'] ) ) {
  44.                     $join_group = Groups_Group::read( $_POST['group_id'] );
  45.                     Groups_User_Group::create(
  46.                             array(
  47.                                     'group_id' => $join_group->group_id,
  48.                                     'user_id' => $user_id
  49.                             )
  50.                             );
  51.                 }
  52.             }
  53.             if ( !Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
  54.                 $submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
  55.                 $output .= '<div class="groups-join">';
  56.                 $output .= '<form action="#" method="post">';
  57.                 $output .= '<input type="hidden" name="groups_action" value="join" />';
  58.                 $output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
  59.                 $output .= '<input type="submit" value="' . $submit_text . '" />';
  60.                 $output .=  wp_nonce_field( $nonce_action, $nonce, true, false );
  61.                 $output .= '</form>';
  62.                 $output .= '</div>';
  63.             } else if ( $display_message ) {
  64.                 if ( $submitted && !$invalid_nonce && isset( $join_group ) && $join_group->group_id === $current_group->group_id ) {
  65.                     $output .= '<div class="groups-join joined">';
  66.                     $output .= sprintf( __( 'The user has joined the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $join_group->name ) );
  67.                     $output .= '</div>';
  68.                 }
  69.                 else if ( $display_is_member && isset( $current_group ) && $current_group !== false ) {
  70.                     $output .= '<div class="groups-join member">';
  71.                     $output .= sprintf( __( 'The user is a member of the %s group.', GROUPS_PLUGIN_DOMAIN ), wp_filter_nohtml_kses( $current_group->name ) );
  72.                     $output .= '</div>';
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     return $output;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement