| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoUsersAddPerson' ) && class_exists( 'WpssoAdmin' ) ) { |
| 14 |
|
| 15 |
class WpssoUsersAddPerson extends WpssoAdmin { |
| 16 |
|
| 17 |
private $errors = array(); |
| 18 |
private $messages = array(); |
| 19 |
private $add_errors = array(); |
| 20 |
|
| 21 |
public function __construct( &$plugin, $id, $name, $lib, $ext ) { |
| 22 |
|
| 23 |
$this->p =& $plugin; |
| 24 |
|
| 25 |
if ( $this->p->debug->enabled ) { |
| 26 |
|
| 27 |
$this->p->debug->mark(); |
| 28 |
} |
| 29 |
|
| 30 |
$this->menu_id = $id; |
| 31 |
$this->menu_name = $name; |
| 32 |
$this->menu_lib = $lib; |
| 33 |
$this->menu_ext = $ext; |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
* Add settings page filters and actions hooks. |
| 38 |
* |
| 39 |
* Called by WpssoAdmin->load_settings_page() after the 'wpsso-action' query is handled. |
| 40 |
*/ |
| 41 |
protected function add_settings_page_callbacks() { |
| 42 |
|
| 43 |
if ( $this->p->debug->enabled ) { |
| 44 |
|
| 45 |
$this->p->debug->mark(); |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! current_user_can( 'create_users' ) ) { // Just in case. |
| 49 |
|
| 50 |
// translators: Please ignore - translation uses a different text domain. |
| 51 |
wp_die( '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' . |
| 52 |
// translators: Please ignore - translation uses a different text domain. |
| 53 |
'<p>' . __( 'Sorry, you are not allowed to create users.' ) . '</p>', 403 ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $_REQUEST[ 'action' ] ) && 'createuser' === $_REQUEST[ 'action' ] ) { |
| 57 |
|
| 58 |
check_admin_referer( 'create-user', '_wpnonce_create-user' ); |
| 59 |
|
| 60 |
$user_id = $this->add_person(); |
| 61 |
|
| 62 |
if ( is_wp_error( $user_id ) ) { |
| 63 |
|
| 64 |
$this->add_errors = $user_id; |
| 65 |
|
| 66 |
} else { |
| 67 |
|
| 68 |
$redirect = add_query_arg( 'update', 'add' ); |
| 69 |
|
| 70 |
wp_redirect( $redirect ); |
| 71 |
|
| 72 |
die(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! empty( $_GET[ 'update' ] ) ) { |
| 77 |
|
| 78 |
if ( 'add' === $_GET[ 'update' ] ) { |
| 79 |
|
| 80 |
$this->messages[] = __( 'Person added.', 'wpsso' ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
wp_enqueue_script( 'user-profile' ); |
| 85 |
} |
| 86 |
|
| 87 |
protected function show_post_body_settings_form() { |
| 88 |
|
| 89 |
if ( ! current_user_can( 'create_users' ) ) { // Just in case. |
| 90 |
|
| 91 |
// translators: Please ignore - translation uses a different text domain. |
| 92 |
wp_die( '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' . |
| 93 |
// translators: Please ignore - translation uses a different text domain. |
| 94 |
'<p>' . __( 'Sorry, you are not allowed to create users.' ) . '</p>', 403 ); |
| 95 |
} |
| 96 |
|
| 97 |
$this->show_notices(); |
| 98 |
|
| 99 |
$contact_methods = wp_get_user_contact_methods(); |
| 100 |
|
| 101 |
$editable_roles = array( 'none' => array( 'name' => _x( '[None]', 'option value', 'wpsso' ) ) ) + array_reverse( get_editable_roles() ); |
| 102 |
|
| 103 |
unset( $editable_roles[ 'person' ] ); |
| 104 |
|
| 105 |
$attr = array(); |
| 106 |
|
| 107 |
$have_submit = isset( $_POST[ 'createuser' ] ); |
| 108 |
|
| 109 |
foreach ( array_merge( array( |
| 110 |
'user_login', // Username. |
| 111 |
'first_name', // First name. |
| 112 |
'last_name', // Last name. |
| 113 |
'email', // Email. |
| 114 |
'url', // Website. |
| 115 |
'description', // Biographical info. |
| 116 |
), array_keys( $contact_methods ) ) as $input ) { |
| 117 |
|
| 118 |
$attr[ $input ] = $have_submit && isset( $_POST[ $input ] ) ? wp_unslash( $_POST[ $input ] ) : ''; |
| 119 |
|
| 120 |
$attr[ $input ] = 'description' === $input ? esc_textarea( $attr[ $input ] ) : esc_attr( $attr[ $input ] ); |
| 121 |
} |
| 122 |
|
| 123 |
?> |
| 124 |
|
| 125 |
<div id="add-person-content"> |
| 126 |
|
| 127 |
<p><?php _e( 'Create a new person for use in Open Graph meta tags and Schema markup.', 'wpsso' ); ?></p> |
| 128 |
|
| 129 |
<form method="post" name="createuser" id="createuser" class="validate" novalidate="novalidate"> |
| 130 |
|
| 131 |
<?php wp_nonce_field( 'create-user', '_wpnonce_create-user' ); ?> |
| 132 |
|
| 133 |
<input type="hidden" name="action" value="createuser" /> |
| 134 |
<input type="hidden" name="send_user_notification" value="0" /> |
| 135 |
<input type="hidden" name="pass1" value="" /> |
| 136 |
<input type="hidden" name="pass2" value="" /> |
| 137 |
<input type="hidden" name="send_user_notification" value="0" /> |
| 138 |
|
| 139 |
<table class="form-table" role="presentation"> |
| 140 |
|
| 141 |
<tr class="form-field form-required"> |
| 142 |
|
| 143 |
<th scope="row"><label for="user_login"><?php _e( 'Username' ); ?> |
| 144 |
<span class="description"><?php _e( '(required)' ); ?></span></label></th> |
| 145 |
|
| 146 |
<td> |
| 147 |
<input name="user_login" type="text" id="user_login" value="<?php echo $attr[ 'user_login' ]; ?>" |
| 148 |
aria-required="true" autocapitalize="none" autocorrect="off" maxlength="60" /> |
| 149 |
<p class="description"><?php _e( 'Usernames cannot be changed (without a plugin).', 'wpsso' ); ?></p> |
| 150 |
</td> |
| 151 |
|
| 152 |
</tr> |
| 153 |
|
| 154 |
<tr class="form-field form-required"> |
| 155 |
|
| 156 |
<th scope="row"><label for="first_name"><?php _e( 'First Name' ); ?> |
| 157 |
<span class="description"><?php _e( '(required)' ); ?></span></label></th> |
| 158 |
|
| 159 |
<td><input name="first_name" type="text" id="first_name" value="<?php echo $attr[ 'first_name' ]; ?>" /></td> |
| 160 |
|
| 161 |
</tr> |
| 162 |
|
| 163 |
<tr class="form-field form-required"> |
| 164 |
|
| 165 |
<th scope="row"> |
| 166 |
<label for="last_name"><?php _e( 'Last Name' ); ?> |
| 167 |
<span class="description"><?php _e( '(required)' ); ?></span> |
| 168 |
</label> |
| 169 |
</th> |
| 170 |
|
| 171 |
<td><input name="last_name" type="text" id="last_name" value="<?php echo $attr[ 'last_name' ]; ?>" /></td> |
| 172 |
|
| 173 |
</tr> |
| 174 |
|
| 175 |
</table> |
| 176 |
|
| 177 |
<h2><?php _e( 'Contact Info', 'wpsso' ); ?></h2> |
| 178 |
|
| 179 |
<table class="form-table" role="presentation"> |
| 180 |
|
| 181 |
<tr class="form-field"> |
| 182 |
|
| 183 |
<th scope="row"><label for="email"><?php _e( 'Email' ); ?></label></th> |
| 184 |
|
| 185 |
<td><input name="email" type="email" id="email" value="<?php echo $attr[ 'email' ]; ?>" /></td> |
| 186 |
|
| 187 |
</tr> |
| 188 |
|
| 189 |
<tr class="form-field"> |
| 190 |
|
| 191 |
<th scope="row"><label for="url"><?php _e( 'Website' ); ?></label></th> |
| 192 |
|
| 193 |
<td><input name="url" type="url" id="url" class="code" value="<?php echo $attr[ 'url' ]; ?>" /></td> |
| 194 |
|
| 195 |
</tr> |
| 196 |
|
| 197 |
<?php foreach ( $contact_methods as $name => $desc ) { |
| 198 |
|
| 199 |
echo '<tr class="user-' . $name . '-wrap">'; |
| 200 |
|
| 201 |
echo '<th><label for="' . $name . '">'; |
| 202 |
|
| 203 |
echo apply_filters( 'user_' . $name . '_label', $desc ); |
| 204 |
|
| 205 |
echo '</label></th>'; |
| 206 |
|
| 207 |
echo '<td><input type="text" name="' . $name . '" id="' . $name . '" value="' . $attr[ $name ] . '" class="regular-text" /></td></tr>'; |
| 208 |
|
| 209 |
} ?> |
| 210 |
|
| 211 |
</table> |
| 212 |
|
| 213 |
<h2><?php _e( 'About the person', 'wpsso' ); ?></h2> |
| 214 |
|
| 215 |
<table class="form-table" role="presentation"> |
| 216 |
|
| 217 |
<tr class="user-description-wrap"> |
| 218 |
|
| 219 |
<th><label for="description"><?php _e( 'Biographical Info' ); ?></label></th> |
| 220 |
|
| 221 |
<td><textarea name="description" id="description" rows="5" cols="30"><?php echo $attr[ 'description' ] ?></textarea></td> |
| 222 |
</tr> |
| 223 |
|
| 224 |
<?php $this->p->user->show_about_section(); ?> |
| 225 |
|
| 226 |
</table> |
| 227 |
|
| 228 |
<?php submit_button( __( 'Add the Person', 'wpsso' ), 'primary', 'createuser', true, array( 'id' => 'createusersub' ) ); ?> |
| 229 |
|
| 230 |
</form> |
| 231 |
|
| 232 |
</div><!-- #add-person-content --><?php |
| 233 |
} |
| 234 |
|
| 235 |
private function show_notices() { |
| 236 |
|
| 237 |
if ( ! empty( $this->errors ) && is_wp_error( $this->errors ) ) { |
| 238 |
|
| 239 |
echo '<div class="error">'; |
| 240 |
echo '<ul>'; |
| 241 |
|
| 242 |
foreach ( $this->errors->get_error_messages() as $err ) { |
| 243 |
|
| 244 |
echo '<li>' . $err . '</li>'; |
| 245 |
} |
| 246 |
|
| 247 |
echo '</ul>'; |
| 248 |
echo '</div>'; |
| 249 |
} |
| 250 |
|
| 251 |
if ( ! empty( $this->messages ) ) { |
| 252 |
|
| 253 |
foreach ( $this->messages as $msg ) { |
| 254 |
|
| 255 |
echo '<div id="message" class="updated notice is-dismissible"><p>' . $msg . '</p></div>'; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if ( ! empty( $this->add_errors ) && is_wp_error( $this->add_errors ) ) { |
| 260 |
|
| 261 |
foreach ( $this->add_errors->get_error_messages() as $message ) { |
| 262 |
|
| 263 |
echo '<div class="error">'; |
| 264 |
echo '<p>' . $message . '</p>'; |
| 265 |
echo '</div>'; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
private function add_person() { |
| 271 |
|
| 272 |
if ( ! current_user_can( 'create_users' ) ) { // Just in case. |
| 273 |
|
| 274 |
// translators: Please ignore - translation uses a different text domain. |
| 275 |
wp_die( '<h1>' . __( 'You need a higher level of permission.' ) . '</h1>' . |
| 276 |
// translators: Please ignore - translation uses a different text domain. |
| 277 |
'<p>' . __( 'Sorry, you are not allowed to create users.' ) . '</p>', 403 ); |
| 278 |
} |
| 279 |
|
| 280 |
$contact_methods = wp_get_user_contact_methods(); |
| 281 |
|
| 282 |
$illegal_logins = apply_filters( 'illegal_user_logins', array() ); |
| 283 |
|
| 284 |
/* |
| 285 |
* Create a user object. |
| 286 |
*/ |
| 287 |
$user = new stdClass; |
| 288 |
|
| 289 |
$user->user_login = isset( $_POST[ 'user_login' ] ) ? |
| 290 |
$user->user_login = sanitize_user( wp_unslash( $_POST[ 'user_login' ] ), $strict = true ) : ''; |
| 291 |
|
| 292 |
$user->user_pass = wp_generate_password( $length = 24, $special_chars = true, $extra_special_chars = false ); |
| 293 |
|
| 294 |
$user->first_name = isset( $_POST[ 'first_name' ] ) ? |
| 295 |
$user->first_name = sanitize_text_field( $_POST[ 'first_name' ] ) : ''; |
| 296 |
|
| 297 |
$user->last_name = isset( $_POST[ 'last_name' ] ) ? |
| 298 |
$user->last_name = sanitize_text_field( $_POST[ 'last_name' ] ) : ''; |
| 299 |
|
| 300 |
$user->role = 'person'; |
| 301 |
|
| 302 |
$user->user_email = isset( $_POST[ 'email' ] ) ? |
| 303 |
$user->user_email = sanitize_text_field( wp_unslash( $_POST[ 'email' ] ) ) : ''; |
| 304 |
|
| 305 |
if ( empty( $_POST[ 'url' ] ) || 'http://' === $_POST[ 'url' ] ) { |
| 306 |
|
| 307 |
$user->user_url = ''; |
| 308 |
|
| 309 |
} else { |
| 310 |
|
| 311 |
$protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); |
| 312 |
|
| 313 |
$user->user_url = esc_url_raw( $_POST[ 'url' ] ); |
| 314 |
|
| 315 |
$user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? |
| 316 |
$user->user_url : 'http://' . $user->user_url; |
| 317 |
} |
| 318 |
|
| 319 |
$user->description = isset( $_POST[ 'description' ] ) ? |
| 320 |
$user->description = trim( $_POST[ 'description' ] ) : ''; |
| 321 |
|
| 322 |
foreach ( $contact_methods as $method => $name ) { |
| 323 |
|
| 324 |
if ( isset( $_POST[ $method ] ) ) { |
| 325 |
|
| 326 |
$user->$method = sanitize_text_field( $_POST[ $method ] ); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
$errors = new WP_Error(); |
| 331 |
|
| 332 |
/* |
| 333 |
* Check the user login. |
| 334 |
*/ |
| 335 |
if ( empty( $user->user_login ) ) { |
| 336 |
|
| 337 |
// translators: Please ignore - translation uses a different text domain. |
| 338 |
$errors->add( 'user_login', __( '<strong>Error</strong>: Please enter a username.' ), array( 'form-field' => 'user_login' ) ); |
| 339 |
|
| 340 |
} elseif ( isset( $_POST[ 'user_login' ] ) && ! validate_username( $_POST[ 'user_login' ] ) ) { |
| 341 |
|
| 342 |
// translators: Please ignore - translation uses a different text domain. |
| 343 |
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ), array( 'form-field' => 'user_login' ) ); |
| 344 |
|
| 345 |
} elseif ( username_exists( $user->user_login ) ) { |
| 346 |
|
| 347 |
// translators: Please ignore - translation uses a different text domain. |
| 348 |
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ), array( 'form-field' => 'user_login' ) ); |
| 349 |
|
| 350 |
} elseif ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ), $strict = true ) ) { |
| 351 |
|
| 352 |
// translators: Please ignore - translation uses a different text domain. |
| 353 |
$errors->add( 'invalid_username', __( '<strong>Error</strong>: Sorry, that username is not allowed.' ), array( 'form-field' => 'user_login' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
* Check the email address. |
| 358 |
*/ |
| 359 |
if ( ! empty( $user->user_email ) ) { |
| 360 |
|
| 361 |
if ( ! is_email( $user->user_email ) ) { |
| 362 |
|
| 363 |
// translators: Please ignore - translation uses a different text domain. |
| 364 |
$errors->add( 'invalid_email', __( '<strong>Error</strong>: The email address isn’t correct.' ), |
| 365 |
array( 'form-field' => 'email' ) ); |
| 366 |
|
| 367 |
} elseif ( email_exists( $user->user_email ) ) { |
| 368 |
|
| 369 |
// translators: Please ignore - translation uses a different text domain. |
| 370 |
$errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered, please choose another one.' ), |
| 371 |
array( 'form-field' => 'email' ) ); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if ( $errors->has_errors() ) { |
| 376 |
|
| 377 |
return $errors; |
| 378 |
} |
| 379 |
|
| 380 |
$user_id = wp_insert_user( $user ); |
| 381 |
|
| 382 |
$this->p->user->save_about_section( $user_id ); |
| 383 |
|
| 384 |
return $user_id; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|