Verified Commit 4f230bcb authored by noplanman's avatar noplanman
Browse files

Rename Wp_Otp_User_Meta functions to make them simpler.

Allow method-chaining for setters.
parent a85fb4a8
......@@ -38,21 +38,21 @@ class Wp_Otp_Admin {
$otp = new TOTP(
$user->user_login,
$user_meta_data->get_user_meta( 'secret' )
$user_meta_data->get( 'secret' )
);
$secret = $otp->getSecret();
$user_meta_data->set_user_meta( 'secret', $secret );
$user_meta_data->set( 'secret', $secret );
$otp_code = trim( $_POST['wp_otp_code'] );
if ( $otp_code && ! $user_meta_data->get_user_meta( 'enabled', false ) ) {
if ( $otp_code && ! $user_meta_data->get( 'enabled', false ) ) {
$otp_window = (int) apply_filters( 'wp_otp_code_expiration_window', 2 );
$verification = $otp->verify( $otp_code, null, $otp_window );
if ( $verification ) {
$otp_recovery = bin2hex( random_bytes( 8 ) );
$user_meta_data->set_user_metas( [
$user_meta_data->set_all( [
'enabled' => true,
'recovery' => $otp_recovery,
'notice' => [
......@@ -66,8 +66,8 @@ class Wp_Otp_Admin {
],
] );
} else {
Wp_Otp_User_Meta::delete();
$user_meta_data->set_user_metas( [
Wp_Otp_User_Meta::clear();
$user_meta_data->set_all( [
'secret' => $secret,
'notice' => [
'type' => 'error',
......@@ -90,7 +90,7 @@ class Wp_Otp_Admin {
*/
public function admin_init() {
if ( isset( $_GET['wp-otp-delete'] ) && 'yes' === $_GET['wp-otp-delete'] ) {
Wp_Otp_User_Meta::delete();
Wp_Otp_User_Meta::clear();
wp_redirect( get_edit_profile_url() . '#wp_otp' );
exit;
}
......@@ -107,7 +107,7 @@ class Wp_Otp_Admin {
$user_meta_data = Wp_Otp_User_Meta::get_instance();
// Get the secret.
$secret = $user_meta_data->get_user_meta( 'secret' );
$secret = $user_meta_data->get( 'secret' );
$otp = new TOTP( $user->user_login, $secret );
// Issuer isn't allowed to have any semicolon.
......@@ -116,7 +116,7 @@ class Wp_Otp_Admin {
// Check if the secret was loaded from the meta or not.
if ( null === $secret ) {
$secret = $otp->getSecret();
$user_meta_data->set_user_meta( 'secret', $secret, true );
$user_meta_data->set( 'secret', $secret, true );
}
/**
......@@ -133,7 +133,7 @@ class Wp_Otp_Admin {
'https://api.qrserver.com/v1/create-qr-code/?data={PROVISIONING_URI}&qzone=2&size=300x300'
) );
$otp_enabled = $user_meta_data->get_user_meta( 'enabled', false );
$otp_enabled = $user_meta_data->get( 'enabled', false );
$otp_apps = [
[
......@@ -201,7 +201,7 @@ class Wp_Otp_Admin {
public function admin_notices() {
$user_meta_data = Wp_Otp_User_Meta::get_instance();
if ( ! $user_meta_data->get_user_meta( 'enabled', false ) ) {
if ( ! $user_meta_data->get( 'enabled', false ) ) {
$this->show_user_notification( [
__( '<strong>Note:</strong> You have not yet configured WP-OTP.', 'wp-otp' ),
sprintf(
......@@ -210,7 +210,7 @@ class Wp_Otp_Admin {
_x( 'Configure now', 'Link text to go to WP-OTP section in user profile', 'wp-otp' )
),
] );
} elseif ( null === $user_meta_data->get_user_meta( 'recovery' ) ) {
} elseif ( null === $user_meta_data->get( 'recovery' ) ) {
$this->show_user_notification( [
__( '<strong>Important:</strong> You have used your WP-OTP recovery hash. You must generate a new one.',
'wp-otp' ),
......@@ -222,14 +222,14 @@ class Wp_Otp_Admin {
], 'error' );
}
if ( $notice = $user_meta_data->get_user_meta( 'notice' ) ) {
if ( $notice = $user_meta_data->get( 'notice' ) ) {
$this->show_user_notification(
(array) $notice['messages'],
$notice['type']
);
// Remove any notices from the user meta.
$user_meta_data->set_user_meta( 'notice', null, true );
$user_meta_data->set( 'notice', null, true );
}
}
}
......@@ -66,9 +66,11 @@ class Wp_Otp_User_Meta {
/**
* Preload the user metadata on initialisation.
*
* @since 0.1.0
*/
private function __construct() {
$this->get_user_meta();
$this->fetch();
}
/**
......@@ -92,22 +94,34 @@ class Wp_Otp_User_Meta {
}
/**
* Get a specific option.
* Fetch the saved user meta data, filling in with the default values.
*
* @since 0.1.0
*
* @param string $key ID of user meta to get.
* @param mixed $default Override default value if option not found.
*
* @return mixed Requested option value.
* @return Wp_Otp_User_Meta Instance of this class.
*/
public function get_user_meta( $key = null, $default = null ) {
private function fetch() {
if ( 0 === count( self::$user_meta ) ) {
self::$user_meta = wp_parse_args(
get_user_meta( self::$user_id, self::$user_meta_key, true ),
self::$default_user_meta
);
}
return $this;
}
/**
* Get a specific option.
*
* @since 0.1.0
*
* @param string $key ID of user meta to get.
* @param mixed $default Override default value if option not found.
*
* @return mixed Requested option value.
*/
public function get( $key = null, $default = null ) {
if ( null !== $key ) {
if ( isset( self::$user_meta[ $key ] ) ) {
// Return found option value.
......@@ -120,6 +134,8 @@ class Wp_Otp_User_Meta {
return self::$default_user_meta[ $key ];
}
}
return $default;
}
/**
......@@ -129,7 +145,7 @@ class Wp_Otp_User_Meta {
*
* @return array All the user meta.
*/
public function get_all_user_meta() {
public function get_all() {
return self::$user_meta;
}
......@@ -141,8 +157,10 @@ class Wp_Otp_User_Meta {
* @param string $key ID of option to get.
* @param mixed $value Value to be set for the passed option.
* @param bool $save Save the user meta immediately after setting them.
*
* @return Wp_Otp_User_Meta Instance of this class.
*/
public function set_user_meta( $key, $value, $save = false ) {
public function set( $key, $value, $save = false ) {
if ( null !== $key ) {
if ( null !== $value ) {
self::$user_meta[ $key ] = $value;
......@@ -152,6 +170,8 @@ class Wp_Otp_User_Meta {
}
$save && $this->save();
return $this;
}
/**
......@@ -161,35 +181,46 @@ class Wp_Otp_User_Meta {
*
* @param array $metas Key-Value pairs of user meta to set.
* @param bool $save Save the user meta immediately after setting them.
*
* @return Wp_Otp_User_Meta Instance of this class.
*/
public function set_user_metas( $metas, $save = false ) {
public function set_all( $metas, $save = false ) {
foreach ( $metas as $key => $value ) {
$this->set_user_meta( $key, $value );
$this->set( $key, $value );
}
$save && $this->save();
return $this;
}
/**
* Save the user meta.
*
* @since 0.1.0
*
* @return Wp_Otp_User_Meta Instance of this class.
*/
public function save() {
update_user_meta( self::$user_id, self::$user_meta_key, self::$user_meta );
return $this;
}
/**
* Save the user meta.
* Clear the user meta.
*
* @since 0.1.0
*
* @return Wp_Otp_User_Meta Instance of this class.
*/
public static function delete() {
if ( delete_user_meta( self::$user_id ?: get_current_user_id(), self::$user_meta_key ) ) {
self::$user_meta = [];
// Reset defaults.
self::get_instance()->get_user_meta();
public static function clear() {
$user_id = self::$user_id ?: get_current_user_id();
if ( delete_user_meta( $user_id, self::$user_meta_key ) ) {
// Reset instance.
self::$instance = null;
}
return self::get_instance( $user_id );
}
}
......@@ -85,7 +85,7 @@ class Wp_Otp_Public {
$user_meta_data = Wp_Otp_User_Meta::get_instance( $user->ID );
if ( true === $user_meta_data->get_user_meta( 'enabled' ) && null !== $user_meta_data->get_user_meta( 'secret' ) ) {
if ( true === $user_meta_data->get( 'enabled' ) && null !== $user_meta_data->get( 'secret' ) ) {
$otp_code = $_POST['wp_otp_code'];
/**
......@@ -97,12 +97,12 @@ class Wp_Otp_Public {
*/
$otp_window = (int) apply_filters( 'wp_otp_code_expiration_window', 2 );
$otp = new TOTP( '', $user_meta_data->get_user_meta( 'secret' ) );
$otp = new TOTP( '', $user_meta_data->get( 'secret' ) );
$verification = $otp->verify( $otp_code, null, $otp_window );
if ( true !== $verification ) {
if ( $otp_code === $user_meta_data->get_user_meta( 'recovery' ) ) {
$user_meta_data->set_user_meta( 'recovery', null, true );
if ( $otp_code === $user_meta_data->get( 'recovery' ) ) {
$user_meta_data->set( 'recovery', null, true );
} else {
return new WP_Error( 'invalid_otp', $otp_invalid_code_text );
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment