| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for getting and updating a donor's consent log. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donor_Consent_Log |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.6.0 |
| 10 |
* @version 1.6.57 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Donor_Consent_Log' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Donor_Consent_Log |
| 22 |
* |
| 23 |
* @since 1.6.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donor_Consent_Log { |
| 26 |
|
| 27 |
/** |
| 28 |
* The donor ID. |
| 29 |
* |
| 30 |
* @since 1.6.0 |
| 31 |
* |
| 32 |
* @var int |
| 33 |
*/ |
| 34 |
private $donor_id; |
| 35 |
|
| 36 |
/** |
| 37 |
* The log. |
| 38 |
* |
| 39 |
* @since 1.6.0 |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
private $log; |
| 44 |
|
| 45 |
/** |
| 46 |
* Create class object. |
| 47 |
* |
| 48 |
* @since 1.6.0 |
| 49 |
* |
| 50 |
* @param int $donor_id The donor ID. |
| 51 |
*/ |
| 52 |
public function __construct( $donor_id ) { |
| 53 |
$this->donor_id = $donor_id; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Add a meta log. |
| 58 |
* |
| 59 |
* @since 1.6.0 |
| 60 |
* |
| 61 |
* @param boolean $consent_given Whether the donor gave their consent. |
| 62 |
* @param string $consent_statement The statement that the donor agreed to. |
| 63 |
* @return int|boolean Meta ID if the key didn't exist, true on successful update, |
| 64 |
* false on failure or if the log has not changed. |
| 65 |
*/ |
| 66 |
public function add( $consent_given, $consent_statement ) { |
| 67 |
$log = $this->get_log(); |
| 68 |
$last_log = end( $log ); |
| 69 |
|
| 70 |
if ( is_array( $last_log ) ) { |
| 71 |
$consent_changed = $last_log['consent_given'] !== $consent_given; |
| 72 |
$statement_changed = $last_log['statement'] !== $consent_statement; |
| 73 |
} else { |
| 74 |
$consent_changed = true; |
| 75 |
$statement_changed = true; |
| 76 |
} |
| 77 |
|
| 78 |
/* Neither the consent nor the statement has changed. */ |
| 79 |
if ( ! $consent_changed && ! $statement_changed ) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
array_push( |
| 84 |
$log, |
| 85 |
array( |
| 86 |
'time' => time(), |
| 87 |
'consent_given' => $consent_given, |
| 88 |
'statement' => $consent_statement, |
| 89 |
) |
| 90 |
); |
| 91 |
|
| 92 |
$ret = update_metadata( 'donor', $this->donor_id, 'consent_log', $log ); |
| 93 |
|
| 94 |
/* Clear the meta_log */ |
| 95 |
unset( |
| 96 |
$this->log |
| 97 |
); |
| 98 |
|
| 99 |
/** |
| 100 |
* Do something when contact consent is changed. |
| 101 |
* |
| 102 |
* @since 1.6.5 |
| 103 |
* |
| 104 |
* @param boolean $consent_changed Whether the actual consent has changed. |
| 105 |
* @param boolean $statement_changed Whether the consent statement changed. |
| 106 |
* @param int $donor_id The donor ID. |
| 107 |
*/ |
| 108 |
do_action( 'charitable_donor_contact_consent_changed', $consent_changed, $statement_changed, $this->donor_id ); |
| 109 |
|
| 110 |
return $ret; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Return the raw meta log. |
| 115 |
* |
| 116 |
* This is stored as donor meta with the `consent_log` meta key. |
| 117 |
* |
| 118 |
* @since 1.6.0 |
| 119 |
* |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
public function get_log() { |
| 123 |
if ( ! isset( $this->log ) ) { |
| 124 |
$this->log = get_metadata( 'donor', $this->donor_id, 'consent_log', true ); |
| 125 |
|
| 126 |
if ( ! is_array( $this->log ) ) { |
| 127 |
$this->log = array(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $this->log; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Return the most recent consent log entry. |
| 136 |
* |
| 137 |
* @since 1.6.5 |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function get_last_log() { |
| 142 |
$log = $this->get_log(); |
| 143 |
|
| 144 |
return end( $log ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
endif; |
| 149 |
|