reset-wp-civicrm-roles-caps.php 15.5 KB
Newer Older
Christian Wach's avatar
Christian Wach committed
1 2 3 4 5 6
<?php /*
--------------------------------------------------------------------------------
Plugin Name: CiviCRM Permissions Sync
Plugin URI: https://develop.tadpole.cc/plugins/reset-wp-civicrm-roles-caps
Description: Keeps CiviCRM permissions in sync with WordPress capabilities so that they are exposed to other plugins.
Author: Tadpole Collective
7 8
Author URI: https://tadpole.cc
Version: 1.0
Christian Wach's avatar
Christian Wach committed
9 10 11 12 13 14
License: GPLv2
Text Domain: reset-wp-civicrm-roles-caps
Domain Path: /languages
--------------------------------------------------------------------------------
Based upon code in https://github.com/civicrm/civicrm-wordpress/blob/master/includes/civicrm.users.php
--------------------------------------------------------------------------------
15 16 17
*/


Christian Wach's avatar
Christian Wach committed
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Set the plugin sync mode.
 *
 * This must be set prior to activating the plugin.
 *
 * Possible values are:
 *
 * 'all':    Syncs permissions to both our custom role and the "Groups" plugin.
 * 'role':   Only syncs permissions to our custom role.
 * 'groups': Only syncs permissions to the "Groups" plugin.
 *
 * The existence of our sync role may confuse existing admin users who see it in
 * the WordPress UI and think that it serves a purpose other than allowing other
 * plugins to discover CiviCRM's permissions.
 *
 * An alternative to this is to use the "Groups" plugin and ensure that user
 * capabilities are derived solely from membership of a "Groups" group.
 *
 * @since 1.0
 */
if ( ! defined( 'CIVICRM_PERMISSIONS_SYNC_MODE' ) ) {
	define( 'CIVICRM_PERMISSIONS_SYNC_MODE', 'groups' );
}

Christian Wach's avatar
Christian Wach committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// Version.
define( 'CIVICRM_PERMISSIONS_SYNC_VERSION', '1.0' );

// Store reference to this file.
define( 'CIVICRM_PERMISSIONS_SYNC_FILE', __FILE__ );

// Store URL to this plugin's directory.
if ( ! defined( 'CIVICRM_PERMISSIONS_SYNC_URL' ) ) {
	define( 'CIVICRM_PERMISSIONS_SYNC_URL', plugin_dir_url( CIVICRM_PERMISSIONS_SYNC_FILE ) );
}

// Store PATH to this plugin's directory.
if ( ! defined( 'CIVICRM_PERMISSIONS_SYNC_PATH' ) ) {
	define( 'CIVICRM_PERMISSIONS_SYNC_PATH', plugin_dir_path( CIVICRM_PERMISSIONS_SYNC_FILE ) );
}
58

Christian Wach's avatar
Christian Wach committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88


/**
 * CiviCRM Permissions Sync class.
 *
 * A class for encapsulating plugin functionality.
 *
 * @since 1.0
 */
class CiviCRM_Permissions_Sync {

	/**
	 * Custom role name.
	 *
	 * @since 1.0
	 * @access public
	 * @var str $custom_role_name The name of the custom role.
	 */
	public $custom_role_name = 'civicrm_admin';

	/**
	 * Additional minimum capabilities.
	 *
	 * @since 1.0
	 * @access public
	 * @var array $min_capabilities The array of additional minimum capabilities.
	 */
	public $min_capabilities = array(
		'access_ajax_api' => 1,
		'view_my_invoices' => 1,
89 90 91
	);


Christian Wach's avatar
Christian Wach committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105

	/**
	 * Initialise this object.
	 *
	 * @since 1.0
	 */
	public function __construct() {

		// Init translation.
		$this->translation();

		// Bail if CiviCRM plugin is not present.
		if ( ! function_exists( 'civi_wp' ) ) {
			return;
106
		}
Christian Wach's avatar
Christian Wach committed
107

108 109 110
		// Do upgrade tasks.
		$this->upgrade_tasks();

Christian Wach's avatar
Christian Wach committed
111 112 113 114 115 116 117 118 119 120
		// Register hooks.
		$this->register_hooks();

		/**
		 * Broadcast that this plugin is now loaded.
		 *
		 * @since 1.0
		 */
		do_action( 'civicrm_permissions_sync_loaded' );

121 122 123 124
	}



Christian Wach's avatar
Christian Wach committed
125
	/**
126 127 128 129 130 131 132 133 134 135 136 137 138
	 * 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
	 *
139
	 * The 'admin_init' hook is recommended for plugin upgrade tasks, so we can
140
	 * use that to perform permissions sync each time this plugin is upgraded.
Christian Wach's avatar
Christian Wach committed
141 142 143
	 *
	 * @since 1.0
	 */
144 145 146 147
	public function upgrade_tasks() {

		// Get installed plugin version for this site.
		$this->plugin_version = get_option( 'civicrm_permissions_sync_version', 'false' );
Christian Wach's avatar
Christian Wach committed
148 149

		/*
150 151 152 153 154
		// If this is a new install.
		if ( $this->plugin_version === 'false' ) {
			// Do something.
		}
		*/
Christian Wach's avatar
Christian Wach committed
155

156 157
		// If the version has changed.
		if ( $this->plugin_version != CIVICRM_PERMISSIONS_SYNC_VERSION ) {
Christian Wach's avatar
Christian Wach committed
158

159
			// Add minimum CiviCRM capabilities to all roles.
160
			add_action( 'admin_init', [ $this, 'capabilities_all_roles' ], 100 );
161 162

			// Sync late on init.
163
			add_action( 'admin_init', [ $this, 'capabilities_sync' ], 100 );
164 165

		}
Christian Wach's avatar
Christian Wach committed
166 167

		/*
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		// 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() {
Christian Wach's avatar
Christian Wach committed
197

198 199
		// Filter minimum CiviCRM capabilities.
		add_filter( 'civicrm_min_capabilities', [ $this, 'capabilities_minimum' ], 20, 1 );
Christian Wach's avatar
Christian Wach committed
200

201 202
		// Sync when CiviCRM activation action fires.
		add_action( 'civicrm_activation', [ $this, 'capabilities_sync' ], 20 );
Christian Wach's avatar
Christian Wach committed
203

204 205
	}

Christian Wach's avatar
Christian Wach committed
206 207 208 209 210 211 212 213 214 215 216 217 218


	/**
	 * Load translation.
	 *
	 * @since 1.0
	 */
	public function translation() {

		load_plugin_textdomain(
			'reset-wp-civicrm-roles-caps', // Unique name.
			false, // Deprecated argument.
			dirname( plugin_basename( __FILE__ ) ) . '/languages/' // Relative path.
219
		);
Christian Wach's avatar
Christian Wach committed
220 221 222 223 224 225 226 227 228 229

	}



	/**
	 * Filter minimum CiviCRM capabilities.
	 *
	 * The standard CiviCRM install misses out a few capabilities which many
	 * installs need to function as expected. They are added here, but may
230 231
	 * themselves be filtered by hooking in to `civicrm_min_capabilities` and
	 * `init` with a priority greater than those used by this plugin.
Christian Wach's avatar
Christian Wach committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The existing minimum capabilities.
	 * @return array $capabilities The modified minimum capabilities.
	 */
	public function capabilities_minimum( $capabilities = array() ) {

		// Add our extra capabilities.
		foreach( $this->min_capabilities AS $capability => $value ) {
			$capabilities[$capability] = 1;
		}

		// --<
		return $capabilities;

248
	}
Christian Wach's avatar
Christian Wach committed
249 250 251 252



	/**
Christian Wach's avatar
Christian Wach committed
253
	 * Add minimum CiviCRM capabilities to all roles.
Christian Wach's avatar
Christian Wach committed
254 255 256 257 258 259
	 *
	 * This method adds a few capabilities which many CiviCRM installs need to
	 * function as expected.
	 *
	 * @since 1.0
	 */
Christian Wach's avatar
Christian Wach committed
260
	public function capabilities_all_roles() {
Christian Wach's avatar
Christian Wach committed
261

Christian Wach's avatar
Christian Wach committed
262 263
		// Fetch roles object.
		$wp_roles = wp_roles();
Christian Wach's avatar
Christian Wach committed
264

Christian Wach's avatar
Christian Wach committed
265 266 267 268 269 270 271
		// Add capabilities to all roles if not already added.
		foreach( $wp_roles->role_names AS $role_name => $title ) {
			$role = $wp_roles->get_role( $role_name );
			foreach( $this->min_capabilities AS $capability => $value ) {
				if ( ! $role->has_cap( $capability ) ) {
					$role->add_cap( $capability );
				}
Christian Wach's avatar
Christian Wach committed
272 273 274
			}
		}

275 276
	}

277

Christian Wach's avatar
Christian Wach committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

	/**
	 * Sync capabilities to WordPress.
	 *
	 * Most plugins that deal with capabilities discover them by inspecting the
	 * roles in WordPress. There are other places that some plugins also inspect
	 * such as Custom Post Types and plugins such as WooCommerce and bbPress. We
	 * don't need to concern ourselves with these subsequent inspections, since
	 * adding all CiviCRM permissions to a WordPress role is enough to make them
	 * discoverable.
	 *
	 * @since 1.0
	 */
	public function capabilities_sync() {

		// Bail if CiviCRM not initialised.
		if ( ! $this->is_civicrm_initialised() ) {
			return;
		}

		// Get all CiviCRM permissions, excluding disabled components and descriptions.
		$permissions = CRM_Core_Permission::basicPermissions( false, false );

		// Convert to WordPress capabilities.
		$capabilities = array();
		foreach( $permissions AS $permission => $title ) {
			$capabilities[] = CRM_Utils_String::munge( strtolower( $permission ) );
		}

		/**
		 * Allow administrator-level capabilities to be filtered.
		 *
		 * @since 1.0
		 *
Christian Wach's avatar
Christian Wach committed
312 313
		 * @param array $capabilities The complete set of CiviCRM capabilities.
		 * @return array $capabilities The modified set of CiviCRM capabilities.
Christian Wach's avatar
Christian Wach committed
314 315 316
		 */
		$capabilities = apply_filters( 'civicrm_permissions_sync_caps_admin', $capabilities );

317
		// Sync permissions to the "Groups" plugin depending on plugin mode.
318 319 320 321
		if ( in_array( CIVICRM_PERMISSIONS_SYNC_MODE, [ 'groups', 'all' ] ) ) {
			$this->capabilities_sync_to_groups( $capabilities );
		}

322
		// Sync permissions to our custom role depending on plugin mode.
323 324 325 326
		if ( in_array( CIVICRM_PERMISSIONS_SYNC_MODE, [ 'role', 'all' ] ) ) {
			$this->capabilities_sync_to_role( $capabilities );
		}

327 328 329
		// Clean up.
		$this->capabilities_delete_missing( $capabilities );

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	}



	/**
	 * Sync capabilities to "Groups" plugin if present.
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The complete set of CiviCRM capabilities.
	 */
	public function capabilities_sync_to_groups( $capabilities ) {

		// Bail if we don't have the "Groups" plugin.
		if ( ! defined( 'GROUPS_CORE_VERSION' ) ) {
			return;
		}

		// Add the capabilities if not already added.
		foreach( $capabilities as $capability ) {
			if ( ! Groups_Capability::read_by_capability( $capability ) ) {
				Groups_Capability::create( array( 'capability' => $capability ) );
			}
		}

	}



	/**
	 * Sync capabilities to a custom role.
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The complete set of CiviCRM capabilities.
	 */
	public function capabilities_sync_to_role( $capabilities ) {

Christian Wach's avatar
Christian Wach committed
368 369 370
		// Get the role to apply all CiviCRM permissions to.
		$custom_role = $this->role_get();

371 372 373 374 375
		// Bail if something went wrong.
		if ( empty( $custom_role ) ) {
			return;
		}

Christian Wach's avatar
Christian Wach committed
376 377 378 379 380 381 382 383 384 385 386
		// Add the capabilities if not already added.
		foreach( $capabilities as $capability ) {
			if ( ! $custom_role->has_cap( $capability ) ) {
				$custom_role->add_cap( $capability );
			}
		}

	}



387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	/**
	 * Delete CiviCRM capabilities when they no longer exist.
	 *
	 * This can happen when an Extension which had previously added permissions
	 * is disabled or uninstalled, for example.
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The complete set of CiviCRM capabilities.
	 */
	public function capabilities_delete_missing( $capabilities ) {

		// Read the stored CiviCRM permissions array.
		$stored = $this->permissions_get();

		// Save and bail if we don't have any stored.
		if ( empty( $stored ) ) {
			$this->permissions_set( $capabilities );
			return;
		}

		// Find the capabilities that are missing in the current CiviCRM data.
		$not_in_current = array_diff( $stored, $capabilities );

		// Delete them from "Groups" depending on plugin mode.
		if ( in_array( CIVICRM_PERMISSIONS_SYNC_MODE, [ 'groups', 'all' ] ) ) {
			//$this->capabilities_delete_from_groups( $not_in_current );
		}

		// Delete them from the custom role depending on plugin mode.
		if ( in_array( CIVICRM_PERMISSIONS_SYNC_MODE, [ 'role', 'all' ] ) ) {
			//$this->capabilities_delete_from_role( $not_in_current );
		}

		// Overwrite the current permissions array.
		$this->permissions_set( $capabilities );

	}



	/**
	 * Delete capabilities from "Groups" plugin if present.
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The array of capabilities to delete.
	 */
	public function capabilities_delete_from_groups( $capabilities ) {

		// Bail if we don't have the "Groups" plugin.
		if ( ! defined( 'GROUPS_CORE_VERSION' ) ) {
			return;
		}

		// Delete the capabilities if not already deleted.
		foreach( $capabilities as $capability ) {
			$groups_cap = Groups_Capability::read_by_capability( $capability );
			if ( ! empty( $groups_cap->capability_id ) ) {
				Groups_Capability::delete( $groups_cap->capability_id );
			}
		}

	}



	/**
	 * Delete capabilities from a custom role.
	 *
	 * @since 1.0
	 *
	 * @param array $capabilities The array of capabilities to delete.
	 */
	public function capabilities_delete_from_role( $capabilities ) {

		// Get the role to delete CiviCRM permissions from.
		$custom_role = $this->role_get();

		// Bail if something went wrong.
		if ( empty( $custom_role ) ) {
			return;
		}

		// Delete the capabilities if not already deleted.
		foreach( $capabilities as $capability ) {
			if ( $custom_role->has_cap( $capability ) ) {
				$custom_role->remove_cap( $capability );
			}
		}

	}



Christian Wach's avatar
Christian Wach committed
482 483 484 485 486 487 488 489 490 491 492 493 494
	/**
	 * Retrieve our custom WordPress role.
	 *
	 * We need a role to which we add all CiviCRM permissions. This makes the
	 * capabilities discoverable by other plugins. This method creates the role
	 * if it doesn't already exist by cloning the 'adminstrator' role.
	 *
	 * @since 1.0
	 *
	 * @return WP_Role|void $custom_role The custom role, or void on failure.
	 */
	public function role_get() {

Christian Wach's avatar
Christian Wach committed
495 496
		// Fetch roles object.
		$wp_roles = wp_roles();
Christian Wach's avatar
Christian Wach committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

		// If the custom role already exists.
		if ( $wp_roles->is_role( $this->custom_role_name ) ) {

			// Get existing role.
			$custom_role = $wp_roles->get_role( $this->custom_role_name );

		} else {

			// Bail if the 'administrator' role is not there for some reason.
			if ( ! $wp_roles->is_role( 'administrator' ) ) {
				return;
			}

			// Grab the 'administrator' role.
			$admin = $wp_roles->get_role( 'administrator' );

			// Add new role.
			$custom_role = add_role(
				$this->custom_role_name,
				__( 'CiviCRM Administrator', 'reset-wp-civicrm-roles-caps' ),
				$admin->capabilities
			);

		}

		// If void then log something.
Christian Wach's avatar
Christian Wach committed
524
		if ( empty( $custom_role ) ) {
Christian Wach's avatar
Christian Wach committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

			// Construct a message.
			$message = sprintf(
				__( 'Could not find CiviCRM sync role: "%s"', 'reset-wp-civicrm-roles-caps' ),
				$this->custom_role_name
			);

			// Add log entry.
			$e = new Exception;
			$trace = $e->getTraceAsString();
			error_log( print_r( array(
				'method' => __METHOD__,
				'message' => $message,
				'backtrace' => $trace,
			), true ) );

		}

		// --<
		return $custom_role;

	}



550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	/**
	 * Get stored CiviCRM permissions.
	 *
	 * @since 1.0
	 *
	 * @return array $permissions The array of stored permissions.
	 */
	public function permissions_get() {

		// Get from option.
		$permissions = get_option( 'civicrm_permissions_sync_perms', 'false' );

		// If no option exists, cast return as array.
		if ( $permissions === 'false' ) {
			$permissions = array();
		}

		// --<
		return $permissions;

	}



	/**
	 * Set stored CiviCRM permissions.
	 *
	 * @since 1.0
	 *
	 * @param array $permissions The array of permissions to store.
	 */
	public function permissions_set( $permissions ) {

		// Set the option.
		update_option( 'civicrm_permissions_sync_perms', $permissions );

	}



Christian Wach's avatar
Christian Wach committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	/**
	 * Check if CiviCRM is initialised.
	 *
	 * @since 1.0
	 *
	 * @return bool True if CiviCRM initialised, false otherwise.
	 */
	public function is_civicrm_initialised() {

		// Init only when CiviCRM is fully installed.
		if ( ! defined( 'CIVICRM_INSTALLED' ) ) return false;
		if ( ! CIVICRM_INSTALLED ) return false;

		// Bail if no CiviCRM init function.
		if ( ! function_exists( 'civi_wp' ) ) return false;

		// Try and initialise CiviCRM.
		return civi_wp()->initialize();

	}



} // Class ends.



/**
 * Get a reference to this plugin.
 *
 * @since 1.0
 *
 * @return CiviCRM_Permissions_Sync $civicrm_permissions_sync The plugin reference.
 */
function civicrm_permissions_sync() {

	// Hold the plugin instance in a static variable.
	static $civicrm_permissions_sync = false;

	// Instantiate plugin if not yet instantiated.
	if ( false === $civicrm_permissions_sync ) {
		$civicrm_permissions_sync = new CiviCRM_Permissions_Sync();
	}

	// --<
	return $civicrm_permissions_sync;

637 638
}

Christian Wach's avatar
Christian Wach committed
639 640 641 642 643
// Init plugin.
add_action( 'plugins_loaded', 'civicrm_permissions_sync' );