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:
<?php
if ( !class_exists( 'Inbound_Automation_Action_Create_User' ) ) {
class Inbound_Automation_Action_Create_User {
function __construct() {
add_filter( 'inbound_automation_actions' , array( __CLASS__ , 'define_action' ) , 1 , 1);
}
public static function define_action( $actions ) {
$actions['create_user'] = array (
'class_name' => get_class(),
'id' => 'create_user',
'label' => __( 'Create User / Add Role' , 'inbound-pro' ),
'description' => __( 'Create a WordPress user account if one does not exist.' , 'inbound-pro' ),
'settings' => array(
array (
'id' => 'role',
'label' => __( 'Set role' , 'inbound-pro' ),
'description' => __('Set which role to add user to.', 'inbound-pro'),
'type' => 'dropdown',
'options' => self::get_roles_that_cant('activate_plugins'),
'default' => 'subscriber'
),
array (
'id' => 'login',
'label' => __( 'Log user in automatically' , 'inbound-pro' ),
'description' => __('In order for this feature to work deferred processing must be disabled.', 'inbound-pro'),
'type' => 'dropdown',
'options' => array(
'yes'=>'yes',
'no'=> 'no'
),
'default' => 'yes'
)
)
);
return $actions;
}
public static function run_action( $action , $trigger_data ) {
$email_address = (isset($trigger_data['lead_data']['email'])) ? $trigger_data['lead_data']['email'] : null ;
$first_name = (isset($trigger_data['lead_data']['wpleads_first_name'])) ? $trigger_data['lead_data']['wpleads_first_name'] : null ;
$last_name = (isset($trigger_data['lead_data']['wpleads_last_name'])) ? $trigger_data['lead_data']['wpleads_last_name'] : null ;
if (email_exists($email_address)) {
inbound_record_log(
__( 'User Already Exists, Skipping.' , 'inbound-pro') ,
$trigger_data['lead_data']['id'] . ' <pre></pre>',
$action['rule_id'],
$action['job_id'],
'action_event'
);
return;
}
$password = wp_generate_password( 12, false );
$userdata = array(
'user_login' => $email_address,
'nickname' => $email_address,
'first_name' => $first_name,
'last_name' => $last_name,
'user_email' => $email_address,
'role' => $action['role'],
'user_pass' => $password
);
$user_id = wp_insert_user( $userdata ) ;
if ( ! is_wp_error( $user_id )) {
$user = new WP_User( $user_id );
wp_new_user_notification( $user_id, $password);
inbound_record_log(
__( 'Created User' , 'inbound-pro') ,
$trigger_data['lead_data']['id'] . ' <pre>' . print_r($user , true) . '</pre>',
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
if ($action['login'] == 'yes') {
$creds = array();
$creds['user_login'] = $email_address;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
wp_set_current_user($user->ID);
inbound_record_log(
__( 'Logged User In' , 'inbound-pro') ,
$creds['user_login'] . print_r($user,true),
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
}
return;
}
inbound_record_log(
__( 'Could not create user' , 'inbound-pro') ,
$trigger_data['lead_data']['id'] . ' <pre></pre>',
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
}
public static function get_roles_that_cant($capability) {
global $wp_roles;
if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();
$available_roles_names = $wp_roles->get_names();
$available_roles_capable = array();
foreach ($available_roles_names as $role_key => $role_name) {
$role_object = get_role( $role_key );
$array_of_capabilities = $role_object->capabilities;
if(!isset($array_of_capabilities[$capability]) || $array_of_capabilities[$capability] == 0){
$available_roles_capable[$role_key] = $role_name;
}
}
return $available_roles_capable;
}
}
new Inbound_Automation_Action_Create_User();
}