Advertisement
verygoodplugins

Untitled

Mar 16th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.41 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7.  
  8. class WPF_Members_Plugin extends WPF_Integrations_Base {
  9.  
  10.     /**
  11.      * Gets things started.
  12.      *
  13.      * @since 3.37.3
  14.      */
  15.  
  16.     public function init() {
  17.         $this->slug = 'members-plugin';
  18.  
  19.         // Show Metabox
  20.         add_action( 'members_load_role_edit', array( $this, 'load' ) );
  21.         add_action( 'members_load_role_new', array( $this, 'load' ) );
  22.  
  23.         // Save role position.
  24.         add_action( 'members_role_updated', array( $this, 'save_meta_box_data' ) );
  25.         add_action( 'members_role_added', array( $this, 'save_meta_box_data' ) );
  26.  
  27.         // Remove role meta
  28.         add_action( 'admin_init', array( $this, 'remove_meta_role' ) );
  29.  
  30.         // Callbacks and filters
  31.         add_action( 'wpf_tags_modified', array( $this, 'tags_modified' ), 10, 2 );
  32.     }
  33.  
  34.  
  35.  
  36.     /**
  37.      * Updates user's role if tag is linked to a Members role is changed.
  38.      *
  39.      * @since 3.37.3
  40.      *
  41.      * @param int   $user_id
  42.      * @param array $user_tags
  43.      */
  44.     public function tags_modified( $user_id, $user_tags ) {
  45.  
  46.         global $wp_roles;
  47.  
  48.         $user = get_userdata( $user_id );
  49.  
  50.         foreach ( $wp_roles->role_names as $slug => $label ) {
  51.  
  52.             $settings = get_option( 'members_role_' . $slug . '_meta' );
  53.  
  54.             if ( empty( $settings ) || empty( $settings['wpf_tag_link'] ) ) {
  55.                 continue;
  56.             }
  57.  
  58.             $tag_id = $settings['wpf_tag_link'][0];
  59.  
  60.             if ( in_array( $tag_id, $user_tags ) && ! in_array( $slug, $user->roles ) && ! user_can( $user_id, 'manage_options' ) ) {
  61.  
  62.                 if ( strpos( $slug, 'members_' ) !== false ) {
  63.  
  64.                     // Logger
  65.                     wpf_log( 'info', $user_id, 'Setting Members plugin role <strong>' . $label . '</strong> from linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'members-plugin' ) );
  66.  
  67.                     // Set Role
  68.                     wp_update_user(
  69.                         array(
  70.                             'ID'   => $user_id,
  71.                             'role' => $slug,
  72.                         )
  73.                     );
  74.  
  75.                 } else {
  76.  
  77.                     // Logger
  78.                     wpf_log( 'info', $user_id, 'Setting user role <strong>' . $label . '</strong> from linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'members-plugin' ) );
  79.  
  80.                     // WordPress roles
  81.                     $user->set_role( $slug );
  82.  
  83.                 }
  84.             } elseif ( ! in_array( $tag_id, $user_tags ) && in_array( $slug, $user->roles ) && ! user_can( $user_id, 'manage_options' ) ) {
  85.  
  86.                 // Logger
  87.                 wpf_log( 'info', $user_id, 'Removing Members plugin member role <strong>' . $label . '</strong> from linked tag <strong>' . wp_fusion()->user->get_tag_label( $tag_id ) . '</strong>', array( 'source' => 'members-plugin' ) );
  88.  
  89.                 $user->remove_role( $slug );
  90.  
  91.                 if ( empty( $user->roles ) ) {
  92.  
  93.                     // We don't want to leave someone with no role so we'll assign the default role
  94.                     $default_role = get_option( 'default_role' );
  95.  
  96.                     wpf_log( 'info', $user_id, 'User was left with no role so assigning default role <strong>' . $default_role . '</strong>.', array( 'source' => 'members-plugin' ) );
  97.  
  98.                     wp_update_user(
  99.                         array(
  100.                             'ID'   => $user_id,
  101.                             'role' => $default_role,
  102.                         )
  103.                     );
  104.                 }
  105.             }
  106.         }
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Remove role meta when the role is removed.
  112.      *
  113.      * @since 3.37.3
  114.      */
  115.     public function remove_meta_role() {
  116.         // Get the current action if sent as request.
  117.         $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : false;
  118.  
  119.         // Get the current action if posted.
  120.         if ( ( isset( $_POST['action'] ) && 'delete' == $_POST['action'] ) || ( isset( $_POST['action2'] ) && 'delete' == $_POST['action2'] ) ) {
  121.             $action = 'bulk-delete';
  122.         }
  123.  
  124.         // Bulk delete role handler.
  125.         if ( 'bulk-delete' === $action ) {
  126.  
  127.             // If roles were selected, let's delete some roles.
  128.             if ( current_user_can( 'delete_roles' ) && isset( $_POST['roles'] ) && is_array( $_POST['roles'] ) ) {
  129.  
  130.                 // Verify the nonce. Nonce created via `WP_List_Table::display_tablenav()`.
  131.                 check_admin_referer( 'bulk-roles' );
  132.  
  133.                 // Loop through each of the selected roles.
  134.                 foreach ( $_POST['roles'] as $role ) {
  135.  
  136.                     $role = members_sanitize_role( $role );
  137.  
  138.                     if ( members_role_exists( $role ) ) {
  139.                         delete_option( "members_role_{$role}_meta" );
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             // Delete single role handler.
  145.         } elseif ( 'delete' === $action ) {
  146.  
  147.             // Make sure the current user can delete roles.
  148.             if ( current_user_can( 'delete_roles' ) ) {
  149.  
  150.                 // Verify the referer.
  151.                 check_admin_referer( 'delete_role', 'members_delete_role_nonce' );
  152.  
  153.                 // Get the role we want to delete.
  154.                 $role = members_sanitize_role( $_GET['role'] );
  155.  
  156.                 // Check that we have a role before attempting to delete it.
  157.                 if ( members_role_exists( $role ) ) {
  158.                     // Delete the role meta.
  159.                     delete_option( "members_role_{$role}_meta" );
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.  
  166.     /**
  167.      * Runs on the page load hook to hook in the meta boxes.
  168.      *
  169.      * @since 3.37.3
  170.      */
  171.  
  172.     public function load() {
  173.         add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Adds meta box, only for Members role page.
  179.      *
  180.      * @since 3.37.3
  181.      *
  182.      * @param object $screen_id
  183.      * @param string $role
  184.      */
  185.     public function add_meta_box( $screen_id, $role = '' ) {
  186.         // If role isn't editable, bail.
  187.         if ( $role && ! members_is_role_editable( $role ) ) {
  188.             return;
  189.         }
  190.  
  191.         add_meta_box( 'wpf-members-meta', 'WP Fusion', array( $this, 'meta_box_callback' ), $screen_id, 'side', 'core' );
  192.     }
  193.  
  194.     /**
  195.      * Displays meta box content.
  196.      *
  197.      * @since 3.37.3
  198.      *
  199.      * @param string $role
  200.      */
  201.     public function meta_box_callback( $role ) {
  202.         // Add an nonce field so we can check for it later.
  203.         wp_nonce_field( 'wpf_meta_box_members', 'wpf_meta_box_members_nonce' );
  204.  
  205.         $settings = array(
  206.             'wpf_tag_link' => array(),
  207.         );
  208.         if ( isset( $_GET['role'] ) ) {
  209.             $settings = get_option( "members_role_{$_GET['role']}_meta" );
  210.         }
  211.  
  212.         /*
  213.         // Apply tags
  214.         */
  215.  
  216.         echo '<p><label><strong>' . sprintf( __( 'Link with %s tag', 'wp-fusion' ), wp_fusion()->crm->name ) . ':</strong></label></p>';
  217.  
  218.         $args = array(
  219.             'setting'     => $settings['wpf_tag_link'],
  220.             'meta_name'   => 'members_tag',
  221.             'field_id'    => 'wpf_tag_link',
  222.             'placeholder' => 'Select Tag',
  223.             'limit'       => 1,
  224.         );
  225.  
  226.         wpf_render_tag_multiselect( $args );
  227.  
  228.         echo '<span class="description">When the selected tag is applied, users will automatically be given the ' . ( isset( $_GET['role'] ) ? $_GET['role'] : '' ) . ' role. <br /><br />When the tag is removed the role will be removed.</span>';
  229.  
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Saves Members meta box data
  235.      *
  236.      * @since 3.37.3
  237.      * @access public
  238.      * @return null
  239.      */
  240.  
  241.     public function save_meta_box_data() {
  242.         // Check if our nonce is set.
  243.         if ( ! isset( $_POST['wpf_meta_box_members_nonce'] ) ) {
  244.             return;
  245.         }
  246.  
  247.         // Verify that the nonce is valid.
  248.         if ( ! wp_verify_nonce( $_POST['wpf_meta_box_members_nonce'], 'wpf_meta_box_members' ) ) {
  249.             return;
  250.         }
  251.  
  252.         // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  253.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  254.             return;
  255.         }
  256.  
  257.         if ( isset( $_GET['role'] ) ) {
  258.             $role_name = $_GET['role'];
  259.         } else {
  260.             $role_name = $_POST['role_name'];
  261.         }
  262.  
  263.         if ( isset( $_POST['members_tag'] ) && isset( $_POST['members_tag']['wpf_tag_link'] ) ) {
  264.             update_option( "members_role_{$role_name}_meta", $_POST['members_tag'], false );
  265.         } else {
  266.             delete_option( "members_role_{$role_name}_meta" );
  267.         }
  268.     }
  269.  
  270.  
  271.  
  272. }
  273.  
  274. new WPF_Members_Plugin();
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement