PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.8
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-section-broadcasts.php

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

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