| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit User class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Settings for the given User. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_User { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Meta Key that stores ConvertKit settings on a per-User basis |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const META_KEY = 'convertkit_tags'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the User ID |
| 25 |
* |
| 26 |
* @since 1.9.6 |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
public $user_id = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Holds the User's Settings |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
private $settings = array(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. Populates the settings based on the given User ID. |
| 41 |
* |
| 42 |
* @since 1.9.6 |
| 43 |
* |
| 44 |
* @param int $user_id User ID. |
| 45 |
*/ |
| 46 |
public function __construct( $user_id ) { |
| 47 |
|
| 48 |
// Assign User's ID to the object. |
| 49 |
$this->user_id = $user_id; |
| 50 |
|
| 51 |
// Get User Meta. |
| 52 |
$meta = get_user_meta( $user_id, self::META_KEY, true ); |
| 53 |
if ( ! $meta ) { |
| 54 |
// Fallback to default settings. |
| 55 |
$meta = $this->get_default_settings(); |
| 56 |
} |
| 57 |
|
| 58 |
// Assign User's Settings to the object. |
| 59 |
$this->settings = $meta; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns settings for the User. |
| 65 |
* |
| 66 |
* @since 1.9.6 |
| 67 |
* |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
public function get() { |
| 71 |
|
| 72 |
return $this->settings; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Saves User settings to the User. |
| 78 |
* |
| 79 |
* @since 1.9.6 |
| 80 |
* |
| 81 |
* @param array $meta Settings. |
| 82 |
*/ |
| 83 |
public function save( $meta ) { |
| 84 |
|
| 85 |
return update_user_meta( $this->user_id, self::META_KEY, $meta ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* The default settings, used to populate the User's Settings when a User |
| 91 |
* has no Settings. |
| 92 |
* |
| 93 |
* @since 1.9.6 |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function get_default_settings() { |
| 98 |
|
| 99 |
$defaults = array(); |
| 100 |
|
| 101 |
/** |
| 102 |
* The default settings, used to populate the User's Settings when a User has no Settings. |
| 103 |
* |
| 104 |
* @since 1.9.6 |
| 105 |
* |
| 106 |
* @param array $defaults Default Settings. |
| 107 |
*/ |
| 108 |
$defaults = apply_filters( 'convertkit_user_get_default_settings', $defaults ); |
| 109 |
|
| 110 |
return $defaults; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|