add-new-media.php
10 months ago
add-new-role.php
10 months ago
add-tag-to-post.php
9 months ago
add-taxonomy-to-post.php
10 months ago
change-role.php
10 months ago
create-category.php
10 months ago
create-comment.php
10 months ago
create-post.php
8 months ago
create-role.php
10 months ago
create-tag.php
10 months ago
create-user-if-not-exists.php
1 year ago
delete-user.php
10 months ago
find-posts.php
4 months ago
find-user-by-email.php
2 years ago
find-user-by-id.php
2 years ago
find-user-meta-by-key.php
2 years ago
find-user-metas.php
2 years ago
get-post-by-id.php
10 months ago
get-post-metadata.php
10 months ago
get-post-taxonomy.php
10 months ago
get-post-terms.php
10 months ago
get-taxonomy-by-name.php
10 months ago
get-user-by-role.php
10 months ago
remove-role.php
10 months ago
remove-user-meta.php
10 months ago
remove-user.php
10 months ago
send-mail.php
4 months ago
set-post-meta.php
10 months ago
set-user-meta.php
10 months ago
update-comment-status.php
10 months ago
update-post-excerpt.php
10 months ago
update-post.php
10 months ago
update-user.php
4 months ago
create-user-if-not-exists.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateUserIfNotExists. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateUserIfNotExists |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\Wordpress\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use Exception; |
| 19 | |
| 20 | /** |
| 21 | * CreateUserIfNotExists |
| 22 | * |
| 23 | * @category CreateUserIfNotExists |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class CreateUserIfNotExists extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'WordPress'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'create_user_if_not_exists'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register action. |
| 50 | * |
| 51 | * @param array $actions action data. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'User: Create new user if not exists with Email', 'suretriggers' ), |
| 57 | 'action' => 'create_user_if_not_exists', |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 60 | |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @return array|object|bool |
| 72 | * @throws Exception Exception. |
| 73 | */ |
| 74 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 75 | $email = sanitize_email( $selected_options['user_email'] ); |
| 76 | $user_name = sanitize_text_field( $selected_options['user_name'] ); |
| 77 | |
| 78 | /** |
| 79 | * User Data |
| 80 | */ |
| 81 | |
| 82 | $user_pass = empty( $selected_options['password'] ) ? wp_generate_password() : $selected_options['password']; |
| 83 | |
| 84 | $userdata = [ |
| 85 | 'user_login' => $selected_options['user_name'], |
| 86 | 'user_email' => $email, |
| 87 | 'first_name' => $selected_options['first_name'], |
| 88 | 'last_name' => $selected_options['last_name'], |
| 89 | 'user_pass' => $user_pass, |
| 90 | 'role' => $selected_options['role'], |
| 91 | ]; |
| 92 | $force_update_user_role = $selected_options['force_update_user_role'] ? 'yes' : 'no'; |
| 93 | |
| 94 | $add_user_roles = $selected_options['add_user_roles'] ? 'yes' : 'no'; |
| 95 | $show_password = $selected_options['show_password']; |
| 96 | |
| 97 | $user = get_user_by( 'email', $email ); |
| 98 | if ( ! $user ) { |
| 99 | $user = get_user_by( 'login', $user_name ); |
| 100 | } |
| 101 | |
| 102 | if ( $user ) { |
| 103 | $user_id = $user->ID; |
| 104 | $userdata['ID'] = $user_id; |
| 105 | |
| 106 | $current_roles = $user->roles; |
| 107 | |
| 108 | $specified_role = $userdata['role']; |
| 109 | $specified_excluded_roles = []; |
| 110 | if ( ! empty( $selected_options['exclude_role'] ) ) { |
| 111 | $specified_excluded_roles = array_column( $selected_options['exclude_role'], 'value' ); |
| 112 | } |
| 113 | $common_roles = array_values( array_intersect( $specified_excluded_roles, $current_roles ) ); |
| 114 | /** |
| 115 | * Skipping if empty value. |
| 116 | */ |
| 117 | if ( empty( $userdata['user_login'] ) ) { |
| 118 | unset( $userdata['user_login'] ); |
| 119 | } |
| 120 | if ( empty( $userdata['first_name'] ) ) { |
| 121 | unset( $userdata['first_name'] ); |
| 122 | } |
| 123 | if ( empty( $userdata['last_name'] ) ) { |
| 124 | unset( $userdata['last_name'] ); |
| 125 | } |
| 126 | if ( empty( $selected_options['password'] ) ) { |
| 127 | unset( $userdata['user_pass'] ); |
| 128 | } |
| 129 | if ( empty( $userdata['role'] ) ) { |
| 130 | unset( $userdata['role'] ); |
| 131 | } else { |
| 132 | if ( ! empty( $common_roles ) ) { |
| 133 | unset( $userdata['role'] ); |
| 134 | } else { |
| 135 | if ( in_array( 'administrator', $current_roles ) ) { |
| 136 | if ( 'yes' != $force_update_user_role ) { |
| 137 | unset( $userdata['role'] ); |
| 138 | } |
| 139 | } else { |
| 140 | if ( 'yes' == $add_user_roles && 'yes' != $force_update_user_role ) { |
| 141 | $user->add_role( $selected_options['role'] ); |
| 142 | unset( $userdata['role'] ); |
| 143 | } else { |
| 144 | $user_role = $user->roles[0]; |
| 145 | |
| 146 | /** |
| 147 | * |
| 148 | * Ignore line |
| 149 | * |
| 150 | * @phpstan-ignore-next-line |
| 151 | */ |
| 152 | $user_role_capabilities = get_role( $user_role )->capabilities; |
| 153 | |
| 154 | /** |
| 155 | * |
| 156 | * Ignore line |
| 157 | * |
| 158 | * @phpstan-ignore-next-line |
| 159 | */ |
| 160 | $specified_role_capabilities = get_role( $specified_role )->capabilities; |
| 161 | |
| 162 | // Check if the user's role has all the capabilities of the specified role. |
| 163 | $has_all_capabilities = true; |
| 164 | foreach ( $specified_role_capabilities as $capability => $value ) { |
| 165 | if ( ! isset( $user_role_capabilities[ $capability ] ) || $user_role_capabilities[ $capability ] !== $value ) { |
| 166 | $has_all_capabilities = false; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if ( $has_all_capabilities ) { |
| 172 | // User's role has all the capabilities of the specified role so don't update it unless it is force update. |
| 173 | if ( 'yes' != $force_update_user_role ) { |
| 174 | unset( $userdata['role'] ); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | wp_update_user( wp_slash( $userdata ) ); |
| 182 | } else { |
| 183 | $user_id = wp_insert_user( wp_slash( $userdata ) ); |
| 184 | } |
| 185 | |
| 186 | if ( ! is_wp_error( $user_id ) ) { |
| 187 | if ( isset( $selected_options['user_meta'] ) && is_array( $selected_options['user_meta'] ) && count( $selected_options['user_meta'] ) ) { |
| 188 | foreach ( $selected_options['user_meta'] as $meta ) { |
| 189 | $meta_value = $meta['metaValue']; |
| 190 | $is_meta_json = json_decode( $meta_value, true ); |
| 191 | if ( null !== $is_meta_json ) { |
| 192 | $meta_value = $is_meta_json; |
| 193 | } |
| 194 | update_user_meta( $user_id, $meta['metaKey'], $meta_value ); |
| 195 | } |
| 196 | } |
| 197 | } else { |
| 198 | return $user_id->errors; |
| 199 | } |
| 200 | |
| 201 | if ( $user_id ) { |
| 202 | $user = get_userdata( $user_id ); |
| 203 | |
| 204 | $user_arr = (array) $user; |
| 205 | if ( isset( $user_arr['data'] ) ) { |
| 206 | if ( empty( $user_arr['data']->user_pass ) ) { |
| 207 | $user_arr['data']->user_pass = __( 'Unable to show the password while updating the user', 'suretriggers' ); |
| 208 | } else { |
| 209 | if ( 'yes' === $show_password ) { |
| 210 | $user_arr['data']->user_pass = $user_pass; |
| 211 | } else { |
| 212 | unset( $user_arr['data']->user_pass ); |
| 213 | } |
| 214 | } |
| 215 | return $user_arr; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return [ 'error' => 'No user has been created or updated' ]; |
| 220 | |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | CreateUserIfNotExists::get_instance(); |
| 225 |