/var/www/html_uk/wp-content/plugins/automatewoo/includes/Actions/Customer_Change_Role.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
<?php

namespace AutomateWoo;

use 
AutomateWoo\Fields\User_Role;

if ( ! 
defined'ABSPATH' ) ) {
    exit;
}

/**
 * @class Action_Customer_Change_Role
 */
class Action_Customer_Change_Role extends Action {

    
/**
     * The data items required by the action.
     *
     * @var array
     */
    
public $required_data_items = [ 'customer' ];

    
/**
     * Method to set the action's admin props.
     */
    
public function load_admin_details() {
        
$this->title       __'Change Role''automatewoo' );
        
$this->group       __'Customer''automatewoo' );
        
$this->description __'Please note that if the customer is a guest this action will do nothing.''automatewoo' );
    }

    
/**
     * Method to load the action's fields.
     */
    
public function load_fields() {
        
$user_type = new Fields\User_Rolefalse$this->allow_all_roles() );
        
$user_type->set_required();

        
$this->add_field$user_type );
    }

    
/**
     * Run the action.
     *
     * @throws \Exception When an error occurs.
     */
    
public function run() {
        
$customer $this->workflow->data_layer()->get_customer();
        if ( ! 
$customer ) {
            return;
        }

        
$role $this->get_option'user_type' );
        
$user $customer->get_user();

        if ( ! 
$this->allow_all_roles() && ( in_array$roleUser_Role::PROTECTED_ROLEStrue ) || in_array$customer->get_role(), User_Role::PROTECTED_ROLEStrue ) ) ) {
            return;
        }

        if ( 
$role && $user ) {
            
$user->set_role$role );
        }
    }

    
/**
     * Check if roles in User_Role::PROTECTED_ROLES should be shown as well.
     *
     * @return bool
     */
    
private function allow_all_roles() {
        return (bool) 
apply_filters'automatewoo/change_role/allow_all_roles'false );
    }
}