Commit 3a80f78c authored by Michael Iseard's avatar Michael Iseard
Browse files

Add ability to create and remove donor secret using action scheduler

parent cfc07771
......@@ -64,6 +64,7 @@ class Kudos_Activator {
city VARCHAR(255),
country VARCHAR(255),
customer_id VARCHAR(255),
secret VARCHAR(255),
PRIMARY KEY (id)
) $charset_collate";
......
......@@ -182,6 +182,7 @@ class Kudos_Donations {
$this->loader->add_action('wp_footer', $plugin_public, 'get_cancel_vars', 1000);
$this->loader->add_action('query_vars', $plugin_public, 'register_vars');
$this->loader->add_action( 'kudos_process_transaction_action', $plugin_public, 'process_transaction', 10, 1 );
$this->loader->add_action( 'kudos_remove_secret_action', $plugin_public, 'remove_donor_secret', 10, 1 );
}
......
......@@ -47,6 +47,10 @@ class Donor extends Entity {
* @var DateTime
*/
public $last_updated;
/**
* @var string
*/
public $secret;
/**
......@@ -69,8 +73,52 @@ class Donor extends Entity {
return $mapper->get_all_by([ 'customer_id' => $this->customer_id]);
}
public function __toString() {
return $this->customer_id;
/**
* Set the donor's secret
*
* @param string $timeout
*
* @return string
* @since 2.0.0
*/
public function create_secret($timeout='+10 minutes') {
$logger = new Logger();
if(class_exists('ActionScheduler')) {
// Remove existing action if exists
as_unschedule_action('kudos_remove_secret_action', [$this->customer_id]);
$timestamp = strtotime($timeout);
// Create new action to remove secret
as_schedule_single_action( $timestamp, 'kudos_remove_secret_action', [$this->customer_id] );
$logger->debug( 'Action "kudos_remove_secret_action" scheduled', [
'datetime' => date_i18n( 'Y-m-d H:i:s', $timestamp )
] );
}
try {
$this->secret = bin2hex(random_bytes(10));
} catch (Throwable $e) {
$logger->error('Unable to create secret for user. ' . $e->getMessage() , ['id' => $this->id]);
}
return $this->secret;
}
/**
* Verify donor's secret
*
* @param string $hash
* @return bool
* @since 2.0.0
*/
public function verify_secret($hash) {
return password_verify($this->secret, $hash);
}
public function clear_secret() {
$this->secret = '';
}
}
\ No newline at end of file
......@@ -469,4 +469,22 @@ class Kudos_Public {
return true;
}
/**
* Remove secret key associated with donor
*
* @param $customer_id
* @return bool|int
* @since 2.0.0
*/
public static function remove_donor_secret($customer_id) {
if($customer_id) {
$mapper = new Mapper(Donor::class);
/** @var Donor $donor */
$donor = $mapper->get_one_by(['customer_id' => $customer_id]);
$donor->clear_secret();
return $mapper->save($donor);
}
return false;
}
}
Supports Markdown
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