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

502 lines 14.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 // Render container element.
61 add_action( 'convertkit_settings_base_render_before', array( $this, 'render_before' ) );
62
63 parent::__construct();
64
65 }
66
67 /**
68 * Registers settings fields for this section.
69 */
70 public function register_fields() {
71
72 add_settings_field(
73 'account_name',
74 __( 'Account Name', 'convertkit' ),
75 array( $this, 'account_name_callback' ),
76 $this->settings_key,
77 $this->name
78 );
79
80 add_settings_field(
81 'api_key',
82 __( 'API Key', 'convertkit' ),
83 array( $this, 'api_key_callback' ),
84 $this->settings_key,
85 $this->name,
86 array(
87 'label_for' => 'api_key',
88 )
89 );
90
91 add_settings_field(
92 'api_secret',
93 __( 'API Secret', 'convertkit' ),
94 array( $this, 'api_secret_callback' ),
95 $this->settings_key,
96 $this->name,
97 array(
98 'label_for' => 'api_secret',
99 )
100 );
101
102 foreach ( convertkit_get_supported_post_types() as $supported_post_type ) {
103 // Get Post Type's Label.
104 $post_type = get_post_type_object( $supported_post_type );
105
106 // Skip if the Post Type doesn't exist.
107 if ( ! $post_type ) {
108 continue;
109 }
110
111 // Add Settings Field.
112 add_settings_field(
113 $supported_post_type . '_form',
114 sprintf(
115 /* translators: Post Type Name */
116 __( 'Default Form (%s)', 'convertkit' ),
117 $post_type->label
118 ),
119 array( $this, 'custom_post_types_callback' ),
120 $this->settings_key,
121 $this->name,
122 array(
123 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form',
124 'post_type' => $supported_post_type,
125 'post_type_object' => $post_type,
126 )
127 );
128 }
129
130 add_settings_field(
131 'debug',
132 __( 'Debug', 'convertkit' ),
133 array( $this, 'debug_callback' ),
134 $this->settings_key,
135 $this->name,
136 array(
137 'label_for' => 'debug',
138 )
139 );
140
141 add_settings_field(
142 'no_scripts',
143 __( 'Disable JavaScript', 'convertkit' ),
144 array( $this, 'no_scripts_callback' ),
145 $this->settings_key,
146 $this->name,
147 array(
148 'label_for' => 'no_scripts',
149 )
150 );
151
152 add_settings_field(
153 'no_css',
154 __( 'Disable CSS', 'convertkit' ),
155 array( $this, 'no_css_callback' ),
156 $this->settings_key,
157 $this->name,
158 array(
159 'label_for' => 'no_css',
160 )
161 );
162
163 }
164
165 /**
166 * Prints help info for this section
167 */
168 public function print_section_info() {
169
170 ?>
171 <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>
172 <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>
173 <p>
174 <?php
175 printf(
176 /* translators: [convertkit] shortcode, wrapped in <code> tags */
177 esc_html__( 'The default form can be inserted into the middle of post or page content by using the %s shortcode.', 'convertkit' ),
178 '<code>[convertkit]</code>'
179 );
180 ?>
181 </p>
182 <?php
183
184 }
185
186 /**
187 * Returns the URL for the ConvertKit documentation for this setting section.
188 *
189 * @since 2.0.8
190 *
191 * @return string Documentation URL.
192 */
193 public function documentation_url() {
194
195 return 'https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin';
196
197 }
198
199 /**
200 * Renders container divs for styling, and attempts to fetch the ConvertKit Account
201 * details if API credentials have been specified.
202 *
203 * @since 1.9.6
204 */
205 public function render_before() {
206
207 // Initialize the API if an API Key and Secret is defined.
208 if ( ! $this->settings->has_api_key_and_secret() ) {
209 return;
210 }
211
212 $this->api = new ConvertKit_API(
213 $this->settings->get_api_key(),
214 $this->settings->get_api_secret(),
215 $this->settings->debug_enabled(),
216 'settings'
217 );
218
219 // Get Account Details, which we'll use in account_name_callback(), but also lets us test
220 // whether the API credentials are valid.
221 $this->account = $this->api->account();
222
223 // Show an error message if Account Details could not be fetched e.g. API credentials supplied are invalid.
224 if ( is_wp_error( $this->account ) ) {
225 // Depending on the error code, maybe persist a notice in the WordPress Administration until the user
226 // fixes the problem.
227 switch ( $this->account->get_error_data( $this->account->get_error_code() ) ) {
228 case 401:
229 // API credentials are invalid.
230 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' );
231 break;
232 }
233
234 $this->output_error( $this->account->get_error_message() );
235 } else {
236 // Remove any existing persistent notice.
237 WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' );
238 }
239
240 }
241
242 /**
243 * Outputs the Account Name
244 *
245 * @since 1.9.6
246 */
247 public function account_name_callback() {
248
249 // Output a notice telling the user to enter their API Key and Secret if they haven't done so yet.
250 if ( ! $this->settings->has_api_key_and_secret() || is_wp_error( $this->account ) ) {
251 echo '<p class="description">' . esc_html__( 'Add a valid API Key and Secret to get started', 'convertkit' ) . '</p>';
252 return;
253 }
254
255 // Output Account Name.
256 $html = sprintf(
257 '<code>%s</code>',
258 isset( $this->account['name'] ) ? esc_attr( $this->account['name'] ) : esc_html__( '(Not specified)', 'convertkit' )
259 );
260 $html .= '<p class="description">' . esc_html__( 'The name of your connected ConvertKit account.', 'convertkit' ) . '</p>';
261
262 // Output has already been run through escaping functions above.
263 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
264 }
265
266 /**
267 * Renders the input for the API Key setting.
268 *
269 * @since 1.9.6
270 */
271 public function api_key_callback() {
272
273 // If the API Key is stored as a constant, it cannot be edited here.
274 if ( $this->settings->is_api_key_a_constant() ) {
275 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
276 $this->settings->get_api_key(),
277 esc_html__( 'Your API Key has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
278 );
279 return;
280 }
281
282 // Output field.
283 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
284 'api_key',
285 esc_attr( $this->settings->get_api_key() ),
286 array(
287 sprintf(
288 /* translators: %1$s: Link to ConvertKit Account */
289 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
290 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Key.', 'convertkit' ) . '</a>'
291 ),
292 sprintf(
293 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Key definition */
294 esc_html__( 'Alternatively specify your API Key in the %1$s file using %2$s', 'convertkit' ),
295 '<code>wp-config.php</code>',
296 '<code>define(\'CONVERTKIT_API_KEY\', \'your-api-key\');</code>'
297 ),
298 ),
299 array( 'regular-text', 'code' )
300 );
301
302 }
303
304 /**
305 * Renders the input for the API Secret setting.
306 *
307 * @since 1.9.6
308 */
309 public function api_secret_callback() {
310
311 // If the API Secret is stored as a constant, it cannot be edited here.
312 if ( $this->settings->is_api_secret_a_constant() ) {
313 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
314 $this->settings->get_api_secret(),
315 esc_html__( 'Your API Secret has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
316 );
317 return;
318 }
319
320 // Output field.
321 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
322 'api_secret',
323 esc_attr( $this->settings->get_api_secret() ),
324 array(
325 sprintf(
326 /* translators: %1$s: Link to ConvertKit Account */
327 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
328 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Secret.', 'convertkit' ) . '</a>'
329 ),
330 sprintf(
331 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Secret definition */
332 esc_html__( 'Alternatively specify your API Secret in the %1$s file using %2$s', 'convertkit' ),
333 '<code>wp-config.php</code>',
334 '<code>define(\'CONVERTKIT_API_SECRET\', \'your-api-secret\');</code>'
335 ),
336 ),
337 array( 'regular-text', 'code' )
338 );
339
340 }
341
342 /**
343 * Renders the input for the Default Form setting for the given Post Type.
344 *
345 * @since 1.9.6
346 *
347 * @param array $args Field arguments.
348 */
349 public function custom_post_types_callback( $args ) {
350
351 // Refresh Forms.
352 if ( ! $this->forms ) {
353 $this->forms = new ConvertKit_Resource_Forms( 'settings' );
354 $this->forms->refresh();
355
356 // Also refresh Landing Pages, Tags and Posts. Whilst not displayed in the Plugin Settings, this ensures up to date
357 // lists are stored for when editing e.g. Pages.
358 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'settings' );
359 $landing_pages->refresh();
360
361 $posts = new ConvertKit_Resource_Posts( 'settings' );
362 $posts->refresh();
363
364 $products = new ConvertKit_Resource_Products( 'settings' );
365 $products->refresh();
366
367 $tags = new ConvertKit_Resource_Tags( 'settings' );
368 $tags->refresh();
369 }
370
371 // Bail if no Forms exist.
372 if ( ! $this->forms->exist() ) {
373 esc_html_e( 'No Forms exist in ConvertKit.', 'convertkit' );
374 echo '<br />' . sprintf(
375 /* translators: Link to sign in to ConvertKit */
376 esc_html__( 'To create a form, %s', 'convertkit' ),
377 '<a href="' . esc_url( convertkit_get_sign_in_url() ) . '" target="_blank">' . esc_html__( 'sign in to ConvertKit', 'convertkit' ) . '</a>'
378 );
379 return;
380 }
381
382 // Build array of select options.
383 $options = array(
384 'default' => esc_html__( 'None', 'convertkit' ),
385 );
386 foreach ( $this->forms->get() as $form ) {
387 $options[ esc_attr( $form['id'] ) ] = esc_html( $form['name'] );
388 }
389
390 // Build description with preview link.
391 $description = false;
392 $preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_url( $args['post_type'] );
393 if ( $preview_url ) {
394 // Include a preview link in the description.
395 $description = sprintf(
396 '%s %s %s',
397 sprintf(
398 /* translators: Post Type name, plural */
399 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
400 $args['post_type_object']->label
401 ),
402 '<a href="' . esc_url( $preview_url ) . '" id="convertkit-preview-form-' . esc_attr( $args['post_type'] ) . '" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
403 esc_html__( 'to preview how this will display.', 'convertkit' )
404 );
405 } else {
406 // Just output the field's description.
407 $description = sprintf(
408 /* translators: Post Type name, plural */
409 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
410 $args['post_type_object']->label
411 );
412 }
413
414 // Build field.
415 $select_field = $this->get_select_field(
416 $args['post_type'] . '_form',
417 $this->settings->get_default_form( $args['post_type'] ),
418 $options,
419 $description,
420 array(
421 'convertkit-select2',
422 'convertkit-preview-output-link',
423 ),
424 array(
425 'data-target' => '#convertkit-preview-form-' . esc_attr( $args['post_type'] ),
426 'data-link' => esc_attr( $preview_url ) . '&convertkit_form_id=',
427 )
428 );
429
430 // Output field.
431 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
432
433 }
434
435 /**
436 * Renders the input for the Debug setting.
437 *
438 * @since 1.9.6
439 */
440 public function debug_callback() {
441
442 // Output field.
443 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
444 'debug',
445 'on',
446 $this->settings->debug_enabled(), // phpcs:ignore WordPress.Security.EscapeOutput
447 esc_html__( 'Log requests to file and output browser console messages.', 'convertkit' ),
448 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' )
449 );
450
451 }
452
453 /**
454 * Renders the input for the Disable Javascript setting.
455 *
456 * @since 1.9.6
457 */
458 public function no_scripts_callback() {
459
460 // Output field.
461 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
462 'no_scripts',
463 'on',
464 $this->settings->scripts_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
465 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' )
466 );
467
468 }
469
470 /**
471 * Renders the input for the Disable CSS setting.
472 *
473 * @since 1.9.6.9
474 */
475 public function no_css_callback() {
476
477 // Output field.
478 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
479 'no_css',
480 'on',
481 $this->settings->css_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
482 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' ),
483 array(
484 sprintf(
485 '%s <a href="%s" target="_blank">%s</a>',
486 esc_html__( 'To customize forms and their styling, use the', 'convertkit' ),
487 esc_url( convertkit_get_form_editor_url() ),
488 esc_html__( 'ConvertKit form editor', 'convertkit' )
489 ),
490 sprintf(
491 '%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',
492 esc_html__( 'For developers who require custom form designs through use of CSS, consider using the', 'convertkit' ),
493 esc_html__( 'or', 'convertkit' ),
494 esc_html__( 'integrations.', 'convertkit' )
495 ),
496 )
497 );
498
499 }
500
501 }
502