PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.6
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.4.6, at admin/section/class-convertkit-admin-settings-broadcasts.php

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