Newer
Older
<?php
/*
+--------------------------------------------------------------------+
+--------------------------------------------------------------------+
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
32
33
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
*
*/
// this file must not accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Define CiviCRM_For_WordPress_Users Class
*/
class CiviCRM_For_WordPress_Users {
/**
* Declare our properties
*/
// init property to store reference to Civi
public $civi;
/**
* Instance constructor
*
* @return object $this The object instance
*/
function __construct() {
// store reference to Civi object
$this->civi = civi_wp();
}
/**
* Register hooks to handle CiviCRM in a WordPress wpBasePage context
*
* @return void
*/
public function register_hooks() {
// add CiviCRM access capabilities to WordPress roles
add_action( 'init', array( $this, 'set_access_capabilities' ) );
// do not hook into user updates if Civi not installed yet
if ( ! CIVICRM_INSTALLED ) return;
// synchronise users on insert and update
add_action( 'user_register', array( $this, 'update_user' ) );
add_action( 'profile_update', array( $this, 'update_user' ) );
// delete ufMatch record when a WordPress user is deleted
add_action( 'deleted_user', array( $this, 'delete_user_ufmatch' ), 10, 1 );
}
/**
* Authentication function used by basepage_register_hooks()
*
* @return bool True if authenticated, false otherwise
*/
public function check_permission( $args ) {
if ( $args[0] != 'civicrm' ) {
return FALSE;
}
$config = CRM_Core_Config::singleton();
// set frontend true
$config->userFrameworkFrontend = TRUE;
require_once 'CRM/Utils/Array.php';
// all profile and file urls, as well as user dashboard and tell-a-friend are valid
$arg1 = CRM_Utils_Array::value(1, $args);
$invalidPaths = array('admin');
if ( in_array( $arg1, $invalidPaths ) ) {
return FALSE;
}
return TRUE;
}
/**
* Called when authentication fails in basepage_register_hooks()
*
* @return string Warning message
*/
public function get_permission_denied() {
return __( 'You do not have permission to access this content.', 'civicrm' );
}
/**
* Keep WordPress user synced with CiviCRM Contact
* Callback function for 'user_register' hook
* Callback function for 'profile_update' hook
*
* CMW: seems to (wrongly) create new CiviCRM Contact every time a user changes their
* first_name or last_name attributes in WordPress.
*
* @param int $user_id The numeric ID of the WordPress user
* @return void
*/
public function update_user( $user_id ) {
$user = get_userdata( $user_id );
if ( $user ) {
$this->sync_user( $user );
}
}
/**
* Keep WordPress user synced with CiviCRM Contact
*
* @param object $user The WordPress user object
* @return void
*/
public function sync_user( $user = FALSE ) {
// sanity check
if ( $user === FALSE OR !is_a($user, 'WP_User') ) {
return;
}
if (!$this->civi->initialize()) {
return;
}
require_once 'CRM/Core/BAO/UFMatch.php';
// this does not return anything, so if we want to do anything further
// to the CiviCRM Contact, we have to search for it all over again...
CRM_Core_BAO_UFMatch::synchronize(
$user, // user object
TRUE, // update = true
'WordPress', // CMS
'Individual' // contact type
);
/*
// IN progress: synchronizeUFMatch does return the contact object, however
$civi_contact = CRM_Core_BAO_UFMatch::synchronizeUFMatch(
$user, // user object
$user->ID, // ID
$user->user_mail, // unique identifier
null // unused
'WordPress' // CMS
'Individual' // contact type
);
// now we can allow other plugins to do their thing
do_action( 'civicrm_contact_synced', $user, $civi_contact );
*/
}
/**
* When a WordPress user is deleted, delete the ufMatch record
* Callback function for 'delete_user' hook
*
* @param $user_id The numerical ID of the WordPress user
* @return void
*/
public function delete_user_ufmatch( $user_id ) {
if (!$this->civi->initialize()) {
return;
}
// delete the ufMatch record
require_once 'CRM/Core/BAO/UFMatch.php';
CRM_Core_BAO_UFMatch::deleteUser($user_id);
}
/**
* Function to create 'anonymous_user' role, if 'anonymous_user' role is not in
* the WordPress installation and assign minimum capabilities for all WordPress roles
*
* The legacy global scope function civicrm_wp_set_capabilities() is called from
* upgrade_4_3_alpha1()
*
* @return void
*/
public function set_wp_user_capabilities() {
global $wp_roles;
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
// Minimum capabilities (Civicrm permissions) arrays
$default_min_capabilities = array(
'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,
);
// 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 );
}
}
// 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 CiviCRM access capabilities to WordPress roles
* this is a callback for the 'init' hook in register_hooks()
*
* The legacy global scope function wp_civicrm_capability() is called by
* postProcess() in civicrm/CRM/ACL/Form/WordPress/Permissions.php
*
* @return void
*/
public function set_access_capabilities() {
// test for existing global
global $wp_roles;
if ( ! isset( $wp_roles ) ) {
$wp_roles = new WP_Roles();
}
// give access to civicrm page menu link to particular roles
$roles = apply_filters( 'civicrm_access_roles', array( 'super admin', 'administrator' ) );
foreach ( $roles as $role ) {
$roleObj = $wp_roles->get_role( $role );
if (
is_object( $roleObj ) &&
is_array( $roleObj->capabilities ) &&
! array_key_exists( 'access_civicrm', $wp_roles->get_role( $role )->capabilities )
) {
$wp_roles->add_cap( $role, 'access_civicrm' );
}
}
}
/**
* Get CiviCRM contact type
*
* @param string $default contact type
* @return string $ctype contact type
*/
public function get_civicrm_contact_type( $default = NULL ) {
// here we are creating a new contact
// get the contact type from the POST variables if any
if ( isset( $_REQUEST['ctype'] ) ) {
$ctype = $_REQUEST['ctype'];
} elseif (
isset( $_REQUEST['edit'] ) &&
isset( $_REQUEST['edit']['ctype'] )
) {
$ctype = $_REQUEST['edit']['ctype'];
} else {
$ctype = $default;
}
if (
$ctype != 'Individual' &&
$ctype != 'Organization' &&
$ctype != 'Household'
) {
$ctype = $default;
}
return $ctype;
}
} // class CiviCRM_For_WordPress_Users ends