PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.9
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.9
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / section / class-convertkit-admin-settings-broadcasts.php

class-convertkit-admin-settings-broadcasts.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.5.9, at admin/section/class-convertkit-admin-settings-broadcasts.php

586 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Settings Broadcasts class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers Broadcasts Settings that can be edited at Settings > ConvertKit > Broadcasts.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Admin_Settings_Broadcasts extends ConvertKit_Settings_Base {
16
17 /**
18 * Constructor.
19 *
20 * @since 2.2.9
21 */
22 public function __construct() {
23
24 // Define the class that reads/writes settings.
25 $this->settings = new ConvertKit_Settings_Broadcasts();
26
27 // Define the settings key.
28 $this->settings_key = $this->settings::SETTINGS_NAME;
29
30 // Define the programmatic name, Title and Tab Text.
31 $this->name = 'broadcasts';
32 $this->title = __( 'Broadcasts', 'convertkit' );
33 $this->tab_text = __( 'Broadcasts', 'convertkit' );
34
35 // Identify that this is beta functionality.
36 $this->is_beta = true;
37
38 // Register and maybe output notices for this settings screen.
39 if ( $this->on_settings_screen( $this->name ) ) {
40 add_filter( 'convertkit_settings_base_register_notices', array( $this, 'register_notices' ) );
41 add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) );
42 }
43
44 // Enqueue scripts and CSS.
45 add_action( 'convertkit_admin_settings_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
46 add_action( 'convertkit_admin_settings_enqueue_styles', array( $this, 'enqueue_styles' ) );
47
48 parent::__construct();
49
50 $this->maybe_import_now();
51
52 }
53
54 /**
55 * Registers success and error notices for the Tools screen, to be displayed
56 * depending on the action.
57 *
58 * @since 2.5.1
59 *
60 * @param array $notices Regsitered success and error notices.
61 * @return array
62 */
63 public function register_notices( $notices ) {
64
65 return array_merge(
66 $notices,
67 array(
68 'broadcast_import_error' => __( 'Broadcasts import failed. Please try again.', 'convertkit' ),
69 'broadcast_import_success' => __( 'Broadcasts import started. Check the Posts screen shortly to confirm Broadcasts imported successfully.', 'convertkit' ),
70 )
71 );
72
73 }
74
75 /**
76 * Import Broadcasts now, if requested through the UI.
77 *
78 * @since 2.2.9
79 */
80 private function maybe_import_now() {
81
82 // Bail if nonce verification fails.
83 if ( ! isset( $_REQUEST['_convertkit_settings_broadcasts_nonce'] ) ) {
84 return false;
85 }
86 if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_broadcasts_nonce'] ), 'convertkit-settings-broadcasts' ) ) {
87 return false;
88 }
89
90 // Run the import task through WordPress' Cron system now.
91 $cron = new ConvertKit_Cron();
92 $result = $cron->run( 'convertkit_resource_refresh_posts' );
93
94 // If an error occured, show it now.
95 if ( is_wp_error( $result ) ) {
96 // Redirect to Broadcasts screen with error.
97 $this->redirect_with_error_description( $result->get_error_message() );
98 return;
99 }
100
101 // If here, the task scheduled.
102 // Redirect with success notice.
103 $this->redirect_with_success_notice( 'broadcast_import_success' );
104
105 }
106
107 /**
108 * Enqueues scripts for the Settings > Broadcasts screen.
109 *
110 * @since 2.2.9
111 *
112 * @param string $section Settings section / tab (general|tools|restrict-content|broadcasts).
113 */
114 public function enqueue_scripts( $section ) {
115
116 // Bail if we're not on the Broadcasts section.
117 if ( $section !== $this->name ) {
118 return;
119 }
120
121 // Enqueue Select2 JS.
122 convertkit_select2_enqueue_scripts();
123
124 // Enqueue JS.
125 wp_enqueue_script( 'convertkit-admin-settings-conditional-display', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/settings-conditional-display.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
126
127 }
128
129 /**
130 * Enqueues styles for the Settings > General screen.
131 *
132 * @since 2.2.9
133 *
134 * @param string $section Settings section / tab (general|tools|restrict-content|broadcasts).
135 */
136 public function enqueue_styles( $section ) {
137
138 // Bail if we're not on the Broadcasts section.
139 if ( $section !== $this->name ) {
140 return;
141 }
142
143 // Enqueue Select2 CSS.
144 convertkit_select2_enqueue_styles();
145
146 }
147
148 /**
149 * Registers settings fields for this section.
150 *
151 * @since 2.2.9
152 */
153 public function register_fields() {
154
155 // Check if DOMDocument is installed.
156 // It should be installed as mosts hosts include php-dom and php-xml modules.
157 // If not, disable Broadcast to Posts import functionality as we can't parse
158 // imported Broadcasts.
159 if ( ! class_exists( 'DOMDocument' ) ) {
160 // Disable saving settings.
161 $this->save_disabled = true;
162
163 // Return if we're not on the Plugin settings screen.
164 if ( ! $this->on_settings_screen( 'broadcasts' ) ) {
165 return;
166 }
167
168 // Output a notice if we're on the Broadcasts settings screen.
169 $this->output_error(
170 __( 'Importing public broadcasts from ConvertKit requires the PHP extensions `php-dom` and `php-xml` to be installed. Work with your web host to do this, and reload the page when done.', 'convertkit' )
171 );
172 return;
173 }
174
175 // Initialize classes that will be used.
176 $posts = new ConvertKit_Resource_Posts( 'cron' );
177
178 // Define description for the 'Enabled' setting.
179 // If enabled, include the next scheduled date and time the Plugin will import broadcasts.
180 // If the next scheduled timestamp is 1, the event is running now.
181 $enabled_description = '';
182 if ( $this->settings->enabled() && $posts->get_cron_event_next_scheduled() && $posts->get_cron_event_next_scheduled() > 1 ) {
183 $enabled_description = sprintf(
184 '%s %s<br />%s <strong>%s</strong> %s',
185 esc_html__( 'Broadcasts will next import at approximately ', 'convertkit' ),
186 // The cron event's next scheduled timestamp is always in UTC.
187 // Display it converted to the WordPress site's timezone.
188 get_date_from_gmt(
189 gmdate( 'Y-m-d H:i:s', $posts->get_cron_event_next_scheduled() ),
190 get_option( 'date_format' ) . ' ' . get_option( 'time_format' )
191 ),
192 esc_html__( 'Broadcasts', 'convertkit' ),
193 esc_html__( 'must', 'convertkit' ),
194 esc_html__( 'have their "Enabled on public feeds" setting enabled in ConvertKit, to be eligible for import.', 'convertkit' )
195 );
196 }
197
198 add_settings_field(
199 'enabled',
200 __( 'Enable Automatic Import', 'convertkit' ),
201 array( $this, 'enable_callback' ),
202 $this->settings_key,
203 $this->name,
204 array(
205 'name' => 'enabled',
206 'label_for' => 'enabled',
207 'label' => __( 'Enables automatic publication of public ConvertKit Broadcasts as WordPress Posts.', 'convertkit' ),
208 'description' => $enabled_description,
209 )
210 );
211
212 // Render import button if the feature is enabled.
213 if ( $this->settings->enabled() && $posts->get_cron_event_next_scheduled() ) {
214 add_settings_field(
215 'import_button',
216 '',
217 array( $this, 'import_button_callback' ),
218 $this->settings_key,
219 $this->name
220 );
221 }
222
223 add_settings_field(
224 'post_status',
225 __( 'Status', 'convertkit' ),
226 array( $this, 'post_status_callback' ),
227 $this->settings_key,
228 $this->name,
229 array(
230 'name' => 'post_status',
231 'label_for' => 'post_status',
232 'description' => __( 'The WordPress Post status to assign imported public broadcasts to.', 'convertkit' ),
233 )
234 );
235
236 add_settings_field(
237 'author_id',
238 __( 'Author', 'convertkit' ),
239 array( $this, 'author_id_callback' ),
240 $this->settings_key,
241 $this->name,
242 array(
243 'name' => 'author_id',
244 'label_for' => 'author_id',
245 'description' => __( 'The WordPress User to set as the author for WordPress Posts created from imported public broadcasts.', 'convertkit' ),
246 )
247 );
248
249 add_settings_field(
250 'category_id',
251 __( 'Category', 'convertkit' ),
252 array( $this, 'category_callback' ),
253 $this->settings_key,
254 $this->name,
255 array(
256 'name' => 'category_id',
257 'label_for' => 'category_id',
258 'description' => __( 'The category to assign imported public broadcasts to.', 'convertkit' ),
259 )
260 );
261
262 add_settings_field(
263 'import_thumbnail',
264 __( 'Include Thumbnail', 'convertkit' ),
265 array( $this, 'import_thumbnail_callback' ),
266 $this->settings_key,
267 $this->name,
268 array(
269 'name' => 'import_thumbnail',
270 'label_for' => 'import_thumbnail',
271 'label' => __( 'If enabled, the Broadcast\'s thumbnail will be used as the WordPress Post\'s featured image.', 'convertkit' ),
272 'description' => '',
273 )
274 );
275
276 add_settings_field(
277 'published_at_min_date',
278 __( 'Earliest Date', 'convertkit' ),
279 array( $this, 'date_callback' ),
280 $this->settings_key,
281 $this->name,
282 array(
283 'name' => 'published_at_min_date',
284 'label_for' => 'published_at_min_date',
285 'description' => __( 'The earliest date to import public broadcasts from, based on the broadcast\'s published date and time.', 'convertkit' ),
286 )
287 );
288
289 add_settings_field(
290 'enabled_export',
291 __( 'Enable Export Actions', 'convertkit' ),
292 array( $this, 'enable_export_callback' ),
293 $this->settings_key,
294 $this->name,
295 array(
296 'name' => 'enabled_export',
297 'label_for' => 'enabled_export',
298 'label' => __( 'Displays actions in WordPress to create draft broadcasts from existing WordPress posts.', 'convertkit' ),
299 )
300 );
301
302 add_settings_field(
303 'no_styles',
304 __( 'Disable Styles', 'convertkit' ),
305 array( $this, 'no_styles_callback' ),
306 $this->settings_key,
307 $this->name,
308 array(
309 'name' => 'no_styles',
310 'label_for' => 'no_styles',
311 'description' => __( 'Removes inline styles and layout when importing broadcasts and exporting posts.', 'convertkit' ),
312 )
313 );
314
315 }
316
317 /**
318 * Prints help info for this section
319 *
320 * @since 2.2.9
321 */
322 public function print_section_info() {
323
324 ?>
325 <span class="convertkit-beta-label"><?php esc_html_e( 'Beta', 'convertkit' ); ?></span>
326 <p class="description"><?php esc_html_e( 'Defines whether public broadcasts ("Enabled on public feeds") in ConvertKit should automatically be published on this site as WordPress Posts, and whether to enable options to create draft ConvertKit Broadcasts from WordPress Posts.', 'convertkit' ); ?></p>
327 <?php
328
329 }
330
331
332 /**
333 * Returns the URL for the ConvertKit documentation for this setting section.
334 *
335 * @since 2.2.9
336 *
337 * @return string Documentation URL.
338 */
339 public function documentation_url() {
340
341 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
342
343 }
344
345 /**
346 * Renders the input for the Enable setting.
347 *
348 * @since 2.2.9
349 *
350 * @param array $args Setting field arguments (name,description).
351 */
352 public function enable_callback( $args ) {
353
354 // Output field.
355 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
356 $args['name'],
357 'on',
358 $this->settings->enabled(), // phpcs:ignore WordPress.Security.EscapeOutput
359 $args['label'], // phpcs:ignore WordPress.Security.EscapeOutput
360 $args['description'] // phpcs:ignore WordPress.Security.EscapeOutput
361 );
362
363 }
364
365 /**
366 * Renders the import button.
367 *
368 * @since 2.2.9
369 */
370 public function import_button_callback() {
371
372 // Define link to import Broadcasts now.
373 $import_url = add_query_arg(
374 array(
375 'page' => '_wp_convertkit_settings',
376 'tab' => 'broadcasts',
377 '_convertkit_settings_broadcasts_nonce' => wp_create_nonce( 'convertkit-settings-broadcasts' ),
378 ),
379 'options-general.php'
380 );
381
382 echo '<a href="' . esc_url( $import_url ) . '" class="button button-secondary enabled">' . esc_html__( 'Import now', 'convertkit' ) . '</a>';
383
384 }
385
386 /**
387 * Renders the input for the status setting.
388 *
389 * @since 2.3.4
390 *
391 * @param array $args Setting field arguments (name,description).
392 */
393 public function post_status_callback( $args ) {
394
395 // Build field.
396 $select_field = $this->get_select_field(
397 $args['name'],
398 $this->settings->post_status(),
399 get_post_statuses(),
400 $args['description'],
401 array(
402 'enabled',
403 'convertkit-select2',
404 )
405 );
406
407 // Output field.
408 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
409
410 }
411
412 /**
413 * Renders the input for the author setting.
414 *
415 * @since 2.3.9
416 *
417 * @param array $args Setting field arguments (name,description).
418 */
419 public function author_id_callback( $args ) {
420
421 // Build field.
422 $select_field = wp_dropdown_users(
423 array(
424 'echo' => false,
425 'selected' => $this->settings->author_id(),
426 'include_selected' => true,
427 'name' => $this->settings_key . '[' . $args['name'] . ']',
428 'id' => $this->settings_key . '_' . $args['name'],
429 'class' => 'enabled convertkit-select2',
430 )
431 );
432
433 // Output field.
434 echo '<div class="convertkit-select2-container">' . $select_field . '</div>' . $this->get_description( $args['description'] ); // phpcs:ignore WordPress.Security.EscapeOutput
435
436 }
437
438 /**
439 * Renders the input for the category setting.
440 *
441 * @since 2.2.9
442 *
443 * @param array $args Setting field arguments (name,description).
444 */
445 public function category_callback( $args ) {
446
447 // Build field.
448 $select_field = wp_dropdown_categories(
449 array(
450 'show_option_none' => __( 'None', 'convertkit' ),
451 'echo' => 0,
452 'hierarhical' => 1,
453 'name' => $this->settings_key . '[' . $args['name'] . ']',
454 'id' => $this->settings_key . '_' . $args['name'],
455 'class' => 'convertkit-select2 enabled',
456 'selected' => $this->settings->category_id(),
457 'taxonomy' => 'category',
458 'hide_empty' => false,
459 )
460 );
461
462 // Output field.
463 echo '<div class="convertkit-select2-container">' . $select_field . '</div>' . $this->get_description( $args['description'] ); // phpcs:ignore WordPress.Security.EscapeOutput
464
465 }
466
467 /**
468 * Renders the input for the Import Thumbnail setting.
469 *
470 * @since 2.4.1
471 *
472 * @param array $args Setting field arguments (name,description).
473 */
474 public function import_thumbnail_callback( $args ) {
475
476 // Output field.
477 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
478 $args['name'],
479 'on',
480 $this->settings->import_thumbnail(), // phpcs:ignore WordPress.Security.EscapeOutput
481 $args['label'], // phpcs:ignore WordPress.Security.EscapeOutput
482 $args['description'], // phpcs:ignore WordPress.Security.EscapeOutput
483 array(
484 'enabled',
485 )
486 );
487
488 }
489
490 /**
491 * Renders the input for the date setting.
492 *
493 * @since 2.2.9
494 *
495 * @param array $args Setting field arguments (name,description).
496 */
497 public function date_callback( $args ) {
498
499 // Output field.
500 echo $this->get_date_field( // phpcs:ignore WordPress.Security.EscapeOutput
501 $args['name'],
502 esc_attr( $this->settings->published_at_min_date() ),
503 $args['description'], // phpcs:ignore WordPress.Security.EscapeOutput
504 array(
505 'enabled',
506 )
507 );
508
509 }
510
511 /**
512 * Renders the input for the Enable Export setting.
513 *
514 * @since 2.4.0
515 *
516 * @param array $args Setting field arguments (name,description).
517 */
518 public function enable_export_callback( $args ) {
519
520 // Output field.
521 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
522 $args['name'],
523 'on',
524 $this->settings->enabled_export(), // phpcs:ignore WordPress.Security.EscapeOutput
525 $args['label'] // phpcs:ignore WordPress.Security.EscapeOutput
526 );
527
528 }
529
530 /**
531 * Renders the input for the No Styles setting.
532 *
533 * @since 2.2.9
534 *
535 * @param array $args Setting field arguments (name,description).
536 */
537 public function no_styles_callback( $args ) {
538
539 // Output field.
540 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
541 $args['name'],
542 'on',
543 $this->settings->no_styles(), // phpcs:ignore WordPress.Security.EscapeOutput
544 $args['description'] // phpcs:ignore WordPress.Security.EscapeOutput
545 );
546
547 }
548
549 /**
550 * Sanitizes the settings prior to being saved.
551 *
552 * @since 2.4.1
553 *
554 * @param array $settings Submitted Settings Fields.
555 * @return array Sanitized Settings with Defaults
556 */
557 public function sanitize_settings( $settings ) {
558
559 // If the 'Include Thumbnail' setting isn't checked, it won't be included
560 // in the array of settings, and the defaults will enable this.
561 // Therefore, if the setting doesn't exist, set it to blank.
562 if ( ! array_key_exists( 'import_thumbnail', $settings ) ) {
563 $settings['import_thumbnail'] = '';
564 }
565
566 // Merge settings with defaults.
567 $settings = wp_parse_args( $settings, $this->settings->get_defaults() );
568
569 // Return settings to be saved.
570 return $settings;
571
572 }
573
574 }
575
576 // Bootstrap.
577 add_action(
578 'convertkit_admin_settings_register_sections',
579 function ( $sections ) {
580
581 $sections['broadcasts'] = new ConvertKit_Admin_Settings_Broadcasts();
582 return $sections;
583
584 }
585 );
586