| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit MCP Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit MCP Settings. |
| 11 |
* |
| 12 |
* @since 3.4.0 |
| 13 |
*/ |
| 14 |
class ConvertKit_Settings_MCP { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 18 |
* |
| 19 |
* @var string |
| 20 |
* |
| 21 |
* @since 3.4.0 |
| 22 |
*/ |
| 23 |
const SETTINGS_NAME = '_wp_convertkit_settings_mcp'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Holds the Settings |
| 27 |
* |
| 28 |
* @var array |
| 29 |
* |
| 30 |
* @since 3.4.0 |
| 31 |
*/ |
| 32 |
private $settings = array(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. Reads settings from options table, falling back to defaults |
| 36 |
* if no settings exist. |
| 37 |
* |
| 38 |
* @since 3.4.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
// Get Settings. |
| 43 |
$settings = get_option( self::SETTINGS_NAME ); |
| 44 |
|
| 45 |
// If no Settings exist, falback to default settings. |
| 46 |
if ( ! $settings ) { |
| 47 |
$this->settings = $this->get_defaults(); |
| 48 |
} else { |
| 49 |
$this->settings = array_merge( $this->get_defaults(), $settings ); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns Plugin settings. |
| 56 |
* |
| 57 |
* @since 3.4.0 |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function get() { |
| 62 |
|
| 63 |
return $this->settings; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns whether the user has access to MCP via a paid plan, |
| 69 |
* and if so whether the MCP server is enabled in the Plugin's settings. |
| 70 |
* |
| 71 |
* @since 3.4.0 |
| 72 |
* |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public function enabled() { |
| 76 |
|
| 77 |
// Bail if the connected Kit account isn't on a paid plan. |
| 78 |
// This queries the cached account details, so no live API call is made. |
| 79 |
$account = new ConvertKit_Resource_Account(); |
| 80 |
if ( ! $account->is_paid_plan() ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
return ( $this->settings['enabled'] === 'on' ? true : false ); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* The default settings, used when the ConvertKit MCP Settings haven't been saved |
| 90 |
* e.g. on a new installation. |
| 91 |
* |
| 92 |
* @since 2.1.0 |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function get_defaults() { |
| 97 |
|
| 98 |
$defaults = array( |
| 99 |
'enabled' => '', // blank|on. |
| 100 |
); |
| 101 |
|
| 102 |
/** |
| 103 |
* The default settings, used when the ConvertKit MCP Settings haven't been saved |
| 104 |
* e.g. on a new installation. |
| 105 |
* |
| 106 |
* @since 3.4.0 |
| 107 |
* |
| 108 |
* @param array $defaults Default settings. |
| 109 |
*/ |
| 110 |
$defaults = apply_filters( 'convertkit_settings_mcp_get_defaults', $defaults ); |
| 111 |
|
| 112 |
return $defaults; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Saves the given array of settings to the WordPress options table. |
| 118 |
* |
| 119 |
* @since 3.4.0 |
| 120 |
* |
| 121 |
* @param array $settings Settings. |
| 122 |
*/ |
| 123 |
public function save( $settings ) { |
| 124 |
|
| 125 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|