PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.2
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-settings-general.php

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

599 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 General class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers General Settings that can be edited at Settings > ConvertKit > General.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Settings_General extends ConvertKit_Settings_Base {
16
17 /**
18 * Holds the API instance.
19 *
20 * @since 1.9.6
21 *
22 * @var ConvertKit_API
23 */
24 private $api;
25
26 /**
27 * Holds the ConvertKit Account Name.
28 *
29 * @since 1.9.6
30 *
31 * @var bool|WP_Error|array
32 */
33 private $account = false;
34
35 /**
36 * Holds the ConvertKit Forms Resource.
37 *
38 * @since 1.9.6
39 *
40 * @var bool|ConvertKit_Resource_Forms;
41 */
42 private $forms = false;
43
44 /**
45 * Constructor.
46 */
47 public function __construct() {
48
49 // Define the class that reads/writes settings.
50 $this->settings = new ConvertKit_Settings();
51
52 // Define the settings key.
53 $this->settings_key = $this->settings::SETTINGS_NAME;
54
55 // Define the programmatic name, Title and Tab Text.
56 $this->name = 'general';
57 $this->title = __( 'General Settings', 'convertkit' );
58 $this->tab_text = __( 'General', 'convertkit' );
59
60 // Enqueue scripts and CSS.
61 add_action( 'convertkit_admin_settings_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
62 add_action( 'convertkit_admin_settings_enqueue_styles', array( $this, 'enqueue_styles' ) );
63
64 // Render container element.
65 add_action( 'convertkit_settings_base_render_before', array( $this, 'render_before' ) );
66
67 parent::__construct();
68
69 }
70
71 /**
72 * Enqueues scripts for the Settings > General screen.
73 *
74 * @since 2.2.4
75 *
76 * @param string $section Settings section / tab (general|tools|restrict-content).
77 */
78 public function enqueue_scripts( $section ) {
79
80 // Bail if we're not on the general section.
81 if ( $section !== $this->name ) {
82 return;
83 }
84
85 // Enqueue Select2 JS.
86 convertkit_select2_enqueue_scripts();
87
88 // Enqueue Preview Output JS.
89 wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
90
91 }
92
93 /**
94 * Enqueues styles for the Settings > General screen.
95 *
96 * @since 2.2.4
97 *
98 * @param string $section Settings section / tab (general|tools|restrict-content).
99 */
100 public function enqueue_styles( $section ) {
101
102 // Bail if we're not on the general section.
103 if ( $section !== $this->name ) {
104 return;
105 }
106
107 // Enqueue Select2 CSS.
108 convertkit_select2_enqueue_styles();
109
110 }
111
112 /**
113 * Registers settings fields for this section.
114 */
115 public function register_fields() {
116
117 add_settings_field(
118 'account_name',
119 __( 'Account Name', 'convertkit' ),
120 array( $this, 'account_name_callback' ),
121 $this->settings_key,
122 $this->name
123 );
124
125 add_settings_field(
126 'api_key',
127 __( 'API Key', 'convertkit' ),
128 array( $this, 'api_key_callback' ),
129 $this->settings_key,
130 $this->name,
131 array(
132 'label_for' => 'api_key',
133 )
134 );
135
136 add_settings_field(
137 'api_secret',
138 __( 'API Secret', 'convertkit' ),
139 array( $this, 'api_secret_callback' ),
140 $this->settings_key,
141 $this->name,
142 array(
143 'label_for' => 'api_secret',
144 )
145 );
146
147 foreach ( convertkit_get_supported_post_types() as $supported_post_type ) {
148 // Get Post Type's Label.
149 $post_type = get_post_type_object( $supported_post_type );
150
151 // Skip if the Post Type doesn't exist.
152 if ( ! $post_type ) {
153 continue;
154 }
155
156 // Add Settings Field.
157 add_settings_field(
158 $supported_post_type . '_form',
159 sprintf(
160 /* translators: Post Type Name */
161 __( 'Default Form (%s)', 'convertkit' ),
162 $post_type->label
163 ),
164 array( $this, 'custom_post_types_callback' ),
165 $this->settings_key,
166 $this->name,
167 array(
168 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form',
169 'post_type' => $supported_post_type,
170 'post_type_object' => $post_type,
171 )
172 );
173 }
174
175 add_settings_field(
176 'non_inline_form',
177 __( 'Default Form (Site Wide)', 'convertkit' ),
178 array( $this, 'non_inline_form_callback' ),
179 $this->settings_key,
180 $this->name,
181 array(
182 'label_for' => 'non_inline_form',
183 )
184 );
185
186 add_settings_field(
187 'debug',
188 __( 'Debug', 'convertkit' ),
189 array( $this, 'debug_callback' ),
190 $this->settings_key,
191 $this->name,
192 array(
193 'label_for' => 'debug',
194 )
195 );
196
197 add_settings_field(
198 'no_scripts',
199 __( 'Disable JavaScript', 'convertkit' ),
200 array( $this, 'no_scripts_callback' ),
201 $this->settings_key,
202 $this->name,
203 array(
204 'label_for' => 'no_scripts',
205 )
206 );
207
208 add_settings_field(
209 'no_css',
210 __( 'Disable CSS', 'convertkit' ),
211 array( $this, 'no_css_callback' ),
212 $this->settings_key,
213 $this->name,
214 array(
215 'label_for' => 'no_css',
216 )
217 );
218
219 }
220
221 /**
222 * Prints help info for this section
223 */
224 public function print_section_info() {
225
226 ?>
227 <p><?php esc_html_e( 'Choosing a default form will embed it at the bottom of every post or page (in single view only) across your site.', 'convertkit' ); ?></p>
228 <p><?php esc_html_e( 'If you wish to turn off form embedding or select a different form for an individual post or page, you can do so using the ConvertKit meta box on the edit page.', 'convertkit' ); ?></p>
229 <p>
230 <?php
231 printf(
232 /* translators: [convertkit] shortcode, wrapped in <code> tags */
233 esc_html__( 'The default form can be inserted into the middle of post or page content by using the %s shortcode.', 'convertkit' ),
234 '<code>[convertkit]</code>'
235 );
236 ?>
237 </p>
238 <?php
239
240 }
241
242 /**
243 * Returns the URL for the ConvertKit documentation for this setting section.
244 *
245 * @since 2.0.8
246 *
247 * @return string Documentation URL.
248 */
249 public function documentation_url() {
250
251 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
252
253 }
254
255 /**
256 * Renders container divs for styling, and attempts to fetch the ConvertKit Account
257 * details if API credentials have been specified.
258 *
259 * @since 1.9.6
260 */
261 public function render_before() {
262
263 // Initialize the API if an API Key and Secret is defined.
264 if ( ! $this->settings->has_api_key_and_secret() ) {
265 return;
266 }
267
268 $this->api = new ConvertKit_API(
269 $this->settings->get_api_key(),
270 $this->settings->get_api_secret(),
271 $this->settings->debug_enabled(),
272 'settings'
273 );
274
275 // Get Account Details, which we'll use in account_name_callback(), but also lets us test
276 // whether the API credentials are valid.
277 $this->account = $this->api->account();
278
279 // Show an error message if Account Details could not be fetched e.g. API credentials supplied are invalid.
280 if ( is_wp_error( $this->account ) ) {
281 // Depending on the error code, maybe persist a notice in the WordPress Administration until the user
282 // fixes the problem.
283 switch ( $this->account->get_error_data( $this->account->get_error_code() ) ) {
284 case 401:
285 // API credentials are invalid.
286 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' );
287 break;
288 }
289
290 $this->output_error( $this->account->get_error_message() );
291 } else {
292 // Remove any existing persistent notice.
293 WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' );
294 }
295
296 }
297
298 /**
299 * Outputs the Account Name
300 *
301 * @since 1.9.6
302 */
303 public function account_name_callback() {
304
305 // Output a notice telling the user to enter their API Key and Secret if they haven't done so yet.
306 if ( ! $this->settings->has_api_key_and_secret() || is_wp_error( $this->account ) ) {
307 echo '<p class="description">' . esc_html__( 'Add a valid API Key and Secret to get started', 'convertkit' ) . '</p>';
308 return;
309 }
310
311 // Output Account Name.
312 $html = sprintf(
313 '<code>%s</code>',
314 isset( $this->account['name'] ) ? esc_attr( $this->account['name'] ) : esc_html__( '(Not specified)', 'convertkit' )
315 );
316 $html .= '<p class="description">' . esc_html__( 'The name of your connected ConvertKit account.', 'convertkit' ) . '</p>';
317
318 // Output has already been run through escaping functions above.
319 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
320 }
321
322 /**
323 * Renders the input for the API Key setting.
324 *
325 * @since 1.9.6
326 */
327 public function api_key_callback() {
328
329 // If the API Key is stored as a constant, it cannot be edited here.
330 if ( $this->settings->is_api_key_a_constant() ) {
331 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
332 $this->settings->get_api_key(),
333 esc_html__( 'Your API Key has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
334 );
335 return;
336 }
337
338 // Output field.
339 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
340 'api_key',
341 esc_attr( $this->settings->get_api_key() ),
342 array(
343 sprintf(
344 /* translators: %1$s: Link to ConvertKit Account */
345 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
346 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Key.', 'convertkit' ) . '</a>'
347 ),
348 sprintf(
349 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Key definition */
350 esc_html__( 'Alternatively specify your API Key in the %1$s file using %2$s', 'convertkit' ),
351 '<code>wp-config.php</code>',
352 '<code>define(\'CONVERTKIT_API_KEY\', \'your-api-key\');</code>'
353 ),
354 ),
355 array( 'regular-text', 'code' )
356 );
357
358 }
359
360 /**
361 * Renders the input for the API Secret setting.
362 *
363 * @since 1.9.6
364 */
365 public function api_secret_callback() {
366
367 // If the API Secret is stored as a constant, it cannot be edited here.
368 if ( $this->settings->is_api_secret_a_constant() ) {
369 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
370 $this->settings->get_api_secret(),
371 esc_html__( 'Your API Secret has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
372 );
373 return;
374 }
375
376 // Output field.
377 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
378 'api_secret',
379 esc_attr( $this->settings->get_api_secret() ),
380 array(
381 sprintf(
382 /* translators: %1$s: Link to ConvertKit Account */
383 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
384 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Secret.', 'convertkit' ) . '</a>'
385 ),
386 sprintf(
387 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Secret definition */
388 esc_html__( 'Alternatively specify your API Secret in the %1$s file using %2$s', 'convertkit' ),
389 '<code>wp-config.php</code>',
390 '<code>define(\'CONVERTKIT_API_SECRET\', \'your-api-secret\');</code>'
391 ),
392 ),
393 array( 'regular-text', 'code' )
394 );
395
396 }
397
398 /**
399 * Renders the input for the Default Form setting for the given Post Type.
400 *
401 * @since 1.9.6
402 *
403 * @param array $args Field arguments.
404 */
405 public function custom_post_types_callback( $args ) {
406
407 // Refresh Forms.
408 if ( ! $this->forms ) {
409 $this->forms = new ConvertKit_Resource_Forms( 'settings' );
410 $this->forms->refresh();
411
412 // Also refresh Landing Pages, Tags and Posts. Whilst not displayed in the Plugin Settings, this ensures up to date
413 // lists are stored for when editing e.g. Pages.
414 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'settings' );
415 $landing_pages->refresh();
416
417 $posts = new ConvertKit_Resource_Posts( 'settings' );
418 $posts->refresh();
419
420 $products = new ConvertKit_Resource_Products( 'settings' );
421 $products->refresh();
422
423 $tags = new ConvertKit_Resource_Tags( 'settings' );
424 $tags->refresh();
425 }
426
427 // Bail if no Forms exist.
428 if ( ! $this->forms->exist() ) {
429 esc_html_e( 'No Forms exist in ConvertKit.', 'convertkit' );
430 echo '<br /><a href="' . esc_url( convertkit_get_new_form_url() ) . '" target="_blank">' . esc_html__( 'Click here to create your first form', 'convertkit' ) . '</a>';
431 return;
432 }
433
434 // Build description with preview link.
435 $description = false;
436 $preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_url( $args['post_type'] );
437 if ( $preview_url ) {
438 // Include a preview link in the description.
439 $description = sprintf(
440 '%s %s %s',
441 sprintf(
442 /* translators: Post Type name, plural */
443 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
444 $args['post_type_object']->label
445 ),
446 '<a href="' . esc_url( $preview_url ) . '" id="convertkit-preview-form-' . esc_attr( $args['post_type'] ) . '" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
447 esc_html__( 'to preview how this will display.', 'convertkit' )
448 );
449 } else {
450 // Just output the field's description.
451 $description = sprintf(
452 /* translators: Post Type name, plural */
453 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
454 $args['post_type_object']->label
455 );
456 }
457
458 // Build field.
459 $select_field = $this->forms->get_select_field_all(
460 $this->settings_key . '[' . $args['post_type'] . '_form]',
461 $this->settings_key . '_' . $args['post_type'] . '_form',
462 array(
463 'convertkit-select2',
464 'convertkit-preview-output-link',
465 ),
466 $this->settings->get_default_form( $args['post_type'] ),
467 array(
468 'default' => esc_html__( 'None', 'convertkit' ),
469 ),
470 array(
471 'data-target' => '#convertkit-preview-form-' . esc_attr( $args['post_type'] ),
472 'data-link' => esc_attr( $preview_url ) . '&convertkit_form_id=',
473 ),
474 $description
475 );
476
477 // Output field.
478 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
479
480 }
481
482
483 /**
484 * Renders the input for the Non-inline Form setting.
485 *
486 * @since 2.2.3
487 *
488 * @param array $args Field arguments.
489 */
490 public function non_inline_form_callback( $args ) {
491
492 // Bail if no non-inline Forms exist.
493 if ( ! $this->forms->non_inline_exist() ) {
494 esc_html_e( 'No non-inline Forms exist in ConvertKit.', 'convertkit' );
495 echo '<br /><a href="' . esc_url( convertkit_get_new_form_url() ) . '" target="_blank">' . esc_html__( 'Click here to create your first modal, slide in or sticky bar form', 'convertkit' ) . '</a>';
496 return;
497 }
498
499 // Build description with preview link.
500 $preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_home_url();
501 $description = sprintf(
502 '%s %s %s',
503 esc_html__( 'Select a non-inline modal, slide in or sticky bar form to automatically display site wide. Ignored if a non-inline form is specified in Default Form settings above, individual Post / Page settings, or any block / shortcode.', 'convertkit' ),
504 '<a href="' . esc_url( $preview_url ) . '" id="convertkit-preview-non-inline-form" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
505 esc_html__( 'to preview how this will display.', 'convertkit' )
506 );
507
508 // Build field.
509 $select_field = $this->forms->get_select_field_non_inline(
510 $this->settings_key . '[non_inline_form]',
511 $this->settings_key . '_non_inline_form',
512 array(
513 'convertkit-select2',
514 'convertkit-preview-output-link',
515 ),
516 $this->settings->get_non_inline_form(),
517 array(
518 '' => esc_html__( 'None', 'convertkit' ),
519 ),
520 array(
521 'data-target' => '#convertkit-preview-non-inline-form',
522 'data-link' => esc_attr( $preview_url ) . '&convertkit_form_id=',
523 ),
524 $description
525 );
526
527 // Output field.
528 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
529
530 }
531
532 /**
533 * Renders the input for the Debug setting.
534 *
535 * @since 1.9.6
536 */
537 public function debug_callback() {
538
539 // Output field.
540 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
541 'debug',
542 'on',
543 $this->settings->debug_enabled(), // phpcs:ignore WordPress.Security.EscapeOutput
544 esc_html__( 'Log requests to file and output browser console messages.', 'convertkit' ),
545 esc_html__( 'You can ignore this unless you\'re working with our support team to resolve an issue. Decheck this option to improve performance.', 'convertkit' )
546 );
547
548 }
549
550 /**
551 * Renders the input for the Disable Javascript setting.
552 *
553 * @since 1.9.6
554 */
555 public function no_scripts_callback() {
556
557 // Output field.
558 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
559 'no_scripts',
560 'on',
561 $this->settings->scripts_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
562 esc_html__( 'Prevent plugin from loading JavaScript files. This will disable the custom content and tagging features of the plugin. Does not apply to landing pages. Use with caution!', 'convertkit' )
563 );
564
565 }
566
567 /**
568 * Renders the input for the Disable CSS setting.
569 *
570 * @since 1.9.6.9
571 */
572 public function no_css_callback() {
573
574 // Output field.
575 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
576 'no_css',
577 'on',
578 $this->settings->css_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
579 esc_html__( 'Prevents loading plugin CSS files. This will disable styling on broadcasts, form trigger buttons, product buttons and member\'s content. Use with caution!', 'convertkit' ),
580 array(
581 sprintf(
582 '%s <a href="%s" target="_blank">%s</a>',
583 esc_html__( 'To customize forms and their styling, use the', 'convertkit' ),
584 esc_url( convertkit_get_form_editor_url() ),
585 esc_html__( 'ConvertKit form editor', 'convertkit' )
586 ),
587 sprintf(
588 '%s <a href="https://wordpress.org/plugins/contact-form-7/" target="_blank">Contact Form 7</a>, <a href="https://wordpress.org/plugins/convertkit-gravity-forms/" target="_blank">Gravity Forms</a> %s <a href="https://wordpress.org/plugins/integrate-convertkit-wpforms/" target="_blank">WPForms</a> %s',
589 esc_html__( 'For developers who require custom form designs through use of CSS, consider using the', 'convertkit' ),
590 esc_html__( 'or', 'convertkit' ),
591 esc_html__( 'integrations.', 'convertkit' )
592 ),
593 )
594 );
595
596 }
597
598 }
599