Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/pmpro-roles/pmpro-roles.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Plugin Name: Paid Memberships Pro - Roles Add On
4 + * Description: Adds a WordPress Role for each Membership Level.
5 + * Plugin URI: https://www.paidmembershipspro.com/add-ons/pmpro-roles/
6 + * Author: Paid Memberships Pro
7 + * Author URI: https://www.paidmembershipspro.com
8 + * Version: 1.5.1
9 + * License: GPLv2 or later
10 + * Text Domain: pmpro-roles
11 + * Domain Path: /languages
12 + */
13 +
14 + define( 'PMPRO_ROLES_VERSION', '1.5.1' );
15 +
16 + class PMPRO_Roles {
17 +
18 + static $role_key = 'pmpro_role_';
19 + static $plugin_slug = 'pmpro-roles';
20 + static $plugin_prefix = 'pmpro_roles_';
21 + static $ajaction = 'pmpro_roles_repair';
22 +
23 + function __construct(){
24 + $this->pmpro_hooks();
25 + $this->wp_hooks();
26 + }
27 +
28 + /**
29 + * Hooks that run only when PMPro is active.
30 + *
31 + * @since 1.4.2
32 + */
33 + function pmpro_hooks() {
34 + add_action( 'pmpro_save_membership_level', array( $this, 'edit_level' ) );
35 + add_action( 'pmpro_delete_membership_level', array( $this, 'delete_level' ) );
36 + if ( defined( 'PMPRO_VERSION' ) && version_compare( '2.5.8', PMPRO_VERSION, '>' ) ) {
37 + // Use legacy functionality to update roles on level change.
38 + add_action( 'pmpro_after_change_membership_level', array($this, 'user_change_level' ), 10, 2 );
39 + } else {
40 + add_action( 'pmpro_after_all_membership_level_changes', array( $this, 'after_all_level_changes' ), 10, 1 );
41 + }
42 +
43 + add_action( 'pmpro_membership_level_after_other_settings', array( 'PMPRO_Roles', 'level_settings' ) );
44 +
45 + add_action( 'pmpro_manage_memberslist_columns', array( $this, 'add_role_column_to_members_list' ) );
46 + add_action( 'pmpro_manage_memberslist_custom_column', array( $this, 'add_role_column_content_to_members_list' ), 10, 2 );
47 + }
48 +
49 + /**
50 + * Hooks that may need to run without PMPro activated.
51 + *
52 + * @return void
53 + */
54 + function wp_hooks() {
55 + add_action( 'init', array( $this, 'load_text_domain' ) );
56 + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( 'PMPRO_Roles', 'add_action_links' ) );
57 + add_filter( 'plugin_row_meta', array( 'PMPRO_Roles', 'plugin_row_meta' ), 10, 2 );
58 + add_filter( 'editable_roles', array( 'PMPRO_Roles', 'remove_list_roles' ), 10, 1 );
59 + add_action( 'admin_init', array( 'PMPRO_Roles', 'delete_and_deactivate' ) );
60 + }
61 +
62 + /**
63 + * Load plugin text domain for translations.
64 + * @since 1.3
65 + */
66 + function load_text_domain() {
67 + load_plugin_textdomain( 'pmpro-roles', false, basename( dirname( __FILE__ ) ) . '/languages' );
68 + }
69 +
70 + /**
71 + * Get an array of roles set for a given level.
72 + * @since 1.4.1
73 + * @param int $level_id The ID of the level to check for.
74 + */
75 + public static function get_roles_for_level( $level_id ) {
76 + // In case a level object is passed in.
77 + if ( is_object( $level_id ) && ! empty( $level_id->id ) ) {
78 + $level_id = $level_id->id;
79 + }
80 +
81 + // Fail if no level.
82 + if ( empty( $level_id ) ) {
83 + return array();
84 + }
85 +
86 + $roles = get_option( self::$plugin_prefix . $level_id );
87 +
88 + // Default to the site role, if no role is found.
89 + if ( empty( $roles ) ) {
90 + $roles = array();
91 + $default_role = get_option( 'default_role' );
92 + $roles[ $default_role ] = ucfirst( $default_role );
93 +
94 + }
95 +
96 + return $roles;
97 + }
98 +
99 + /**
100 + * Settings for the edit level admin screen. Creates and saves role selection per level.
101 + * SECURITY: Nonce checks are run in paid-memberships-pro/adminpages/membershiplevels.php which once passed and OK, run the pmpro_save_membership_level hook.
102 + * @since 1.3
103 + */
104 + function edit_level( $saveid ) {
105 + global $wpdb;
106 +
107 + //by being here, we know we already have the $_REQUEST we need, so no need to check.
108 + $capabilities = self::capabilities( self::$role_key . $saveid ) ?: array( 'read' => true );
109 +
110 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
111 + if ( ! empty( $_REQUEST['pmpro_roles_level_present'] ) ) {
112 +
113 + if ( ! empty( $_REQUEST['pmpro_roles_level'] ) ) {
114 + $level_roles = $_REQUEST['pmpro_roles_level'];
115 + } else {
116 + // If no role chosen, use the default.
117 + $level_roles = array();
118 + $default_role = get_option( 'default_role' );
119 + $level_roles[ $default_role ] = ucfirst( $default_role );
120 + }
121 +
122 + // We detect that the draft_role has been selected, so lets try to create it. (This can now happen whenever the role doesn't exist not just on new level creation)
123 + if ( ! empty( $level_roles['pmpro_draft_role'] ) ) {
124 + unset( $level_roles['pmpro_draft_role'] ); // Remove it from the array.
125 + $role_name = sanitize_text_field( $_REQUEST['name'] );
126 + add_role( PMPRO_Roles::$role_key.$saveid, $role_name, $capabilities );
127 + $level_roles[PMPRO_Roles::$role_key.$saveid] = $role_name; // Got to add the newly created role to the level_roles array.
128 + remove_role( 'pmpro_draft_role' ); // Delete the role entirely in case it exists, we no longer need it at this point forward.
129 + }
130 +
131 + if ( isset( $_REQUEST['edit'] ) && $_REQUEST['edit'] < 0 ) {
132 + foreach( $level_roles as $role_key => $role_name ){
133 + if ( $role_key === 'pmpro_role_'. $saveid ) {
134 + $capabilities = PMPRO_Roles::capabilities( $role_key );
135 + add_role( $role_key, $role_name, $capabilities );
136 + }
137 + }
138 + } else {
139 + //have to get all roles and find ours because get_role() doesn't yield the role's "pretty" name, only its index.
140 + $roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' );
141 +
142 + if(!is_array( $roles ) ) return;
143 +
144 + foreach( $level_roles as $role_key => $role_name ){
145 +
146 + if( !isset( $roles[$role_key] ) ){
147 + $capabilities = PMPRO_Roles::capabilities( $role_key );
148 + add_role( $role_key, sanitize_text_field( $_REQUEST['name'] ), $capabilities[$role_key] );
149 + return;
150 + }
151 +
152 + if( ( strpos( $role_key, PMPRO_Roles::$role_key ) !== FALSE ) && sanitize_text_field( $_REQUEST['name'] ) !== $role_name ) {
153 + PMPRO_Roles::update_role_name( $role_key, sanitize_text_field( $_REQUEST['name'] ) );
154 + }
155 +
156 + }
157 + }
158 +
159 + update_option( 'pmpro_roles_'.$saveid, $level_roles );
160 +
161 + }
162 +
163 + }
164 +
165 + /**
166 + * Delete custom PMPro role on level delete.
167 + * @since 1.0
168 + */
169 + function delete_level( $delete_id ) {
170 + $role_key = PMPRO_Roles::$role_key . $delete_id;
171 + if( !empty( $role_key ) ){
172 + remove_role( $role_key );
173 + }
174 +
175 + delete_option( 'pmpro_roles_' . $delete_id );
176 +
177 + do_action( 'pmpro_roles_delete_membership_level', $delete_id );
178 + }
179 +
180 + /**
181 + * Update a PMPro Roles Name if level name changes.
182 + * @since 1.3
183 + */
184 + function update_role_name( $role, $name ){
185 + if( strpos( $role, PMPRO_Roles::$role_key ) !== FALSE ) {
186 + $roles_array = get_option( 'wp_user_roles', true );
187 + if( !empty( $roles_array[$role] ) ){
188 + $roles_array[$role]['name'] = sanitize_text_field( $name );
189 + $updated = update_option( 'wp_user_roles', $roles_array );
190 + }
191 + }
192 + }
193 +
194 + /**
195 + * Update users' roles after their membership levels are changed.
196 + *
197 + * @param array $old_user_levels $user_id => array $old_levels
198 + */
199 + function after_all_level_changes( $old_user_levels ) {
200 + foreach ( $old_user_levels as $user_id => $old_levels ) {
201 + // Get the user who changed levels.
202 + $user = new WP_User( $user_id );
203 +
204 + // Ignore administrators.
205 + if ( in_array( 'administrator', $user->roles ) ) {
206 + continue;
207 + }
208 +
209 + // Get the user's current active membership levels.
210 + $new_levels = pmpro_getMembershipLevelsForUser( $user_id );
211 +
212 + $new_roles = array();
213 + $old_roles = array();
214 +
215 + // Add default role to $old_roles if it is already one of the user roles.
216 + $default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
217 + if ( in_array( $default_role, $user->roles ) ) {
218 + $old_roles = array( $default_role );
219 + }
220 +
221 + // Build an array of all roles assigned to the user's old membership levels.
222 + foreach ( $old_levels as $old_level ) {
223 + $old_level_roles = self::get_roles_for_level( $old_level->id );
224 + if ( ! empty( $old_level_roles ) && is_array( $old_level_roles ) ) {
225 + $old_roles = array_merge( $old_roles, array_keys( $old_level_roles ) );
226 + }
227 + }
228 +
229 + // Build an array of all roles assigned to the user's new membership levels.
230 + foreach ( $new_levels as $new_level ) {
231 + $new_level_roles = self::get_roles_for_level( $new_level->id );
232 + if ( ! empty( $new_level_roles ) && is_array( $new_level_roles ) ) {
233 + $new_roles = array_merge( $new_roles, array_keys( $new_level_roles ) );
234 + }
235 + }
236 +
237 + // Remove duplicates in the array of old and new roles.
238 + $old_roles = array_unique( $old_roles );
239 + $new_roles = array_unique( $new_roles );
240 +
241 + // Build a unique array of roles to add and remove from user.
242 + $add_roles = $new_roles;
243 + $remove_roles = array_values( array_diff( $old_roles, $new_roles ) );
244 +
245 + // Add roles to user.
246 + foreach ( $add_roles as $add_role ) {
247 + $user->add_role( $add_role );
248 + }
249 +
250 + // Remove roles from user.
251 + foreach ( $remove_roles as $remove_role ) {
252 + $user->remove_role( $remove_role );
253 + }
254 +
255 + // Handle case where user has no role.
256 + if ( empty( $user->roles ) ) {
257 + $user->add_role( $default_role );
258 + }
259 +
260 + // Allows user's to be inside the foreach loop and hook in to do things.
261 + do_action( 'pmpro_roles_after_role_change', $user, $old_user_levels, $old_roles, $new_roles );
262 +
263 + }
264 + }
265 +
266 + /**
267 + * Change user role based on level change. (Now supports MMPU)
268 + * No longer being used if PMPro version >= 2.5.8.
269 + *
270 + * @since 1.0
271 + */
272 + function user_change_level($level_id, $user_id){
273 +
274 + global $pmpro_checkout_levels;
275 + //get user object
276 + $wp_user_object = new WP_User($user_id);
277 + //ignore admins
278 + if( in_array( 'administrator', $wp_user_object->roles ) )
279 + return;
280 +
281 + // Check if user is cancelling.
282 + if( defined( 'PMPROMMPU_DIR' ) && !empty( $_REQUEST['levelstocancel'] ) ) { //Adds support for MMPU
283 + $levels_to_cancel = explode( " ", $_REQUEST['levelstocancel'] );
284 + if( !empty( $levels_to_cancel ) ){
285 + foreach( $levels_to_cancel as $ltc ){
286 + $wp_user_object->remove_role( PMPRO_Roles::$role_key.intval( $ltc ) );
287 + }
288 + }
289 +
290 + } else if( $level_id == 0 ) {
291 + $default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
292 + $wp_user_object->set_role( $default_role );
293 + } else {
294 + if( !empty( $pmpro_checkout_levels ) ){
295 + //Adds support for MMPU
296 + foreach( $pmpro_checkout_levels as $co_level ){
297 + $roles = self::get_roles_for_level( $co_level->id );
298 + if( is_array( $roles ) && ! empty( $roles ) ){
299 + foreach( $roles as $role_key => $role_name ){
300 + $wp_user_object->add_role( $role_key );
301 + }
302 + } else {
303 + $wp_user_object->set_role( PMPRO_Roles::$role_key . $co_level->id );
304 + }
305 + }
306 + } else if( $level_id > 0 ){
307 + $roles = self::get_roles_for_level( $level_id );
308 + if( is_array( $roles ) && ! empty( $roles ) ){
309 + $count = 1;
310 + foreach( $roles as $role_key => $role_name ){
311 + if( $count == 1 ){
312 + $wp_user_object->set_role( $role_key );
313 + } else {
314 + $wp_user_object->add_role( $role_key );
315 + }
316 + $count++;
317 + }
318 + } else {
319 + $wp_user_object->set_role( PMPRO_Roles::$role_key.intval( $level_id ) );
320 + }
321 + }
322 + }
323 + }
324 +
325 + /**
326 + * Show a list of all available roles as a checkbox inside level settings.
327 + * @since 1.3
328 + */
329 + public static function level_settings() {
330 + ?>
331 + <hr />
332 +
333 + <h3><?php esc_html_e( 'Role Settings', 'pmpro-roles' ); ?></h3>
334 + <p class="description">
335 + <?php
336 + $allowed_pmpro_roles_description_html = array (
337 + 'a' => array (
338 + 'href' => array(),
339 + 'target' => array(),
340 + 'title' => array(),
341 + ),
342 + );
343 +
344 + // translators: %s is a link to the download page for the Roles Add On.
345 + 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' );
346 + echo '<p>' . esc_html__( 'If you do not select a custom role for users of this membership level, the user will be assigned the "New User Default Role" as defined under Settings > General in the WordPress admin', 'pmpro-roles' ) . '</p>';
347 + ?>
348 + </p>
349 + <table class="form-table">
350 + <tbody>
351 + <?php
352 +
353 + $level_id = absint( filter_input( INPUT_GET, 'edit', FILTER_DEFAULT ) );
354 +
355 + global $wp_roles;
356 +
357 + $all_roles = $wp_roles->roles;
358 +
359 + $editable_roles = apply_filters('editable_roles', $all_roles);
360 +
361 + $saved_roles = self::get_roles_for_level( $level_id );
362 +
363 + asort( $editable_roles ); //Display alphabetically
364 +
365 + if( !empty( $editable_roles ) ){
366 + ?>
367 + <tr>
368 + <th scope="row" valign="top"><label><?php esc_html_e( 'Roles', 'pmpro-roles' ); ?>:</label></th>
369 + <td>
370 + <?php
371 + // Build the selectors for the checkbox list based on number of levels.
372 + $classes = array();
373 + $classes[] = "pmpro_checkbox_box";
374 + if ( count( $editable_roles ) > 5 ) {
375 + $classes[] = "pmpro_scrollable";
376 + }
377 + $class = implode( ' ', array_unique( $classes ) );
378 + ?>
379 + <div class="<?php echo esc_attr( $class ); ?>">
380 + <input type="hidden" name="pmpro_roles_level_present" value="1" />
381 + <?php
382 + //New level, choose if they want to create a role for this level
383 + if ( ! $wp_roles->is_role( PMPRO_Roles::$role_key.$level_id ) || $_REQUEST['edit'] < 0 ) { ?>
384 + <div class="pmpro_clickable" style="border-bottom-width: 4px;">
385 + <input type='checkbox' name='pmpro_roles_level[pmpro_draft_role]' value='pmpro_draft_role' id='pmpro_draft_role' />
386 + <label for='pmpro_draft_role'>
387 + <em><?php esc_html_e( 'Create a new custom role for this membership level', 'pmpro-roles' ); ?></em>
388 + </label>
389 + </div>
390 + <?php
391 + }
392 +
393 + $custom_pmpro_role = PMPRO_Roles::$role_key.$level_id;
394 +
395 + $checked = '';
396 +
397 + if ( isset( $saved_roles[$custom_pmpro_role] ) || empty( $saved_roles ) ) {
398 + $checked = 'checked=true';
399 + }
400 +
401 + if ( isset( $editable_roles[$custom_pmpro_role] ) ) { ?>
402 + <div class="pmpro_clickable" style="border-bottom-width: 4px;">
403 + <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 ); ?> />
404 + <label for='<?php echo esc_attr( $custom_pmpro_role ); ?>'>
405 + <?php echo esc_html( $editable_roles[$custom_pmpro_role]['name'] ); ?>
406 + <?php printf( "<code>" . esc_html( 'pmpro_role_%s' ) . "</code>", $level_id ); ?>
407 + </label>
408 + </div>
409 + <?php
410 + }
411 +
412 + $exclude_other_pmpro_roles = apply_filters( 'pmpro_roles_exclude_other_pmpro_roles', true, $level_id );
413 +
414 + foreach ( $editable_roles as $key => $role ) {
415 + $checked = '';
416 + //Backwards compat here, if $saved_roles is empty, set the default level's role as checked
417 + if ( empty( $saved_roles ) ) {
418 + if ( PMPRO_Roles::$role_key.$level_id == $key ) {
419 + $checked = 'checked=true';
420 + }
421 + }
422 +
423 + if ( isset( $saved_roles[$key] ) ) {
424 + $checked = 'checked=true';
425 + }
426 +
427 + if ( $exclude_other_pmpro_roles ) {
428 + //excluding the pmpro_role_ roles here
429 + if ( $key != 'pmpro_role_' . $level_id ) { //Show this one first
430 + ?>
431 + <div class="pmpro_clickable">
432 + <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 ); ?> />
433 + <label for='<?php echo esc_attr( $key ); ?>'>
434 + <?php echo esc_html( $role['name'] ); ?>
435 + <?php echo "<code>". esc_html( $key ). "</code>"; ?>
436 + </label>
437 + </div>
438 + <?php
439 + }
440 + } else {
441 + //include all roles. No checks needed
442 + ?>
443 + <div class="pmpro_clickable">
444 + <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 ); ?> />
445 + <label for='<?php echo esc_attr( $key ); ?>'>
446 + <?php echo esc_html( $role['name'] ); ?>
447 + <?php echo "<code>". esc_html( $key ). "</code>"; ?>
448 + </label>
449 + </div>
450 + <?php
451 + }
452 + }
453 + ?>
454 + </div> <!-- end checkbox_box -->
455 + </td>
456 + </tr>
457 + <?php
458 + }
459 + ?>
460 + </tbody>
461 + </table>
462 + <?php
463 + }
464 +
465 + /**
466 + * Helper function to remove the administrator role from level settings.
467 + * @since 1.3
468 + */
469 + public static function remove_list_roles( $roles ){
470 +
471 + if( !function_exists( 'pmpro_getAllLevels' ) ){
472 + return $roles;
473 + }
474 +
475 + if( !empty( $_REQUEST['edit'] ) ){
476 +
477 + $edit_level = intval( $_REQUEST['edit'] );
478 +
479 + $all_levels = pmpro_getAllLevels( true, false );
480 +
481 + if( apply_filters( 'pmpro_roles_hide_admin_role', true, $edit_level ) ){
482 + //Take admins out of the array first
483 + unset( $roles['administrator'] );
484 + }
485 +
486 + foreach( $all_levels as $level_key => $level ){
487 + if( $level_key !== $edit_level ){
488 + if( isset( $roles[PMPRO_Roles::$role_key.$level_key] ) ){
489 + unset( $roles[PMPRO_Roles::$role_key.$level_key] );
490 + }
491 + }
492 + }
493 +
494 + }
495 +
496 + return $roles;
497 +
498 + }
499 +
500 + /**
501 + * Add a "Role" column to the Members List table.
502 + * @since TBD
503 + * @param array $columns The columns in the members list table.
504 + * @return array The columns in the members list table, with the "Role" column added.
505 + */
506 + public function add_role_column_to_members_list( $columns ) {
507 + $columns['role'] = __( 'Role', 'pmpro-roles' );
508 + return $columns;
509 + }
510 +
511 + /**
512 + * Add content to the "Role" column in the Members List table.
513 + * @since 1.4
514 + * @param string $column_name The name of the column.
515 + * @param int $user_id The ID of the user.
516 + * @return void
517 + */
518 + public function add_role_column_content_to_members_list( $column_name, $user_id ) {
519 + global $wp_roles;
520 + if ( $column_name === 'role' && ! empty( $wp_roles->roles ) ) {
521 + $user = get_userdata( $user_id );
522 + $roles = $user->roles;
523 +
524 + // Get the display name for each role
525 + $role_names = array_map( function ( $role ) use ( $wp_roles ) {
526 + return isset( $wp_roles->roles[$role]['name'] ) ? translate_user_role( $wp_roles->roles[$role]['name'] ) : ucfirst( $role );
527 + }, $roles );
528 +
529 + echo esc_html( implode( ', ', $role_names ) );
530 + }
531 + }
532 +
533 + /**
534 + * Initial function to run on install. Create roles for each existing level.
535 + * @since 1.0
536 + */
537 + public static function install() {
538 + // Only run this code if PMPro is active.
539 + if ( function_exists( 'pmpro_getAllLevels' ) ) {
540 + $levels = pmpro_getAllLevels( true, false );
541 + } else {
542 + $levels = false;
543 + }
544 +
545 + // No levels, no roles to create.
546 + if ( ! $levels ) {
547 + return;
548 + }
549 +
550 + // Get all capabilities for the custom roles.
551 + $capabilities = PMPRO_Roles::capabilities();
552 +
553 + foreach ( $levels as $level ) {
554 + $role_key = PMPRO_Roles::$role_key . $level->id;
555 + //the role doesn't exist for this level
556 + if ( ! get_role( $role_key ) ) {
557 + add_role( $role_key, $level->name, $capabilities[$level->id] );
558 + }
559 + }
560 + }
561 +
562 + /**
563 + * Assign capabilities to custom roles.
564 + * @since 1.0
565 + */
566 + public static function capabilities( $role_key = null ) {
567 + $all_levels = pmpro_getAllLevels( true, false );
568 + $capabilities = array();
569 +
570 + if( !empty( $role_key ) ){
571 + if( strpos( $role_key, PMPRO_Roles::$role_key ) !== false){
572 + $capabilities[$role_key] = array( 'read' => true );
573 + } else {
574 + $caps = array();
575 + //Get the caps of this role
576 + $role_caps = get_role( $role_key )->capabilities;
577 + if( !empty( $role_caps ) ){
578 + foreach( $role_caps as $cap ){
579 + $caps[$cap] = true;
580 + }
581 + }
582 + $capabilities[$role_key] = $caps;
583 + }
584 + } else {
585 + foreach ( $all_levels as $key => $value ) {
586 + $capabilities[$key] = array( 'read' => true );
587 + }
588 + }
589 +
590 + $capabilities = apply_filters( 'pmpro_roles_default_caps', $capabilities );
591 +
592 + return $capabilities;
593 + }
594 +
595 + /**
596 + * Add "Delete Roles and Deactivate" link to plugins page
597 + * @since 1.0
598 + */
599 + public static function add_action_links($links) {
600 + // Only add this if plugin is active.
601 + if( is_plugin_active( 'pmpro-roles/pmpro-roles.php' ) ) {
602 + $new_links = array(
603 + '<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>',
604 + );
605 + return array_merge($new_links, $links);
606 + }
607 +
608 + return $links;
609 + }
610 +
611 + /**
612 + * Add links to the plugin row meta
613 + */
614 + public static function plugin_row_meta( $links, $file ) {
615 + if ( strpos( $file, 'pmpro-roles' ) !== false ) {
616 + $new_links = array(
617 + '<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>',
618 + '<a href="' . esc_url( 'https://paidmembershipspro.com/support/' ) . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro-roles' ) ) . '">' . esc_html__( 'Support', 'pmpro-roles' ) . '</a>',
619 + );
620 + $links = array_merge( $links, $new_links );
621 + }
622 + return $links;
623 + }
624 +
625 + /**
626 + * Process delete and deactivate if clicked.
627 + */
628 + public static function delete_and_deactivate() {
629 + //see if our param was passed
630 + if(empty($_REQUEST['pmpro_roles_delete_and_deactivate']))
631 + return;
632 +
633 + //check nonce
634 + check_admin_referer('pmpro_roles_delete_and_deactivate');
635 +
636 + //find roles based on levels
637 + global $wpdb;
638 + $roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' );
639 +
640 + foreach($roles as $key => $role) {
641 + //is this a pmpro role?
642 + if(strpos($key, PMPRO_Roles::$role_key) !== FALSE ) {
643 + //change all users with those roles to have the default role
644 + $users = get_users( array( 'role' => $key ) );
645 +
646 + foreach($users as $user) {
647 + if ( count( $user->roles ) > 1 ){
648 + $user->remove_role( $key );
649 + } else {
650 + $default_role = apply_filters( 'pmpro_roles_downgraded_role', get_option( 'default_role' ) );
651 + $user->set_role( $default_role );
652 + }
653 + }
654 +
655 + //delete the roles
656 + remove_role($key);
657 + }
658 + }
659 +
660 + // Remove the pmpro_draft_role if it exists.
661 + remove_role( 'pmpro_draft_role' );
662 +
663 + //deactivate the plugin
664 + deactivate_plugins( plugin_basename( __FILE__ ) );
665 +
666 + //output deactivated notice:
667 + ?>
668 + <div id="message" class="updated notice is-dismissible">
669 + <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>
670 + </div>
671 + <?php
672 + }
673 + } // End of class.
674 + register_activation_hook( __FILE__, array( 'PMPRO_Roles', 'install' ) );
675 +
676 + /**
677 + * Initialize PMPro Roles on plugins_loaded and once PMPro is active.
678 + *
679 + * @since 1.4.2
680 + */
681 + function pmpro_roles_plugins_loaded() {
682 + if ( defined( 'PMPRO_VERSION' ) ) {
683 + new PMPRO_Roles;
684 + }
685 + }
686 + add_action( 'plugins_loaded', 'pmpro_roles_plugins_loaded' );
687 +