| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress user administration API. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * Creates a new user from the "Users" form using $_POST information. 11 * 12 * @since 2.0.0 13 * 14 * @return int|WP_Error WP_Error or User ID. 15 */ 16 function add_user() { 17 return edit_user(); 18 } 19 20 /** 21 * Edit user settings based on contents of $_POST 22 * 23 * Used on user-edit.php and profile.php to manage and process user options, passwords etc. 24 * 25 * @since 2.0.0 26 * 27 * @param int $user_id Optional. User ID. 28 * @return int|WP_Error User ID of the updated user or WP_Error on failure. 29 */ 30 function edit_user( $user_id = 0 ) { 31 $wp_roles = wp_roles(); 32 $user = new stdClass(); 33 $user_id = (int) $user_id; 34 if ( $user_id ) { 35 $update = true; 36 $user->ID = $user_id; 37 $userdata = get_userdata( $user_id ); 38 $user->user_login = wp_slash( $userdata->user_login ); 39 } else { 40 $update = false; 41 } 42 43 if ( ! $update && isset( $_POST['user_login'] ) ) { 44 $user->user_login = sanitize_user( wp_unslash( $_POST['user_login'] ), true ); 45 } 46 47 $errors = new WP_Error(); 48 49 $pass1 = ''; 50 $pass2 = ''; 51 if ( isset( $_POST['pass1'] ) ) { 52 $pass1 = trim( $_POST['pass1'] ); 53 } 54 if ( isset( $_POST['pass2'] ) ) { 55 $pass2 = trim( $_POST['pass2'] ); 56 } 57 58 if ( isset( $_POST['role'] ) && current_user_can( 'promote_users' ) && ( ! $user_id || current_user_can( 'promote_user', $user_id ) ) ) { 59 $new_role = sanitize_text_field( $_POST['role'] ); 60 61 // If the new role isn't editable by the logged-in user die with error. 62 $editable_roles = get_editable_roles(); 63 if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) { 64 wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 ); 65 } 66 67 $potential_role = $wp_roles->role_objects[ $new_role ] ?? false; 68 69 /* 70 * Don't let anyone with 'promote_users' edit their own role to something without it. 71 * Multisite super admins can freely edit their roles, they possess all caps. 72 */ 73 if ( 74 ( is_multisite() && current_user_can( 'manage_network_users' ) ) || 75 get_current_user_id() !== $user_id || 76 ( $potential_role && $potential_role->has_cap( 'promote_users' ) ) 77 ) { 78 $user->role = $new_role; 79 } 80 } 81 82 if ( isset( $_POST['email'] ) ) { 83 $maybe_email = wp_unslash( $_POST['email'] ); 84 if ( is_string( $maybe_email ) && is_email( $maybe_email ) ) { 85 $user->user_email = $maybe_email; 86 } else { 87 $errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ), array( 'form-field' => 'email' ) ); 88 } 89 } 90 if ( isset( $_POST['url'] ) ) { 91 if ( empty( $_POST['url'] ) || 'http://' === $_POST['url'] ) { 92 $user->user_url = ''; 93 } else { 94 $user->user_url = sanitize_url( $_POST['url'] ); 95 $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); 96 $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url; 97 } 98 } 99 if ( isset( $_POST['first_name'] ) ) { 100 $user->first_name = sanitize_text_field( $_POST['first_name'] ); 101 } 102 if ( isset( $_POST['last_name'] ) ) { 103 $user->last_name = sanitize_text_field( $_POST['last_name'] ); 104 } 105 if ( isset( $_POST['nickname'] ) ) { 106 $user->nickname = sanitize_text_field( $_POST['nickname'] ); 107 } 108 if ( isset( $_POST['display_name'] ) ) { 109 $user->display_name = sanitize_text_field( $_POST['display_name'] ); 110 } 111 112 if ( isset( $_POST['description'] ) ) { 113 $user->description = trim( $_POST['description'] ); 114 } 115 116 foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) { 117 if ( isset( $_POST[ $method ] ) ) { 118 $user->$method = sanitize_text_field( $_POST[ $method ] ); 119 } 120 } 121 122 if ( isset( $_POST['locale'] ) ) { 123 $locale = sanitize_text_field( $_POST['locale'] ); 124 if ( 'site-default' === $locale ) { 125 $locale = ''; 126 } elseif ( '' === $locale ) { 127 $locale = 'en_US'; 128 } elseif ( ! in_array( $locale, get_available_languages(), true ) ) { 129 if ( current_user_can( 'install_languages' ) && wp_can_install_language_pack() ) { 130 if ( ! wp_download_language_pack( $locale ) ) { 131 $locale = ''; 132 } 133 } else { 134 $locale = ''; 135 } 136 } 137 138 $user->locale = $locale; 139 } 140 141 if ( $update ) { 142 $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true'; 143 $user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true'; 144 $user->infinite_scrolling = isset( $_POST['infinite_scrolling'] ) && 'false' === $_POST['infinite_scrolling'] ? 'false' : 'true'; 145 $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'modern'; 146 $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false'; 147 } 148 149 $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' === $_POST['comment_shortcuts'] ? 'true' : ''; 150 151 $user->use_ssl = 0; 152 if ( ! empty( $_POST['use_ssl'] ) ) { 153 $user->use_ssl = 1; 154 } 155 156 /* checking that username has been typed */ 157 if ( '' === $user->user_login ) { 158 $errors->add( 'user_login', __( '<strong>Error:</strong> Please enter a username.' ) ); 159 } 160 161 /* checking that nickname has been typed */ 162 if ( $update && empty( $user->nickname ) ) { 163 $errors->add( 'nickname', __( '<strong>Error:</strong> Please enter a nickname.' ) ); 164 } 165 166 /** 167 * Fires before the password and confirm password fields are checked for congruity. 168 * 169 * @since 1.5.1 170 * 171 * @param string $user_login The username. 172 * @param string $pass1 The password (passed by reference). 173 * @param string $pass2 The confirmed password (passed by reference). 174 */ 175 do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); 176 177 // Check for blank password when adding a user. 178 if ( ! $update && empty( $pass1 ) ) { 179 $errors->add( 'pass', __( '<strong>Error:</strong> Please enter a password.' ), array( 'form-field' => 'pass1' ) ); 180 } 181 182 // Check for "\" in password. 183 if ( str_contains( wp_unslash( $pass1 ), '\\' ) ) { 184 $errors->add( 'pass', __( '<strong>Error:</strong> Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); 185 } 186 187 // Checking the password has been typed twice the same. 188 if ( ( $update || ! empty( $pass1 ) ) && $pass1 !== $pass2 ) { 189 $errors->add( 'pass', __( '<strong>Error:</strong> Passwords do not match. Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) ); 190 } 191 192 if ( ! empty( $pass1 ) ) { 193 $user->user_pass = $pass1; 194 } 195 196 if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) { 197 $errors->add( 'user_login', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); 198 } 199 200 if ( ! $update && username_exists( $user->user_login ) ) { 201 $errors->add( 'user_login', __( '<strong>Error:</strong> This username is already registered. Please choose another one.' ) ); 202 } 203 204 /** This filter is documented in wp-includes/user.php */ 205 $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); 206 207 if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) { 208 $errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) ); 209 } 210 211 // Checking email address. 212 if ( empty( $user->user_email ) ) { 213 $errors->add( 'empty_email', __( '<strong>Error:</strong> Please enter an email address.' ), array( 'form-field' => 'email' ) ); 214 } elseif ( ! is_email( $user->user_email ) ) { 215 $errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ), array( 'form-field' => 'email' ) ); 216 } else { 217 $owner_id = email_exists( $user->user_email ); 218 if ( $owner_id && ( ! $update || ( $owner_id !== $user->ID ) ) ) { 219 $errors->add( 'email_exists', __( '<strong>Error:</strong> This email is already registered. Please choose another one.' ), array( 'form-field' => 'email' ) ); 220 } 221 } 222 223 /** 224 * Fires before user profile update errors are returned. 225 * 226 * @since 2.8.0 227 * 228 * @param WP_Error $errors WP_Error object (passed by reference). 229 * @param bool $update Whether this is a user update. 230 * @param stdClass $user User object (passed by reference). 231 */ 232 do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); 233 234 if ( $errors->has_errors() ) { 235 return $errors; 236 } 237 238 if ( $update ) { 239 $user_id = wp_update_user( $user ); 240 } else { 241 $user_id = wp_insert_user( $user ); 242 $notify = isset( $_POST['send_user_notification'] ) ? 'both' : 'admin'; 243 244 /** 245 * Fires after a new user has been created. 246 * 247 * @since 4.4.0 248 * 249 * @param int|WP_Error $user_id ID of the newly created user or WP_Error on failure. 250 * @param string $notify Type of notification that should happen. See 251 * wp_send_new_user_notifications() for more information. 252 */ 253 do_action( 'edit_user_created_user', $user_id, $notify ); 254 } 255 return $user_id; 256 } 257 258 /** 259 * Fetch a filtered list of user roles that the current user is 260 * allowed to edit. 261 * 262 * Simple function whose main purpose is to allow filtering of the 263 * list of roles in the $wp_roles object so that plugins can remove 264 * inappropriate ones depending on the situation or user making edits. 265 * Specifically because without filtering anyone with the edit_users 266 * capability can edit others to be administrators, even if they are 267 * only editors or authors. This filter allows admins to delegate 268 * user management. 269 * 270 * @since 2.8.0 271 * 272 * @return array[] Array of arrays containing role information. 273 */ 274 function get_editable_roles() { 275 $all_roles = wp_roles()->roles; 276 277 /** 278 * Filters the list of editable roles. 279 * 280 * @since 2.8.0 281 * 282 * @param array[] $all_roles Array of arrays containing role information. 283 */ 284 $editable_roles = apply_filters( 'editable_roles', $all_roles ); 285 286 return $editable_roles; 287 } 288 289 /** 290 * Retrieve user data and filter it. 291 * 292 * @since 2.0.5 293 * 294 * @param int $user_id User ID. 295 * @return WP_User|false WP_User object on success, false on failure. 296 */ 297 function get_user_to_edit( $user_id ) { 298 $user = get_userdata( $user_id ); 299 300 if ( $user ) { 301 $user->filter = 'edit'; 302 } 303 304 return $user; 305 } 306 307 /** 308 * Retrieve the user's drafts. 309 * 310 * @since 2.0.0 311 * 312 * @global wpdb $wpdb WordPress database abstraction object. 313 * 314 * @param int $user_id User ID. 315 * @return object[] The user's draft posts, with 'ID' and 'post_title' keys. 316 */ 317 function get_users_drafts( $user_id ) { 318 global $wpdb; 319 $query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id ); 320 321 /** 322 * Filters the SQL query string for the user's drafts query. 323 * 324 * @since 2.0.0 325 * 326 * @param string $query The user's drafts query string. 327 */ 328 $query = apply_filters( 'get_users_drafts', $query ); 329 return $wpdb->get_results( $query ); 330 } 331 332 /** 333 * Delete user and optionally reassign posts and links to another user. 334 * 335 * Note that on a Multisite installation the user only gets removed from the site 336 * and does not get deleted from the database. 337 * 338 * If the `$reassign` parameter is not assigned to a user ID, then all posts will 339 * be deleted of that user. The action {@see 'delete_user'} that is passed the user ID 340 * being deleted will be run after the posts are either reassigned or deleted. 341 * The user meta will also be deleted that are for that user ID. 342 * 343 * @since 2.0.0 344 * 345 * @global wpdb $wpdb WordPress database abstraction object. 346 * 347 * @param int $id User ID. 348 * @param int $reassign Optional. Reassign posts and links to new User ID. 349 * @return bool True when finished. 350 */ 351 function wp_delete_user( $id, $reassign = null ) { 352 global $wpdb; 353 354 if ( ! is_numeric( $id ) ) { 355 return false; 356 } 357 358 $id = (int) $id; 359 $user = new WP_User( $id ); 360 361 if ( ! $user->exists() ) { 362 return false; 363 } 364 365 // Normalize $reassign to null or a user ID. 'novalue' was an older default. 366 if ( 'novalue' === $reassign ) { 367 $reassign = null; 368 } elseif ( null !== $reassign ) { 369 $reassign = (int) $reassign; 370 } 371 372 /** 373 * Fires immediately before a user is deleted from the site. 374 * 375 * Note that on a Multisite installation the user only gets removed from the site 376 * and does not get deleted from the database. 377 * 378 * @since 2.0.0 379 * @since 5.5.0 Added the `$user` parameter. 380 * 381 * @param int $id ID of the user to delete. 382 * @param int|null $reassign ID of the user to reassign posts and links to. 383 * Default null, for no reassignment. 384 * @param WP_User $user WP_User object of the user to delete. 385 */ 386 do_action( 'delete_user', $id, $reassign, $user ); 387 388 if ( null === $reassign ) { 389 $post_types_to_delete = array(); 390 foreach ( get_post_types( array(), 'objects' ) as $post_type ) { 391 if ( $post_type->delete_with_user ) { 392 $post_types_to_delete[] = $post_type->name; 393 } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) { 394 $post_types_to_delete[] = $post_type->name; 395 } 396 } 397 398 /** 399 * Filters the list of post types to delete with a user. 400 * 401 * @since 3.4.0 402 * 403 * @param string[] $post_types_to_delete Array of post types to delete. 404 * @param int $id User ID. 405 */ 406 $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); 407 $post_types_to_delete = implode( "', '", $post_types_to_delete ); 408 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); 409 if ( $post_ids ) { 410 foreach ( $post_ids as $post_id ) { 411 wp_delete_post( $post_id ); 412 } 413 } 414 415 // Clean links. 416 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 417 418 if ( $link_ids ) { 419 foreach ( $link_ids as $link_id ) { 420 wp_delete_link( $link_id ); 421 } 422 } 423 } else { 424 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 425 $wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) ); 426 if ( ! empty( $post_ids ) ) { 427 foreach ( $post_ids as $post_id ) { 428 clean_post_cache( $post_id ); 429 } 430 } 431 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 432 $wpdb->update( $wpdb->links, array( 'link_owner' => $reassign ), array( 'link_owner' => $id ) ); 433 if ( ! empty( $link_ids ) ) { 434 foreach ( $link_ids as $link_id ) { 435 clean_bookmark_cache( $link_id ); 436 } 437 } 438 } 439 440 // FINALLY, delete user. 441 if ( is_multisite() ) { 442 remove_user_from_blog( $id, get_current_blog_id() ); 443 } else { 444 $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 445 foreach ( $meta as $mid ) { 446 delete_metadata_by_mid( 'user', $mid ); 447 } 448 449 $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); 450 } 451 452 clean_user_cache( $user ); 453 454 /** 455 * Fires immediately after a user is deleted from the site. 456 * 457 * Note that on a Multisite installation the user may not have been deleted from 458 * the database depending on whether `wp_delete_user()` or `wpmu_delete_user()` 459 * was called. 460 * 461 * @since 2.9.0 462 * @since 5.5.0 Added the `$user` parameter. 463 * 464 * @param int $id ID of the deleted user. 465 * @param int|null $reassign ID of the user to reassign posts and links to. 466 * Default null, for no reassignment. 467 * @param WP_User $user WP_User object of the deleted user. 468 */ 469 do_action( 'deleted_user', $id, $reassign, $user ); 470 471 return true; 472 } 473 474 /** 475 * Remove all capabilities from user. 476 * 477 * @since 2.1.0 478 * 479 * @param int $id User ID. 480 */ 481 function wp_revoke_user( $id ) { 482 $id = (int) $id; 483 484 $user = new WP_User( $id ); 485 $user->remove_all_caps(); 486 } 487 488 /** 489 * @since 2.8.0 490 * 491 * @global int $user_ID Current user ID. 492 * 493 * @param false $errors Deprecated. 494 */ 495 function default_password_nag_handler( $errors = false ) { 496 global $user_ID; 497 // Short-circuit it. 498 if ( ! get_user_option( 'default_password_nag' ) ) { 499 return; 500 } 501 502 // get_user_setting() = JS-saved UI setting. Else no-js-fallback code. 503 if ( 'hide' === get_user_setting( 'default_password_nag' ) 504 || isset( $_GET['default_password_nag'] ) && '0' === $_GET['default_password_nag'] 505 ) { 506 delete_user_setting( 'default_password_nag' ); 507 update_user_meta( $user_ID, 'default_password_nag', false ); 508 } 509 } 510 511 /** 512 * @since 2.8.0 513 * 514 * @param int $user_ID User ID. 515 * @param WP_User $old_data The user object before the update. 516 */ 517 function default_password_nag_edit_user( $user_ID, $old_data ) { 518 // Short-circuit it. 519 if ( ! get_user_option( 'default_password_nag', $user_ID ) ) { 520 return; 521 } 522 523 $new_data = get_userdata( $user_ID ); 524 525 // Remove the nag if the password has been changed. 526 if ( $new_data->user_pass !== $old_data->user_pass ) { 527 delete_user_setting( 'default_password_nag' ); 528 update_user_meta( $user_ID, 'default_password_nag', false ); 529 } 530 } 531 532 /** 533 * @since 2.8.0 534 * 535 * @global string $pagenow The filename of the current screen. 536 */ 537 function default_password_nag() { 538 global $pagenow; 539 540 // Short-circuit it. 541 if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) { 542 return; 543 } 544 545 $default_password_nag_message = sprintf( 546 '<p><strong>%1$s</strong> %2$s</p>', 547 __( 'Notice:' ), 548 __( 'You are using the auto-generated password for your account. Would you like to change it?' ) 549 ); 550 $default_password_nag_message .= sprintf( 551 '<p><a href="%1$s">%2$s</a> | ', 552 esc_url( get_edit_profile_url() . '#password' ), 553 __( 'Yes, take me to my profile page' ) 554 ); 555 $default_password_nag_message .= sprintf( 556 '<a href="%1$s" id="default-password-nag-no">%2$s</a></p>', 557 '?default_password_nag=0', 558 __( 'No thanks, do not remind me again' ) 559 ); 560 561 wp_admin_notice( 562 $default_password_nag_message, 563 array( 564 'additional_classes' => array( 'error', 'default-password-nag' ), 565 'paragraph_wrap' => false, 566 ) 567 ); 568 } 569 570 /** 571 * Optional SSL preference that can be turned on by hooking to the 'personal_options' action. 572 * 573 * See the {@see 'personal_options'} action. 574 * 575 * @since 2.7.0 576 * 577 * @param WP_User $user User data object. 578 */ 579 function use_ssl_preference( $user ) { 580 ?> 581 <tr class="user-use-ssl-wrap"> 582 <th scope="row"><?php _e( 'Use https' ); ?></th> 583 <td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked( '1', $user->use_ssl ); ?> /> <?php _e( 'Always use https when visiting the admin' ); ?></label></td> 584 </tr> 585 <?php 586 } 587 588 /** 589 * @since MU (3.0.0) 590 * 591 * @param string $text The email body text. 592 * @return string User site invitation email message. 593 */ 594 function admin_created_user_email( $text ) { 595 $roles = get_editable_roles(); 596 $role = $roles[ $_REQUEST['role'] ]; 597 598 if ( '' !== get_bloginfo( 'name' ) ) { 599 $site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); 600 } else { 601 $site_title = parse_url( home_url(), PHP_URL_HOST ); 602 } 603 604 return sprintf( 605 /* translators: 1: Site title, 2: Site URL, 3: User role. */ 606 __( 607 'Hi, 608 You\'ve been invited to join \'%1$s\' at 609 %2$s with the role of %3$s. 610 If you do not want to join this site please ignore 611 this email. This invitation will expire in a few days. 612 613 Please click the following link to activate your user account: 614 %%s' 615 ), 616 $site_title, 617 home_url(), 618 wp_specialchars_decode( translate_user_role( $role['name'] ) ) 619 ); 620 } 621 622 /** 623 * Checks if the Authorize Application Password request is valid. 624 * 625 * @since 5.6.0 626 * @since 6.2.0 Allow insecure HTTP connections for the local environment. 627 * @since 6.3.2 Validates the success and reject URLs to prevent `javascript` pseudo protocol from being executed. 628 * 629 * @param array $request { 630 * The array of request data. All arguments are optional and may be empty. 631 * 632 * @type string $app_name The suggested name of the application. 633 * @type string $app_id A UUID provided by the application to uniquely identify it. 634 * @type string $success_url The URL the user will be redirected to after approving the application. 635 * @type string $reject_url The URL the user will be redirected to after rejecting the application. 636 * } 637 * @param WP_User $user The user authorizing the application. 638 * @return true|WP_Error True if the request is valid, a WP_Error object contains errors if not. 639 */ 640 function wp_is_authorize_application_password_request_valid( $request, $user ) { 641 $error = new WP_Error(); 642 643 if ( isset( $request['success_url'] ) ) { 644 $validated_success_url = wp_is_authorize_application_redirect_url_valid( $request['success_url'] ); 645 if ( is_wp_error( $validated_success_url ) ) { 646 $error->add( 647 $validated_success_url->get_error_code(), 648 $validated_success_url->get_error_message() 649 ); 650 } 651 } 652 653 if ( isset( $request['reject_url'] ) ) { 654 $validated_reject_url = wp_is_authorize_application_redirect_url_valid( $request['reject_url'] ); 655 if ( is_wp_error( $validated_reject_url ) ) { 656 $error->add( 657 $validated_reject_url->get_error_code(), 658 $validated_reject_url->get_error_message() 659 ); 660 } 661 } 662 663 if ( ! empty( $request['app_id'] ) && ! wp_is_uuid( $request['app_id'] ) ) { 664 $error->add( 665 'invalid_app_id', 666 __( 'The application ID must be a UUID.' ) 667 ); 668 } 669 670 /** 671 * Fires before application password errors are returned. 672 * 673 * @since 5.6.0 674 * 675 * @param WP_Error $error The error object. 676 * @param array $request The array of request data. 677 * @param WP_User $user The user authorizing the application. 678 */ 679 do_action( 'wp_authorize_application_password_request_errors', $error, $request, $user ); 680 681 if ( $error->has_errors() ) { 682 return $error; 683 } 684 685 return true; 686 } 687 688 /** 689 * Validates the redirect URL protocol scheme. 690 * 691 * The `http` scheme is allowed for loopback IP addresses (127.0.0.1, [::1]) 692 * and local environments. The `javascript` and `data` protocols are always rejected. 693 * 694 * @since 6.3.2 695 * 696 * @param string $url The redirect URL to be validated. 697 * @return true|WP_Error True if the redirect URL is valid, a WP_Error object otherwise. 698 */ 699 function wp_is_authorize_application_redirect_url_valid( $url ) { 700 $bad_protocols = array( 'javascript', 'data' ); 701 if ( empty( $url ) ) { 702 return true; 703 } 704 705 // Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1 706 $valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/'; 707 if ( ! preg_match( $valid_scheme_regex, $url ) ) { 708 return new WP_Error( 709 'invalid_redirect_url_format', 710 __( 'Invalid URL format.' ) 711 ); 712 } 713 714 /** 715 * Filters the list of invalid protocols used in applications redirect URLs. 716 * 717 * @since 6.3.2 718 * 719 * @param string[] $bad_protocols Array of invalid protocols. 720 * @param string $url The redirect URL to be validated. 721 */ 722 $invalid_protocols = apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url ); 723 $invalid_protocols = array_map( 'strtolower', $invalid_protocols ); 724 725 $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); 726 $host = wp_parse_url( $url, PHP_URL_HOST ); 727 $is_local = 'local' === wp_get_environment_type(); 728 729 // Validates if the proper URI format is applied to the URL. 730 if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) { 731 return new WP_Error( 732 'invalid_redirect_url_format', 733 __( 'Invalid URL format.' ) 734 ); 735 } 736 737 // Allow insecure HTTP connections to locally hosted applications. 738 $is_loopback = in_array( 739 strtolower( $host ), 740 array( '127.0.0.1', '[::1]' ), 741 true 742 ); 743 744 if ( 'http' === $scheme && ! $is_local && ! $is_loopback ) { 745 return new WP_Error( 746 'invalid_redirect_scheme', 747 __( 'The URL must be served over a secure connection.' ) 748 ); 749 } 750 751 return true; 752 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Aug 14 08:20:23 2026 | Cross-referenced by PHPXref |