wp-civi-caps.php 7.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
/*
Plugin Name: Resync Default CiviCRM permissions
Plugin URI: https://tadpole.cc
Description: Resync Default CiviCRM permissions
Author: Tadpole Collective  
Author URI: https://tadpole.cc
Version: 1.0
License:    GPLv2

Based upon code in https://github.com/civicrm/civicrm-wordpress/blob/master/includes/civicrm.users.php  
*/

function tc_reset_wp_user_capabilities() {

	global $wp_roles;
	if ( ! isset( $wp_roles ) ) {
		$wp_roles = new WP_Roles();
	}

21
	// Minimum capabilities (CiviCRM permissions) arrays
22
	$default_min_capabilities = array(
23
		'read'                                        => 1,
24 25 26 27 28 29 30 31 32 33 34 35
		'access_civimail_subscribe_unsubscribe_pages' => 1,
		'access_all_custom_data'                      => 1,
		'access_uploaded_files'                       => 1,
		'make_online_contributions'                   => 1,
		'profile_create'                              => 1,
		'profile_edit'                                => 1,
		'profile_view'                                => 1,
		'register_for_events'                         => 1,
		'view_event_info'                             => 1,
		'sign_civicrm_petition'                       => 1,
		'view_public_civimail_content'                => 1,
		'access_ajax_api'                             => 1,
36
		'view_my_invoices'                            => 1,
37 38 39 40 41 42 43 44 45 46 47 48 49
	);

	// allow other plugins to filter
	$min_capabilities = apply_filters( 'civicrm_min_capabilities', $default_min_capabilities );

	// Assign the Minimum capabilities (CiviCRM permissions) to all WP roles
	foreach ( $wp_roles->role_names as $role => $name ) {
		$roleObj = $wp_roles->get_role( $role );
		foreach ( $min_capabilities as $capability_name => $capability_value ) {
			$roleObj->add_cap( $capability_name );
		}
	}

50
	// Admin capabilities (CiviCRM permissions) arrays
51 52 53 54 55 56 57 58 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 89 90 91 92 93 94 95 96 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 130
	$default_admin_capabilities = array(
		'read'                                        => 1,
		'access_ajax_api'                             => 1,
		'access_all_cases_and_activities'             => 1,
		'access_all_custom_data'                      => 1,
		'access_civicontribute'                       => 1,
		'access_civicrm'                              => 1,
		'access_civievent'                            => 1,
		'access_civigrant'                            => 1,
		'access_civimail'                             => 1,
		'access_civimail_subscribe_unsubscribe_pages' => 1,
		'access_civimember'                           => 1,
		'access_civipledge'                           => 1,
		'access_civireport'                           => 1,
		'access_contact_dashboard'                    => 1,
		'access_contact_reference_fields'             => 1,
		'access_deleted_contacts'                     => 1,
		'access_my_cases_and_activities'              => 1,
		'access_report_criteria'                      => 1,
		'access_uploaded_files'                       => 1,
		'add_cases'                                   => 1,
		'add_contacts'                                => 1,
		'administer_civicampaign'                     => 1,
		'administer_civicase'                         => 1,
		'administer_civicrm'                          => 1,
		'administer_dedupe_rules'                     => 1,
		'administer_reports'                          => 1,
		'administer_reserved_groups'                  => 1,
		'administer_reserved_reports'                 => 1,
		'administer_reserved_tags'                    => 1,
		'administer_tagsets'                          => 1,
		'create_manual_batch'                         => 1,
		'delete_activities'                           => 1,
		'delete_all_manual_batches'                   => 1,
		'delete_contacts'                             => 1,
		'delete_in_civicase'                          => 1,
		'delete_in_civicontribute'                    => 1,
		'delete_in_civievent'                         => 1,
		'delete_in_civigrant'                         => 1,
		'delete_in_civimail'                          => 1,
		'delete_in_civimember'                        => 1,
		'delete_in_civipledge'                        => 1,
		'delete_own_manual_batches'                   => 1,
		'edit_all_contacts'                           => 1,
		'edit_all_events'                             => 1,
		'edit_all_manual_batches'                     => 1,
		'edit_contributions'                          => 1,
		'edit_event_participants'                     => 1,
		'edit_grants'                                 => 1,
		'edit_groups'                                 => 1,
		'edit_memberships'                            => 1,
		'edit_own_manual_batches'                     => 1,
		'edit_pledges'                                => 1,
		'export_all_manual_batches'                   => 1,
		'export_own_manual_batches'                   => 1,
		'gotv_campaign_contacts'                      => 1,
		'import_contacts'                             => 1,
		'interview_campaign_contacts'                 => 1,
		'make_online_contributions'                   => 1,
		'manage_campaign'                             => 1,
		'merge_duplicate_contacts'                    => 1,
		'profile_create'                              => 1,
		'profile_edit'                                => 1,
		'profile_listings'                            => 1,
		'profile_listings_and_forms'                  => 1,
		'profile_view'                                => 1,
		'register_for_events'                         => 1,
		'release_campaign_contacts'                   => 1,
		'reserve_campaign_contacts'                   => 1,
		'sign_civicrm_petition'                       => 1,
		'translate_civicrm'                           => 1,
		'view_all_activities'                         => 1,
		'view_all_contacts'                           => 1,
		'view_all_manual_batches'                     => 1,
		'view_all_notes'                              => 1,
		'view_debug_output'                           => 1,
		'view_event_info'                             => 1,
		'view_event_participants'                     => 1,
		'view_own_manual_batches'                     => 1,
		'view_public_civimail_content'                => 1,
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		'administer_payment_processors'               => 1,
		'administer_private_reports'                  => 1,
		'close_all_manual_batches'                    => 1,
		'close_own_manual_batches'                    => 1,
		'edit_api_keys'                               => 1,
		'edit_message_templates'                      => 1,
		'edit_my_contact'                             => 1,
		'edit_own_api_keys'                           => 1,
		'force_merge_duplicate_contacts'              => 1,
		'import_sql_datasource'                       => 1,
		'manage_event_profiles'                       => 1,
		'manage_tags'                                 => 1,
		'reopen_all_manual_batches'                   => 1,
		'reopen_own_manual_batches'                   => 1,
		'view_my_contact'                             => 1,
		'view_my_invoices'                            => 1,
		'view_report_sql'                             => 1,
148
		'skip_ids_check'							  => 1,
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	);

	$admin_capabilities = apply_filters( 'civicrm_admin_capabilities', $default_admin_capabilities );

	// Add the 'anonymous_user' role with minimum capabilities.
	if ( ! in_array( 'anonymous_user', $wp_roles->roles ) ) {
		add_role(
			'anonymous_user',
			__( 'Anonymous User', 'civicrm' ),
			$min_capabilities
		);
	}

	// Add the 'civicrm_admin' role with all capabilities.
	if ( ! in_array( 'civicrm_admin', $wp_roles->roles ) ) {
		add_role(
			'civicrm_admin',
			__( 'CiviCRM Administrator', 'civicrm' ),
			$admin_capabilities
		);
	}
170 171 172 173 174
	$roleObj = get_role( 'civicrm_admin' );
	foreach ( $admin_capabilities as $capability_name => $capability_value ) {
		$roleObj->add_cap( $capability_name );
	}

175 176 177 178

}

register_activation_hook( __FILE__, 'tc_reset_wp_user_capabilities' );