| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Plugin Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Plugin Settings. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_Settings { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
const SETTINGS_NAME = '_wp_convertkit_settings'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the Settings |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private $settings = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. Reads settings from options table, falling back to defaults |
| 32 |
* if no settings exist. |
| 33 |
* |
| 34 |
* @since 1.9.6 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
// Get Settings. |
| 39 |
$settings = get_option( self::SETTINGS_NAME ); |
| 40 |
|
| 41 |
// If no Settings exist, falback to default settings. |
| 42 |
if ( ! $settings ) { |
| 43 |
$this->settings = $this->get_defaults(); |
| 44 |
} else { |
| 45 |
$this->settings = array_merge( $this->get_defaults(), $settings ); |
| 46 |
} |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns Plugin settings. |
| 52 |
* |
| 53 |
* @since 1.9.6 |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public function get() { |
| 58 |
|
| 59 |
return $this->settings; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the API Key Plugin setting. |
| 65 |
* |
| 66 |
* @since 1.9.6 |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function get_api_key() { |
| 71 |
|
| 72 |
// Return API Key from constant, if defined. |
| 73 |
if ( defined( 'CONVERTKIT_API_KEY' ) ) { |
| 74 |
return CONVERTKIT_API_KEY; |
| 75 |
} |
| 76 |
|
| 77 |
// Return API Key from settings. |
| 78 |
return $this->settings['api_key']; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns whether the API Key has been set in the Plugin settings. |
| 84 |
* |
| 85 |
* @since 1.9.6 |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function has_api_key() { |
| 90 |
|
| 91 |
return ( ! empty( $this->get_api_key() ) ? true : false ); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns whether the API Key is stored as a constant in the wp-config.php file. |
| 97 |
* |
| 98 |
* @since 1.9.6 |
| 99 |
* |
| 100 |
* @return bool |
| 101 |
*/ |
| 102 |
public function is_api_key_a_constant() { |
| 103 |
|
| 104 |
return defined( 'CONVERTKIT_API_KEY' ); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the API Secret Plugin setting. |
| 110 |
* |
| 111 |
* @since 1.9.6 |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public function get_api_secret() { |
| 116 |
|
| 117 |
// Return API Secret from constant, if defined. |
| 118 |
if ( defined( 'CONVERTKIT_API_SECRET' ) ) { |
| 119 |
return CONVERTKIT_API_SECRET; |
| 120 |
} |
| 121 |
|
| 122 |
// Return API Secret from settings. |
| 123 |
return $this->settings['api_secret']; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns whether the API Secret has been set in the Plugin settings. |
| 129 |
* |
| 130 |
* @since 1.9.6 |
| 131 |
* |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
public function has_api_secret() { |
| 135 |
|
| 136 |
return ( ! empty( $this->get_api_secret() ) ? true : false ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns whether the API Secret is stored as a constant in the wp-config.php file. |
| 142 |
* |
| 143 |
* @since 1.9.6 |
| 144 |
* |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function is_api_secret_a_constant() { |
| 148 |
|
| 149 |
return defined( 'CONVERTKIT_API_SECRET' ); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns whether the API Key and Secret have been set in the Plugin settings. |
| 155 |
* |
| 156 |
* @since 1.9.6 |
| 157 |
* |
| 158 |
* @return bool |
| 159 |
*/ |
| 160 |
public function has_api_key_and_secret() { |
| 161 |
|
| 162 |
return $this->has_api_key() && $this->has_api_secret(); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns the Default Form Plugin setting. |
| 168 |
* |
| 169 |
* @since 1.9.6 |
| 170 |
* |
| 171 |
* @param string $post_type Post Type. |
| 172 |
* @return string|int Default Form (default|form id) |
| 173 |
*/ |
| 174 |
public function get_default_form( $post_type ) { |
| 175 |
|
| 176 |
// Return default if this Post Type doesn't exist as a setting. |
| 177 |
if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) { |
| 178 |
return 'default'; |
| 179 |
} |
| 180 |
|
| 181 |
// Backward compat. where older Plugin versions would store API errors in the option value |
| 182 |
// with id = -2 and name = 'Error contacting API'. |
| 183 |
if ( is_array( $this->settings[ $post_type . '_form' ] ) ) { |
| 184 |
return 'default'; |
| 185 |
} |
| 186 |
|
| 187 |
return $this->settings[ $post_type . '_form' ]; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns whether the Default Form has been set in the Plugin settings. |
| 193 |
* |
| 194 |
* @since 1.9.6 |
| 195 |
* |
| 196 |
* @param string $post_type Post Type. |
| 197 |
* @return bool Post Type has a Default Form setting specified in Plugin Settings. |
| 198 |
*/ |
| 199 |
public function has_default_form( $post_type ) { |
| 200 |
|
| 201 |
return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false ); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns whether debugging is enabled in the Plugin settings. |
| 207 |
* |
| 208 |
* @since 1.9.6 |
| 209 |
* |
| 210 |
* @return bool |
| 211 |
*/ |
| 212 |
public function debug_enabled() { |
| 213 |
|
| 214 |
return ( $this->settings['debug'] === 'on' ? true : false ); |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns whether scripts are disabled in the Plugin settings. |
| 220 |
* |
| 221 |
* @since 1.9.6 |
| 222 |
* |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
public function scripts_disabled() { |
| 226 |
|
| 227 |
return ( $this->settings['no_scripts'] === 'on' ? true : false ); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns whether stylesheets are disabled in the Plugin settings. |
| 233 |
* |
| 234 |
* @since 1.9.6.9 |
| 235 |
* |
| 236 |
* @return bool |
| 237 |
*/ |
| 238 |
public function css_disabled() { |
| 239 |
|
| 240 |
return ( $this->settings['no_css'] === 'on' ? true : false ); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| 246 |
* e.g. on a new installation. |
| 247 |
* |
| 248 |
* @since 1.9.6 |
| 249 |
* |
| 250 |
* @return array |
| 251 |
*/ |
| 252 |
public function get_defaults() { |
| 253 |
|
| 254 |
$defaults = array( |
| 255 |
'api_key' => '', // string. |
| 256 |
'api_secret' => '', // string. |
| 257 |
'debug' => '', // blank|on. |
| 258 |
'no_scripts' => '', // blank|on. |
| 259 |
'no_css' => '', // blank|on. |
| 260 |
); |
| 261 |
|
| 262 |
// Add Post Type Default Forms. |
| 263 |
foreach ( convertkit_get_supported_post_types() as $post_type ) { |
| 264 |
$defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| 269 |
* e.g. on a new installation. |
| 270 |
* |
| 271 |
* @since 1.9.6 |
| 272 |
* |
| 273 |
* @param array $defaults |
| 274 |
*/ |
| 275 |
$defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults ); |
| 276 |
|
| 277 |
return $defaults; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Saves the given array of settings to the WordPress options table. |
| 283 |
* |
| 284 |
* @since 1.9.8.4 |
| 285 |
* |
| 286 |
* @param array $settings Settings. |
| 287 |
*/ |
| 288 |
public function save( $settings ) { |
| 289 |
|
| 290 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|