wp/wp-includes/class-wp-application-passwords.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    41 	const PW_LENGTH = 24;
    41 	const PW_LENGTH = 24;
    42 
    42 
    43 	/**
    43 	/**
    44 	 * Checks if Application Passwords are being used by the site.
    44 	 * Checks if Application Passwords are being used by the site.
    45 	 *
    45 	 *
    46 	 * This returns true if at least one App Password has ever been created.
    46 	 * This returns true if at least one Application Password has ever been created.
    47 	 *
    47 	 *
    48 	 * @since 5.6.0
    48 	 * @since 5.6.0
    49 	 *
    49 	 *
    50 	 * @return bool
    50 	 * @return bool
    51 	 */
    51 	 */
    59 	 *
    59 	 *
    60 	 * @since 5.6.0
    60 	 * @since 5.6.0
    61 	 * @since 5.7.0 Returns WP_Error if application name already exists.
    61 	 * @since 5.7.0 Returns WP_Error if application name already exists.
    62 	 *
    62 	 *
    63 	 * @param int   $user_id  User ID.
    63 	 * @param int   $user_id  User ID.
    64 	 * @param array $args     Information about the application password.
    64 	 * @param array $args     {
       
    65 	 *     Arguments used to create the application password.
       
    66 	 *
       
    67 	 *     @type string $name   The name of the application password.
       
    68 	 *     @type string $app_id A UUID provided by the application to uniquely identify it.
       
    69 	 * }
    65 	 * @return array|WP_Error The first key in the array is the new password, the second is its detailed information.
    70 	 * @return array|WP_Error The first key in the array is the new password, the second is its detailed information.
    66 	 *                        A WP_Error instance is returned on error.
    71 	 *                        A WP_Error instance is returned on error.
    67 	 */
    72 	 */
    68 	public static function create_new_application_password( $user_id, $args = array() ) {
    73 	public static function create_new_application_password( $user_id, $args = array() ) {
    69 		if ( ! empty( $args['name'] ) ) {
    74 		if ( ! empty( $args['name'] ) ) {
   108 		 * Fires when an application password is created.
   113 		 * Fires when an application password is created.
   109 		 *
   114 		 *
   110 		 * @since 5.6.0
   115 		 * @since 5.6.0
   111 		 *
   116 		 *
   112 		 * @param int    $user_id      The user ID.
   117 		 * @param int    $user_id      The user ID.
   113 		 * @param array  $new_item     The details about the created password.
   118 		 * @param array  $new_item     {
   114 		 * @param string $new_password The unhashed generated app password.
   119 		 *     The details about the created password.
   115 		 * @param array  $args         Information used to create the application password.
   120 		 *
       
   121 		 *     @type string $uuid      The unique identifier for the application password.
       
   122 		 *     @type string $app_id    A UUID provided by the application to uniquely identify it.
       
   123 		 *     @type string $name      The name of the application password.
       
   124 		 *     @type string $password  A one-way hash of the password.
       
   125 		 *     @type int    $created   Unix timestamp of when the password was created.
       
   126 		 *     @type null   $last_used Null.
       
   127 		 *     @type null   $last_ip   Null.
       
   128 		 * }
       
   129 		 * @param string $new_password The unhashed generated application password.
       
   130 		 * @param array  $args         {
       
   131 		 *     Arguments used to create the application password.
       
   132 		 *
       
   133 		 *     @type string $name   The name of the application password.
       
   134 		 *     @type string $app_id A UUID provided by the application to uniquely identify it.
       
   135 		 * }
   116 		 */
   136 		 */
   117 		do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args );
   137 		do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args );
   118 
   138 
   119 		return array( $new_password, $new_item );
   139 		return array( $new_password, $new_item );
   120 	}
   140 	}
   123 	 * Gets a user's application passwords.
   143 	 * Gets a user's application passwords.
   124 	 *
   144 	 *
   125 	 * @since 5.6.0
   145 	 * @since 5.6.0
   126 	 *
   146 	 *
   127 	 * @param int $user_id User ID.
   147 	 * @param int $user_id User ID.
   128 	 * @return array The list of app passwords.
   148 	 * @return array {
       
   149 	 *     The list of app passwords.
       
   150 	 *
       
   151 	 *     @type array ...$0 {
       
   152 	 *         @type string      $uuid      The unique identifier for the application password.
       
   153 	 *         @type string      $app_id    A UUID provided by the application to uniquely identify it.
       
   154 	 *         @type string      $name      The name of the application password.
       
   155 	 *         @type string      $password  A one-way hash of the password.
       
   156 	 *         @type int         $created   Unix timestamp of when the password was created.
       
   157 	 *         @type int|null    $last_used The Unix timestamp of the GMT date the application password was last used.
       
   158 	 *         @type string|null $last_ip   The IP address the application password was last used by.
       
   159 	 *     }
       
   160 	 * }
   129 	 */
   161 	 */
   130 	public static function get_user_application_passwords( $user_id ) {
   162 	public static function get_user_application_passwords( $user_id ) {
   131 		$passwords = get_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, true );
   163 		$passwords = get_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, true );
   132 
   164 
   133 		if ( ! is_array( $passwords ) ) {
   165 		if ( ! is_array( $passwords ) ) {
   149 
   181 
   150 		return $passwords;
   182 		return $passwords;
   151 	}
   183 	}
   152 
   184 
   153 	/**
   185 	/**
   154 	 * Gets a user's application password with the given uuid.
   186 	 * Gets a user's application password with the given UUID.
   155 	 *
   187 	 *
   156 	 * @since 5.6.0
   188 	 * @since 5.6.0
   157 	 *
   189 	 *
   158 	 * @param int    $user_id User ID.
   190 	 * @param int    $user_id User ID.
   159 	 * @param string $uuid    The password's uuid.
   191 	 * @param string $uuid    The password's UUID.
   160 	 * @return array|null The application password if found, null otherwise.
   192 	 * @return array|null The application password if found, null otherwise.
   161 	 */
   193 	 */
   162 	public static function get_user_application_password( $user_id, $uuid ) {
   194 	public static function get_user_application_password( $user_id, $uuid ) {
   163 		$passwords = static::get_user_application_passwords( $user_id );
   195 		$passwords = static::get_user_application_passwords( $user_id );
   164 
   196 
   170 
   202 
   171 		return null;
   203 		return null;
   172 	}
   204 	}
   173 
   205 
   174 	/**
   206 	/**
   175 	 * Checks if application name exists for this user.
   207 	 * Checks if an application password with the given name exists for this user.
   176 	 *
   208 	 *
   177 	 * @since 5.7.0
   209 	 * @since 5.7.0
   178 	 *
   210 	 *
   179 	 * @param int    $user_id User ID.
   211 	 * @param int    $user_id User ID.
   180 	 * @param string $name    Application name.
   212 	 * @param string $name    Application name.
   181 	 * @return bool Whether provided application name exists or not.
   213 	 * @return bool Whether the provided application name exists.
   182 	 */
   214 	 */
   183 	public static function application_name_exists_for_user( $user_id, $name ) {
   215 	public static function application_name_exists_for_user( $user_id, $name ) {
   184 		$passwords = static::get_user_application_passwords( $user_id );
   216 		$passwords = static::get_user_application_passwords( $user_id );
   185 
   217 
   186 		foreach ( $passwords as $password ) {
   218 		foreach ( $passwords as $password ) {
   196 	 * Updates an application password.
   228 	 * Updates an application password.
   197 	 *
   229 	 *
   198 	 * @since 5.6.0
   230 	 * @since 5.6.0
   199 	 *
   231 	 *
   200 	 * @param int    $user_id User ID.
   232 	 * @param int    $user_id User ID.
   201 	 * @param string $uuid    The password's uuid.
   233 	 * @param string $uuid    The password's UUID.
   202 	 * @param array  $update  Information about the application password to update.
   234 	 * @param array  $update  Information about the application password to update.
   203 	 * @return true|WP_Error True if successful, otherwise a WP_Error instance is returned on error.
   235 	 * @return true|WP_Error True if successful, otherwise a WP_Error instance is returned on error.
   204 	 */
   236 	 */
   205 	public static function update_application_password( $user_id, $uuid, $update = array() ) {
   237 	public static function update_application_password( $user_id, $uuid, $update = array() ) {
   206 		$passwords = static::get_user_application_passwords( $user_id );
   238 		$passwords = static::get_user_application_passwords( $user_id );
   250 	 * Records that an application password has been used.
   282 	 * Records that an application password has been used.
   251 	 *
   283 	 *
   252 	 * @since 5.6.0
   284 	 * @since 5.6.0
   253 	 *
   285 	 *
   254 	 * @param int    $user_id User ID.
   286 	 * @param int    $user_id User ID.
   255 	 * @param string $uuid    The password's uuid.
   287 	 * @param string $uuid    The password's UUID.
   256 	 * @return true|WP_Error True if the usage was recorded, a WP_Error if an error occurs.
   288 	 * @return true|WP_Error True if the usage was recorded, a WP_Error if an error occurs.
   257 	 */
   289 	 */
   258 	public static function record_application_password_usage( $user_id, $uuid ) {
   290 	public static function record_application_password_usage( $user_id, $uuid ) {
   259 		$passwords = static::get_user_application_passwords( $user_id );
   291 		$passwords = static::get_user_application_passwords( $user_id );
   260 
   292 
   288 	 * Deletes an application password.
   320 	 * Deletes an application password.
   289 	 *
   321 	 *
   290 	 * @since 5.6.0
   322 	 * @since 5.6.0
   291 	 *
   323 	 *
   292 	 * @param int    $user_id User ID.
   324 	 * @param int    $user_id User ID.
   293 	 * @param string $uuid    The password's uuid.
   325 	 * @param string $uuid    The password's UUID.
   294 	 * @return true|WP_Error Whether the password was successfully found and deleted, a WP_Error otherwise.
   326 	 * @return true|WP_Error Whether the password was successfully found and deleted, a WP_Error otherwise.
   295 	 */
   327 	 */
   296 	public static function delete_application_password( $user_id, $uuid ) {
   328 	public static function delete_application_password( $user_id, $uuid ) {
   297 		$passwords = static::get_user_application_passwords( $user_id );
   329 		$passwords = static::get_user_application_passwords( $user_id );
   298 
   330 
   350 
   382 
   351 		return 0;
   383 		return 0;
   352 	}
   384 	}
   353 
   385 
   354 	/**
   386 	/**
   355 	 * Sets a users application passwords.
   387 	 * Sets a user's application passwords.
   356 	 *
   388 	 *
   357 	 * @since 5.6.0
   389 	 * @since 5.6.0
   358 	 *
   390 	 *
   359 	 * @param int   $user_id   User ID.
   391 	 * @param int   $user_id   User ID.
   360 	 * @param array $passwords Application passwords.
   392 	 * @param array $passwords Application passwords.