Skip to content

Commit

Permalink
Improve Telegram login handler (#224)
Browse files Browse the repository at this point in the history
* Improve Telegram login handler

* Update LoginHandler.php
  • Loading branch information
irshadahmad21 authored Dec 5, 2024
1 parent 40663d4 commit d179cce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-onions-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wptelegram-login": patch
---

Improved login handler to add more actions and filters
26 changes: 18 additions & 8 deletions plugins/wptelegram-login/src/shared/LoginHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,12 @@ public function save_user_data( $auth_data, $wp_user_id = null ) {
*/
$user_login = apply_filters( 'wptelegram_login_unique_username', $unique_username );

$user_pass = wp_generate_password();

$role = WPTG_Login()->options()->get( 'user_role' );

// Create the user without first and last name to avoid wp_insert_user() failing on multi-byte characters.
$userdata = compact( 'user_pass', 'user_login', 'role' );
$userdata = [
'user_pass' => wp_generate_password(),
'user_login' => $user_login,
'role' => WPTG_Login()->options()->get( 'user_role' ),
];

/**
* Filter the user data before inserting the user into the database.
Expand All @@ -583,6 +583,14 @@ public function save_user_data( $auth_data, $wp_user_id = null ) {
*/
$userdata = apply_filters( 'wptelegram_login_insert_user_data', $userdata, $auth_data );

/**
* Fires before the user is inserted into the database.
*
* @param array $userdata The user data inserted into the database.
* @param array $auth_data The user data from Telegram.
*/
do_action( 'wptelegram_login_pre_insert_user', $userdata, $auth_data );

$wp_user_id = wp_insert_user( $userdata );

if ( is_wp_error( $wp_user_id ) ) {
Expand All @@ -601,9 +609,11 @@ public function save_user_data( $auth_data, $wp_user_id = null ) {

/* Update the user */

$ID = $wp_user_id; // phpcs:ignore WordPress.NamingConventions.ValidVariableName -- Ignore snake_case

$userdata = compact( 'ID', 'first_name', 'last_name' );
$userdata = [
'ID' => $wp_user_id,
'first_name' => $first_name,
'last_name' => $last_name,
];

/**
* Filter the user data before updating the user in the database.
Expand Down

0 comments on commit d179cce

Please sign in to comment.