Lock s2Member roles during plugin update

Do not reset s2Member WordPress Plugin Roles

s2Member Roles

Data: You use s2Member WordPress plugin from PrimoThemes successfully.
For those who don’t know, this plugin supports powerful membership capabilities, protect members only content, integrates with PayPal, supports recurring billing, custom pages for registration, etc. It is a right tool if you wish to sell some of your blog content to your subscribers or just restrict access to some posts, pages or downloads.
Finally, you know s2Member and WordPress good enough to make your own changes to its user permissions systems. You edited one of S2member created user roles: “s2Member Level 1”, “s2Member Level 2”, “s2Member Level 3” or “s2Member Level 4” and you added some new capabilities to it. You can use User Role Editor WordPress plugin, which helps you make such changes very easy.
Problem: You lose all your custom changes to the s2Member user roles after every s2Member plugin update. What’s going wrong? What to do? Who can help?
Decision: Read this post to know how to leave your customized s2Member user roles unchanged.
1st, let’s answer on the question, what’s going? When you click update plugin link WordPress deactivates that plugin, download new version, replaces old files with new package and activates updated plugin again. So we need to analyse what s2Member with user roles doing during its activation.
File s2member/includes/hooks.inc.php has this line:

174
register_activation_hook ($GLOBALS["WS_PLUGIN__"]["s2member"]["l"], "c_ws_plugin__s2member_installation::activate");

Go to class c_ws_plugin__s2member_installation definition and see code for activate method. It could be found at installation.inc.php file, line 28:

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class c_ws_plugin__s2member_installation
{
	/**
	* Activation routines for s2Member.
	*
	* @package s2Member\Installation
	* @since 3.5
	*
	* @return null
	*/
	public static function activate ($reactivation_reason = FALSE)
	{
		global $wpdb; /* Global database object reference. */
		global $current_site, $current_blog; /* Multisite. */
		/**/
		do_action ("ws_plugin__s2member_before_activation", get_defined_vars ());
		/**/
		c_ws_plugin__s2member_roles_caps::config_roles (); /* Config Roles/Caps. */
		/**/

At line 45 above we see c_ws_plugin__s2member_roles_caps class config_roles() method call. It seems that we are very near to our purpose. Let’s see to the config_roles() code. Class c_ws_plugin__s2member_roles_caps definition is placed to roles-caps.inc.php file.

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class c_ws_plugin__s2member_roles_caps
{
/**
* Configures Roles/Capabilities.
*
* @package s2Member\Roles_Caps
* @since 110524RC
*
* @return null
*
* @todo Finalize support for unlimited Levels/Roles.
* @todo Finalize support for independent Capabilities.
*/
public static function config_roles ()
{
	do_action ("ws_plugin__s2member_before_config_roles", get_defined_vars ());
	/**/
	if (!apply_filters ("ws_plugin__s2member_lock_roles_caps", false))
	{
	c_ws_plugin__s2member_roles_caps::unlink_roles ();

At line 47 unlink_roles() method is called which deletes all existing s2Member roles. The rest part of code creates s2Member roles again from the scratch. That’s why you loses all your custom changes in the s2Member plugin user roles after every s2Member update.

Is it possible to install update and save your changes untouched? Yes, it’s possible, thanks to s2Member plugin author. Look at the line 45. Roles will be initialized just in case if filter with name ‘ws_plugin__s2member_lock_roles_caps’ doesn’t exist.
Congratulations! We found the decision. Just add following code to your theme functions.php file:

function s2member_lock_roles_caps() {
  return true;
}
add_filter('ws_plugin__s2member_lock_roles_caps', 's2member_lock_roles_caps');

Defining this filter you protect any changes you made to s2Member user roles. s2Member will not touch its roles during update anymore. The only thing you should remember, if s2Member update will changes anything in its roles you can miss that changes. So read update notes carefully to not miss important update in the roles stuff.

Tags: , , ,