PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.7
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.7
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 2.3.1 All 196 releases
← All changes | includes/class-convertkit-resource-forms.php +34 -320 3.3.32.4.7 View file →
@@ -11,9 +11,9 @@
11 11 * ConvertKit Forms data stored locally from the API.
12 12 *
13 13 * @since 1.9.6
14 14 */
15 -class ConvertKit_Resource_Forms extends ConvertKit_Resource_V4 {
15 +class ConvertKit_Resource_Forms extends ConvertKit_Resource {
16 16
17 17 /**
18 18 * Holds the Settings Key that stores site wide ConvertKit settings
19 19 *
@@ -36,87 +36,25 @@
36 36 * @param bool|string $context Context.
37 37 */
38 38 public function __construct( $context = false ) {
39 39
40 - // Initialize the API if the Access Token has been defined in the Plugin Settings.
40 + // Initialize the API if the API Key and Secret have been defined in the Plugin Settings.
41 41 $settings = new ConvertKit_Settings();
42 - if ( $settings->has_access_and_refresh_token() ) {
43 - $this->api = new ConvertKit_API_V4(
44 - CONVERTKIT_OAUTH_CLIENT_ID,
45 - CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
46 - $settings->get_access_token(),
47 - $settings->get_refresh_token(),
42 + if ( $settings->has_api_key_and_secret() ) {
43 + $this->api = new ConvertKit_API(
44 + $settings->get_api_key(),
45 + $settings->get_api_secret(),
48 46 $settings->debug_enabled(),
49 47 $context
50 48 );
51 49 }
52 50
53 - // Get last query time and existing resources.
54 - $this->last_queried = get_option( $this->settings_name . '_last_queried' );
55 - $this->resources = get_option( $this->settings_name );
51 + // Call parent initialization function.
52 + parent::init();
56 53
57 54 }
58 55
59 56 /**
60 - * Fetches resources (forms, landing pages or tags) from the API, storing them in the options table
61 - * with a last queried timestamp.
62 - *
63 - * If the refresh results in a 401, removes the access and refresh tokens from the settings.
64 - *
65 - * @since 3.1.2
66 - *
67 - * @return WP_Error|array
68 - */
69 - public function refresh() {
70 -
71 - // Call parent refresh method.
72 - $result = parent::refresh();
73 -
74 - // If an error occurred, maybe delete credentials from the Plugin's settings
75 - // if the error is a 401 unauthorized.
76 - if ( is_wp_error( $result ) ) {
77 - convertkit_maybe_delete_credentials( $result, CONVERTKIT_OAUTH_CLIENT_ID );
78 - }
79 -
80 - return $result;
81 -
82 - }
83 -
84 - /**
85 - * Returns all inline forms based on the sort order.
86 - *
87 - * @since 2.7.3
88 - *
89 - * @return bool|array
90 - */
91 - public function get_inline() {
92 -
93 - // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated
94 - // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins,
95 - // get_by() won't be available and will cause an E_ERROR, crashing the site.
96 - // @see https://wordpress.org/support/topic/error-1795/.
97 - if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function.
98 - return false;
99 - }
100 -
101 - return $this->get_by( 'format', array( 'inline' ) );
102 -
103 - }
104 -
105 - /**
106 - * Returns whether any inline forms exist in the options table.
107 - *
108 - * @since 2.7.3
109 - *
110 - * @return bool
111 - */
112 - public function inline_exist() {
113 -
114 - return (bool) $this->get_inline();
115 -
116 - }
117 -
118 - /**
119 57 * Returns all non-inline forms based on the sort order.
120 58 *
121 59 * @since 2.2.4
122 60 *
@@ -127,9 +65,9 @@
127 65 // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated
128 66 // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins,
129 67 // get_by() won't be available and will cause an E_ERROR, crashing the site.
130 68 // @see https://wordpress.org/support/topic/error-1795/.
131 - if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function.
69 + if ( ! method_exists( $this, 'get_by' ) ) {
132 70 return false;
133 71 }
134 72
135 73 return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) );
@@ -135,8 +73,9 @@
135 73 return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) );
136 74
137 75 }
138 76
77 +
139 78 /**
140 79 * Returns whether any non-inline forms exist in the options table.
141 80 *
142 81 * @since 2.2.4
@@ -153,34 +92,8 @@
153 92
154 93 }
155 94
156 95 /**
157 - * Determines if the given Form ID is a legacy Form or Landing Page.
158 - *
159 - * @since 2.5.0
160 - *
161 - * @param int $id Form or Landing Page ID.
162 - */
163 - public function is_legacy( $id ) {
164 -
165 - // Get Form.
166 - $form = $this->get_by_id( (int) $id );
167 -
168 - // Return false if no Form exists.
169 - if ( ! $form ) {
170 - return false;
171 - }
172 -
173 - // If the `format` key exists, this is not a legacy Form.
174 - if ( array_key_exists( 'format', $form ) ) {
175 - return false;
176 - }
177 -
178 - return true;
179 -
180 - }
181 -
182 - /**
183 96 * Returns a <select> field populated with all forms, based on the given parameters.
184 97 *
185 98 * @since 2.3.9
186 99 *
@@ -208,11 +121,11 @@
208 121
209 122 }
210 123
211 124 /**
212 - * Outputs a <select> field populated with all forms, based on the given parameters.
125 + * Returns a <select> field populated with all non-inline forms, based on the given parameters.
213 126 *
214 - * @since 2.8.5
127 + * @since 2.3.9
215 128 *
216 129 * @param string $name Name.
217 130 * @param string $id ID.
218 131 * @param bool|array $css_classes <select> CSS class(es).
@@ -219,13 +132,14 @@
219 132 * @param string $selected_option <option> value to mark as selected.
220 133 * @param bool|array $prepend_options <option> elements to prepend before resources.
221 134 * @param bool|array $attributes <select> attributes.
222 135 * @param bool|string|array $description Description.
136 + * @return string HTML Select Field
223 137 */
224 - public function output_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {
138 + public function get_select_field_non_inline( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {
225 139
226 - $this->output_select_field(
227 - $this->get(),
140 + return $this->get_select_field(
141 + $this->get_non_inline(),
228 142 $name,
229 143 $id,
230 144 $css_classes,
231 145 $selected_option,
@@ -236,67 +150,8 @@
236 150
237 151 }
238 152
239 153 /**
240 - * Returns a <select> field populated with all non-inline forms, based on the given parameters.
241 - *
242 - * @since 2.3.9
243 - *
244 - * @param string $name Name.
245 - * @param string $id ID.
246 - * @param bool|array $css_classes <select> CSS class(es).
247 - * @param array $selected_options <option> values to mark as selected.
248 - * @param bool|array $prepend_options <option> elements to prepend before resources.
249 - * @param bool|array $attributes <select> attributes.
250 - * @param bool|string|array $description Description.
251 - * @return string HTML Select Field
252 - */
253 - public function get_select_field_non_inline( $name, $id, $css_classes, $selected_options, $prepend_options = false, $attributes = false, $description = false ) {
254 -
255 - return $this->get_multi_select_field(
256 - $this->get_non_inline(),
257 - $name,
258 - $id,
259 - $css_classes,
260 - $selected_options,
261 - $prepend_options,
262 - $attributes,
263 - $description
264 - );
265 -
266 - }
267 -
268 - /**
269 - * Outputs a <select> field populated with all non-inline forms, based on the given parameters.
270 - *
271 - * @since 2.3.9
272 - *
273 - * @param string $name Name.
274 - * @param string $id ID.
275 - * @param bool|array $css_classes <select> CSS class(es).
276 - * @param array $selected_options <option> values to mark as selected.
277 - * @param bool|array $prepend_options <option> elements to prepend before resources.
278 - * @param bool|array $attributes <select> attributes.
279 - * @param bool|string|array $description Description.
280 - */
281 - public function output_select_field_non_inline( $name, $id, $css_classes, $selected_options, $prepend_options = false, $attributes = false, $description = false ) {
282 -
283 - echo wp_kses(
284 - $this->get_select_field_non_inline(
285 - $name,
286 - $id,
287 - $css_classes,
288 - $selected_options,
289 - $prepend_options,
290 - $attributes,
291 - $description
292 - ),
293 - convertkit_kses_allowed_html()
294 - );
295 -
296 - }
297 -
298 - /**
299 154 * Returns a <select> field populated with the resources, based on the given parameters.
300 155 *
301 156 * @since 2.3.9
302 157 *
@@ -377,123 +232,8 @@
377 232
378 233 }
379 234
380 235 /**
381 - * Outputs a <select> field populated with the resources, based on the given parameters.
382 - *
383 - * @since 2.8.5
384 - *
385 - * @param array $forms Forms.
386 - * @param string $name Name.
387 - * @param string $id ID.
388 - * @param bool|array $css_classes <select> CSS class(es).
389 - * @param string $selected_option <option> value to mark as selected.
390 - * @param bool|array $prepend_options <option> elements to prepend before resources.
391 - * @param bool|array $attributes <select> attributes.
392 - * @param bool|string|array $description Description.
393 - */
394 - private function output_select_field( $forms, $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {
395 -
396 - echo wp_kses(
397 - $this->get_select_field(
398 - $forms,
399 - $name,
400 - $id,
401 - $css_classes,
402 - $selected_option,
403 - $prepend_options,
404 - $attributes,
405 - $description
406 - ),
407 - convertkit_kses_allowed_html()
408 - );
409 -
410 - }
411 -
412 - /**
413 - * Returns a <select> field populated with the resources, based on the given parameters,
414 - * that supports multiple selection.
415 - *
416 - * @since 2.6.9
417 - *
418 - * @param array $forms Forms.
419 - * @param string $name Name.
420 - * @param string $id ID.
421 - * @param bool|array $css_classes <select> CSS class(es).
422 - * @param array $selected_options <option> values to mark as selected.
423 - * @param bool|array $prepend_options <option> elements to prepend before resources.
424 - * @param bool|array $attributes <select> attributes.
425 - * @param bool|string|array $description Description.
426 - * @return string HTML Select Field
427 - */
428 - private function get_multi_select_field( $forms, $name, $id, $css_classes, $selected_options = array(), $prepend_options = false, $attributes = false, $description = false ) {
429 -
430 - $html = sprintf(
431 - '<select name="%s[]" id="%s" class="%s" multiple',
432 - esc_attr( $name ),
433 - esc_attr( $id ),
434 - esc_attr( ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ) )
435 - );
436 -
437 - // Append any attributes.
438 - if ( $attributes ) {
439 - foreach ( $attributes as $key => $value ) {
440 - $html .= sprintf(
441 - ' %s="%s"',
442 - esc_attr( $key ),
443 - esc_attr( $value )
444 - );
445 - }
446 - }
447 -
448 - // Close select tag.
449 - $html .= '>';
450 -
451 - // If any prepended options exist, add them now.
452 - if ( $prepend_options ) {
453 - foreach ( $prepend_options as $value => $label ) {
454 - $html .= sprintf(
455 - '<option value="%s" data-preserve-on-refresh="1"%s>%s</option>',
456 - esc_attr( $value ),
457 - ( in_array( $value, $selected_options, true ) ? ' selected' : '' ),
458 - esc_attr( $label )
459 - );
460 - }
461 - }
462 -
463 - // Iterate through resources, if they exist, building <option> elements.
464 - if ( $forms ) {
465 - foreach ( $forms as $form ) {
466 - // Legacy forms don't include a `format` key, so define them as inline.
467 - $html .= sprintf(
468 - '<option value="%s"%s>%s [%s]</option>',
469 - esc_attr( $form['id'] ),
470 - ( in_array( $form['id'], $selected_options, true ) ? ' selected' : '' ),
471 - esc_attr( $form['name'] ),
472 - ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' )
473 - );
474 - }
475 - }
476 -
477 - // Close select.
478 - $html .= '</select>';
479 -
480 - // If no description is provided, return the select field now.
481 - if ( ! $description ) {
482 - return $html;
483 - }
484 -
485 - // Append description before returning field.
486 - if ( ! is_array( $description ) ) {
487 - return $html . '<p class="description">' . $description . '</p>';
488 - }
489 -
490 - // Return description lines in a paragraph, using breaklines for each description entry in the array.
491 - return $html . '<p class="description">' . implode( '<br />', $description ) . '</p>';
492 -
493 - }
494 -
495 - /**
496 236 * Returns the HTML/JS markup for the given Form ID.
497 237 *
498 238 * Legacy Forms will return HTML.
499 239 * Current Forms will return a <script> embed string.
@@ -499,13 +239,12 @@
499 239 * Current Forms will return a <script> embed string.
500 240 *
501 241 * @since 1.9.6
502 242 *
503 - * @param int $id Form ID.
504 - * @param int $post_id Post ID that requested the Form.
243 + * @param int $id Form ID.
505 244 * @return WP_Error|string
506 245 */
507 - public function get_html( $id, $post_id = 0 ) {
246 + public function get_html( $id ) {
508 247
509 248 // Cast ID to integer.
510 249 $id = absint( $id );
511 250
@@ -519,43 +258,33 @@
519 258 return new WP_Error(
520 259 'convertkit_resource_forms_get_html',
521 260 sprintf(
522 261 /* translators: ConvertKit Form ID */
523 - __( 'Kit Form ID %s does not exist on Kit.', 'convertkit' ),
262 + __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ),
524 263 $id
525 - ),
526 - 404
264 + )
527 265 );
528 266 }
529 267
530 - // Initialize Settings.
531 - $settings = new ConvertKit_Settings();
532 -
533 268 // If no uid is present in the Form API data, this is a legacy form that's served by directly fetching the HTML
534 - // from forms.kit.com.
269 + // from forms.convertkit.com.
535 270 if ( ! isset( $this->resources[ $id ]['uid'] ) ) {
536 - // Bail if no Access Token is specified in the Plugin Settings.
537 - if ( ! $settings->has_access_token() ) {
271 + // Initialize Settings.
272 + $settings = new ConvertKit_Settings();
273 +
274 + // Bail if no API Key is specified in the Plugin Settings.
275 + if ( ! $settings->has_api_key() ) {
538 276 return new WP_Error(
539 277 'convertkit_resource_forms_get_html',
540 - __( 'Kit Legacy Form could not be fetched as no Access Token specified in Plugin Settings', 'convertkit' )
278 + __( 'ConvertKit Legacy Form could not be fetched as no API Key specified in Plugin Settings', 'convertkit' )
541 279 );
542 280 }
543 281
544 282 // Initialize the API.
545 - $api = new ConvertKit_API_V4(
546 - CONVERTKIT_OAUTH_CLIENT_ID,
547 - CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
548 - $settings->get_access_token(),
549 - $settings->get_refresh_token(),
550 - $settings->debug_enabled(),
551 - 'output_form'
552 - );
283 + $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' );
553 284
554 285 // Return Legacy Form HTML.
555 - // We now call get_html() with the `embed_url` property, instead of get_form_html() with the `id` property,
556 - // because `embed_url` includes the API Key.
557 - return $api->get_html( $this->resources[ $id ]['embed_url'] );
286 + return $api->get_form_html( $id );
558 287 }
559 288
560 289 // If the form's format is not an inline form, add the inline script before the closing </body> tag.
561 290 // This prevents a modal form's overlay being constrained by the WordPress Theme's styles,
@@ -563,26 +292,16 @@
563 292 // displaying twice.
564 293 if ( $this->resources[ $id ]['format'] !== 'inline' ) {
565 294 add_filter(
566 295 'convertkit_output_scripts_footer',
567 - function ( $scripts ) use ( $id, $post_id, $settings ) {
296 + function ( $scripts ) use ( $id ) {
568 297
569 - // Build script.
570 - $script = array(
571 - 'async' => true,
572 - 'data-uid' => $this->resources[ $id ]['uid'],
573 - 'src' => $this->resources[ $id ]['embed_js'],
574 - 'data-kit-limit-per-session' => $settings->non_inline_form_limit_per_session() ? '1' : '0',
298 + $scripts[] = array(
299 + 'async' => true,
300 + 'data-uid' => $this->resources[ $id ]['uid'],
301 + 'src' => $this->resources[ $id ]['embed_js'],
575 302 );
576 303
577 - // If debugging is enabled, add the post ID to the script.
578 - if ( $settings->debug_enabled() ) {
579 - $script['data-kit-source-post-id'] = $post_id;
580 - }
581 -
582 - // Add the script to the scripts array.
583 - $scripts[] = $script;
584 -
585 304 return $scripts;
586 305
587 306 }
588 307 );
@@ -611,13 +330,8 @@
611 330 'async' => true,
612 331 'data-uid' => $this->resources[ $id ]['uid'],
613 332 'src' => $this->resources[ $id ]['embed_js'],
614 333 );
615 -
616 - // If debugging is enabled, add the post ID to the script.
617 - if ( $settings->debug_enabled() ) {
618 - $script['data-kit-source-post-id'] = $post_id;
619 - }
620 334
621 335 /**
622 336 * Filter the form <script> key/value pairs immediately before the script is output.
623 337 *