Commit 2c1657dd authored by Christian Wach's avatar Christian Wach

Switch hooks to upgrade routine

parent 53db5912
......@@ -80,6 +80,9 @@ class CiviCRM_Permissions_Sync {
return;
}
// Do upgrade tasks.
$this->upgrade_tasks();
// Register hooks.
$this->register_hooks();
......@@ -95,37 +98,83 @@ class CiviCRM_Permissions_Sync {
/**
* Register hooks.
* Perform upgrade tasks.
*
* If this plugin is activated after CiviCRM itself is activated, then we
* need another way to trigger sync which doesn't rely on CiviCRM's hooks.
*
* @see $this->register_hooks()
*
* This method is written as a substitute for registering activation hooks
* because, in multisite, a network-activated plugin will not inform all
* sites in the network of it's activation.
*
* @see https://core.trac.wordpress.org/ticket/14170#comment:68
*
* The 'init' hook is where most role-related changes are made, so we can
* use that to perform permissions sync each time this plugin is upgraded.
*
* @since 1.0
*/
public function register_hooks() {
public function upgrade_tasks() {
// Get installed plugin version for this site.
$this->plugin_version = get_option( 'civicrm_permissions_sync_version', 'false' );
/*
* The following two hooks are native to the CiviCRM WordPress plugin.
*
* If this plugin is not active when CiviCRM itself is activated, then
* (obviously) the callbacks will never run. If it is active, however,
* then this is quite neat.
*/
// If this is a new install.
if ( $this->plugin_version === 'false' ) {
// Do something.
}
*/
// Filter minimum CiviCRM capabilities.
add_filter( 'civicrm_min_capabilities', [ $this, 'capabilities_minimum' ], 20, 1 );
// If the version has changed.
if ( $this->plugin_version != CIVICRM_PERMISSIONS_SYNC_VERSION ) {
// Sync when CiviCRM activation action fires.
add_action( 'civicrm_activation', [ $this, 'capabilities_sync' ], 20 );
// Add minimum CiviCRM capabilities to all roles.
add_action( 'init', [ $this, 'capabilities_all_roles' ], 100 );
// Sync late on init.
add_action( 'init', [ $this, 'capabilities_sync' ], 100 );
}
/*
* If this plugin is activated after CiviCRM itself is activated, then
* we need other events to hook into. The 'init' hook is where most
* role-related changes are made, so use that.
*/
// For specific upgrades, use something like the following.
if ( version_compare( CIVICRM_PERMISSIONS_SYNC_VERSION, '1.0.1', '>=' ) ) {
// Do something.
}
*/
// Store version if there has been a change.
if ( $this->plugin_version != CIVICRM_PERMISSIONS_SYNC_VERSION ) {
update_option( 'civicrm_permissions_sync_version', CIVICRM_PERMISSIONS_SYNC_VERSION );
$this->plugin_version = CIVICRM_PERMISSIONS_SYNC_VERSION;
}
}
/**
* Register hooks.
*
* The two hooks referenced here are native to the CiviCRM WordPress plugin.
* If this plugin active when CiviCRM itself is activated, then these hooks
* provide a neat way of ensuring capabilities are synced at the point when
* CiviCRM is activated.
*
* @see $this->upgrade_tasks()
*
* @since 1.0
*/
public function register_hooks() {
// Add minimum CiviCRM capabilities to all roles.
add_action( 'init', [ $this, 'capabilities_all_roles' ], 100 );
// Filter minimum CiviCRM capabilities.
add_filter( 'civicrm_min_capabilities', [ $this, 'capabilities_minimum' ], 20, 1 );
// Sync late on init.
add_action( 'init', [ $this, 'capabilities_sync' ], 100 );
// Sync when CiviCRM activation action fires.
add_action( 'civicrm_activation', [ $this, 'capabilities_sync' ], 20 );
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment