Advertisement
verygoodplugins

Untitled

Apr 7th, 2020
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.04 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP Fusion - User Roles
  4. Description: Allows linking a tag with a WordPress user role to automatically set roles when tags are modified.
  5. Plugin URI: https://verygoodplugins.com/
  6. Version: 1.0
  7. Author: Very Good Plugins
  8. Author URI: https://verygoodplugins.com/
  9. */
  10.  
  11. function wpf_user_roles_tags_modified( $user_id, $user_tags ) {
  12.  
  13.     // Don't run for admins
  14.     if ( user_can( $user_id, 'manage_options' ) ) {
  15.         return;
  16.     }
  17.  
  18.     $settings = get_option( 'wpf_roles_settings', array() );
  19.  
  20.     $user = get_user_by( 'id', $user_id );
  21.  
  22.     remove_action( 'profile_update', array( wp_fusion()->user, 'profile_update' ), 10, 2 );
  23.  
  24.     foreach ( $settings as $role_slug => $setting ) {
  25.  
  26.         if ( empty( $setting['tag_link'] ) ) {
  27.             continue;
  28.         }
  29.  
  30.         $result = array_intersect( $user_tags, $setting['tag_link'] );
  31.  
  32.         if ( ! empty( $result ) && ! in_array( $role_slug, $user->roles ) ) {
  33.  
  34.             wp_fusion()->logger->handle( 'info', $user_id, 'Adding user role <strong>' . $role_slug . '</strong>, triggered by linked tag <strong>' . $setting['tag_link'][0] . '</strong>' );
  35.  
  36.             $user->set_role( $role_slug );
  37.  
  38.         } elseif ( empty( $result ) && in_array( $role_slug, $user->roles ) ) {
  39.  
  40.             wp_fusion()->logger->handle( 'info', $user_id, 'Removing user role <strong>' . $role_slug . '</strong>, triggered by linked tag <strong>' . $setting['tag_link'][0] . '</strong>' );
  41.  
  42.             $user->remove_role( $role_slug );
  43.  
  44.         }
  45.     }
  46.  
  47. }
  48.  
  49. add_action( 'wpf_tags_modified', 'wpf_user_roles_tags_modified', 10, 2 );
  50.  
  51. function wpf_user_roles_admin_menu() {
  52.  
  53.     $id = add_submenu_page(
  54.         'options-general.php',
  55.         'WP Fusion - User Roles',
  56.         'WP Fusion Roles',
  57.         'manage_options',
  58.         'wpf-roles-settings',
  59.         'wpf_user_roles_render_admin_menu'
  60.     );
  61.  
  62.     add_action( 'load-' . $id, 'wpf_user_roles_enqueue_scripts' );
  63.  
  64. }
  65.  
  66. add_action( 'admin_menu', 'wpf_user_roles_admin_menu' );
  67.  
  68. function wpf_user_roles_enqueue_scripts() {
  69.  
  70.     wp_enqueue_style( 'bootstrap', WPF_DIR_URL . 'includes/admin/options/css/bootstrap.min.css' );
  71.     wp_enqueue_style( 'options-css', WPF_DIR_URL . 'includes/admin/options/css/options.css' );
  72.     wp_enqueue_style( 'wpf-options', WPF_DIR_URL . 'assets/css/wpf-options.css' );
  73.  
  74. }
  75.  
  76. function wpf_user_roles_render_admin_menu() {
  77.  
  78.     // Save settings
  79.     if ( isset( $_POST['wpf_roles_settings_nonce'] ) && wp_verify_nonce( $_POST['wpf_roles_settings_nonce'], 'wpf_roles_settings' ) ) {
  80.         update_option( 'wpf_roles_settings', $_POST['wpf-settings'] );
  81.         echo '<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>';
  82.     }
  83.  
  84.     ?>
  85.  
  86.     <div class="wrap">
  87.         <h2>WP Fusion Roles</h2>
  88.  
  89.         <form id="wpf-roles-settings" action="" method="post">
  90.             <?php wp_nonce_field( 'wpf_roles_settings', 'wpf_roles_settings_nonce' ); ?>
  91.             <input type="hidden" name="action" value="update">
  92.  
  93.  
  94.             <?php global $wp_roles; ?>
  95.  
  96.             <?php $all_roles      = $wp_roles->roles; ?>
  97.             <?php $editable_roles = apply_filters( 'editable_roles', $all_roles ); ?>
  98.  
  99.             <?php $settings       = get_option( 'wpf_roles_settings', array() ); ?>
  100.  
  101.             <table class="table table-hover" id="wpf-mm-products-table">
  102.                 <thead>
  103.                 <tr>
  104.                     <th>Role</th>
  105.                     <th>Link With Tag</th>
  106.                 </tr>
  107.                 </thead>
  108.                 <tbody>
  109.  
  110.                 <?php foreach ( $editable_roles as $slug => $role ) : ?>
  111.  
  112.                     <?php
  113.                     if ( ! isset( $settings[ $slug ]['tag_link'] ) ) {
  114.                         $settings[ $slug ]['tag_link'] = array();
  115.                     }
  116.                     ?>
  117.  
  118.                     <tr>
  119.                         <td><?php echo $role['name']; ?></td>
  120.                         <td id="wpf-tags-td">
  121.                             <?php
  122.  
  123.                             $args = array(
  124.                                 'setting'      => $settings[ $slug ],
  125.                                 'meta_name'    => 'wpf-settings',
  126.                                 'field_id'     => $slug,
  127.                                 'field_sub_id' => 'tag_link',
  128.                                 'placeholder'  => 'Select a tag',
  129.                                 'limit'        => 1,
  130.                             );
  131.  
  132.                             wpf_render_tag_multiselect( $args );
  133.  
  134.                             ?>
  135.                            
  136.                         </td>
  137.                     </tr>
  138.  
  139.                 <?php endforeach; ?>
  140.  
  141.                 </tbody>
  142.  
  143.             </table>
  144.  
  145.             <p class="submit"><input name="Submit" type="submit" class="button-primary" value="Save Changes"/>
  146.             </p>
  147.  
  148.         </form>
  149.  
  150.     </div>
  151.  
  152.     <?php
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement