Verified Commit 1b7b5413 authored by noplanman's avatar noplanman
Browse files

Remove WPINC checks from class files.

Fix bug that prevented notices from showing.
Perform fixes/suggestions by Scrutinizer.
parent 5cc2efa9
......@@ -12,8 +12,6 @@ namespace Wp_Otp;
use OTPHP\TOTP;
use WP_User;
defined( 'WPINC' ) || exit;
/**
* The admin-specific functionality of the plugin.
*
......@@ -92,9 +90,9 @@ class Wp_Otp_Admin {
* @since 0.1.0
*/
public function admin_init() {
if ( array_key_exists( 'wp-otp-delete', $_GET ) ) {
if ( isset( $_GET['wp-otp-delete'] ) && 'yes' === $_GET['wp-otp-delete'] ) {
Wp_Otp_User_Meta::delete();
wp_redirect( get_edit_profile_url() );
wp_redirect( get_edit_profile_url() . '#wp_otp' );
exit;
}
}
......@@ -175,7 +173,7 @@ class Wp_Otp_Admin {
* @return void
*/
public function show_user_notification( array $messages, $type = 'notice' ) {
if ( ! empty( $messages ) ) {
if ( empty( $messages ) ) {
return;
}
......
......@@ -55,7 +55,7 @@
printf(
'%1$s <a href="%2$s" onclick = "return confirm(\'%3$s\')">%4$s</a>',
__( 'WP-OTP is already configured.', 'wp-otp' ),
get_edit_profile_url() . '?wp-otp-delete',
add_query_arg( 'wp-otp-delete', 'yes' ),
'Are you sure you want to reconfigure WP-OTP?',
__( 'Reconfigure?', 'wp-otp' )
);
......
......@@ -12,8 +12,6 @@
namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* Define the internationalisation functionality.
*
......
......@@ -9,8 +9,6 @@
namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* Register all actions and filters for the plugin.
*
......
......@@ -9,8 +9,6 @@
namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* Handle all activation, deactivation and uninstallation tasks.
*
......
......@@ -9,8 +9,6 @@
namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* Manage all the user metadata.
*
......@@ -18,17 +16,26 @@ defined( 'WPINC' ) || exit;
*/
class Wp_Otp_User_Meta {
/**
* @var Wp_Otp_User_Meta Instance of this class.
* Instance of this class.
*
* @since 0.1.0
* @var Wp_Otp_User_Meta
*/
private static $instance;
/**
* @var string Meta key to save the data in the user options.
* Meta key to save the data in the user options.
*
* @since 0.1.0
* @var string
*/
private static $user_meta_key = WP_OTP_SLUG;
/**
* @var array All default user meta.
* All default user meta.
*
* @since 0.1.0
* @var array
*/
private static $default_user_meta = [
'counter' => null,
......@@ -42,51 +49,60 @@ class Wp_Otp_User_Meta {
];
/**
* @var array All user meta.
* All user meta.
*
* @since 0.1.0
* @var array
*/
private static $user_meta;
/**
* User ID of the user whose meta data is managed.
*
* @since 0.1.0
* @var int
*/
private static $user_id = 0;
private static $user_id;
/** Singleton, keep private. */
final private function __clone() {
}
/** Singleton, keep private. */
final private function __construct( $user_id ) {
self::$user_id = $user_id;
/**
* Preload the user metadata on initialisation.
*/
private function __construct() {
$this->get_user_meta();
}
/**
* Create / Get the instance of this class.
*
* @todo: This class needs updating, so that data isn't static for the first called user_id.
* @todo : This class needs updating, so that data isn't static for the first called user_id.
*
* @since 0.1.0
*
* @param int $user_id ID of the user to load the meta data for.
*
* @return Wp_Otp_User_Meta Instance of this class.
*/
public static function get_instance( $user_id = null ) {
public static function get_instance( $user_id = 0 ) {
if ( null === self::$instance ) {
self::$instance = new self( $user_id ?: get_current_user_id() );
self::$user_id = $user_id ?: get_current_user_id();
self::$instance = new self;
}
return self::$instance;
}
/**
* Get a specific option.
*
* @param string $key ID of user meta to get.
* @param array|string $default Override default value if option not found.
* @since 0.1.0
*
* @param string $key ID of user meta to get.
* @param mixed $default Override default value if option not found.
*
* @return array|string Requested option value.
* @return mixed Requested option value.
*/
public function get_user_meta( $key = null, $default = null ) {
if ( null === self::$user_meta ) {
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
......@@ -109,6 +125,8 @@ class Wp_Otp_User_Meta {
/**
* Get all user meta.
*
* @since 0.1.0
*
* @return array All the user meta.
*/
public function get_all_user_meta() {
......@@ -118,9 +136,11 @@ class Wp_Otp_User_Meta {
/**
* Set a certain user meta.
*
* @param string $key ID of option to get.
* @param array|string $value Value to be set for the passed option.
* @param bool $save Save the user meta immediately after setting them.
* @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.
*/
public function set_user_meta( $key, $value, $save = false ) {
if ( null !== $key ) {
......@@ -137,6 +157,8 @@ class Wp_Otp_User_Meta {
/**
* Set multiple user metas.
*
* @since 0.1.0
*
* @param array $metas Key-Value pairs of user meta to set.
* @param bool $save Save the user meta immediately after setting them.
*/
......@@ -150,6 +172,8 @@ class Wp_Otp_User_Meta {
/**
* Save the user meta.
*
* @since 0.1.0
*/
public function save() {
update_user_meta( self::$user_id, self::$user_meta_key, self::$user_meta );
......@@ -157,12 +181,15 @@ class Wp_Otp_User_Meta {
/**
* Save the user meta.
*
* @since 0.1.0
*/
public static function delete() {
delete_user_meta( self::$user_id, self::$user_meta_key );
self::$user_meta = null;
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();
// Reset defaults.
self::get_instance()->get_user_meta();
}
}
}
......@@ -8,8 +8,6 @@
namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* The core plugin class.
*
......@@ -49,7 +47,7 @@ class Wp_Otp {
*/
private function load_dependencies() {
/**
* The class responsible for activateion, deactivation and deletion of the plugin.
* The class responsible for activation, deactivation and deletion of the plugin.
*/
require_once __DIR__ . '/class-wp-otp-setup.php';
......
......@@ -15,8 +15,6 @@ use OTPHP\TOTP;
use WP_Error;
use WP_User;
defined( 'WPINC' ) || exit;
/**
* The public-facing functionality of the plugin.
*
......
......@@ -13,9 +13,7 @@ namespace Wp_Otp;
defined( 'WPINC' ) || exit;
/**
* Define constants.
*/
// Define constants.
define( 'WP_OTP_SLUG', 'wp-otp' );
define( 'WP_OTP_VERSION', '0.1.0' );
......
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