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

461 lines 13.1 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 * Renders container divs for styling, and attempts to fetch the ConvertKit Account
188 * details if API credentials have been specified.
189 *
190 * @since 1.9.6
191 */
192 public function render_before() {
193
194 // Initialize the API if an API Key and Secret is defined.
195 if ( ! $this->settings->has_api_key_and_secret() ) {
196 return;
197 }
198
199 $this->api = new ConvertKit_API(
200 $this->settings->get_api_key(),
201 $this->settings->get_api_secret(),
202 $this->settings->debug_enabled(),
203 'settings'
204 );
205
206 // Get Account Details, which we'll use in account_name_callback(), but also lets us test
207 // whether the API credentials are valid.
208 $this->account = $this->api->account();
209
210 // Show an error message if Account Details could not be fetched e.g. API credentials supplied are invalid.
211 if ( is_wp_error( $this->account ) ) {
212 $this->output_error( $this->account->get_error_message() );
213 }
214
215 }
216
217 /**
218 * Outputs the Account Name
219 *
220 * @since 1.9.6
221 */
222 public function account_name_callback() {
223
224 // Output a notice telling the user to enter their API Key and Secret if they haven't done so yet.
225 if ( ! $this->settings->has_api_key_and_secret() || is_wp_error( $this->account ) ) {
226 echo '<p class="description">' . esc_html__( 'Add a valid API Key and Secret to get started', 'convertkit' ) . '</p>';
227 return;
228 }
229
230 // Output Account Name.
231 $html = sprintf(
232 '<code>%s</code>',
233 isset( $this->account['name'] ) ? esc_attr( $this->account['name'] ) : esc_html__( '(Not specified)', 'convertkit' )
234 );
235 $html .= '<p class="description">' . esc_html__( 'The name of your connected ConvertKit account.', 'convertkit' ) . '</p>';
236
237 // Output has already been run through escaping functions above.
238 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
239 }
240
241 /**
242 * Renders the input for the API Key setting.
243 *
244 * @since 1.9.6
245 */
246 public function api_key_callback() {
247
248 // If the API Key is stored as a constant, it cannot be edited here.
249 if ( $this->settings->is_api_key_a_constant() ) {
250 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
251 $this->settings->get_api_key(),
252 esc_html__( 'Your API Key has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
253 );
254 return;
255 }
256
257 // Output field.
258 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
259 'api_key',
260 esc_attr( $this->settings->get_api_key() ),
261 array(
262 sprintf(
263 /* translators: %1$s: Link to ConvertKit Account */
264 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
265 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Key.', 'convertkit' ) . '</a>'
266 ),
267 sprintf(
268 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Key definition */
269 esc_html__( 'Alternatively specify your API Key in the %1$s file using %2$s', 'convertkit' ),
270 '<code>wp-config.php</code>',
271 '<code>define(\'CONVERTKIT_API_KEY\', \'your-api-key\');</code>'
272 ),
273 )
274 );
275
276 }
277
278 /**
279 * Renders the input for the API Secret setting.
280 *
281 * @since 1.9.6
282 */
283 public function api_secret_callback() {
284
285 // If the API Secret is stored as a constant, it cannot be edited here.
286 if ( $this->settings->is_api_secret_a_constant() ) {
287 echo $this->get_masked_value( // phpcs:ignore WordPress.Security.EscapeOutput
288 $this->settings->get_api_secret(),
289 esc_html__( 'Your API Secret has been defined in your wp-config.php file. For security, it is not displayed here.', 'convertkit' )
290 );
291 return;
292 }
293
294 // Output field.
295 echo $this->get_text_field( // phpcs:ignore WordPress.Security.EscapeOutput
296 'api_secret',
297 esc_attr( $this->settings->get_api_secret() ),
298 array(
299 sprintf(
300 /* translators: %1$s: Link to ConvertKit Account */
301 esc_html__( '%1$s Required for proper plugin function.', 'convertkit' ),
302 '<a href="' . esc_url( convertkit_get_api_key_url() ) . '" target="_blank">' . esc_html__( 'Get your ConvertKit API Secret.', 'convertkit' ) . '</a>'
303 ),
304 sprintf(
305 /* translators: Account, %1$s: wp-config.php, %2$s: <code> block for API Secret definition */
306 esc_html__( 'Alternatively specify your API Secret in the %1$s file using %2$s', 'convertkit' ),
307 '<code>wp-config.php</code>',
308 '<code>define(\'CONVERTKIT_API_SECRET\', \'your-api-secret\');</code>'
309 ),
310 )
311 );
312
313 }
314
315 /**
316 * Renders the input for the Default Form setting for the given Post Type.
317 *
318 * @since 1.9.6
319 *
320 * @param array $args Field arguments.
321 */
322 public function custom_post_types_callback( $args ) {
323
324 // Refresh Forms.
325 if ( ! $this->forms ) {
326 $this->forms = new ConvertKit_Resource_Forms( 'settings' );
327 $this->forms->refresh();
328
329 // Also refresh Landing Pages, Tags and Posts. Whilst not displayed in the Plugin Settings, this ensures up to date
330 // lists are stored for when editing e.g. Pages.
331 $landing_pages = new ConvertKit_Resource_Landing_Pages( 'settings' );
332 $landing_pages->refresh();
333
334 $posts = new ConvertKit_Resource_Posts( 'settings' );
335 $posts->refresh();
336
337 $products = new ConvertKit_Resource_Products( 'settings' );
338 $products->refresh();
339
340 $tags = new ConvertKit_Resource_Tags( 'settings' );
341 $tags->refresh();
342 }
343
344 // Bail if no Forms exist.
345 if ( ! $this->forms->exist() ) {
346 esc_html_e( 'No Forms exist in ConvertKit.', 'convertkit' );
347 echo '<br />' . sprintf(
348 /* translators: Link to sign in to ConvertKit */
349 esc_html__( 'To create a form, %s', 'convertkit' ),
350 '<a href="' . esc_url( convertkit_get_sign_in_url() ) . '" target="_blank">' . esc_html__( 'sign in to ConvertKit', 'convertkit' ) . '</a>'
351 );
352 return;
353 }
354
355 // Build array of select options.
356 $options = array(
357 'default' => esc_html__( 'None', 'convertkit' ),
358 );
359 foreach ( $this->forms->get() as $form ) {
360 $options[ esc_attr( $form['id'] ) ] = esc_html( $form['name'] );
361 }
362
363 // Build description with preview link.
364 $description = false;
365 $preview_url = WP_ConvertKit()->get_class( 'preview_output' )->get_preview_form_url( $args['post_type'] );
366 if ( $preview_url ) {
367 // Include a preview link in the description.
368 $description = sprintf(
369 '%s %s %s',
370 sprintf(
371 /* translators: Post Type name, plural */
372 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
373 $args['post_type_object']->label
374 ),
375 '<a href="' . esc_attr( $preview_url ) . '" id="convertkit-preview-form-' . esc_attr( $args['post_type'] ) . '" target="_blank">' . esc_html__( 'Click here', 'convertkit' ) . '</a>',
376 esc_html__( 'to preview how this will display.', 'convertkit' )
377 );
378 } else {
379 // Just output the field's description.
380 $description = sprintf(
381 /* translators: Post Type name, plural */
382 esc_html__( 'Select a form above to automatically output below all %s.', 'convertkit' ),
383 $args['post_type_object']->label
384 );
385 }
386
387 // Build field.
388 $select_field = $this->get_select_field(
389 $args['post_type'] . '_form',
390 $this->settings->get_default_form( $args['post_type'] ),
391 $options,
392 $description,
393 array(
394 'convertkit-select2',
395 'convertkit-preview-output-link',
396 ),
397 array(
398 'data-target' => '#convertkit-preview-form-' . esc_attr( $args['post_type'] ),
399 'data-link' => esc_attr( $preview_url ) . '&convertkit_form_id=',
400 )
401 );
402
403 // Output field.
404 echo '<div class="convertkit-select2-container">' . $select_field . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput
405
406 }
407
408 /**
409 * Renders the input for the Debug setting.
410 *
411 * @since 1.9.6
412 */
413 public function debug_callback() {
414
415 // Output field.
416 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
417 'debug',
418 'on',
419 $this->settings->debug_enabled(), // phpcs:ignore WordPress.Security.EscapeOutput
420 esc_html__( 'Log requests to file and output browser console messages.', 'convertkit' ),
421 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' )
422 );
423
424 }
425
426 /**
427 * Renders the input for the Disable Javascript setting.
428 *
429 * @since 1.9.6
430 */
431 public function no_scripts_callback() {
432
433 // Output field.
434 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
435 'no_scripts',
436 'on',
437 $this->settings->scripts_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
438 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' )
439 );
440
441 }
442
443 /**
444 * Renders the input for the Disable CSS setting.
445 *
446 * @since 1.9.6.9
447 */
448 public function no_css_callback() {
449
450 // Output field.
451 echo $this->get_checkbox_field( // phpcs:ignore WordPress.Security.EscapeOutput
452 'no_css',
453 'on',
454 $this->settings->css_disabled(), // phpcs:ignore WordPress.Security.EscapeOutput
455 esc_html__( 'Prevent plugin from loading CSS files. This will disable styling on the broadcasts shortcode and block. Use with caution!', 'convertkit' )
456 );
457
458 }
459
460 }
461