Skip to content

Custom User Role based on Unlock Status #51

@goodlookingout

Description

@goodlookingout

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions