| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Broadcasts Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to read ConvertKit Broadcasts Settings. |
| 11 |
* |
| 12 |
* @since 2.2.9 |
| 13 |
*/ |
| 14 |
class ConvertKit_Settings_Broadcasts { |
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 18 |
* |
| 19 |
* @var string |
| 20 |
* |
| 21 |
* @since 2.2.9 |
| 22 |
*/ |
| 23 |
const SETTINGS_NAME = '_wp_convertkit_settings_broadcasts'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Holds the Settings |
| 27 |
* |
| 28 |
* @var array |
| 29 |
* |
| 30 |
* @since 2.2.9 |
| 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 2.2.9 |
| 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 2.2.9 |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function get() { |
| 62 |
|
| 63 |
return $this->settings; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns Broadcasts settings value for the given key. |
| 69 |
* |
| 70 |
* @since 2.2.9 |
| 71 |
* |
| 72 |
* @param string $key Setting Key. |
| 73 |
* @return string Value |
| 74 |
*/ |
| 75 |
public function get_by_key( $key ) { |
| 76 |
|
| 77 |
// If the setting doesn't exist, bail. |
| 78 |
if ( ! array_key_exists( $key, $this->settings ) ) { |
| 79 |
return ''; |
| 80 |
} |
| 81 |
|
| 82 |
// If the setting is empty, fallback to the default. |
| 83 |
if ( empty( $this->settings[ $key ] ) ) { |
| 84 |
$defaults = $this->get_defaults(); |
| 85 |
return $defaults[ $key ]; |
| 86 |
} |
| 87 |
|
| 88 |
return $this->settings[ $key ]; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns whether Broadcasts are enabled in the Plugin settings. |
| 94 |
* |
| 95 |
* @since 2.2.9 |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
public function enabled() { |
| 100 |
|
| 101 |
return ( $this->settings['enabled'] === 'on' ? true : false ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the WordPress Author ID to assign imported Broadcasts to. |
| 107 |
* |
| 108 |
* @since 2.2.9 |
| 109 |
* |
| 110 |
* @return int |
| 111 |
*/ |
| 112 |
public function author_id() { |
| 113 |
|
| 114 |
return $this->settings['author_id']; |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns the WordPress Category ID to assign imported Broadcasts to. |
| 120 |
* |
| 121 |
* @since 2.2.9 |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function category_id() { |
| 126 |
|
| 127 |
return $this->settings['category_id']; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns the earliest date that Broadcasts should be imported, |
| 133 |
* based on their published_at date. |
| 134 |
* |
| 135 |
* @since 2.2.9 |
| 136 |
* |
| 137 |
* @return string Date (yyyy-mm-dd) |
| 138 |
*/ |
| 139 |
public function published_at_min_date() { |
| 140 |
|
| 141 |
return $this->settings['published_at_min_date']; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns whether exporting Posts to Broadcasts is enabled in the Plugin settings. |
| 147 |
* |
| 148 |
* @since 2.4.0 |
| 149 |
* |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public function enabled_export() { |
| 153 |
|
| 154 |
return ( $this->settings['enabled_export'] === 'on' ? true : false ); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns whether Broadcasts should have their styles imported. |
| 160 |
* |
| 161 |
* @since 2.2.9 |
| 162 |
* |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
public function no_styles() { |
| 166 |
|
| 167 |
return ( $this->settings['no_styles'] === 'on' ? true : false ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Returns whether imported Broadcasts should have their Restrict Content |
| 173 |
* setting defined, if the Broadcast is marked as paid. |
| 174 |
* |
| 175 |
* @since 2.2.9 |
| 176 |
* |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
public function restrict_content_enabled() { |
| 180 |
|
| 181 |
return ! empty( $this->settings['restrict_content'] ); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the Restrict Content setting to assign to imported Broadcasts |
| 187 |
* |
| 188 |
* @since 2.2.9 |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function restrict_content() { |
| 193 |
|
| 194 |
return $this->settings['restrict_content']; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* The default settings, used when the ConvertKit Broadcasts Settings haven't been saved |
| 200 |
* e.g. on a new installation. |
| 201 |
* |
| 202 |
* @since 2.2.9 |
| 203 |
* |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public function get_defaults() { |
| 207 |
|
| 208 |
$defaults = array( |
| 209 |
'enabled' => '', |
| 210 |
'author_id' => get_current_user_id(), |
| 211 |
'category_id' => '', |
| 212 |
|
| 213 |
// By default, only import Broadcasts as Posts for the last 30 days. |
| 214 |
'published_at_min_date' => gmdate( 'Y-m-d', strtotime( '-30 days' ) ), |
| 215 |
|
| 216 |
'enabled_export' => '', |
| 217 |
'no_styles' => '', |
| 218 |
); |
| 219 |
|
| 220 |
/** |
| 221 |
* The default settings, used when the ConvertKit Broadcasts Settings haven't been saved |
| 222 |
* e.g. on a new installation. |
| 223 |
* |
| 224 |
* @since 2.2.9 |
| 225 |
* |
| 226 |
* @param array $defaults |
| 227 |
*/ |
| 228 |
$defaults = apply_filters( 'convertkit_settings_broadcasts_get_defaults', $defaults ); |
| 229 |
|
| 230 |
return $defaults; |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Saves the given array of settings to the WordPress options table. |
| 236 |
* |
| 237 |
* @since 2.2.9 |
| 238 |
* |
| 239 |
* @param array $settings Settings. |
| 240 |
*/ |
| 241 |
public function save( $settings ) { |
| 242 |
|
| 243 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|