| 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 |
// Update Access Token when refreshed by the API class. |
| 49 |
add_action( 'convertkit_api_refresh_token', array( $this, 'update_credentials' ), 10, 2 ); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns Plugin settings. |
| 55 |
* |
| 56 |
* @since 1.9.6 |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function get() { |
| 61 |
|
| 62 |
return $this->settings; |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the API Key Plugin setting. |
| 68 |
* |
| 69 |
* @since 1.9.6 |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function get_api_key() { |
| 74 |
|
| 75 |
// Return API Key from constant, if defined. |
| 76 |
if ( defined( 'CONVERTKIT_API_KEY' ) ) { |
| 77 |
return CONVERTKIT_API_KEY; |
| 78 |
} |
| 79 |
|
| 80 |
// Return API Key from settings. |
| 81 |
return $this->settings['api_key']; |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns whether the API Key has been set in the Plugin settings. |
| 87 |
* |
| 88 |
* @since 1.9.6 |
| 89 |
* |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public function has_api_key() { |
| 93 |
|
| 94 |
return ( ! empty( $this->get_api_key() ) ? true : false ); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns whether the API Key is stored as a constant in the wp-config.php file. |
| 100 |
* |
| 101 |
* @since 1.9.6 |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function is_api_key_a_constant() { |
| 106 |
|
| 107 |
return defined( 'CONVERTKIT_API_KEY' ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the API Secret Plugin setting. |
| 113 |
* |
| 114 |
* @since 1.9.6 |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_api_secret() { |
| 119 |
|
| 120 |
// Return API Secret from constant, if defined. |
| 121 |
if ( defined( 'CONVERTKIT_API_SECRET' ) ) { |
| 122 |
return CONVERTKIT_API_SECRET; |
| 123 |
} |
| 124 |
|
| 125 |
// Return API Secret from settings. |
| 126 |
return $this->settings['api_secret']; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns whether the API Secret has been set in the Plugin settings. |
| 132 |
* |
| 133 |
* @since 1.9.6 |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public function has_api_secret() { |
| 138 |
|
| 139 |
return ( ! empty( $this->get_api_secret() ) ? true : false ); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns whether the API Secret is stored as a constant in the wp-config.php file. |
| 145 |
* |
| 146 |
* @since 1.9.6 |
| 147 |
* |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
public function is_api_secret_a_constant() { |
| 151 |
|
| 152 |
return defined( 'CONVERTKIT_API_SECRET' ); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns whether the API Key and Secret have been set in the Plugin settings. |
| 158 |
* |
| 159 |
* @since 1.9.6 |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public function has_api_key_and_secret() { |
| 164 |
|
| 165 |
return $this->has_api_key() && $this->has_api_secret(); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the Access Token Plugin setting. |
| 171 |
* |
| 172 |
* @since 2.5.0 |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public function get_access_token() { |
| 177 |
|
| 178 |
// Return Access Token from settings. |
| 179 |
return $this->settings['access_token']; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Returns whether the Access Token has been set in the Plugin settings. |
| 185 |
* |
| 186 |
* @since 2.5.0 |
| 187 |
* |
| 188 |
* @return bool |
| 189 |
*/ |
| 190 |
public function has_access_token() { |
| 191 |
|
| 192 |
return ( ! empty( $this->get_access_token() ) ? true : false ); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns the Refresh Token Plugin setting. |
| 198 |
* |
| 199 |
* @since 2.5.0 |
| 200 |
* |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
public function get_refresh_token() { |
| 204 |
|
| 205 |
// Return Refresh Token from settings. |
| 206 |
return $this->settings['refresh_token']; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns whether the Refresh Token has been set in the Plugin settings. |
| 212 |
* |
| 213 |
* @since 2.5.0 |
| 214 |
* |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public function has_refresh_token() { |
| 218 |
|
| 219 |
return ( ! empty( $this->get_refresh_token() ) ? true : false ); |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns whether to use Access and Refresh Tokens for API requests, |
| 225 |
* based on whether an Access Token and Refresh Token have been saved |
| 226 |
* in the Plugin settings. |
| 227 |
* |
| 228 |
* @since 2.5.0 |
| 229 |
* |
| 230 |
* @return bool |
| 231 |
*/ |
| 232 |
public function has_access_and_refresh_token() { |
| 233 |
|
| 234 |
return $this->has_access_token() && $this->has_refresh_token(); |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns the Access Token expiry timestamp. |
| 240 |
* |
| 241 |
* @since 2.5.0 |
| 242 |
* |
| 243 |
* @return int |
| 244 |
*/ |
| 245 |
public function get_token_expiry() { |
| 246 |
|
| 247 |
// Return Token Expiry from settings. |
| 248 |
return $this->settings['token_expires']; |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the Default Form Plugin setting. |
| 254 |
* |
| 255 |
* @since 1.9.6 |
| 256 |
* |
| 257 |
* @param string $post_type Post Type. |
| 258 |
* @return string|int Default Form (default|form id) |
| 259 |
*/ |
| 260 |
public function get_default_form( $post_type ) { |
| 261 |
|
| 262 |
// Return default if this Post Type doesn't exist as a setting. |
| 263 |
if ( ! array_key_exists( $post_type . '_form', $this->settings ) ) { |
| 264 |
return 'default'; |
| 265 |
} |
| 266 |
|
| 267 |
// Backward compat. where older Plugin versions would store API errors in the option value |
| 268 |
// with id = -2 and name = 'Error contacting API'. |
| 269 |
if ( is_array( $this->settings[ $post_type . '_form' ] ) ) { |
| 270 |
return 'default'; |
| 271 |
} |
| 272 |
|
| 273 |
return $this->settings[ $post_type . '_form' ]; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Returns whether the Default Form has been set in the Plugin settings. |
| 279 |
* |
| 280 |
* @since 1.9.6 |
| 281 |
* |
| 282 |
* @param string $post_type Post Type. |
| 283 |
* @return bool Post Type has a Default Form setting specified in Plugin Settings. |
| 284 |
*/ |
| 285 |
public function has_default_form( $post_type ) { |
| 286 |
|
| 287 |
return ( ! empty( $this->settings[ $post_type . '_form' ] ) ? true : false ); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns the Default Form Position Plugin setting. |
| 293 |
* |
| 294 |
* @since 2.5.8 |
| 295 |
* |
| 296 |
* @param string $post_type Post Type. |
| 297 |
* @return string|int Default Form (default|form id) |
| 298 |
*/ |
| 299 |
public function get_default_form_position( $post_type ) { |
| 300 |
|
| 301 |
// Return after_content if this Post Type's position doesn't exist as a setting. |
| 302 |
if ( ! array_key_exists( $post_type . '_form_position', $this->settings ) ) { |
| 303 |
return 'after_content'; |
| 304 |
} |
| 305 |
|
| 306 |
return $this->settings[ $post_type . '_form_position' ]; |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Returns the Global non-inline Form Plugin setting. |
| 312 |
* |
| 313 |
* @since 2.3.3 |
| 314 |
* |
| 315 |
* @return string|int Non-inline Form (blank string|form id) |
| 316 |
*/ |
| 317 |
public function get_non_inline_form() { |
| 318 |
|
| 319 |
// Return blank string if no inline form is specified. |
| 320 |
if ( ! $this->has_non_inline_form() ) { |
| 321 |
return ''; |
| 322 |
} |
| 323 |
|
| 324 |
return $this->settings['non_inline_form']; |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns whether the Global non-inline Form has been set in the Plugin settings. |
| 330 |
* |
| 331 |
* @since 2.3.3 |
| 332 |
* |
| 333 |
* @return bool Global non-inline Form setting specified in Plugin Settings. |
| 334 |
*/ |
| 335 |
public function has_non_inline_form() { |
| 336 |
|
| 337 |
return ( ! empty( $this->settings['non_inline_form'] ) ? true : false ); |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Returns whether debugging is enabled in the Plugin settings. |
| 343 |
* |
| 344 |
* @since 1.9.6 |
| 345 |
* |
| 346 |
* @return bool |
| 347 |
*/ |
| 348 |
public function debug_enabled() { |
| 349 |
|
| 350 |
return ( $this->settings['debug'] === 'on' ? true : false ); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Returns whether scripts are disabled in the Plugin settings. |
| 356 |
* |
| 357 |
* @since 1.9.6 |
| 358 |
* |
| 359 |
* @return bool |
| 360 |
*/ |
| 361 |
public function scripts_disabled() { |
| 362 |
|
| 363 |
return ( $this->settings['no_scripts'] === 'on' ? true : false ); |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Returns whether stylesheets are disabled in the Plugin settings. |
| 369 |
* |
| 370 |
* @since 1.9.6.9 |
| 371 |
* |
| 372 |
* @return bool |
| 373 |
*/ |
| 374 |
public function css_disabled() { |
| 375 |
|
| 376 |
return ( $this->settings['no_css'] === 'on' ? true : false ); |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| 382 |
* e.g. on a new installation. |
| 383 |
* |
| 384 |
* @since 1.9.6 |
| 385 |
* |
| 386 |
* @return array |
| 387 |
*/ |
| 388 |
public function get_defaults() { |
| 389 |
|
| 390 |
$defaults = array( |
| 391 |
// OAuth. |
| 392 |
'access_token' => '', // string. |
| 393 |
'refresh_token' => '', // string. |
| 394 |
'token_expires' => '', // integer. |
| 395 |
|
| 396 |
// API Key. Retained if needed for backward compat. |
| 397 |
'api_key' => '', // string. |
| 398 |
'api_secret' => '', // string. |
| 399 |
|
| 400 |
// Settings. |
| 401 |
'non_inline_form' => '', // string. |
| 402 |
'debug' => '', // blank|on. |
| 403 |
'no_scripts' => '', // blank|on. |
| 404 |
'no_css' => '', // blank|on. |
| 405 |
); |
| 406 |
|
| 407 |
// Add Post Type Default Forms. |
| 408 |
foreach ( convertkit_get_supported_post_types() as $post_type ) { |
| 409 |
$defaults[ $post_type . '_form' ] = 0; // -1, 0 or Form ID. |
| 410 |
$defaults[ $post_type . '_form_position' ] = 'after_content'; // before_content,after_content. |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* The default settings, used when the ConvertKit Plugin Settings haven't been saved |
| 415 |
* e.g. on a new installation. |
| 416 |
* |
| 417 |
* @since 1.9.6 |
| 418 |
* |
| 419 |
* @param array $defaults Default Settings. |
| 420 |
*/ |
| 421 |
$defaults = apply_filters( 'convertkit_settings_get_defaults', $defaults ); |
| 422 |
|
| 423 |
return $defaults; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Saves the new access token, refresh token and its expiry when the API |
| 429 |
* class automatically refreshes an outdated access token. |
| 430 |
* |
| 431 |
* @since 2.5.0 |
| 432 |
* |
| 433 |
* @param array $result New Access Token, Refresh Token and Expiry. |
| 434 |
* @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. |
| 435 |
*/ |
| 436 |
public function update_credentials( $result, $client_id ) { |
| 437 |
|
| 438 |
// Don't save these credentials if they're not for this Client ID. |
| 439 |
// They're for another ConvertKit Plugin that uses OAuth. |
| 440 |
if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { |
| 441 |
return; |
| 442 |
} |
| 443 |
|
| 444 |
$this->save( |
| 445 |
array( |
| 446 |
'access_token' => $result['access_token'], |
| 447 |
'refresh_token' => $result['refresh_token'], |
| 448 |
'token_expires' => ( $result['created_at'] + $result['expires_in'] ), |
| 449 |
) |
| 450 |
); |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Deletes any existing access token, refresh token and its expiry from the Plugin settings. |
| 456 |
* |
| 457 |
* @since 2.5.0 |
| 458 |
*/ |
| 459 |
public function delete_credentials() { |
| 460 |
|
| 461 |
$this->save( |
| 462 |
array( |
| 463 |
'access_token' => '', |
| 464 |
'refresh_token' => '', |
| 465 |
'token_expires' => '', |
| 466 |
) |
| 467 |
); |
| 468 |
|
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Saves the given array of settings to the WordPress options table. |
| 473 |
* |
| 474 |
* @since 1.9.8.4 |
| 475 |
* |
| 476 |
* @param array $settings Settings. |
| 477 |
*/ |
| 478 |
public function save( $settings ) { |
| 479 |
|
| 480 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 481 |
|
| 482 |
// Reload settings in class, to reflect changes. |
| 483 |
$this->settings = get_option( self::SETTINGS_NAME ); |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
} |
| 488 |
|