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