Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reader registration on donate #1655

Merged
merged 22 commits into from
May 27, 2022

Conversation

miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented May 19, 2022

All Submissions:

Changes proposed in this Pull Request:

Implements reader registration functionality to the donation block.

Auth intention

If the reader registration detects an existing user, a cookie stores the auth intention that is accessible through the Reader_Activation::get_auth_intention() method. This API can be used across other plugins to improve UX over resolving the reader authentication, subscription, donation, etc.

This PR currently implements the auth intention to the wp_login_form() defaults, which is used by the core's Login block. Unfortunately this hook is not applied to the WooCommerce's My Account page login form.

The reader

The PR also proposes the definition of a reader user, which can be any user with the np_reader meta or within the roles subscriber or customer. Identifying a reader helps us build flows tied specifically to this type of user, such as the magic link functionality.

Email verification

Reader registration aims for a quick user registration and doesn't impose any validation for a new reader to be created and authenticated. The Reader_Activation::verify_reader_email() method is public but also executed on the reset password form action hook (resetpass_form). Once a registered reader clicks on the reset password link received on their email, it'll trigger the method to make the reader verified.

Experimental flag

This PR also proposes that all experimental features related to reader activation should be placed behind NEWSPACK_EXPERIMENTAL_READER_ACTIVATION, or the Reader_Activation::is_enabled() method for easier deprecation of the EXPERIMENTAL flag in the future.

How to test the changes in this Pull Request:

  1. Check out this branch and make sure you have the AMP Plus flag and Reader Revenue setup with a test Stripe account
  2. Add the experimental flag to your config: define( 'NEWSPACK_EXPERIMENTAL_READER_ACTIVATION', true );
  3. While logged out, visit a page with the Donate block and send a test donation with an email that does not exist
  4. Visit WooCommerce's my-account/orders page and confirm you are logged in and you see the donation order
  5. Log out, donate again with the same email
  6. Attempt to visit my-account/orders and confirm you are not authenticated
  7. Check the browser cookies and confirm the np_auth_intention cookie with the email as the value
  8. Logged-in as admin, visit the Orders dashboard and confirm the last donation is not connected to the existing customer

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@miguelpeixe miguelpeixe requested a review from a team as a code owner May 19, 2022 23:13
@miguelpeixe miguelpeixe self-assigned this May 19, 2022
@miguelpeixe miguelpeixe added the [Status] Needs Review The issue or pull request needs to be reviewed label May 19, 2022
@yogeshbeniwal
Copy link
Contributor

@miguelpeixe Depending on what is the next/roadmap for reader revenue, shall there be an option to change title of button from donate.

Screenshot

@miguelpeixe
Copy link
Member Author

Hi, @yogeshbeniwal! I don't think we have that in our roadmap. I like that suggestion, do you mind filing an issue so we can have a dedicated place for this discussion?

@adekbadek adekbadek self-requested a review May 20, 2022 15:22
Copy link
Member

@adekbadek adekbadek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some stuff to iron out, but for the general approach is good!

Unfortunately [login form] hook is not applied to the WooCommerce's My Account page login form.

Is there no WooCommerce hook that could be used here?

includes/class-reader-activation.php Show resolved Hide resolved
includes/class-reader-activation.php Show resolved Hide resolved
includes/class-reader-activation.php Outdated Show resolved Hide resolved
includes/class-reader-activation.php Outdated Show resolved Hide resolved
includes/class-reader-activation.php Outdated Show resolved Hide resolved
includes/class-reader-activation.php Show resolved Hide resolved
includes/reader-revenue/class-stripe-connection.php Outdated Show resolved Hide resolved
} else {
Logger::log( 'This order will result in a membership, creating account for user.' );
$user_login = sanitize_title( $full_name );
$user_id = wc_create_new_customer( $email_address, $user_login, '', [ 'display_name' => $full_name ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user creation is duplicated – here and in the Reader Activation module. The module should expose a method for handling user creation, and the is_enabled only used for the reader-activation-flow specific tasks: currently adding the meta fields. Or, the user creation should be delegated to the main Newspack class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call duplicated, the original intended behavior is preserved while reader activation is experimental.

WP core already provides a straightforward API for generic user creation that should be used if not making use of reader activation logic. I don't see why reader activation would provide methods outside of its concept.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about a method that would either wp_insert_user or wc_create_new_customer – but on a second thought it should not be used here because at this point we only wanna handle customer creation, not user creation. I was overthinking it.

Still, I think it'd be more readable if Reader_Activation - in this code block - only set metadata on the created user, not take over the user creation and flow. Something like:

$user_id = wc_create_new_customerif ( Reader_Activation::is_enabled() ) {
  Reader_Activation::setup_reader( $user_id );
}
wp_set_current_user …

But it's not a blocker.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "reader" is a fairly flexible concept:

https://github.com/Automattic/newspack-plugin/blob/feat/reader-activation-donate-block/includes/class-reader-activation.php#L131-L154

Currently any subscriber or customer is eligible to use the reader-related tools, including register_reader( $email ). What happens when we call setup_reader( $user_id )?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify on why we have the user meta to also determine the same thing: the user roles that determine a reader are filterable. If by any chance this is filtered to an empty array, we must still be able to know readers that were created through this flow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when we call setup_reader( $user_id )?

The meta fields are set up. Actually, it can be register_reader – it can be called with an existing user ID.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting idea, we should probably do it in a separate method to not necessarily trigger all the side effects of a reader registration if we just want to set the user as a reader.

If you don't mind, I think we could work on that on a separate PR, once we have more practical use on other cases.

includes/class-reader-activation.php Outdated Show resolved Hide resolved
includes/class-reader-activation.php Show resolved Hide resolved
@miguelpeixe miguelpeixe requested a review from adekbadek May 24, 2022 18:52
@miguelpeixe
Copy link
Member Author

634c385 (with a typo fix at b0e8696) adds an additional test to assert that a regular subscriber is also a reader.

@miguelpeixe
Copy link
Member Author

Another small change with c052066. Use wp_set_auth_cookie() $remember by default. We want that auth to last for a better overall reader experience.

Copy link
Member

@adekbadek adekbadek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One renaming was neglected, otherwise it's looking great! Good to see some tests, too.

includes/class-reader-activation.php Outdated Show resolved Hide resolved
} else {
Logger::log( 'This order will result in a membership, creating account for user.' );
$user_login = sanitize_title( $full_name );
$user_id = wc_create_new_customer( $email_address, $user_login, '', [ 'display_name' => $full_name ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about a method that would either wp_insert_user or wc_create_new_customer – but on a second thought it should not be used here because at this point we only wanna handle customer creation, not user creation. I was overthinking it.

Still, I think it'd be more readable if Reader_Activation - in this code block - only set metadata on the created user, not take over the user creation and flow. Something like:

$user_id = wc_create_new_customerif ( Reader_Activation::is_enabled() ) {
  Reader_Activation::setup_reader( $user_id );
}
wp_set_current_user …

But it's not a blocker.

@miguelpeixe miguelpeixe requested a review from adekbadek May 27, 2022 12:14
@github-actions github-actions bot added [Status] Approved The pull request has been reviewed and is ready to merge and removed [Status] Needs Review The issue or pull request needs to be reviewed labels May 27, 2022
@miguelpeixe
Copy link
Member Author

Thank you for reviewing, @adekbadek! 🙇

@miguelpeixe miguelpeixe merged commit 5821b57 into master May 27, 2022
@miguelpeixe miguelpeixe deleted the feat/reader-activation-donate-block branch May 27, 2022 15:31
@miguelpeixe miguelpeixe mentioned this pull request May 27, 2022
6 tasks
matticbot pushed a commit that referenced this pull request Jun 2, 2022
# [1.84.0-alpha.1](v1.83.1...v1.84.0-alpha.1) (2022-06-02)

### Bug Fixes

* **reader-revenue:** initial order state with total of 0 ([7c30b09](7c30b09))

### Features

* **ads:** handle gam default ad units ([#1654](#1654)) ([321b98e](321b98e))
* reader registration on donate ([#1655](#1655)) ([5821b57](5821b57))
* remove theme selection from setup wizard ([#1656](#1656)) ([94e4580](94e4580))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.84.0-alpha.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Jun 13, 2022
# [1.84.0](v1.83.3...v1.84.0) (2022-06-13)

### Bug Fixes

* **ads:** resolve conflicts from hotfix merge ([#1685](#1685)) ([8ce12cd](8ce12cd))
* **reader-revenue:** initial order state with total of 0 ([7c30b09](7c30b09))

### Features

* **ads:** handle gam default ad units ([#1654](#1654)) ([321b98e](321b98e))
* **analytics:** automatically link GA4 with Site Kit ([#1698](#1698)) ([266135f](266135f))
* reader registration on donate ([#1655](#1655)) ([5821b57](5821b57))
* remove theme selection from setup wizard ([#1656](#1656)) ([94e4580](94e4580))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.84.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @alpha released [Status] Approved The pull request has been reviewed and is ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants