| @@ -1,0 +1,265 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Role | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Actions\User_Role | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_User_Role extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the action | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->action = $integration . '_user_role'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the action | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_action( $this->action, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'Add, change or remove role to user', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'Add, change or remove <strong>role</strong> to user', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Operation (add, change or remove). %2$s: Role. %3$s: User. */ | |
| 40 | + 'edit_label' => sprintf( __( '%1$s role %2$s to %3$s', 'automatorwp' ), '{operation}', '{role}', '{user}' ), | |
| 41 | + /* translators: %1$s: Operation (add, change or remove). %2$s: Role. %3$s: User. */ | |
| 42 | + 'log_label' => sprintf( __( '%1$s role %2$s to %3$s', 'automatorwp' ), '{operation}', '{role}', '{user}' ), | |
| 43 | + 'options' => array( | |
| 44 | + 'operation' => array( | |
| 45 | + 'from' => 'operation', | |
| 46 | + 'fields' => array( | |
| 47 | + 'operation' => array( | |
| 48 | + 'name' => __( 'Operation:', 'automatorwp' ), | |
| 49 | + 'type' => 'select', | |
| 50 | + 'options' => array( | |
| 51 | + 'add' => __( 'Add', 'automatorwp' ), | |
| 52 | + 'change' => __( 'Change', 'automatorwp' ), | |
| 53 | + 'remove' => __( 'Remove', 'automatorwp' ), | |
| 54 | + ), | |
| 55 | + 'default' => 'add' | |
| 56 | + ), | |
| 57 | + ) | |
| 58 | + ), | |
| 59 | + 'role' => array( | |
| 60 | + 'from' => 'role', | |
| 61 | + 'default' => '', | |
| 62 | + 'fields' => array( | |
| 63 | + 'role' => automatorwp_utilities_role_field( array( | |
| 64 | + 'option_custom' => true, | |
| 65 | + ) ), | |
| 66 | + 'role_custom' => automatorwp_utilities_custom_field( array( | |
| 67 | + 'option_custom_desc' => __( 'Role name.', 'automatorwp' ) | |
| 68 | + ) ), | |
| 69 | + ) | |
| 70 | + ), | |
| 71 | + 'user' => array( | |
| 72 | + 'default' => __ ( 'user', 'automatorwp' ), | |
| 73 | + 'fields' => array( | |
| 74 | + 'user_id' => array( | |
| 75 | + 'name' => __( 'User ID:', 'automatorwp' ), | |
| 76 | + 'desc' => __( 'The user\'s ID to update their role. Leave empty to assign the user that completes the automation.', 'automatorwp' ), | |
| 77 | + 'type' => 'text', | |
| 78 | + 'default' => '' | |
| 79 | + ), | |
| 80 | + ), | |
| 81 | + ), | |
| 82 | + ), | |
| 83 | + 'tags' => array_merge( | |
| 84 | + automatorwp_utilities_user_tags() | |
| 85 | + ) | |
| 86 | + ) ); | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Action execution function | |
| 92 | + * | |
| 93 | + * @since 1.0.0 | |
| 94 | + * | |
| 95 | + * @param stdClass $action The action object | |
| 96 | + * @param int $user_id The user ID | |
| 97 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 98 | + * @param stdClass $automation The action's automation object | |
| 99 | + */ | |
| 100 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 101 | + | |
| 102 | + // Shorthand | |
| 103 | + $operation = $action_options['operation']; | |
| 104 | + $role = $action_options['role']; | |
| 105 | + $user_id_target = absint( $action_options['user_id'] ); | |
| 106 | + $this->user_data = array(); | |
| 107 | + | |
| 108 | + if( $user_id_target === 0 ) { | |
| 109 | + $user_id_target = $user_id; | |
| 110 | + } | |
| 111 | + | |
| 112 | + $user = get_userdata( $user_id_target ); | |
| 113 | + | |
| 114 | + $this->user_id = $user_id_target; | |
| 115 | + | |
| 116 | + // The user fields | |
| 117 | + $user_fields = array( | |
| 118 | + 'user_login', | |
| 119 | + 'user_email', | |
| 120 | + 'first_name', | |
| 121 | + 'last_name', | |
| 122 | + 'user_url', | |
| 123 | + 'user_pass', | |
| 124 | + 'display_name', | |
| 125 | + ); | |
| 126 | + | |
| 127 | + foreach( $user_fields as $user_field ) { | |
| 128 | + $this->user_data[$user_field] = $user->$user_field; | |
| 129 | + } | |
| 130 | + | |
| 131 | + // Bail if user does not exists | |
| 132 | + if( ! $user ) { | |
| 133 | + return; | |
| 134 | + } | |
| 135 | + | |
| 136 | + // Ensure operation default value | |
| 137 | + if( empty( $operation ) ) { | |
| 138 | + $operation = 'add'; | |
| 139 | + } | |
| 140 | + | |
| 141 | + // Bail if empty role to assign | |
| 142 | + if( $role === 'any' || empty( $role ) ) { | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 146 | + $roles = automatorwp_get_editable_roles(); | |
| 147 | + | |
| 148 | + // Bail if empty role to assign | |
| 149 | + if( ! isset( $roles[$role] ) ) { | |
| 150 | + return; | |
| 151 | + } | |
| 152 | + | |
| 153 | + switch ( $operation ) { | |
| 154 | + case 'add': | |
| 155 | + // Add the role to the user | |
| 156 | + $user->add_role( $role ); | |
| 157 | + break; | |
| 158 | + case 'change': | |
| 159 | + // Set the role to the user | |
| 160 | + $user->set_role( $role ); | |
| 161 | + break; | |
| 162 | + case 'remove': | |
| 163 | + // Bail if user hasn't this role | |
| 164 | + if( ! in_array( $role, $user->roles ) ) { | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 168 | + // Don't remove any role if is the last role | |
| 169 | + if( count( $user->roles ) === 1 ) { | |
| 170 | + return; | |
| 171 | + } | |
| 172 | + | |
| 173 | + // Remove the role to the user | |
| 174 | + $user->remove_role( $role ); | |
| 175 | + break; | |
| 176 | + } | |
| 177 | + | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Register required hooks | |
| 182 | + * | |
| 183 | + * @since 1.0.0 | |
| 184 | + */ | |
| 185 | + public function hooks() { | |
| 186 | + | |
| 187 | + // Log meta data | |
| 188 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 189 | + | |
| 190 | + // Log fields | |
| 191 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 192 | + | |
| 193 | + parent::hooks(); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Action custom log meta | |
| 198 | + * | |
| 199 | + * @since 1.0.0 | |
| 200 | + * | |
| 201 | + * @param array $log_meta Log meta data | |
| 202 | + * @param stdClass $action The action object | |
| 203 | + * @param int $user_id The user ID | |
| 204 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 205 | + * @param stdClass $automation The action's automation object | |
| 206 | + * | |
| 207 | + * @return array | |
| 208 | + */ | |
| 209 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 210 | + | |
| 211 | + // Bail if action type don't match this action | |
| 212 | + if( $action->type !== $this->action ) | |
| 213 | + return $log_meta; | |
| 214 | + | |
| 215 | + // Store user fields | |
| 216 | + $user_fields = array( | |
| 217 | + 'user_login', | |
| 218 | + 'user_email', | |
| 219 | + 'first_name', | |
| 220 | + 'last_name', | |
| 221 | + 'user_url', | |
| 222 | + 'user_pass', | |
| 223 | + 'display_name', | |
| 224 | + ); | |
| 225 | + | |
| 226 | + foreach( $user_fields as $user_field ) { | |
| 227 | + $log_meta[$user_field] = $this->user_data[$user_field]; | |
| 228 | + } | |
| 229 | + | |
| 230 | + // Store user ID | |
| 231 | + $log_meta['user_id'] = $this->user_id; | |
| 232 | + | |
| 233 | + return $log_meta; | |
| 234 | + } | |
| 235 | + | |
| 236 | + /** | |
| 237 | + * Action custom log fields | |
| 238 | + * | |
| 239 | + * @since 1.0.0 | |
| 240 | + * | |
| 241 | + * @param array $log_fields The log fields | |
| 242 | + * @param stdClass $log The log object | |
| 243 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 244 | + * | |
| 245 | + * @return array | |
| 246 | + */ | |
| 247 | + public function log_fields( $log_fields, $log, $object ) { | |
| 248 | + | |
| 249 | + // Bail if log is not assigned to this action | |
| 250 | + if( $log->type !== 'action' || $object->type !== $this->action ) | |
| 251 | + return $log_fields; | |
| 252 | + | |
| 253 | + $log_fields['user_id_meta'] = array( | |
| 254 | + 'name' => __( 'User:', 'automatorwp' ), | |
| 255 | + 'desc' => __( 'The user the action runs for.', 'automatorwp' ), | |
| 256 | + 'type' => 'text', | |
| 257 | + ); | |
| 258 | + | |
| 259 | + return $log_fields; | |
| 260 | + } | |
| 261 | + | |
| 262 | +} | |
| 263 | + | |
| 264 | +new AutomatorWP_WordPress_User_Role( 'wordpress' ); | |
| 265 | +new AutomatorWP_WordPress_User_Role( 'users' ); | |