| 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 whether Broadcasts are enabled in the Plugin settings. |
| 69 |
* |
| 70 |
* @since 2.2.9 |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public function enabled() { |
| 75 |
|
| 76 |
// Check if DOMDocument is installed. |
| 77 |
// It should be installed as mosts hosts include php-dom and php-xml modules. |
| 78 |
// If not, disable Broadcast to Posts import functionality as we can't parse |
| 79 |
// imported Broadcasts. |
| 80 |
if ( ! class_exists( 'DOMDocument' ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
return ( $this->settings['enabled'] === 'on' ? true : false ); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the WordPress Author ID to assign imported Broadcasts to. |
| 90 |
* |
| 91 |
* @since 2.2.9 |
| 92 |
* |
| 93 |
* @return int |
| 94 |
*/ |
| 95 |
public function author_id() { |
| 96 |
|
| 97 |
return $this->settings['author_id']; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the WordPress Post Status to assign to Posts created from imported Broadcasts. |
| 103 |
* |
| 104 |
* @since 2.3.4 |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function post_status() { |
| 109 |
|
| 110 |
return $this->settings['post_status']; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns the WordPress Category ID to assign imported Broadcasts to. |
| 116 |
* |
| 117 |
* @since 2.2.9 |
| 118 |
* |
| 119 |
* @return int |
| 120 |
*/ |
| 121 |
public function category_id() { |
| 122 |
|
| 123 |
return $this->settings['category_id']; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns whether to import the thumbnail to the Featured Image. |
| 129 |
* |
| 130 |
* @since 2.4.1 |
| 131 |
* |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
public function import_thumbnail() { |
| 135 |
|
| 136 |
return ( $this->settings['import_thumbnail'] === 'on' ? true : false ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns whether to import the thumbnail to the Featured Image. |
| 142 |
* |
| 143 |
* @since 2.6.3 |
| 144 |
* |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function import_images() { |
| 148 |
|
| 149 |
return ( $this->settings['import_images'] === 'on' ? true : false ); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns the earliest date that Broadcasts should be imported, |
| 155 |
* based on their published_at date. |
| 156 |
* |
| 157 |
* @since 2.2.9 |
| 158 |
* |
| 159 |
* @return string Date (yyyy-mm-dd) |
| 160 |
*/ |
| 161 |
public function published_at_min_date() { |
| 162 |
|
| 163 |
return $this->settings['published_at_min_date']; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns whether exporting Posts to Broadcasts is enabled in the Plugin settings. |
| 169 |
* |
| 170 |
* @since 2.4.0 |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
public function enabled_export() { |
| 175 |
|
| 176 |
return ( $this->settings['enabled_export'] === 'on' ? true : false ); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns whether Broadcasts should have their styles imported. |
| 182 |
* |
| 183 |
* @since 2.2.9 |
| 184 |
* |
| 185 |
* @return bool |
| 186 |
*/ |
| 187 |
public function no_styles() { |
| 188 |
|
| 189 |
return ( $this->settings['no_styles'] === 'on' ? true : false ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns this settings group's programmatic name. |
| 195 |
* |
| 196 |
* @since 3.4.0 |
| 197 |
* |
| 198 |
* @return string |
| 199 |
*/ |
| 200 |
public function get_name() { |
| 201 |
|
| 202 |
return 'broadcasts'; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the title of this settings group. |
| 208 |
* |
| 209 |
* @since 3.4.0 |
| 210 |
* |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
public function get_title() { |
| 214 |
|
| 215 |
return __( 'Broadcasts Settings', 'convertkit' ); |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns the keys in this settings group that hold credentials or other |
| 221 |
* sensitive values. |
| 222 |
* |
| 223 |
* @since 3.4.0 |
| 224 |
* |
| 225 |
* @return string[] |
| 226 |
*/ |
| 227 |
public function get_secret_keys() { |
| 228 |
|
| 229 |
return array(); |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns the JSON Schema describing this settings group, in the shape |
| 235 |
* stored by save() / returned by get(), excluding secret keys. |
| 236 |
* |
| 237 |
* @since 3.4.0 |
| 238 |
* |
| 239 |
* @return array |
| 240 |
*/ |
| 241 |
public function get_schema() { |
| 242 |
|
| 243 |
return array( |
| 244 |
'type' => 'object', |
| 245 |
'additionalProperties' => false, |
| 246 |
'properties' => array( |
| 247 |
'enabled' => array( |
| 248 |
'type' => 'string', |
| 249 |
'enum' => array( '', 'on' ), |
| 250 |
'description' => __( 'Whether importing Broadcasts from Kit to WordPress Posts is enabled.', 'convertkit' ), |
| 251 |
), |
| 252 |
'author_id' => array( |
| 253 |
'type' => 'integer', |
| 254 |
'minimum' => 1, |
| 255 |
'description' => __( 'WordPress User ID to assign as the Post author when importing Broadcasts.', 'convertkit' ), |
| 256 |
), |
| 257 |
'post_status' => array( |
| 258 |
'type' => 'string', |
| 259 |
'description' => __( 'WordPress Post status to assign to Posts created from imported Broadcasts (e.g. publish, draft).', 'convertkit' ), |
| 260 |
), |
| 261 |
'category_id' => array( |
| 262 |
'type' => array( 'integer', 'string' ), |
| 263 |
'description' => __( 'WordPress Category ID to assign to Posts created from imported Broadcasts. Blank for none.', 'convertkit' ), |
| 264 |
), |
| 265 |
'import_thumbnail' => array( |
| 266 |
'type' => 'string', |
| 267 |
'enum' => array( '', 'on' ), |
| 268 |
'description' => __( 'Whether to import the Broadcast thumbnail as the Post\'s Featured Image.', 'convertkit' ), |
| 269 |
), |
| 270 |
'import_images' => array( |
| 271 |
'type' => 'string', |
| 272 |
'enum' => array( '', 'on' ), |
| 273 |
'description' => __( 'Whether to import images referenced in the Broadcast\'s content into the WordPress Media Library.', 'convertkit' ), |
| 274 |
), |
| 275 |
'published_at_min_date' => array( |
| 276 |
'type' => 'string', |
| 277 |
'format' => 'date', |
| 278 |
'description' => __( 'Earliest published_at date (YYYY-MM-DD) of Broadcasts to import.', 'convertkit' ), |
| 279 |
), |
| 280 |
'enabled_export' => array( |
| 281 |
'type' => 'string', |
| 282 |
'enum' => array( '', 'on' ), |
| 283 |
'description' => __( 'Whether exporting WordPress Posts to Kit Broadcasts is enabled.', 'convertkit' ), |
| 284 |
), |
| 285 |
'no_styles' => array( |
| 286 |
'type' => 'string', |
| 287 |
'enum' => array( '', 'on' ), |
| 288 |
'description' => __( 'Whether inline styles on imported Broadcast content should be stripped.', 'convertkit' ), |
| 289 |
), |
| 290 |
), |
| 291 |
); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* The default settings, used when the ConvertKit Broadcasts Settings haven't been saved |
| 297 |
* e.g. on a new installation. |
| 298 |
* |
| 299 |
* @since 2.2.9 |
| 300 |
* |
| 301 |
* @return array |
| 302 |
*/ |
| 303 |
public function get_defaults() { |
| 304 |
|
| 305 |
$defaults = array( |
| 306 |
'enabled' => '', |
| 307 |
'author_id' => get_current_user_id(), |
| 308 |
'post_status' => 'publish', |
| 309 |
'category_id' => '', |
| 310 |
'import_thumbnail' => 'on', |
| 311 |
'import_images' => '', |
| 312 |
|
| 313 |
// By default, only import Broadcasts as Posts for the last 30 days. |
| 314 |
'published_at_min_date' => gmdate( 'Y-m-d', strtotime( '-30 days' ) ), |
| 315 |
|
| 316 |
'enabled_export' => '', |
| 317 |
'no_styles' => '', |
| 318 |
); |
| 319 |
|
| 320 |
/** |
| 321 |
* The default settings, used when the ConvertKit Broadcasts Settings haven't been saved |
| 322 |
* e.g. on a new installation. |
| 323 |
* |
| 324 |
* @since 2.2.9 |
| 325 |
* |
| 326 |
* @param array $defaults Default settings. |
| 327 |
*/ |
| 328 |
$defaults = apply_filters( 'convertkit_settings_broadcasts_get_defaults', $defaults ); |
| 329 |
|
| 330 |
return $defaults; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Saves the given array of settings to the WordPress options table. |
| 336 |
* |
| 337 |
* @since 2.2.9 |
| 338 |
* |
| 339 |
* @param array $settings Settings. |
| 340 |
*/ |
| 341 |
public function save( $settings ) { |
| 342 |
|
| 343 |
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 344 |
|
| 345 |
// Reload settings in class, to reflect changes. |
| 346 |
$this->refresh_settings(); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Reloads settings from the options table so this instance has the latest values. |
| 352 |
* |
| 353 |
* @since 3.3.4 |
| 354 |
*/ |
| 355 |
private function refresh_settings() { |
| 356 |
|
| 357 |
$settings = get_option( self::SETTINGS_NAME ); |
| 358 |
|
| 359 |
if ( ! $settings ) { |
| 360 |
$this->settings = $this->get_defaults(); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
$this->settings = array_merge( $this->get_defaults(), $settings ); |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
} |
| 369 |
|