| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Options |
| 4 |
{ |
| 5 |
/** |
| 6 |
* The option keys that we can save to the options table |
| 7 |
* |
| 8 |
* @var array |
| 9 |
*/ |
| 10 |
protected $settings = [ |
| 11 |
'logtivity_site_api_key', |
| 12 |
'logtivity_disable_default_logging', |
| 13 |
'logtivity_should_store_user_id', |
| 14 |
'logtivity_should_store_ip', |
| 15 |
'logtivity_should_log_profile_link', |
| 16 |
'logtivity_should_log_username', |
| 17 |
'logtivity_enable_debug_mode', |
| 18 |
'logtivity_latest_response', |
| 19 |
]; |
| 20 |
|
| 21 |
/** |
| 22 |
* The option keys that we can save to the options table |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $rules = [ |
| 27 |
'logtivity_site_api_key' => 'is_string', |
| 28 |
'logtivity_disable_default_logging' => 'is_bool', |
| 29 |
'logtivity_should_store_user_id' => 'is_bool', |
| 30 |
'logtivity_should_store_ip' => 'is_bool', |
| 31 |
'logtivity_should_log_profile_link' => 'is_bool', |
| 32 |
'logtivity_should_log_username' => 'is_bool', |
| 33 |
'logtivity_enable_debug_mode' => 'is_bool', |
| 34 |
'logtivity_latest_response' => 'is_array', |
| 35 |
]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the admin settings or the plugin |
| 39 |
* |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function getOptions() |
| 43 |
{ |
| 44 |
$options = []; |
| 45 |
|
| 46 |
foreach ($this->settings as $setting) { |
| 47 |
$options[$setting] = get_option($setting); |
| 48 |
} |
| 49 |
|
| 50 |
return $options; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get an option from the database |
| 55 |
* |
| 56 |
* @param string $key |
| 57 |
* @return mixed |
| 58 |
*/ |
| 59 |
public function getOption($key) |
| 60 |
{ |
| 61 |
if (!in_array($key, $this->settings)) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
return get_option($key); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Should we store the user id? |
| 70 |
* |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public function shouldStoreUserId() |
| 74 |
{ |
| 75 |
return $this->getOption('logtivity_should_store_user_id'); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Should we store the users IP? |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function shouldStoreIp() |
| 84 |
{ |
| 85 |
return $this->getOption('logtivity_should_store_ip'); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Should we store the users profile link? |
| 90 |
* |
| 91 |
* @return bool |
| 92 |
*/ |
| 93 |
public function shouldStoreProfileLink() |
| 94 |
{ |
| 95 |
return $this->getOption('logtivity_should_log_profile_link'); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Should we store the users username? |
| 100 |
* |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function shouldStoreUsername() |
| 104 |
{ |
| 105 |
return $this->getOption('logtivity_should_log_username'); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Should we be logging the response from the API |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
public function shouldLogLatestResponse() |
| 114 |
{ |
| 115 |
return $this->getOption('logtivity_enable_debug_mode'); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Update the options for this plugin |
| 120 |
* |
| 121 |
* @param array $data |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function update($data = null) |
| 125 |
{ |
| 126 |
if ($data) { |
| 127 |
foreach ($this->settings as $setting) |
| 128 |
{ |
| 129 |
if (array_key_exists($setting, $data) && $this->validateSetting($setting, $data[$setting])) { |
| 130 |
update_option($setting, $data[$setting]); |
| 131 |
} |
| 132 |
} |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
foreach ($this->settings as $setting) |
| 137 |
{ |
| 138 |
if (isset($_POST[$setting]) && $this->validateSetting($setting, $_POST[$setting])) { |
| 139 |
update_option($setting, $_POST[$setting]); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Validate that the passed parameters are in the correct format |
| 146 |
* |
| 147 |
* @param string $setting |
| 148 |
* @param string $value |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
protected function validateSetting($setting, $value) |
| 152 |
{ |
| 153 |
$method = $this->rules[$setting]; |
| 154 |
|
| 155 |
if ($method == 'is_bool') { |
| 156 |
return $method((bool) $value); |
| 157 |
} |
| 158 |
|
| 159 |
return $method($value); |
| 160 |
} |
| 161 |
} |