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(metering): dispatch RAS activity on content restriction #3437

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented Sep 20, 2024

All Submissions:

Changes proposed in this Pull Request:

1200550061930446-as-1208347477266676/f

Because our content restriction strategy implements new layers on top of Woo Memberships for metered content restriction, we don't fully support 3rd parties to leverage our implementation for their content features.

2 public methods can already be used to detect content restriction on the Newspack side:

  • Newspack\Memberships::is_post_restricted( $post_id );: whether the post is restricted on the backend.
  • Newspack\Memberships\Metering::is_metering();: whether the restricted post (method above) should be allowed to render due to metering rules. This will always be reading from the global $post and should only be used in a is_singular() context.

The second method will also return true if the metering is supposed to happen in the front-end (anonymous metering). That's what we don't have an API for.

This PR proposes this API to be a listener on a RAS activity dispatch so that it can be used like this:

window.newspackRAS = window.newspackRAS || [];
window.newspackRAS.push( ras => {
	ras.on( 'activity', function( ev ) {
		const { action, data } = ev.details;
		if ( 'metering_restricted' === action ) {
			// Do stuff here.
			console.log( data );
		}
	} );
} );

The data payload:

{
  "post_id": "117",          // Restricted post ID
  "metering": {              // Current metering configuration for the reader
    "content": [             // List of allowed post IDs
      "122"
    ],
    "expiration": 1726887600 // Metering expiration
  }
}

How to test the changes in this Pull Request:

  1. Make sure you have RAS configured with Woo Memberships and content gating
  2. Edit your content gate to allow metered access of 1 post for anonymous readers
  3. In a fresh session, visit 2 articles so you hit the gate
  4. Open DevTools, inspect localStorage, and confirm the np_reader_1_activity item includes a metering_restricted activity with the data payload as exemplified above

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 added the [Status] Needs Review The issue or pull request needs to be reviewed label Sep 20, 2024
@miguelpeixe miguelpeixe self-assigned this Sep 20, 2024
@miguelpeixe miguelpeixe requested a review from a team as a code owner September 20, 2024 13:20
Copy link
Contributor

@dkoo dkoo left a comment

Choose a reason for hiding this comment

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

Nice and simple solution using the reader data API!

@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 Sep 20, 2024
@miguelpeixe miguelpeixe merged commit 4e1c262 into trunk Sep 25, 2024
9 checks passed
@miguelpeixe miguelpeixe deleted the feat/ras-dispatch-metering-restricted branch September 25, 2024 08:50
matticbot pushed a commit that referenced this pull request Oct 14, 2024
# [5.6.0-alpha.1](v5.5.1...v5.6.0-alpha.1) (2024-10-14)

### Bug Fixes

* cancelled subscriptions sync ([#3466](#3466)) ([6cad50a](6cad50a))
* **woocommerce:** update how order meta are updated ([#2711](#2711)) ([ae75548](ae75548))

### Features

* **metering:** dispatch RAS activity on content restriction ([#3437](#3437)) ([4e1c262](4e1c262))
@matticbot
Copy link
Contributor

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

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Oct 28, 2024
# [5.6.0](v5.5.2...v5.6.0) (2024-10-28)

### Bug Fixes

* cancelled subscriptions sync ([#3466](#3466)) ([6cad50a](6cad50a))
* **woocommerce:** update how order meta are updated ([#2711](#2711)) ([ae75548](ae75548))

### Features

* add user name to woocommerce data events ([#3473](#3473)) ([2b57d27](2b57d27))
* command to initialize cron job to slowly backfill CAP term data ([#3425](#3425)) ([0b0d79a](0b0d79a))
* **metering:** dispatch RAS activity on content restriction ([#3437](#3437)) ([4e1c262](4e1c262))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 5.6.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.

3 participants