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

543 lines 15.8 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 'debug',
177 __( 'Debug', 'convertkit' ),
178 array( $this, 'debug_callback' ),
179 $this->settings_key,
180 $this->name,
181 array(
182 'label_for' => 'debug',
183 )
184 );
185
186 add_settings_field(
187 'no_scripts',
188 __( 'Disable JavaScript', 'convertkit' ),
189 array( $this, 'no_scripts_callback' ),
190 $this->settings_key,
191 $this->name,
192 array(
193 'label_for' => 'no_scripts',
194 )
195 );
196
197 add_settings_field(
198 'no_css',
199 __( 'Disable CSS', 'convertkit' ),
200 array( $this, 'no_css_callback' ),
201 $this->settings_key,
202 $this->name,
203 array(
204 'label_for' => 'no_css',
205 )
206 );
207
208 }
209
210 /**
211 * Prints help info for this section
212 */
213 public function print_section_info() {
214
215 ?>
216 <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>
217 <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>
218 <p>
219 <?php
220 printf(
221 /* translators: [convertkit] shortcode, wrapped in <code> tags */
222 esc_html__( 'The default form can be inserted into the middle of post or page content by using the %s shortcode.', 'convertkit' ),
223 '<code>[convertkit]</code>'
224 );
225 ?>
226 </p>
227 <?php
228
229 }
230
231 /**
232 * Returns the URL for the ConvertKit documentation for this setting section.
233 *
234 * @since 2.0.8
235 *
236 * @return string Documentation URL.
237 */
238 public function documentation_url() {
239
240 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
241
242 }
243
244 /**
245 * Renders container divs for styling, and attempts to fetch the ConvertKit Account
246 * details if API credentials have been specified.
247 *
248 * @since 1.9.6
249 */
250 public function render_before() {
251
252 // Initialize the API if an API Key and Secret is defined.
253 if ( ! $this->settings->has_api_key_and_secret() ) {
254 return;
255 }
256
257 $this->api = new ConvertKit_API(
258 $this->settings->get_api_key(),
259 $this->settings->get_api_secret(),
260 $this->settings->debug_enabled(),
261 'settings'
262 );
263
264 // Get Account Details, which we'll use in account_name_callback(), but also lets us test
265 // whether the API credentials are valid.
266 $this->account = $this->api->account();
267
268 // Show an error message if Account Details could not be fetched e.g. API credentials supplied are invalid.
269 if ( is_wp_error( $this->account ) ) {
270 // Depending on the error code, maybe persist a notice in the WordPress Administration until the user
271 // fixes the problem.
272 switch ( $this->account->get_error_data( $this->account->get_error_code() ) ) {
273 case 401:
274 // API credentials are invalid.
275 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' );
276 break;
277 }
278
279 $this->output_error( $this->account->get_error_message() );
280 } else {
281 // Remove any existing persistent notice.
282 WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' );
283 }
284
285 }
286
287 /**
288 * Outputs the Account Name
289 *
290 * @since 1.9.6
291 */
292 public function account_name_callback() {
293
294 // Output a notice telling the user to enter their API Key and Secret if they haven't done so yet.
295 if ( ! $this->settings->has_api_key_and_secret() || is_wp_error( $this->account ) ) {
296 echo '<p class="description">' . esc_html__( 'Add a valid API Key and Secret to get started', 'convertkit' ) . '</p>';
297 return;
298 }
299
300 // Output Account Name.
301 $html = sprintf(
302 '<code>%s</code>',
303 isset( $this->account['name'] ) ? esc_attr( $this->account['name'] ) : esc_html__( '(Not specified)', 'convertkit' )
304 );
305 $html .= '<p class="description">' . esc_html__( 'The name of your connected ConvertKit account.', 'convertkit' ) . '</p>';
306
307 // Output has already been run through escaping functions above.
308 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
309 }
310
311 /**
312 * Renders the input for the API Key setting.
313 *
314 * @since 1.9.6
315 */
316 public function api_key_callback() {
317
318 // If the API Key is stored as a constant, it cannot be edited here.
319 if ( $this->settings->is_api_key_a_constant() ) {
320 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
321 $this->settings->get_api_key(),
322 esc_html__( 'Your API Key has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
323 );
324 return;
325 }
326
327 // Output field.
328 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
329 'api_key',
330 esc_attr( $this->settings->get_api_key() ),
331 array(
332 sprintf(
333 /* translators: %1$s: Link to ConvertKit Account */
334 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
335 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Key.', 'convertkit' ) . '</a>'
336 ),
337 sprintf(
338 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Key definition */
339 esc_html__( 'Alternatively specify your API Key in the %1$s file using %2$s', 'convertkit' ),
340 '<code>wp-config.php</code>',
341 '<code>define(\'CONVERTKIT_API_KEY\', \'your-api-key\');</code>'
342 ),
343 ),
344 array( 'regular-text', 'code' )
345 );
346
347 }
348
349 /**
350 * Renders the input for the API Secret setting.
351 *
352 * @since 1.9.6
353 */
354 public function api_secret_callback() {
355
356 // If the API Secret is stored as a constant, it cannot be edited here.
357 if ( $this->settings->is_api_secret_a_constant() ) {
358 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
359 $this->settings->get_api_secret(),
360 esc_html__( 'Your API Secret has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
361 );
362 return;
363 }
364
365 // Output field.
366 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
367 'api_secret',
368 esc_attr( $this->settings->get_api_secret() ),
369 array(
370 sprintf(
371 /* translators: %1$s: Link to ConvertKit Account */
372 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
373 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Secret.', 'convertkit' ) . '</a>'
374 ),
375 sprintf(
376 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Secret definition */
377 esc_html__( 'Alternatively specify your API Secret in the %1$s file using %2$s', 'convertkit' ),
378 '<code>wp-config.php</code>',
379 '<code>define(\'CONVERTKIT_API_SECRET\', \'your-api-secret\');</code>'
380 ),
381 ),
382 array( 'regular-text', 'code' )
383 );
384
385 }
386
387 /**
388 * Renders the input for the Default Form setting for the given Post Type.
389 *
390 * @since 1.9.6
391 *
392 * @param array $args Field arguments.
393 */
394 public function custom_post_types_callback( $args ) {
395
396 // Refresh Forms.
397 if ( ! $this->forms ) {
398 $this->forms = new ConvertKit_Resource_Forms( 'settings' );
399 $this->forms->refresh();
400
401 // Also refresh Landing Pages, Tags and Posts. Whilst not displayed in the Plugin Settings, this ensures up to date
402 // lists are stored for when editing e.g. Pages.
403 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'settings' );
404 $landing_pages->refresh();
405
406 $posts = new ConvertKit_Resource_Posts( 'settings' );
407 $posts->refresh();
408
409 $products = new ConvertKit_Resource_Products( 'settings' );
410 $products->refresh();
411
412 $tags = new ConvertKit_Resource_Tags( 'settings' );
413 $tags->refresh();
414 }
415
416 // Bail if no Forms exist.
417 if ( ! $this->forms->exist() ) {
418 esc_html_e( 'No Forms exist in ConvertKit.', 'convertkit' );
419 echo '<br /><a href="' . esc_url( convertkit_get_new_form_url() ) . '" target="_blank">' . esc_html__( 'Click here to create your first form', 'convertkit' ) . '</a>';
420 return;
421 }
422
423 // Build array of select options.
424 $options = array(
425 'default' => esc_html__( 'None', 'convertkit' ),
426 );
427 foreach ( $this->forms->get() as $form ) {
428 $options[ esc_attr( $form['id'] ) ] = esc_html( $form['name'] );
429 }
430
431 // Build description with preview link.
432 $description = false;
433 $preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_url( $args['post_type'] );
434 if ( $preview_url ) {
435 // Include a preview link in the description.
436 $description = sprintf(
437 '%s %s %s',
438 sprintf(
439 /* translators: Post Type name, plural */
440 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
441 $args['post_type_object']->label
442 ),
443 '<a href="' . esc_url( $preview_url ) . '" id="convertkit-preview-form-' . esc_attr( $args['post_type'] ) . '" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
444 esc_html__( 'to preview how this will display.', 'convertkit' )
445 );
446 } else {
447 // Just output the field's description.
448 $description = sprintf(
449 /* translators: Post Type name, plural */
450 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
451 $args['post_type_object']->label
452 );
453 }
454
455 // Build field.
456 $select_field = $this->get_select_field(
457 $args['post_type'] . '_form',
458 $this->settings->get_default_form( $args['post_type'] ),
459 $options,
460 $description,
461 array(
462 'convertkit-select2',
463 'convertkit-preview-output-link',
464 ),
465 array(
466 'data-target' => '#convertkit-preview-form-' . esc_attr( $args['post_type'] ),
467 'data-link' => esc_attr( $preview_url ) . '&convertkit_form_id=',
468 )
469 );
470
471 // Output field.
472 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
473
474 }
475
476 /**
477 * Renders the input for the Debug setting.
478 *
479 * @since 1.9.6
480 */
481 public function debug_callback() {
482
483 // Output field.
484 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
485 'debug',
486 'on',
487 $this->settings->debug_enabled(), // phpcs:ignore WordPress.Security.EscapeOutput
488 esc_html__( 'Log requests to file and output browser console messages.', 'convertkit' ),
489 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' )
490 );
491
492 }
493
494 /**
495 * Renders the input for the Disable Javascript setting.
496 *
497 * @since 1.9.6
498 */
499 public function no_scripts_callback() {
500
501 // Output field.
502 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
503 'no_scripts',
504 'on',
505 $this->settings->scripts_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
506 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' )
507 );
508
509 }
510
511 /**
512 * Renders the input for the Disable CSS setting.
513 *
514 * @since 1.9.6.9
515 */
516 public function no_css_callback() {
517
518 // Output field.
519 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
520 'no_css',
521 'on',
522 $this->settings->css_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
523 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' ),
524 array(
525 sprintf(
526 '%s <a href="%s" target="_blank">%s</a>',
527 esc_html__( 'To customize forms and their styling, use the', 'convertkit' ),
528 esc_url( convertkit_get_form_editor_url() ),
529 esc_html__( 'ConvertKit form editor', 'convertkit' )
530 ),
531 sprintf(
532 '%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',
533 esc_html__( 'For developers who require custom form designs through use of CSS, consider using the', 'convertkit' ),
534 esc_html__( 'or', 'convertkit' ),
535 esc_html__( 'integrations.', 'convertkit' )
536 ),
537 )
538 );
539
540 }
541
542 }
543