class-wp-otp-user-meta.php 4.56 KB
Newer Older
noplanman's avatar
noplanman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/**
 * User metadata management
 *
 * @package    Wp_Otp
 * @subpackage User_Meta
 * @since      0.1.0
 */

namespace Wp_Otp;

/**
 * Manage all the user metadata.
 *
 * @since 0.1.0
 */
class Wp_Otp_User_Meta {
	/**
19 20 21
	 * Instance of this class.
	 *
	 * @since 0.1.0
noplanman's avatar
noplanman committed
22
	 * @var Wp_Otp_User_Meta|null
noplanman's avatar
noplanman committed
23
	 */
noplanman's avatar
noplanman committed
24
	private static ?Wp_Otp_User_Meta $instance = null;
noplanman's avatar
noplanman committed
25 26

	/**
27 28 29 30
	 * Meta key to save the data in the user options.
	 *
	 * @since 0.1.0
	 * @var string
noplanman's avatar
noplanman committed
31
	 */
noplanman's avatar
noplanman committed
32
	private static string $user_meta_key = WP_OTP_SLUG;
noplanman's avatar
noplanman committed
33 34

	/**
35 36 37 38
	 * All default user meta.
	 *
	 * @since 0.1.0
	 * @var array
noplanman's avatar
noplanman committed
39
	 */
noplanman's avatar
noplanman committed
40
	private static array $default_user_meta = [
noplanman's avatar
noplanman committed
41 42 43 44 45 46 47 48 49 50 51
		'counter' => null,
		'digest'  => 'sha1',
		'digits'  => 6,
		'enabled' => false,
		'method'  => 'totp',
		'notices' => null,
		'period'  => 30,
		'secret'  => null,
	];

	/**
52 53 54 55
	 * All user meta.
	 *
	 * @since 0.1.0
	 * @var array
noplanman's avatar
noplanman committed
56
	 */
noplanman's avatar
noplanman committed
57
	private static array $user_meta = [];
noplanman's avatar
noplanman committed
58

59 60 61 62 63 64
	/**
	 * User ID of the user whose meta data is managed.
	 *
	 * @since 0.1.0
	 * @var int
	 */
noplanman's avatar
noplanman committed
65
	private static int $user_id = 0;
noplanman's avatar
noplanman committed
66

67 68
	/**
	 * Preload the user metadata on initialisation.
69 70
	 *
	 * @since 0.1.0
71 72
	 */
	private function __construct() {
73
		$this->fetch();
noplanman's avatar
noplanman committed
74 75 76 77 78
	}

	/**
	 * Create / Get the instance of this class.
	 *
79 80 81
	 * @todo  : This class needs updating, so that data isn't static for the first called user_id.
	 *
	 * @since 0.1.0
noplanman's avatar
noplanman committed
82 83 84 85 86
	 *
	 * @param int $user_id ID of the user to load the meta data for.
	 *
	 * @return Wp_Otp_User_Meta Instance of this class.
	 */
87
	public static function get_instance( $user_id = 0 ): Wp_Otp_User_Meta {
noplanman's avatar
noplanman committed
88
		if ( null === self::$instance ) {
89
			self::$user_id  = $user_id ?: get_current_user_id();
90
			self::$instance = new self();
noplanman's avatar
noplanman committed
91 92 93 94 95 96
		}

		return self::$instance;
	}

	/**
97
	 * Fetch the saved user meta data, filling in with the default values.
noplanman's avatar
noplanman committed
98
	 *
99 100
	 * @since 0.1.0
	 *
101
	 * @return Wp_Otp_User_Meta Instance of this class.
noplanman's avatar
noplanman committed
102
	 */
103
	private function fetch(): Wp_Otp_User_Meta {
104
		if ( 0 === count( self::$user_meta ) ) {
noplanman's avatar
noplanman committed
105 106 107 108 109
			self::$user_meta = wp_parse_args(
				get_user_meta( self::$user_id, self::$user_meta_key, true ),
				self::$default_user_meta
			);
		}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

		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 ) {
noplanman's avatar
noplanman committed
125 126 127 128
		if ( null !== $key ) {
			if ( isset( self::$user_meta[ $key ] ) ) {
				// Return found option value.
				return self::$user_meta[ $key ];
129 130 131
			}

			if ( null !== $default ) {
noplanman's avatar
noplanman committed
132 133
				// Return overridden default value.
				return $default;
134 135 136
			}

			if ( isset( self::$default_user_meta[ $key ] ) ) {
noplanman's avatar
noplanman committed
137 138 139 140
				// Return default option value.
				return self::$default_user_meta[ $key ];
			}
		}
141 142

		return $default;
noplanman's avatar
noplanman committed
143 144 145 146 147
	}

	/**
	 * Get all user meta.
	 *
148 149
	 * @since 0.1.0
	 *
noplanman's avatar
noplanman committed
150 151
	 * @return array All the user meta.
	 */
152
	public function get_all(): array {
noplanman's avatar
noplanman committed
153 154 155 156 157 158
		return self::$user_meta;
	}

	/**
	 * Set a certain user meta.
	 *
159 160 161 162 163
	 * @since 0.1.0
	 *
	 * @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.
164 165
	 *
	 * @return Wp_Otp_User_Meta Instance of this class.
noplanman's avatar
noplanman committed
166
	 */
noplanman's avatar
noplanman committed
167
	public function set( string $key, $value, bool $save = false ): Wp_Otp_User_Meta {
noplanman's avatar
noplanman committed
168 169 170 171 172 173 174 175 176
		if ( null !== $key ) {
			if ( null !== $value ) {
				self::$user_meta[ $key ] = $value;
			} else {
				unset( self::$user_meta[ $key ] );
			}
		}

		$save && $this->save();
177 178

		return $this;
noplanman's avatar
noplanman committed
179 180 181 182 183
	}

	/**
	 * Set multiple user metas.
	 *
184 185
	 * @since 0.1.0
	 *
noplanman's avatar
noplanman committed
186 187
	 * @param array $metas Key-Value pairs of user meta to set.
	 * @param bool  $save  Save the user meta immediately after setting them.
188 189
	 *
	 * @return Wp_Otp_User_Meta Instance of this class.
noplanman's avatar
noplanman committed
190
	 */
noplanman's avatar
noplanman committed
191
	public function set_all( array $metas, bool $save = false ): Wp_Otp_User_Meta {
noplanman's avatar
noplanman committed
192
		foreach ( $metas as $key => $value ) {
193
			$this->set( $key, $value );
noplanman's avatar
noplanman committed
194 195 196
		}

		$save && $this->save();
197 198

		return $this;
noplanman's avatar
noplanman committed
199 200 201 202
	}

	/**
	 * Save the user meta.
203 204
	 *
	 * @since 0.1.0
205 206
	 *
	 * @return Wp_Otp_User_Meta Instance of this class.
noplanman's avatar
noplanman committed
207
	 */
208
	public function save(): Wp_Otp_User_Meta {
noplanman's avatar
noplanman committed
209
		update_user_meta( self::$user_id, self::$user_meta_key, self::$user_meta );
210 211

		return $this;
noplanman's avatar
noplanman committed
212 213 214
	}

	/**
215
	 * Clear the user meta.
216 217
	 *
	 * @since 0.1.0
218 219
	 *
	 * @return Wp_Otp_User_Meta Instance of this class.
noplanman's avatar
noplanman committed
220
	 */
221
	public static function clear(): Wp_Otp_User_Meta {
222 223 224 225
		$user_id = self::$user_id ?: get_current_user_id();
		if ( delete_user_meta( $user_id, self::$user_meta_key ) ) {
			// Reset instance.
			self::$instance = null;
226
		}
227 228

		return self::get_instance( $user_id );
noplanman's avatar
noplanman committed
229 230
	}
}