Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: class-wp-application-passwords.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WP_Application_Passwords class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @since 5.6.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Class for displaying, modifying, and sanitizing application passwords.
[9] Fix | Delete
*
[10] Fix | Delete
* @package WordPress
[11] Fix | Delete
*/
[12] Fix | Delete
#[AllowDynamicProperties]
[13] Fix | Delete
class WP_Application_Passwords {
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* The application passwords user meta key.
[17] Fix | Delete
*
[18] Fix | Delete
* @since 5.6.0
[19] Fix | Delete
*
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
const USERMETA_KEY_APPLICATION_PASSWORDS = '_application_passwords';
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* The option name used to store whether application passwords are in use.
[26] Fix | Delete
*
[27] Fix | Delete
* @since 5.6.0
[28] Fix | Delete
*
[29] Fix | Delete
* @var string
[30] Fix | Delete
*/
[31] Fix | Delete
const OPTION_KEY_IN_USE = 'using_application_passwords';
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* The generated application password length.
[35] Fix | Delete
*
[36] Fix | Delete
* @since 5.6.0
[37] Fix | Delete
*
[38] Fix | Delete
* @var int
[39] Fix | Delete
*/
[40] Fix | Delete
const PW_LENGTH = 24;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Checks if application passwords are being used by the site.
[44] Fix | Delete
*
[45] Fix | Delete
* This returns true if at least one application password has ever been created.
[46] Fix | Delete
*
[47] Fix | Delete
* @since 5.6.0
[48] Fix | Delete
*
[49] Fix | Delete
* @return bool
[50] Fix | Delete
*/
[51] Fix | Delete
public static function is_in_use() {
[52] Fix | Delete
$network_id = get_main_network_id();
[53] Fix | Delete
return (bool) get_network_option( $network_id, self::OPTION_KEY_IN_USE );
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
/**
[57] Fix | Delete
* Creates a new application password.
[58] Fix | Delete
*
[59] Fix | Delete
* @since 5.6.0
[60] Fix | Delete
* @since 5.7.0 Returns WP_Error if application name already exists.
[61] Fix | Delete
*
[62] Fix | Delete
* @param int $user_id User ID.
[63] Fix | Delete
* @param array $args {
[64] Fix | Delete
* Arguments used to create the application password.
[65] Fix | Delete
*
[66] Fix | Delete
* @type string $name The name of the application password.
[67] Fix | Delete
* @type string $app_id A UUID provided by the application to uniquely identify it.
[68] Fix | Delete
* }
[69] Fix | Delete
* @return array|WP_Error {
[70] Fix | Delete
* Application password details, or a WP_Error instance if an error occurs.
[71] Fix | Delete
*
[72] Fix | Delete
* @type string $0 The unhashed generated application password.
[73] Fix | Delete
* @type array $1 {
[74] Fix | Delete
* The details about the created password.
[75] Fix | Delete
*
[76] Fix | Delete
* @type string $uuid The unique identifier for the application password.
[77] Fix | Delete
* @type string $app_id A UUID provided by the application to uniquely identify it.
[78] Fix | Delete
* @type string $name The name of the application password.
[79] Fix | Delete
* @type string $password A one-way hash of the password.
[80] Fix | Delete
* @type int $created Unix timestamp of when the password was created.
[81] Fix | Delete
* @type null $last_used Null.
[82] Fix | Delete
* @type null $last_ip Null.
[83] Fix | Delete
* }
[84] Fix | Delete
* }
[85] Fix | Delete
*/
[86] Fix | Delete
public static function create_new_application_password( $user_id, $args = array() ) {
[87] Fix | Delete
if ( ! empty( $args['name'] ) ) {
[88] Fix | Delete
$args['name'] = sanitize_text_field( $args['name'] );
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
if ( empty( $args['name'] ) ) {
[92] Fix | Delete
return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ), array( 'status' => 400 ) );
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
if ( self::application_name_exists_for_user( $user_id, $args['name'] ) ) {
[96] Fix | Delete
return new WP_Error( 'application_password_duplicate_name', __( 'Each application name should be unique.' ), array( 'status' => 409 ) );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
$new_password = wp_generate_password( static::PW_LENGTH, false );
[100] Fix | Delete
$hashed_password = wp_hash_password( $new_password );
[101] Fix | Delete
[102] Fix | Delete
$new_item = array(
[103] Fix | Delete
'uuid' => wp_generate_uuid4(),
[104] Fix | Delete
'app_id' => empty( $args['app_id'] ) ? '' : $args['app_id'],
[105] Fix | Delete
'name' => $args['name'],
[106] Fix | Delete
'password' => $hashed_password,
[107] Fix | Delete
'created' => time(),
[108] Fix | Delete
'last_used' => null,
[109] Fix | Delete
'last_ip' => null,
[110] Fix | Delete
);
[111] Fix | Delete
[112] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[113] Fix | Delete
$passwords[] = $new_item;
[114] Fix | Delete
$saved = static::set_user_application_passwords( $user_id, $passwords );
[115] Fix | Delete
[116] Fix | Delete
if ( ! $saved ) {
[117] Fix | Delete
return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
$network_id = get_main_network_id();
[121] Fix | Delete
if ( ! get_network_option( $network_id, self::OPTION_KEY_IN_USE ) ) {
[122] Fix | Delete
update_network_option( $network_id, self::OPTION_KEY_IN_USE, true );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Fires when an application password is created.
[127] Fix | Delete
*
[128] Fix | Delete
* @since 5.6.0
[129] Fix | Delete
*
[130] Fix | Delete
* @param int $user_id The user ID.
[131] Fix | Delete
* @param array $new_item {
[132] Fix | Delete
* The details about the created password.
[133] Fix | Delete
*
[134] Fix | Delete
* @type string $uuid The unique identifier for the application password.
[135] Fix | Delete
* @type string $app_id A UUID provided by the application to uniquely identify it.
[136] Fix | Delete
* @type string $name The name of the application password.
[137] Fix | Delete
* @type string $password A one-way hash of the password.
[138] Fix | Delete
* @type int $created Unix timestamp of when the password was created.
[139] Fix | Delete
* @type null $last_used Null.
[140] Fix | Delete
* @type null $last_ip Null.
[141] Fix | Delete
* }
[142] Fix | Delete
* @param string $new_password The unhashed generated application password.
[143] Fix | Delete
* @param array $args {
[144] Fix | Delete
* Arguments used to create the application password.
[145] Fix | Delete
*
[146] Fix | Delete
* @type string $name The name of the application password.
[147] Fix | Delete
* @type string $app_id A UUID provided by the application to uniquely identify it.
[148] Fix | Delete
* }
[149] Fix | Delete
*/
[150] Fix | Delete
do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args );
[151] Fix | Delete
[152] Fix | Delete
return array( $new_password, $new_item );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* Gets a user's application passwords.
[157] Fix | Delete
*
[158] Fix | Delete
* @since 5.6.0
[159] Fix | Delete
*
[160] Fix | Delete
* @param int $user_id User ID.
[161] Fix | Delete
* @return array {
[162] Fix | Delete
* The list of app passwords.
[163] Fix | Delete
*
[164] Fix | Delete
* @type array ...$0 {
[165] Fix | Delete
* @type string $uuid The unique identifier for the application password.
[166] Fix | Delete
* @type string $app_id A UUID provided by the application to uniquely identify it.
[167] Fix | Delete
* @type string $name The name of the application password.
[168] Fix | Delete
* @type string $password A one-way hash of the password.
[169] Fix | Delete
* @type int $created Unix timestamp of when the password was created.
[170] Fix | Delete
* @type int|null $last_used The Unix timestamp of the GMT date the application password was last used.
[171] Fix | Delete
* @type string|null $last_ip The IP address the application password was last used by.
[172] Fix | Delete
* }
[173] Fix | Delete
* }
[174] Fix | Delete
*/
[175] Fix | Delete
public static function get_user_application_passwords( $user_id ) {
[176] Fix | Delete
$passwords = get_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, true );
[177] Fix | Delete
[178] Fix | Delete
if ( ! is_array( $passwords ) ) {
[179] Fix | Delete
return array();
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
$save = false;
[183] Fix | Delete
[184] Fix | Delete
foreach ( $passwords as $i => $password ) {
[185] Fix | Delete
if ( ! isset( $password['uuid'] ) ) {
[186] Fix | Delete
$passwords[ $i ]['uuid'] = wp_generate_uuid4();
[187] Fix | Delete
$save = true;
[188] Fix | Delete
}
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
if ( $save ) {
[192] Fix | Delete
static::set_user_application_passwords( $user_id, $passwords );
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
return $passwords;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Gets a user's application password with the given UUID.
[200] Fix | Delete
*
[201] Fix | Delete
* @since 5.6.0
[202] Fix | Delete
*
[203] Fix | Delete
* @param int $user_id User ID.
[204] Fix | Delete
* @param string $uuid The password's UUID.
[205] Fix | Delete
* @return array|null The application password if found, null otherwise.
[206] Fix | Delete
*/
[207] Fix | Delete
public static function get_user_application_password( $user_id, $uuid ) {
[208] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[209] Fix | Delete
[210] Fix | Delete
foreach ( $passwords as $password ) {
[211] Fix | Delete
if ( $password['uuid'] === $uuid ) {
[212] Fix | Delete
return $password;
[213] Fix | Delete
}
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
return null;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Checks if an application password with the given name exists for this user.
[221] Fix | Delete
*
[222] Fix | Delete
* @since 5.7.0
[223] Fix | Delete
*
[224] Fix | Delete
* @param int $user_id User ID.
[225] Fix | Delete
* @param string $name Application name.
[226] Fix | Delete
* @return bool Whether the provided application name exists.
[227] Fix | Delete
*/
[228] Fix | Delete
public static function application_name_exists_for_user( $user_id, $name ) {
[229] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[230] Fix | Delete
[231] Fix | Delete
foreach ( $passwords as $password ) {
[232] Fix | Delete
if ( strtolower( $password['name'] ) === strtolower( $name ) ) {
[233] Fix | Delete
return true;
[234] Fix | Delete
}
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
return false;
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
/**
[241] Fix | Delete
* Updates an application password.
[242] Fix | Delete
*
[243] Fix | Delete
* @since 5.6.0
[244] Fix | Delete
*
[245] Fix | Delete
* @param int $user_id User ID.
[246] Fix | Delete
* @param string $uuid The password's UUID.
[247] Fix | Delete
* @param array $update Information about the application password to update.
[248] Fix | Delete
* @return true|WP_Error True if successful, otherwise a WP_Error instance is returned on error.
[249] Fix | Delete
*/
[250] Fix | Delete
public static function update_application_password( $user_id, $uuid, $update = array() ) {
[251] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[252] Fix | Delete
[253] Fix | Delete
foreach ( $passwords as &$item ) {
[254] Fix | Delete
if ( $item['uuid'] !== $uuid ) {
[255] Fix | Delete
continue;
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
if ( ! empty( $update['name'] ) ) {
[259] Fix | Delete
$update['name'] = sanitize_text_field( $update['name'] );
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$save = false;
[263] Fix | Delete
[264] Fix | Delete
if ( ! empty( $update['name'] ) && $item['name'] !== $update['name'] ) {
[265] Fix | Delete
$item['name'] = $update['name'];
[266] Fix | Delete
$save = true;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
if ( $save ) {
[270] Fix | Delete
$saved = static::set_user_application_passwords( $user_id, $passwords );
[271] Fix | Delete
[272] Fix | Delete
if ( ! $saved ) {
[273] Fix | Delete
return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
/**
[278] Fix | Delete
* Fires when an application password is updated.
[279] Fix | Delete
*
[280] Fix | Delete
* @since 5.6.0
[281] Fix | Delete
*
[282] Fix | Delete
* @param int $user_id The user ID.
[283] Fix | Delete
* @param array $item The updated app password details.
[284] Fix | Delete
* @param array $update The information to update.
[285] Fix | Delete
*/
[286] Fix | Delete
do_action( 'wp_update_application_password', $user_id, $item, $update );
[287] Fix | Delete
[288] Fix | Delete
return true;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* Records that an application password has been used.
[296] Fix | Delete
*
[297] Fix | Delete
* @since 5.6.0
[298] Fix | Delete
*
[299] Fix | Delete
* @param int $user_id User ID.
[300] Fix | Delete
* @param string $uuid The password's UUID.
[301] Fix | Delete
* @return true|WP_Error True if the usage was recorded, a WP_Error if an error occurs.
[302] Fix | Delete
*/
[303] Fix | Delete
public static function record_application_password_usage( $user_id, $uuid ) {
[304] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[305] Fix | Delete
[306] Fix | Delete
foreach ( $passwords as &$password ) {
[307] Fix | Delete
if ( $password['uuid'] !== $uuid ) {
[308] Fix | Delete
continue;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
// Only record activity once a day.
[312] Fix | Delete
if ( $password['last_used'] + DAY_IN_SECONDS > time() ) {
[313] Fix | Delete
return true;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
$password['last_used'] = time();
[317] Fix | Delete
$password['last_ip'] = $_SERVER['REMOTE_ADDR'];
[318] Fix | Delete
[319] Fix | Delete
$saved = static::set_user_application_passwords( $user_id, $passwords );
[320] Fix | Delete
[321] Fix | Delete
if ( ! $saved ) {
[322] Fix | Delete
return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
return true;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
// Specified application password not found!
[329] Fix | Delete
return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* Deletes an application password.
[334] Fix | Delete
*
[335] Fix | Delete
* @since 5.6.0
[336] Fix | Delete
*
[337] Fix | Delete
* @param int $user_id User ID.
[338] Fix | Delete
* @param string $uuid The password's UUID.
[339] Fix | Delete
* @return true|WP_Error Whether the password was successfully found and deleted, a WP_Error otherwise.
[340] Fix | Delete
*/
[341] Fix | Delete
public static function delete_application_password( $user_id, $uuid ) {
[342] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[343] Fix | Delete
[344] Fix | Delete
foreach ( $passwords as $key => $item ) {
[345] Fix | Delete
if ( $item['uuid'] === $uuid ) {
[346] Fix | Delete
unset( $passwords[ $key ] );
[347] Fix | Delete
$saved = static::set_user_application_passwords( $user_id, $passwords );
[348] Fix | Delete
[349] Fix | Delete
if ( ! $saved ) {
[350] Fix | Delete
return new WP_Error( 'db_error', __( 'Could not delete application password.' ) );
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Fires when an application password is deleted.
[355] Fix | Delete
*
[356] Fix | Delete
* @since 5.6.0
[357] Fix | Delete
*
[358] Fix | Delete
* @param int $user_id The user ID.
[359] Fix | Delete
* @param array $item The data about the application password.
[360] Fix | Delete
*/
[361] Fix | Delete
do_action( 'wp_delete_application_password', $user_id, $item );
[362] Fix | Delete
[363] Fix | Delete
return true;
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
/**
[371] Fix | Delete
* Deletes all application passwords for the given user.
[372] Fix | Delete
*
[373] Fix | Delete
* @since 5.6.0
[374] Fix | Delete
*
[375] Fix | Delete
* @param int $user_id User ID.
[376] Fix | Delete
* @return int|WP_Error The number of passwords that were deleted or a WP_Error on failure.
[377] Fix | Delete
*/
[378] Fix | Delete
public static function delete_all_application_passwords( $user_id ) {
[379] Fix | Delete
$passwords = static::get_user_application_passwords( $user_id );
[380] Fix | Delete
[381] Fix | Delete
if ( $passwords ) {
[382] Fix | Delete
$saved = static::set_user_application_passwords( $user_id, array() );
[383] Fix | Delete
[384] Fix | Delete
if ( ! $saved ) {
[385] Fix | Delete
return new WP_Error( 'db_error', __( 'Could not delete application passwords.' ) );
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
foreach ( $passwords as $item ) {
[389] Fix | Delete
/** This action is documented in wp-includes/class-wp-application-passwords.php */
[390] Fix | Delete
do_action( 'wp_delete_application_password', $user_id, $item );
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
return count( $passwords );
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
return 0;
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
/**
[400] Fix | Delete
* Sets a user's application passwords.
[401] Fix | Delete
*
[402] Fix | Delete
* @since 5.6.0
[403] Fix | Delete
*
[404] Fix | Delete
* @param int $user_id User ID.
[405] Fix | Delete
* @param array $passwords Application passwords.
[406] Fix | Delete
*
[407] Fix | Delete
* @return bool
[408] Fix | Delete
*/
[409] Fix | Delete
protected static function set_user_application_passwords( $user_id, $passwords ) {
[410] Fix | Delete
return update_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, $passwords );
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Sanitizes and then splits a password into smaller chunks.
[415] Fix | Delete
*
[416] Fix | Delete
* @since 5.6.0
[417] Fix | Delete
*
[418] Fix | Delete
* @param string $raw_password The raw application password.
[419] Fix | Delete
* @return string The chunked password.
[420] Fix | Delete
*/
[421] Fix | Delete
public static function chunk_password( $raw_password ) {
[422] Fix | Delete
$raw_password = preg_replace( '/[^a-z\d]/i', '', $raw_password );
[423] Fix | Delete
[424] Fix | Delete
return trim( chunk_split( $raw_password, 4, ' ' ) );
[425] Fix | Delete
}
[426] Fix | Delete
}
[427] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function