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

Custom User Role based on Unlock Status #51

Open
goodlookingout opened this issue May 31, 2022 · 0 comments
Open

Custom User Role based on Unlock Status #51

goodlookingout opened this issue May 31, 2022 · 0 comments

Comments

@goodlookingout
Copy link
Contributor

I've been trying to wrap my head around getting Unlock to change user roles based on Unlock status, Similar to the event.detail.state (locked/unlocked) in the 'Unlock Event Handler' [Script] .

I've found a solution that works for me but can be improved.
It's just a few lines of code added.

The code below instructs Unlock to check user roles and change/remove roles based on unlock block status.

ex. User registers to site & gets default subscriber role. User pays to view content that's locked. Unlock detects user is paid subscriber, removes lock to content . User 'subscriber' role gets removed & 'contributor' role gets added. Unlock Detects user is no longer a paid subscriber , locks content & removes 'contributor role & sets subscriber role.

Located in the (class-unlock-box-block.php) file

`$locks = $attributes['locks'];

	$settings = get_option( 'unlock_protocol_settings', array() );
	$networks = isset( $settings['networks'] ) ? $settings['networks'] : array();
	// Get the author
	$author = wp_get_current_user();

	if ( Unlock::has_access( $networks, $locks ) ) {
		// Remove role
   $author->remove_role( 'subscriber' );

   // Add role
   $author->set_role( 'contributor' );
	return $content;
	}

	// Remove role
   $author->remove_role( 'contributor' );

   // Add role
   $author->set_role( 'subscriber' );
	return $this->get_checkout_url( $attributes );
}

class-unlock-box-block copy.txt
`
What would be neat is an option for each lock to determine a user role . Since we can add multiple locks to a block why not add multiple roles corresponding to those locks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant