| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable User Functions. |
| 4 |
* |
| 5 |
* User related functions. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/User |
| 8 |
* @author Eric Daams |
| 9 |
* @copyright Copyright (c) 2022, Studio 164a |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.0.0 |
| 12 |
* @version 1.6.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns a Charitable_User object for the given user. |
| 22 |
* |
| 23 |
* This will first attempt to retrieve it from the object cache to prevent duplicate objects. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* |
| 27 |
* @param int $user_id The ID of the user to retrieve. |
| 28 |
* @param boolean $force Optional. Whether to force an update of the local cache from the persistent. |
| 29 |
* @return Charitable_User |
| 30 |
*/ |
| 31 |
function charitable_get_user( $user_id, $force = false ) { |
| 32 |
if ( is_a( $user_id, 'WP_User' ) ) { |
| 33 |
$user_id = $user_id->ID; |
| 34 |
} |
| 35 |
|
| 36 |
$user = wp_cache_get( $user_id, 'charitable_user', $force ); |
| 37 |
|
| 38 |
if ( ! $user ) { |
| 39 |
$user = new Charitable_User( $user_id ); |
| 40 |
wp_cache_set( $user_id, $user, 'charitable_user' ); |
| 41 |
} |
| 42 |
|
| 43 |
return $user; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns a mapping of user keys. |
| 48 |
* |
| 49 |
* This is needed because the key used in forms is not always the |
| 50 |
* same as they key used for storing the database value. |
| 51 |
* |
| 52 |
* @since 1.4.0 |
| 53 |
* |
| 54 |
* @return string[] |
| 55 |
*/ |
| 56 |
function charitable_get_user_mapped_keys() { |
| 57 |
/** |
| 58 |
* Filter the mapping of shorthand keys to meta keys. |
| 59 |
* |
| 60 |
* The meta keys are used in the wp_usermeta table. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param array $keys User meta keys. |
| 65 |
*/ |
| 66 |
return apply_filters( |
| 67 |
'charitable_donor_mapped_keys', |
| 68 |
array( |
| 69 |
'email' => 'user_email', |
| 70 |
'company' => 'donor_company', |
| 71 |
'address' => 'donor_address', |
| 72 |
'address_2' => 'donor_address_2', |
| 73 |
'city' => 'donor_city', |
| 74 |
'state' => 'donor_state', |
| 75 |
'postcode' => 'donor_postcode', |
| 76 |
'zip' => 'donor_postcode', |
| 77 |
'country' => 'donor_country', |
| 78 |
'phone' => 'donor_phone', |
| 79 |
'user_description' => 'description', |
| 80 |
) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns a list of the core user keys. |
| 86 |
* |
| 87 |
* Core user keys are any keys that can be passed to wp_update_user or wp_insert_user. |
| 88 |
* |
| 89 |
* @see wp_update_user |
| 90 |
* @see wp_insert_user |
| 91 |
* |
| 92 |
* @since 1.4.0 |
| 93 |
* |
| 94 |
* @return string[] |
| 95 |
*/ |
| 96 |
function charitable_get_user_core_keys() { |
| 97 |
return array( |
| 98 |
'ID', |
| 99 |
'user_pass', |
| 100 |
'user_login', |
| 101 |
'user_nicename', |
| 102 |
'user_url', |
| 103 |
'user_email', |
| 104 |
'display_name', |
| 105 |
'nickname', |
| 106 |
'first_name', |
| 107 |
'last_name', |
| 108 |
'rich_editing', |
| 109 |
'date_registered', |
| 110 |
'role', |
| 111 |
'jabber', |
| 112 |
'aim', |
| 113 |
'yim', |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns a list of the donor keys. |
| 119 |
* |
| 120 |
* These keys correspond to the columns in the wp_charitable_donors table. |
| 121 |
* |
| 122 |
* @since 1.6.2 |
| 123 |
* |
| 124 |
* @return string[] |
| 125 |
*/ |
| 126 |
function charitable_get_donor_keys() { |
| 127 |
return array( |
| 128 |
'donor_id', |
| 129 |
'user_id', |
| 130 |
'email', |
| 131 |
'first_name', |
| 132 |
'last_name', |
| 133 |
'date_joined', |
| 134 |
'date_erased', |
| 135 |
'contact_consent', |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Return the email address when data is erased. |
| 141 |
* |
| 142 |
* @since 1.6.0 |
| 143 |
* |
| 144 |
* @param string $email The email address to check. |
| 145 |
* @return boolean True if the email is valid; false if it is marked as invalid. |
| 146 |
*/ |
| 147 |
function charitable_is_valid_email_address( $email ) { |
| 148 |
$invalid = array(); |
| 149 |
|
| 150 |
if ( function_exists( 'wp_privacy_anonymize_data' ) ) { |
| 151 |
$invalid[] = wp_privacy_anonymize_data( 'email' ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Filter the list of invalid email addresses used by Charitable. |
| 156 |
* |
| 157 |
* @since 1.6.0 |
| 158 |
* |
| 159 |
* @param string[] $addresses A list of strings that are invalid email addresses. |
| 160 |
*/ |
| 161 |
$invalid = apply_filters( 'charitable_invalid_email_addresses', $invalid ); |
| 162 |
|
| 163 |
return strlen( $email ) && ! in_array( $email, $invalid ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Return whether donors can be added without an email address. |
| 168 |
* |
| 169 |
* @since 1.6.0 |
| 170 |
* |
| 171 |
* @return boolean True if donors can be added without an email address. False otherwise. |
| 172 |
*/ |
| 173 |
function charitable_permit_donor_without_email() { |
| 174 |
/** |
| 175 |
* Filter whether donors can be added without an email address. |
| 176 |
* |
| 177 |
* Prior to Charitable 1.6, this was never permitted. As of Charitable 1.6, it's possible |
| 178 |
* to support manual donations without an email address by using this filter. |
| 179 |
* |
| 180 |
* NOTE: By default, the public donation form still requires an email address, so this |
| 181 |
* primarily affects programattically created donors, or donors created via manual |
| 182 |
* donations in the admin. |
| 183 |
* |
| 184 |
* @see https://github.com/Charitable/Charitable/issues/535 |
| 185 |
* |
| 186 |
* @since 1.6.0 |
| 187 |
* |
| 188 |
* @param boolean $permitted Whether donors can be added without an email address. |
| 189 |
*/ |
| 190 |
return apply_filters( 'charitable_permit_donor_without_email', false ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get a donor ID based on an email address. |
| 195 |
* |
| 196 |
* @since 1.6.2 |
| 197 |
* |
| 198 |
* @param string $email The email address. |
| 199 |
* @return int |
| 200 |
*/ |
| 201 |
function charitable_get_donor_id_by_email( $email ) { |
| 202 |
return charitable_get_table( 'donors' )->get_donor_id_by_email( $email ); |
| 203 |
} |
| 204 |
|