forked from strangerstudios/pmpro-roles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-roles.php
executable file
·684 lines (584 loc) · 22 KB
/
pmpro-roles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
524
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
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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
/**
* Plugin Name: Paid Memberships Pro - Roles Add On
* Description: Adds a WordPress Role for each Membership Level.
* Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-roles/
* Author: Paid Memberships Pro
* Author URI: https://www.paidmembershipspro.com
* Version: 1.4.2
* License: GPL2
* Text Domain: pmpro-roles
* Domain Path: /languages
*/
class PMPRO_Roles {
static $role_key = 'pmpro_role_';
static $plugin_slug = 'pmpro-roles';
static $plugin_prefix = 'pmpro_roles_';
static $ajaction = 'pmpro_roles_repair';
function __construct(){
$this->pmpro_hooks();
$this->wp_hooks();
}
/**
* Hooks that run only when PMPro is active.
*
* @since 1.4.2
*/
function pmpro_hooks() {
add_action( 'pmpro_save_membership_level', array( $this, 'edit_level' ) );
add_action( 'pmpro_delete_membership_level', array( $this, 'delete_level' ) );
if ( defined( 'PMPRO_VERSION' ) && version_compare( '2.5.8', PMPRO_VERSION, '>' ) ) {
// Use legacy functionality to update roles on level change.
add_action( 'pmpro_after_change_membership_level', array($this, 'user_change_level' ), 10, 2 );
} else {
add_action( 'pmpro_after_all_membership_level_changes', array( $this, 'after_all_level_changes' ), 10, 1 );
}
add_action( 'pmpro_membership_level_after_other_settings', array( 'PMPRO_Roles', 'level_settings' ) );
}
/**
* Hooks that may need to run without PMPro activated.
*
* @return void
*/
function wp_hooks() {
add_action( 'admin_enqueue_scripts', array($this, 'enqueue_admin_scripts' ) );
add_action( 'init', array( $this, 'load_text_domain' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( 'PMPRO_Roles', 'add_action_links' ) );
add_action( 'wp_ajax_' . PMPRO_Roles::$ajaction, array( $this, 'install' ) );
add_filter( 'plugin_row_meta', array( 'PMPRO_Roles', 'plugin_row_meta' ), 10, 2 );
add_filter( 'editable_roles', array( 'PMPRO_Roles', 'remove_list_roles' ), 10, 1 );
add_action( 'admin_init', array( 'PMPRO_Roles', 'delete_and_deactivate' ) );
}
/**
* Load plugin text domain for translations.
* @since 1.3
*/
function load_text_domain() {
load_plugin_textdomain( 'pmpro-roles', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Javascript for admin area.
*/
function enqueue_admin_scripts($hook) {
if( 'toplevel_page_pmpro-membershiplevels' != $hook ) {
return;
}
wp_enqueue_script( PMPRO_Roles::$plugin_prefix.'admin', plugin_dir_url( __FILE__ ) . '/admin.js' );
wp_enqueue_style( PMPRO_Roles::$plugin_prefix.'admin', plugin_dir_url( __FILE__ ) . '/admin.css' );
$nonce = wp_create_nonce( PMPRO_Roles::$ajaction );
$vars = array(
'desc' => esc_html__('Levels not matching up, or missing?', PMPRO_Roles::$plugin_slug ),
'repair' => esc_html__('Repair', PMPRO_Roles::$plugin_slug ),
'working' => esc_html__('Working...', PMPRO_Roles::$plugin_slug ),
'done' => esc_html__('Done!', PMPRO_Roles::$plugin_slug ),
'fixed' => esc_html__( 'role connections were needed/repaired.', PMPRO_Roles::$plugin_slug ),
'failed' => esc_html__( 'An error occurred while repairing roles.', PMPRO_Roles::$plugin_slug ),
'ajaction' => PMPRO_Roles::$ajaction,
'nonce' => $nonce,
);
$key = PMPRO_Roles::$plugin_prefix.'vars';
wp_localize_script( PMPRO_Roles::$plugin_prefix . 'admin', 'key', array( 'key'=>$key ) );
wp_localize_script( PMPRO_Roles::$plugin_prefix . 'admin', $key, $vars );
}
/**
* Get an array of roles set for a given level.
* @since 1.4.1
* @param int $level_id The ID of the level to check for.
*/
public static function get_roles_for_level( $level_id ) {
// In case a level object is passed in.
if ( is_object( $level_id ) && ! empty( $level_id->id ) ) {
$level_id = $level_id->id;
}
// Fail if no level.
if ( empty( $level_id ) ) {
return array();
}
$roles = get_option( self::$plugin_prefix . $level_id );
// Default to the role created for this level.
if ( empty( $roles ) ) {
$roles = array();
$level = pmpro_getLevel( $level_id );
if ( ! empty( $level ) ) {
$roles[ self::$role_key . $level_id ] = $level->name;
}
}
return $roles;
}
/**
* Settings for the edit level admin screen. Creates and saves role selection per level.
* @since 1.3
*/
function edit_level( $saveid ) {
//by being here, we know we already have the $_REQUEST we need, so no need to check.
$capabilities = self::capabilities( self::$role_key . $saveid );
if ( ! empty( $_REQUEST['pmpro_roles_level_present'] ) ) {
if ( ! empty( $_REQUEST['pmpro_roles_level'] ) ) {
$level_roles = $_REQUEST['pmpro_roles_level'];
} else {
// If no role chosen, use the default.
$level_roles = array();
$level_roles[ self::$role_key . $saveid ] = sanitize_text_field( $_REQUEST['name'] );
}
//created a new level
if( $_REQUEST['edit'] < 0 ) {
foreach( $level_roles as $role_key => $role_name ){
if( $role_key === 'pmpro_draft_role' ){
add_role( PMPRO_Roles::$role_key.$saveid, sanitize_text_field( $_REQUEST['name'] ), array( 'read' => true ) );
}
if ( $role_key === 'pmpro_role_'. $saveid ) {
$capabilities = PMPRO_Roles::capabilities( $role_key );
add_role( $role_key, $role_name, $capabilities );
}
}
} else {
global $wpdb;
//have to get all roles and find ours because get_role() doesn't yield the role's "pretty" name, only its index.
$roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' );
if(!is_array( $roles ) ) return;
foreach( $level_roles as $role_key => $role_name ){
if( !isset( $roles[$role_key] ) ){
$capabilities = PMPRO_Roles::capabilities( $role_key );
add_role( $role_key, sanitize_text_field( $_REQUEST['name'] ), $capabilities[$role_key] );
return;
}
if( ( strpos( $role_key, PMPRO_Roles::$role_key ) !== FALSE ) && sanitize_text_field( $_REQUEST['name'] ) !== $role_name ) {
PMPRO_Roles::update_role_name( $role_key, sanitize_text_field( $_REQUEST['name'] ) );
}
}
}
if( !empty( $level_roles['pmpro_draft_role'] ) ){
unset( $level_roles['pmpro_draft_role'] );
$level_roles[PMPRO_Roles::$role_key.$saveid] = sanitize_text_field( $_REQUEST['name'] );
}
update_option( 'pmpro_roles_'.$saveid, $level_roles );
}
}
/**
* Delete custom PMPro role on level delete.
* @since 1.0
*/
function delete_level( $delete_id ) {
$role_key = PMPRO_Roles::$role_key . $delete_id;
if( !empty( $role_key ) ){
remove_role( $role_key );
}
delete_option( 'pmpro_roles_' . $delete_id );
do_action( 'pmpro_roles_delete_membership_level', $delete_id );
}
/**
* Update a PMPro Roles Name if level name changes.
* @since 1.3
*/
function update_role_name( $role, $name ){
if( strpos( $role, PMPRO_Roles::$role_key ) !== FALSE ) {
$roles_array = get_option( 'wp_user_roles', true );
if( !empty( $roles_array[$role] ) ){
$roles_array[$role]['name'] = sanitize_text_field( $name );
$updated = update_option( 'wp_user_roles', $roles_array );
}
}
}
/**
* Update users' roles after their membership levels are changed.
*
* @param array $old_user_levels $user_id => array $old_levels
*/
function after_all_level_changes( $old_user_levels ) {
foreach ( $old_user_levels as $user_id => $old_levels ) {
// Get the user who changed levels.
$user = new WP_User( $user_id );
// Ignore administrators.
if ( in_array( 'administrator', $user->roles ) ) {
continue;
}
// Get the user's current active membership levels.
$new_levels = pmpro_getMembershipLevelsForUser( $user_id );
$new_roles = array();
$old_roles = array();
// Build an array of all roles assigned to the user's old membership levels.
foreach ( $old_levels as $old_level ) {
$old_level_roles = self::get_roles_for_level( $old_level->id );
if ( ! empty( $old_level_roles ) && is_array( $old_level_roles ) ) {
$old_roles = array_merge( $old_roles, array_keys( $old_level_roles ) );
}
}
// Build an array of all roles assigned to the user's new membership levels.
foreach ( $new_levels as $new_level ) {
$new_level_roles = self::get_roles_for_level( $new_level->id );
if ( ! empty( $new_level_roles ) && is_array( $new_level_roles ) ) {
$new_roles = array_merge( $new_roles, array_keys( $new_level_roles ) );
}
}
// Remove duplicates in the array of old and new roles.
$old_roles = array_unique( $old_roles );
$new_roles = array_unique( $new_roles );
// Build a unique array of roles to add and remove from user.
$add_roles = $new_roles;
$remove_roles = array_values( array_diff( $old_roles, $new_roles ) );
// Add roles to user.
foreach ( $add_roles as $add_role ) {
$user->add_role( $add_role );
}
// Remove roles from user.
foreach ( $remove_roles as $remove_role ) {
$user->remove_role( $remove_role );
}
// Handle case where role can be "simplified" to a single role.
$default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
if ( in_array( $default_role, $user->roles, true ) && count( $user->roles ) == 2 ) {
$user->remove_role( $default_role );
}
// Handle case where user has no role.
if ( empty( $user->roles ) ) {
$user->add_role( $default_role );
}
// Allows user's to be inside the foreach loop and hook in to do things.
do_action( 'pmpro_roles_after_role_change', $user, $old_user_levels, $old_roles, $new_roles );
}
}
/**
* Change user role based on level change. (Now supports MMPU)
* No longer being used if PMPro version >= 2.5.8.
*
* @since 1.0
*/
function user_change_level($level_id, $user_id){
global $pmpro_checkout_levels;
//get user object
$wp_user_object = new WP_User($user_id);
//ignore admins
if( in_array( 'administrator', $wp_user_object->roles ) )
return;
// Check if user is cancelling.
if( defined( 'PMPROMMPU_DIR' ) && !empty( $_REQUEST['levelstocancel'] ) ) { //Adds support for MMPU
$levels_to_cancel = explode( " ", $_REQUEST['levelstocancel'] );
if( !empty( $levels_to_cancel ) ){
foreach( $levels_to_cancel as $ltc ){
$wp_user_object->remove_role( PMPRO_Roles::$role_key.intval( $ltc ) );
}
}
} else if( $level_id == 0 ) {
$default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
$wp_user_object->set_role( $default_role );
} else {
if( !empty( $pmpro_checkout_levels ) ){
//Adds support for MMPU
foreach( $pmpro_checkout_levels as $co_level ){
$roles = self::get_roles_for_level( $co_level->id );
if( is_array( $roles ) && ! empty( $roles ) ){
foreach( $roles as $role_key => $role_name ){
$wp_user_object->add_role( $role_key );
}
} else {
$wp_user_object->set_role( PMPRO_Roles::$role_key . $co_level->id );
}
}
} else if( $level_id > 0 ){
$roles = self::get_roles_for_level( $level_id );
if( is_array( $roles ) && ! empty( $roles ) ){
$count = 1;
foreach( $roles as $role_key => $role_name ){
if( $count == 1 ){
$wp_user_object->set_role( $role_key );
} else {
$wp_user_object->add_role( $role_key );
}
$count++;
}
} else {
$wp_user_object->set_role( PMPRO_Roles::$role_key.intval( $level_id ) );
}
}
}
}
/**
* Show a list of all available roles as a checkbox inside level settings.
* @since 1.3
*/
public static function level_settings() {
?>
<hr />
<h3><?php esc_html_e( 'Role Settings', 'pmpro-roles' ); ?></h3>
<p class="description">
<?php
$allowed_pmpro_roles_description_html = array (
'a' => array (
'href' => array(),
'target' => array(),
'title' => array(),
),
);
echo sprintf( wp_kses( __( 'Choose one or more roles to be assigned for members of this level. <a href="%s" title="Paid Memberships Pro - Roles Add On" target="_blank">Visit the documentation page</a> for more information.', 'pmpro-roles' ), $allowed_pmpro_roles_description_html ), 'https://www.paidmembershipspro.com/add-ons/pmpro-roles//?utm_source=plugin&utm_medium=pmpro-membershiplevels&utm_campaign=add-ons&utm_content=pmpro-roles' );
?>
</p>
<table class="form-table">
<tbody>
<?php
$level_id = absint( filter_input( INPUT_GET, 'edit', FILTER_DEFAULT ) );
global $wp_roles;
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
$saved_roles = self::get_roles_for_level( $level_id );
asort( $editable_roles ); //Display alphabetically
if( !empty( $editable_roles ) ){
?>
<tr>
<th scope="row" valign="top"><label><?php esc_html_e( 'Roles', 'pmpro-roles' ); ?>:</label></th>
<td>
<?php
// Build the selectors for the checkbox list based on number of levels.
$classes = array();
$classes[] = "pmpro_checkbox_box";
if ( count( $editable_roles ) > 5 ) {
$classes[] = "pmpro_scrollable";
}
$class = implode( ' ', array_unique( $classes ) );
?>
<div class="<?php echo esc_attr( $class ); ?>">
<input type="hidden" name="pmpro_roles_level_present" value="1" />
<?php
//New level, choose if they want to create a role for this level
if ( $_REQUEST['edit'] < 0 ) { ?>
<div class="pmpro_clickable" style="border-bottom-width: 4px;">
<input type='checkbox' name='pmpro_roles_level[pmpro_draft_role]' value='pmpro_draft_role' id='pmpro_draft_role' />
<label for='pmpro_draft_role'>
<em><?php _e('Create a new custom role for this membership level', 'pmpro-roles'); ?></em>
</label>
</div>
<?php
}
$custom_pmpro_role = PMPRO_Roles::$role_key.$level_id;
$checked = '';
if ( isset( $saved_roles[$custom_pmpro_role] ) || empty( $saved_roles ) ) {
$checked = 'checked=true';
}
if ( isset( $editable_roles[$custom_pmpro_role] ) ) { ?>
<div class="pmpro_clickable" style="border-bottom-width: 4px;">
<input type='checkbox' name='pmpro_roles_level[<?php echo esc_attr( $custom_pmpro_role ); ?>]' value='<?php echo esc_attr( $editable_roles[$custom_pmpro_role]["name"] ); ?>' id='<?php echo esc_attr( $custom_pmpro_role ); ?>' <?php echo esc_attr( $checked ); ?> />
<label for='<?php echo esc_attr( $custom_pmpro_role ); ?>'>
<?php echo esc_html( $editable_roles[$custom_pmpro_role]['name'] ); ?>
<?php printf( "<code>" . esc_html( 'pmpro_role_%s', 'pmpro-roles' ) . "</code>", $level_id ); ?>
</label>
</div>
<?php
}
$exclude_other_pmpro_roles = apply_filters( 'pmpro_roles_exclude_other_pmpro_roles', true, $level_id );
foreach ( $editable_roles as $key => $role ) {
$checked = '';
//Backwards compat here, if $saved_roles is empty, set the default level's role as checked
if ( empty( $saved_roles ) ) {
if ( PMPRO_Roles::$role_key.$level_id == $key ) {
$checked = 'checked=true';
}
}
if ( isset( $saved_roles[$key] ) ) {
$checked = 'checked=true';
}
if ( $exclude_other_pmpro_roles ) {
//excluding the pmpro_role_ roles here
if ( $key != 'pmpro_role_' . $level_id ) { //Show this one first
?>
<div class="pmpro_clickable">
<input type='checkbox' name='pmpro_roles_level[<?php echo esc_attr( $key ); ?>]' value='<?php echo esc_attr( $role["name"] ); ?>' id='<?php echo esc_attr( $key ); ?>' <?php echo esc_attr( $checked ); ?> />
<label for='<?php echo esc_attr( $key ); ?>'>
<?php echo esc_html( $role['name'] ); ?>
<?php echo "<code>". esc_html( $key ). "</code>"; ?>
</label>
</div>
<?php
}
} else {
//include all roles. No checks needed
?>
<div class="pmpro_clickable">
<input type='checkbox' name='pmpro_roles_level[<?php echo esc_attr( $key ); ?>]' value='<?php echo esc_attr( $role["name"] ); ?>' id='<?php echo esc_attr( $key ); ?>' <?php echo esc_attr( $checked ); ?> />
<label for='<?php echo esc_attr( $key ); ?>'>
<?php echo esc_html( $role['name'] ); ?>
<?php echo "<code>". esc_html( $key ). "</code>"; ?>
</label>
</div>
<?php
}
}
?>
</div> <!-- end checkbox_box -->
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
/**
* Helper function to remove the administrator role from level settings.
* @since 1.3
*/
public static function remove_list_roles( $roles ){
if( !function_exists( 'pmpro_getAllLevels' ) ){
return $roles;
}
if( !empty( $_REQUEST['edit'] ) ){
$edit_level = intval( $_REQUEST['edit'] );
$all_levels = pmpro_getAllLevels( true, false );
if( apply_filters( 'pmpro_roles_hide_admin_role', true, $edit_level ) ){
//Take admins out of the array first
unset( $roles['administrator'] );
}
foreach( $all_levels as $level_key => $level ){
if( $level_key !== $edit_level ){
if( isset( $roles[PMPRO_Roles::$role_key.$level_key] ) ){
unset( $roles[PMPRO_Roles::$role_key.$level_key] );
}
}
}
}
return $roles;
}
/**
* Initial function to run on install. Create roles for each existing level.
* @since 1.0
*/
public static function install() {
global $wpdb;
if( defined( 'DOING_AJAX' ) && DOING_AJAX ){
check_ajax_referer( PMPRO_Roles::$ajaction );
}
$levels = $wpdb->get_results( "SELECT * FROM $wpdb->pmpro_membership_levels" );
if( !$levels ) {
if( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
die( 'failed' );
}
else {
return;
}
}
$capabilities = PMPRO_Roles::capabilities();
$i = 0;
foreach ( $levels as $level ) {
$role_key = PMPRO_Roles::$role_key . $level->id;
//the role doesn't exist for this level
if( !get_role( $role_key ) ) {
$i++;
add_role( $role_key, $level->name, $capabilities[$level->id] );
}
}
if( defined( 'DOING_AJAX' ) && DOING_AJAX ){
if($i > 0){
echo $i;
}
else{
echo __('No', PMPRO_Roles::$plugin_slug);
}
die();
}
}
/**
* Assign capabilities to custom roles.
* @since 1.0
*/
public static function capabilities( $role_key = null ) {
$all_levels = pmpro_getAllLevels( true, false );
$capabilities = array();
if( !empty( $role_key ) ){
if( strpos( $role_key, PMPRO_Roles::$role_key ) !== false){
$capabilities[$role_key] = array( 'read' => true );
} else {
$caps = array();
//Get the caps of this role
$role_caps = get_role( $role_key )->capabilities;
if( !empty( $role_caps ) ){
foreach( $role_caps as $cap ){
$caps[$cap] = true;
}
}
$capabilities[$role_key] = $caps;
}
} else {
foreach ( $all_levels as $key => $value ) {
$capabilities[$key] = array( 'read' => true );
}
}
$capabilities = apply_filters( 'pmpro_roles_default_caps', $capabilities );
return $capabilities;
}
/**
* Add "Delete Roles and Deactivate" link to plugins page
* @since 1.0
*/
public static function add_action_links($links) {
// Only add this if plugin is active.
if( is_plugin_active( 'pmpro-roles/pmpro-roles.php' ) ) {
$new_links = array(
'<a href="' . wp_nonce_url(get_admin_url(NULL, 'plugins.php?pmpro_roles_delete_and_deactivate=1'), 'pmpro_roles_delete_and_deactivate') . '">' . esc_html__( 'Delete Roles and Deactivate', 'pmpro-roles' ) . '</a>',
);
return array_merge($new_links, $links);
}
return $links;
}
/**
* Add links to the plugin row meta
*/
public static function plugin_row_meta( $links, $file ) {
if ( strpos( $file, 'pmpro-roles' ) !== false ) {
$new_links = array(
'<a href="' . esc_url( 'https://www.paidmembershipspro.com/add-ons/pmpro-roles/' ) . '" title="' . esc_attr( __( 'View Documentation', 'pmpro-roles' ) ) . '">' . esc_html__( 'Docs', 'pmpro-roles' ) . '</a>',
'<a href="' . esc_url( 'https://paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro-roles' ) ) . '">' . esc_html__( 'Support', 'pmpro-roles' ) . '</a>',
);
$links = array_merge( $links, $new_links );
}
return $links;
}
/**
* Process delete and deactivate if clicked.
*/
public static function delete_and_deactivate() {
//see if our param was passed
if(empty($_REQUEST['pmpro_roles_delete_and_deactivate']))
return;
//check nonce
check_admin_referer('pmpro_roles_delete_and_deactivate');
//find roles based on levels
global $wpdb;
$roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' );
foreach($roles as $key => $role) {
//is this a pmpro role?
if(strpos($key, PMPRO_Roles::$role_key) !== FALSE ) {
//change all users with those roles to have the default role
$users = get_users( array( 'role' => $key ) );
foreach($users as $user) {
if ( count( $user->roles ) > 1 ){
$user->remove_role( $key );
} else {
$default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
$user->set_role( $default_role );
}
}
//delete the roles
remove_role($key);
}
}
//deactivate the plugin
deactivate_plugins( plugin_basename( __FILE__ ) );
//output deactivated notice:
?>
<div id="message" class="updated notice is-dismissible">
<p><?php esc_html_e( 'Plugin deactivated', 'pmpro-roles' );?>.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'pmpro-roles' );?></span></button>
</div>
<?php
}
}
register_activation_hook( __FILE__, array( 'PMPRO_Roles', 'install' ) );
/**
* Initialize PMPro Roles on plugins_loaded and once PMPro is active.
*
* @since 1.4.2
*/
function pmpro_roles_plugins_loaded() {
if ( defined( 'PMPRO_VERSION' ) ) {
new PMPRO_Roles;
}
}
add_action( 'plugins_loaded', 'pmpro_roles_plugins_loaded' );