Closed knit-pay closed 3 years ago
Sorry for the delay and thanks for your idea. In the s2Member extension we do something similar:
It's a few lines of code to retrieve a WordPress user by email and to create a new one:
We understand it would be easier if these two functions would be combined in a function. In Laravel Eloquent there are model methods like User::firstOrCreate()
:
https://laravel.com/docs/8.x/eloquent#retrieving-or-creating-models
You could implement something similar in your payment / extension library. A quick example:
<?php
namespace KnitPay\WordPress\Pay\Users;
use WP_Error;
use WP_User;
class User {
public static function first_or_create( $email ) {
$result = \get_user_by( 'email', $email );
if ( $result instanceof WP_User ) {
return $result;
}
$result = \wp_create_user( $email, wp_generate_password( 10 ), $email );
if ( $result instanceof WP_Error ) {
throw new \Exception( $result->get_error_message() );
}
return new WP_User( $result );
}
}
https://stackoverflow.com/questions/17935647/symfony2-doctrine-findoneorcreate
Thanks for the answer. Yes, I already know about these functions. And yes, we can use it like you have used it in s2member extension.
https://developer.wordpress.org/reference/functions/get_user_by/
https://developer.wordpress.org/reference/functions/wp_create_user/
My idea is to have this function in wp-pay/core so that we can directly call this function in all the extension where ever it is required instead of re-writing the same code in all the extensions.
Yes, you are more than welcome to develop something like this. If would be great if you can also share some usage examples. Are the payment extensions you are working on also available on GitHub? For example, with s2Member we also need the password for the account confirmation email. This has to be taken into account when implementing such a function. I can imagine that in addition to a password, other usermeta will also play a role in the future.
Hello Team
in some WordPress plugins, there is an option to make the payment before the user signup or even enters his email. For such plugins, it would be great if we will have a common function in the core to create a user if the user does not exist. We can call this function during "pronamic_payment_statusupdate" action.
This function can take a parameter of "email address" and can return user_id or WordPress user object. This function will first try to find if any existing user is there with the provided email address and refund if exists. or if it will not find any existing user, then it will create a new user and return it.
What do you think about it? I can also work on this enhancement if you are busy, please suggest which file and class will be a good place for this kind of function?