pronamic / wp-pay-core

Core components for the WordPress payment processing library. This library is used in the WordPress plugin Pronamic Pay: https://www.pronamicpay.com/, but also allows other plugin developers to set up a payment plugin.
https://www.wp-pay.org/
GNU General Public License v3.0
27 stars 3 forks source link

Create user if user does not exists #38

Closed knit-pay closed 3 years ago

knit-pay commented 3 years ago

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?

remcotolsma commented 3 years ago

Sorry for the delay and thanks for your idea. In the s2Member extension we do something similar:

https://github.com/wp-pay-extensions/s2member/blob/3f5953a9539c339f8e8e435484d280d1b7b0c399/src/Extension.php#L178-L198

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

knit-pay commented 3 years ago

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.

remcotolsma commented 3 years ago

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.