reset-wp-civicrm-roles-caps.php 9.27 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
// 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 ) );
}
33

Christian Wach's avatar
Christian Wach committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63


/**
 * 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,
64 65 66
	);


Christian Wach's avatar
Christian Wach committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80

	/**
	 * 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;
81
		}
Christian Wach's avatar
Christian Wach committed
82 83 84 85 86 87 88 89 90 91 92

		// Register hooks.
		$this->register_hooks();

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

93 94 95 96
	}



Christian Wach's avatar
Christian Wach committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	/**
	 * Register hooks.
	 *
	 * @since 1.0
	 */
	public function register_hooks() {

		/*
		 * 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.
		 */

		// Filter minimum CiviCRM capabilities.
		add_filter( 'civicrm_min_capabilities', [ $this, 'capabilities_minimum' ], 20, 1 );

		// Sync when CiviCRM activation action fires.
		add_action( 'civicrm_activation', [ $this, 'capabilities_sync' ], 20 );

		/*
		 * 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.
		 */

		// Add to minimum CiviCRM capabilities.
		add_action( 'init', [ $this, 'capabilities_anon' ], 100 );

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

130 131
	}

Christian Wach's avatar
Christian Wach committed
132 133 134 135 136 137 138 139 140 141 142 143 144


	/**
	 * 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.
145
		);
Christian Wach's avatar
Christian Wach committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	}



	/**
	 * 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
	 * themselves be filtered if desired.
	 *
	 * @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;
		}

		/**
		 * Allow capabilities to be filtered further.
		 *
		 * @since 1.0
		 *
		 * @param array $capabilities The minimum capabilities.
		 * @return array $capabilities The modified capabilities.
		 */
		$capabilities = apply_filters( 'civicrm_permissions_sync_caps_min', $capabilities );

		// --<
		return $capabilities;

183
	}
Christian Wach's avatar
Christian Wach committed
184 185 186 187 188 189 190 191 192 193 194 195 196



	/**
	 * Filter minimum CiviCRM capabilities for the anonymous user.
	 *
	 * This method adds a few capabilities which many CiviCRM installs need to
	 * function as expected.
	 *
	 * @since 1.0
	 */
	public function capabilities_anon() {

Christian Wach's avatar
Christian Wach committed
197 198
		// Fetch roles object.
		$wp_roles = wp_roles();
Christian Wach's avatar
Christian Wach committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

		// Bail if the anonymous_user role does not exist.
		if ( ! $wp_roles->is_role( 'anonymous_user' ) ) {
			return;
		}

		// Get anonymous_user role.
		$anon = $wp_roles->get_role( 'anonymous_user' );

		// Add the capabilities if not already added.
		foreach( $this->min_capabilities AS $capability => $value ) {
			if ( ! $anon->has_cap( $capability ) ) {
				$anon->add_cap( $capability );
			}
		}

215 216
	}

217

Christian Wach's avatar
Christian Wach committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	/**
	 * 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
		 *
		 * @param array $capabilities The minimum capabilities.
		 * @return array $capabilities The modified capabilities.
		 */
		$capabilities = apply_filters( 'civicrm_permissions_sync_caps_admin', $capabilities );

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

260 261 262 263 264
		// Bail if something went wrong.
		if ( empty( $custom_role ) ) {
			return;
		}

Christian Wach's avatar
Christian Wach committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		// Add the capabilities if not already added.
		foreach( $capabilities as $capability ) {
			if ( ! $custom_role->has_cap( $capability ) ) {
				$custom_role->add_cap( $capability );
			}
		}

	}



	/**
	 * 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
289 290
		// Fetch roles object.
		$wp_roles = wp_roles();
Christian Wach's avatar
Christian Wach committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

		// 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
318
		if ( empty( $custom_role ) ) {
Christian Wach's avatar
Christian Wach committed
319 320 321 322 323 324 325 326 327 328 329 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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

			// 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;

	}



	/**
	 * 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;

391 392
}

Christian Wach's avatar
Christian Wach committed
393 394 395 396 397
// Init plugin.
add_action( 'plugins_loaded', 'civicrm_permissions_sync' );