PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.4.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.4.8
3.4.4 3.4.3 3.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 All 197 releases
← All changes | admin/section/class-convertkit-settings-general.php +86 -394 2.2.01.4.8 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * ConvertKit Settings General class.
3 + * ConvertKit General Settings class
4 4 *
5 5 * @package ConvertKit
6 6 * @author ConvertKit
7 7 */
@@ -6,161 +6,60 @@
6 6 * @author ConvertKit
7 7 */
8 8
9 9 /**
10 - * Registers General Settings that can be edited at Settings > ConvertKit > General.
11 - *
12 - * @package ConvertKit
13 - * @author ConvertKit
10 + * Class ConvertKit_Settings_General
14 11 */
15 12 class ConvertKit_Settings_General extends ConvertKit_Settings_Base {
16 13
17 14 /**
18 - * Holds the API instance.
19 - *
20 - * @since 1.9.6
21 - *
22 - * @var ConvertKit_API
15 + * Constructor
23 16 */
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 17 public function __construct() {
18 + $this->settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG;
19 + $this->name = 'general';
20 + $this->title = __( 'General Settings', 'convertkit' );
21 + $this->tab_text = __( 'General', 'convertkit' );
48 22
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 23 parent::__construct();
64 -
65 24 }
66 25
67 26 /**
68 - * Registers settings fields for this section.
27 + * Register and add settings
69 28 */
70 29 public function register_fields() {
71 -
72 30 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 31 'api_key',
82 - __( 'API Key', 'convertkit' ),
32 + 'API Key',
83 33 array( $this, 'api_key_callback' ),
84 34 $this->settings_key,
85 - $this->name,
86 - array(
87 - 'label_for' => 'api_key',
88 - )
35 + $this->name
89 36 );
90 37
91 38 add_settings_field(
92 39 'api_secret',
93 - __( 'API Secret', 'convertkit' ),
40 + 'API Secret',
94 41 array( $this, 'api_secret_callback' ),
95 42 $this->settings_key,
96 - $this->name,
97 - array(
98 - 'label_for' => 'api_secret',
99 - )
43 + $this->name
100 44 );
101 45
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 46 add_settings_field(
131 - 'debug',
132 - __( 'Debug', 'convertkit' ),
133 - array( $this, 'debug_callback' ),
47 + 'default_form',
48 + 'Default Form',
49 + array( $this, 'default_form_callback' ),
134 50 $this->settings_key,
135 51 $this->name,
136 - array(
137 - 'label_for' => 'debug',
138 - )
52 + $this->api->get_resources( 'forms' )
139 53 );
140 54
141 55 add_settings_field(
142 - 'no_scripts',
143 - __( 'Disable JavaScript', 'convertkit' ),
144 - array( $this, 'no_scripts_callback' ),
56 + 'debug',
57 + 'Debug',
58 + array( $this, 'debug_callback' ),
145 59 $this->settings_key,
146 - $this->name,
147 - array(
148 - 'label_for' => 'no_scripts',
149 - )
60 + $this->name
150 61 );
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 62 }
164 63
165 64 /**
166 65 * Prints help info for this section
@@ -165,323 +64,116 @@
165 64 /**
166 65 * Prints help info for this section
167 66 */
168 67 public function print_section_info() {
169 -
170 68 ?>
171 69 <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>
70 + <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><?php
71 + /* translators: 1: shortcode */ ?>
72 + <p><?php printf( esc_html__( 'The default form can be inserted into the middle of post or page content by using the %s shortcode.', 'convertkit' ), '<code>[convertkit]</code>' ); ?></p>
182 73 <?php
183 -
184 74 }
185 75
186 76 /**
187 - * Returns the URL for the ConvertKit documentation for this setting section.
188 - *
189 - * @since 2.0.8
190 - *
191 - * @return string Documentation URL.
77 + * Renders the input for api key entry
192 78 */
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'
79 + public function api_key_callback() {
80 + $html = sprintf(
81 + '<input type="text" class="regular-text code" id="api_key" name="%s[api_key]" value="%s" />',
82 + $this->settings_key,
83 + isset( $this->options['api_key'] ) ? esc_attr( $this->options['api_key'] ) : ''
217 84 );
218 85
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();
86 + $html .= '<p class="description"><a href="https://app.convertkit.com/account/edit" target="_blank">' . __( 'Get your ConvertKit API Key', 'convertkit' ) . '</a></p>';
222 87
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 -
88 + echo $html; // WPCS: XSS ok.
240 89 }
241 90
242 91 /**
243 - * Outputs the Account Name
244 - *
245 - * @since 1.9.6
92 + * Renders the input for api key entry
246 93 */
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' )
94 + public function api_secret_callback() {
95 + $html = sprintf(
96 + '<input type="text" class="regular-text code" id="api_secret" name="%s[api_secret]" value="%s" />',
97 + $this->settings_key,
98 + isset( $this->options['api_secret'] ) ? esc_attr( $this->options['api_secret'] ) : ''
259 99 );
260 - $html .= '<p class="description">' . esc_html__( 'The name of your connected ConvertKit account.', 'convertkit' ) . '</p>';
261 100
262 - // Output has already been run through escaping functions above.
263 - echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
264 - }
101 + $html .= '<p class="description"><a href="https://app.convertkit.com/account/edit" target="_blank">';
102 + $html .= __( 'Get your ConvertKit API Secret.', 'convertkit' ) . '</a>';
103 + $html .= ' ' . __( 'This setting is required to unsubscribe subscribers.', 'convertkit' ) . '</p>';
265 104
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 -
105 + echo $html; // WPCS: XSS ok.
302 106 }
303 107
304 108 /**
305 - * Renders the input for the API Secret setting.
109 + * Renders the form select list
306 110 *
307 - * @since 1.9.6
111 + * @param array $forms Form listing.
308 112 */
309 - public function api_secret_callback() {
113 + public function default_form_callback( $forms ) {
310 114
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;
115 + // Check for error in response.
116 + if ( isset( $forms[0]['id'] ) && '-2' === $forms[0]['id'] ) {
117 + $html = '<p class="error">' . __( 'Error connecting to API. Please verify your site can connect to <code>https://api.convertkit.com</code>','convertkit' ) . '</p>';
118 + } else {
119 + $html = sprintf( '<select id="default_form" name="%s[default_form]">', $this->settings_key );
120 + $html .= '<option value="default">' . __( 'None', 'convertkit' ) . '</option>';
121 + foreach ( $forms as $form ) {
122 + $html .= sprintf(
123 + '<option value="%s" %s>%s</option>',
124 + esc_attr( $form['id'] ),
125 + selected( $this->options['default_form'], $form['id'], false ),
126 + esc_html( $form['name'] )
127 + );
128 + }
129 + $html .= '</select>';
318 130 }
319 131
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();
132 + if ( empty( $this->options['api_key'] ) ) {
133 + $html .= '<p class="description">' . __( 'Enter your API Key above to get your available forms.', 'convertkit' ) . '</p>';
369 134 }
370 135
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;
136 + if ( empty( $forms ) ) {
137 + $html .= '<p class="description">' . __( 'There are no forms setup in your account. You can go <a href="https://app.convertkit.com/landing_pages/new" target="_blank">here</a> to create one.', 'convertkit' ) . '</p>';
380 138 }
381 139
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_attr( $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 -
140 + echo $html; // WPCS: XSS ok.
433 141 }
434 142
435 143 /**
436 - * Renders the input for the Debug setting.
437 - *
438 - * @since 1.9.6
144 + * Renders the input for debug setting
439 145 */
440 146 public function debug_callback() {
441 147
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 - );
148 + $debug = '';
149 + if ( isset( $this->options['debug'] ) && 'on' === $this->options['debug'] ) {
150 + $debug = 'checked';
151 + }
450 152
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' )
153 + echo sprintf( // WPCS: XSS OK
154 + '<input type="checkbox" class="" id="debug" name="%s[debug]" %s />%s',
155 + $this->settings_key,
156 + $debug,
157 + __( 'Save connection data to a log file.','convertkit' )
466 158 );
467 159
468 160 }
469 161
470 162 /**
471 - * Renders the input for the Disable CSS setting.
163 + * Sanitizes the settings
472 164 *
473 - * @since 1.9.6.9
165 + * @param array $settings The settings fields submitted.
166 + * @return array Sanitized settings.
474 167 */
475 - public function no_css_callback() {
168 + public function sanitize_settings( $settings ) {
476 169
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__( 'Prevent plugin from loading CSS files. This will disable styling on broadcasts, product buttons and member\'s content. Use with caution!', 'convertkit' )
483 - );
484 -
170 + // Clear the api transient.
171 + delete_transient( 'convertkit_get_api_response' );
172 + return shortcode_atts( array(
173 + 'api_key' => '',
174 + 'api_secret' => '',
175 + 'default_form' => 0,
176 + 'debug' => '',
177 + ), $settings );
485 178 }
486 -
487 179 }