我正在编辑一个插件的代码,以添加我需要的一个小功能。我几乎成功了,但我真的很挣扎,不明白为什么
$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;
}
}