为什么Get_User_by()在我的代码中不起作用?

时间:2019-11-10 作者:anastasia_pz

我正在编辑一个插件的代码,以添加我需要的一个小功能。我几乎成功了,但我真的很挣扎,不明白为什么

$user_id_for_um_groups = get_user_by( \'email\', $email );
在这里不起作用。

我在数据库的user\\u id2列中获得0(我需要获得用户ID)。

我在下面粘贴了完整的代码,我添加的代码被注释为“添加的代码”。

我将非常感谢您的帮助,因为我已经花了几天时间试图理解为什么它不起作用。谢谢;-)

<?php
public function bulk_add_users() {
    if ( isset( $_GET[\'action\'] ) && \'bulk-add-users\' === $_GET[\'action\'] && isset( $_GET[\'group-id\'] ) ) {
        $group_id       = absint( $_GET[\'group-id\'] );
        $first_names    = $_REQUEST[\'first_name\'];
        $last_names     = $_REQUEST[\'last_name\'];
        $emails         = $_REQUEST[\'email\'];
        $error_results  = [];
        $insert_results = [];
        if ( $emails ) {
            $role = apply_filters( \'uo-groups-user-role\', get_option( \'default_role\', \'subscriber\' ) );
            foreach ( $emails as $k => $email ) {
                if ( ! empty( $email ) ) {
                    if ( is_email( $email ) ) {
                        $first       = $first_names[ $k ];
                        $last        = $last_names[ $k ];
                        $email       = sanitize_email( $email );
                        $is_existing = email_exists( $email );

                        if ( is_numeric( $is_existing ) ) {
                            $user_id     = $is_existing;
                            $user_groups = learndash_get_users_group_ids( $user_id, true );
                            if ( in_array( $group_id, $user_groups ) ) {
                                $error_results[] = sprintf( __( \'Line #%d: %s is existing user of group.\', \'uncanny-learndash-groups\' ), $k + 1, $email );
                                continue;
                            }
                            $user_data = array(
                                \'user_email\' => $email,
                                \'user_id\'    => $user_id,
                                \'first_name\' => $first,
                                \'last_name\'  => $last,
                                \'role\'       => $role,
                            );

                            if ( isset( $user_data[\'first_name\'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, \'first_name\', $user_data[\'first_name\'] );
                            }

                            if ( isset( $user_data[\'last_name\'] ) && ! $is_existing ) {
                                update_user_meta( $user_id, \'last_name\', $user_data[\'last_name\'] );
                            }

                            $restApi = new RestApiEndPoints();
                            $restApi->add_existing_user( $user_data, false, $group_id, 0, \'not redeemed\', false );

                        } else {
                            $user_data = array(
                                \'user_login\' => $email,
                                \'user_email\' => $email,
                                \'first_name\' => $first,
                                \'last_name\'  => $last,
                                \'role\'       => $role,
                                \'group_id\'   => $group_id,
                            );
                            $restApi   = new RestApiEndPoints();
                            $restApi->add_invite_user( $user_data, false, false, false );

                        }
                        $insert_results[] = sprintf( __( \'%s added & invited successfully.\', \'uncanny-learndash-groups\' ), $email );

                    //ADDED CODE
                     global $wpdb;

                     $user_id_for_um_groups = get_user_by( \'email\', $email );

                     $current_group_id_for_um_groups = $wpdb->get_var("SELECT ID FROM wpso_posts WHERE post_excerpt = $group_id");
                     $current_user_id_for_um_groups = get_current_user_id();
                     $wpdb->insert( \'wpso_um_groups_members\', array( 
                     \'group_id\' => $current_group_id_for_um_groups,
                     \'user_id1\' => $user_id_for_um_groups,
                     \'user_id2\' => $current_user_id_for_um_groups,
                     \'status\' => \'approved\',
                     \'role\' => \'member\') );
                    //END OF ADDED CODE

                    } else {
                        $error_results[] = sprintf( __( \'Line #%d: Email (%s) not correct.\', \'uncanny-learndash-groups\' ), $k + 1, $email );
                    }
                } else {
                    if ( ! empty( $first_names[ $k ] ) || ! empty( $last_names[ $k ] ) ) {
                        $error_results[] = sprintf( __( \'Line #%d: Email field is empty.\', \'uncanny-learndash-groups\' ), $k + 1 );
                    }
                }
            }
        }
        $url = SharedFunctions::get_group_management_page_id( true );
        $url .= \'?group-id=\' . $group_id;
        $url .= \'&bulk=1\';
        if ( ! empty( $error_results ) ) {
            $url .= \'&bulk-errors=\' . urlencode( join( \'<br /> \', $error_results ) );
        }
        if ( ! empty( $insert_results ) ) {
            $url .= \'&success-invited=\' . urlencode( join( \'<br /> \', $insert_results ) );
        }

        wp_safe_redirect( $url );
        exit;
    }
}

1 个回复
最合适的回答,由SO网友:Debbie Kurth 整理而成

我调整了您的代码,以确保在尝试处理之前定义了$电子邮件。我在问题所在的地方停下来。如果您遵循所有的If和else代码,当电子邮件地址不是特定组(订阅者、管理员、客户等)的一部分时,“continue”常驻者将跳过处理并转到下一封电子邮件。因此,您的呼叫可能不会被调用,因此您将无法返回结果。

我还在代码段顶部的表单条目中添加了一些重要的过滤器。

唯一的其他路径是,如果电子邮件未定义。

 public function bulk_add_users() 
    {
     if ( isset( $_GET[\'action\'] ) && \'bulk-add-users\' === $_GET[\'action\'] && isset( $_GET[\'group-id\'] ) ) {
         $group_id       = sanitize_text_field(absint( $_GET[\'group-id\']) );
         $first_names    = sanitize_text_field($_REQUEST[\'first_name\']);
         $last_names     = sanitize_text_field($_REQUEST[\'last_name\']);
         $emails         =  filter_var($_REQUEST[\'email\'], FILTER_VALIDATE_EMAIL);  
         $error_results  = [];
         $insert_results = [];

        if ( $emails ) 
         {
           $role = apply_filters( \'uo-groups-user-role\', get_option( \'default_role\', \'subscriber\' ) );
             foreach ( $emails as $k => $email ) 
            {
             if ( ! empty( $email ) ) 
               {
               if ( is_email( $email ) )
                  {
                   $first       = $first_names[ $k ];
                   $last        = $last_names[ $k ];
                   $email       = sanitize_email( $email );
                   $is_existing = email_exists( $email );
                   if ( is_numeric( $is_existing ) ) 
                     {
                     $user_id     = $is_existing;
                      $user_groups = learndash_get_users_group_ids( $user_id, true );

                      // THE POTENTIAL SKIP IS HERE. IF THE EMAIL IS NOT IN THE GROUP
                      if ( in_array( $group_id, $user_groups ) ) 
                      {
                      $error_results[] = sprintf( __( \'Line #%d: %s is existing user of group.\', \'uncanny-learndash-groups\' ), $k + 1, $email );

                      //*************IF YOU CONTINUE HERE, IT IS POSSIBLE TO NEVER REACH YOUR CODE
                       continue;     
                      }  // if ( in_array( $group_id, $user_groups ) 

                    //-------------------------------------
                    // Go here only if the email is in the group
                     $user_data = array(
                             \'user_email\' => $email,
                             \'user_id\'    => $user_id,
                             \'first_name\' => $first,
                             \'last_name\'  => $last,
                            \'role\'       => $role,
                        );
                    if ( isset( $user_data[\'first_name\'] ) && ! $is_existing )
                      update_user_meta( $user_id, \'first_name\', $user_data[\'first_name\'] );

                    if ( isset( $user_data[\'last_name\'] ) && ! $is_existing ) 
                            update_user_meta( $user_id, \'last_name\', $user_data[\'last_name\'] );

                        $restApi = new RestApiEndPoints();
                        $restApi->add_existing_user( $user_data, false, $group_id, 0, \'not redeemed\', false );




                }   //  if ( is_numeric( $is_existing ) )
              else 
                {
                 $user_data = array(
                 \'user_login\' => $email,
                 \'user_email\' => $email,
                 \'first_name\' => $first,
                 \'last_name\'  => $last,
                 \'role\'       => $role,
                 \'group_id\'   => $group_id,
                 );
                 $restApi   = new RestApiEndPoints();
                 $restApi->add_invite_user( $user_data, false, false, false );

               } //  if ( is_numeric( $is_existing ) ) 

                $insert_results[] = sprintf( __( \'%s added & invited successfully.\', \'uncanny-learndash-groups\' ), $email );


                //------------YOUR ADDED CODE
                // Not ever reached on a continue or if the email is not defined in the form
                 global $wpdb;

              $user_id_for_um_groups = get_user_by( \'email\', $email );
              $current_group_id_for_um_groups = $wpdb->get_var("SELECT ID FROM wpso_posts WHERE post_excerpt = $group_id");
              $current_user_id_for_um_groups = get_current_user_id();
              $wpdb->insert( \'wpso_um_groups_members\', array( 
              \'group_id\' => $current_group_id_for_um_groups,
              \'user_id1\' => $user_id_for_um_groups,
              \'user_id2\' => $current_user_id_for_um_groups,
              \'status\' => \'approved\',
              \'role\' => \'member\') );
             //----------- YOUR END OF ADDED CODE
              } 
            else
              $error_results[] = sprintf( __( \'Line #%d: Email (%s) not correct.\', \'uncanny-learndash-groups\' ), $k + 1, $email );

             }     // if ( is_email( $email ) ) 
          }        //  if ( ! empty( $email ) ) 
        }          //  foreach ( $emails as $k => $email ) 
     }            // if ( $emails ) 


      .............................
   } // public function bulk_add_users() 

相关推荐

Calling hooks in functions

我习惯于使用主题挂钩,定期添加功能,但难以使用插件挂钩来过滤内容。这是我所知的极限,我试图理解这个过程,但没有用。我正在使用GigPress 管理事件。它允许您添加一个节目(基于日期),并将该节目与帖子(标准WP帖子)关联。我已经创建了一个自定义的帖子类型—productions—来关联节目,而不仅仅是一般的博客帖子。插件有一个钩子-gigpress_related_post_types —允许您用自己选择的任何CPT交换帖子。如果我直接编辑插件,它就会工作,将“帖子”替换为“产品”。如果我添加了我希望是