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