← All changes
|
includes/class-convertkit-settings-broadcasts.php
+169
-35
2.3.1
→
3.4.3
View file →
| @@ -64,55 +64,51 @@ | ||
| 64 | 64 | |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
| 68 | - * Returns Broadcasts settings value for the given key. | |
| 68 | + * Returns whether Broadcasts are enabled in the Plugin settings. | |
| 69 | 69 | * |
| 70 | 70 | * @since 2.2.9 |
| 71 | 71 | * |
| 72 | - * @param string $key Setting Key. | |
| 73 | - * @return string Value | |
| 72 | + * @return bool | |
| 74 | 73 | */ |
| 75 | - public function get_by_key( $key ) { | |
| 74 | + public function enabled() { | |
| 76 | 75 | |
| 77 | - // If the setting doesn't exist, bail. | |
| 78 | - if ( ! array_key_exists( $key, $this->settings ) ) { | |
| 79 | - return ''; | |
| 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; | |
| 80 | 82 | } |
| 81 | 83 | |
| 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 | - } | |
| 84 | + return ( $this->settings['enabled'] === 'on' ? true : false ); | |
| 87 | 85 | |
| 88 | - return $this->settings[ $key ]; | |
| 89 | - | |
| 90 | 86 | } |
| 91 | 87 | |
| 92 | 88 | /** |
| 93 | - * Returns whether Broadcasts are enabled in the Plugin settings. | |
| 89 | + * Returns the WordPress Author ID to assign imported Broadcasts to. | |
| 94 | 90 | * |
| 95 | 91 | * @since 2.2.9 |
| 96 | 92 | * |
| 97 | - * @return bool | |
| 93 | + * @return int | |
| 98 | 94 | */ |
| 99 | - public function enabled() { | |
| 95 | + public function author_id() { | |
| 100 | 96 | |
| 101 | - return ( $this->settings['enabled'] === 'on' ? true : false ); | |
| 97 | + return $this->settings['author_id']; | |
| 102 | 98 | |
| 103 | 99 | } |
| 104 | 100 | |
| 105 | 101 | /** |
| 106 | - * Returns the WordPress Author ID to assign imported Broadcasts to. | |
| 102 | + * Returns the WordPress Post Status to assign to Posts created from imported Broadcasts. | |
| 107 | 103 | * |
| 108 | - * @since 2.2.9 | |
| 104 | + * @since 2.3.4 | |
| 109 | 105 | * |
| 110 | - * @return int | |
| 106 | + * @return string | |
| 111 | 107 | */ |
| 112 | - public function author_id() { | |
| 108 | + public function post_status() { | |
| 113 | 109 | |
| 114 | - return $this->settings['author_id']; | |
| 110 | + return $this->settings['post_status']; | |
| 115 | 111 | |
| 116 | 112 | } |
| 117 | 113 | |
| 118 | 114 | /** |
| @@ -128,8 +124,34 @@ | ||
| 128 | 124 | |
| 129 | 125 | } |
| 130 | 126 | |
| 131 | 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 | + /** | |
| 132 | 154 | * Returns the earliest date that Broadcasts should be imported, |
| 133 | 155 | * based on their published_at date. |
| 134 | 156 | * |
| 135 | 157 | * @since 2.2.9 |
| @@ -142,8 +164,21 @@ | ||
| 142 | 164 | |
| 143 | 165 | } |
| 144 | 166 | |
| 145 | 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 | + /** | |
| 146 | 181 | * Returns whether Broadcasts should have their styles imported. |
| 147 | 182 | * |
| 148 | 183 | * @since 2.2.9 |
| 149 | 184 | * |
| @@ -155,35 +190,110 @@ | ||
| 155 | 190 | |
| 156 | 191 | } |
| 157 | 192 | |
| 158 | 193 | /** |
| 159 | - * Returns whether imported Broadcasts should have their Restrict Content | |
| 160 | - * setting defined, if the Broadcast is marked as paid. | |
| 194 | + * Returns this settings group's programmatic name. | |
| 161 | 195 | * |
| 162 | - * @since 2.2.9 | |
| 196 | + * @since 3.4.0 | |
| 163 | 197 | * |
| 164 | - * @return bool | |
| 198 | + * @return string | |
| 165 | 199 | */ |
| 166 | - public function restrict_content_enabled() { | |
| 200 | + public function get_name() { | |
| 167 | 201 | |
| 168 | - return ! empty( $this->settings['restrict_content'] ); | |
| 202 | + return 'broadcasts'; | |
| 169 | 203 | |
| 170 | 204 | } |
| 171 | 205 | |
| 172 | 206 | /** |
| 173 | - * Returns the Restrict Content setting to assign to imported Broadcasts | |
| 207 | + * Returns the title of this settings group. | |
| 174 | 208 | * |
| 175 | - * @since 2.2.9 | |
| 209 | + * @since 3.4.0 | |
| 176 | 210 | * |
| 177 | 211 | * @return string |
| 178 | 212 | */ |
| 179 | - public function restrict_content() { | |
| 213 | + public function get_title() { | |
| 180 | 214 | |
| 181 | - return $this->settings['restrict_content']; | |
| 215 | + return __( 'Broadcasts Settings', 'convertkit' ); | |
| 182 | 216 | |
| 183 | 217 | } |
| 184 | 218 | |
| 185 | 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 | + /** | |
| 186 | 296 | * The default settings, used when the ConvertKit Broadcasts Settings haven't been saved |
| 187 | 297 | * e.g. on a new installation. |
| 188 | 298 | * |
| 189 | 299 | * @since 2.2.9 |
| @@ -194,14 +304,17 @@ | ||
| 194 | 304 | |
| 195 | 305 | $defaults = array( |
| 196 | 306 | 'enabled' => '', |
| 197 | 307 | 'author_id' => get_current_user_id(), |
| 308 | + 'post_status' => 'publish', | |
| 198 | 309 | 'category_id' => '', |
| 310 | + 'import_thumbnail' => 'on', | |
| 311 | + 'import_images' => '', | |
| 199 | 312 | |
| 200 | 313 | // By default, only import Broadcasts as Posts for the last 30 days. |
| 201 | 314 | 'published_at_min_date' => gmdate( 'Y-m-d', strtotime( '-30 days' ) ), |
| 202 | 315 | |
| 203 | - 'restrict_content' => '', | |
| 316 | + 'enabled_export' => '', | |
| 204 | 317 | 'no_styles' => '', |
| 205 | 318 | ); |
| 206 | 319 | |
| 207 | 320 | /** |
| @@ -209,9 +322,9 @@ | ||
| 209 | 322 | * e.g. on a new installation. |
| 210 | 323 | * |
| 211 | 324 | * @since 2.2.9 |
| 212 | 325 | * |
| 213 | - * @param array $defaults | |
| 326 | + * @param array $defaults Default settings. | |
| 214 | 327 | */ |
| 215 | 328 | $defaults = apply_filters( 'convertkit_settings_broadcasts_get_defaults', $defaults ); |
| 216 | 329 | |
| 217 | 330 | return $defaults; |
| @@ -227,8 +340,29 @@ | ||
| 227 | 340 | */ |
| 228 | 341 | public function save( $settings ) { |
| 229 | 342 | |
| 230 | 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 ); | |
| 231 | 365 | |
| 232 | 366 | } |
| 233 | 367 | |
| 234 | 368 | } |