PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.5
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 +24 -273 3.3.22.5.5 View file →
@@ -49,74 +49,14 @@
49 49 $context
50 50 );
51 51 }
52 52
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 );
53 + // Call parent initialization function.
54 + parent::init();
56 55
57 56 }
58 57
59 58 /**
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 occured, 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 59 * Returns all non-inline forms based on the sort order.
120 60 *
121 61 * @since 2.2.4
122 62 *
@@ -127,9 +67,9 @@
127 67 // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated
128 68 // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins,
129 69 // get_by() won't be available and will cause an E_ERROR, crashing the site.
130 70 // @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.
71 + if ( ! method_exists( $this, 'get_by' ) ) {
132 72 return false;
133 73 }
134 74
135 75 return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) );
@@ -135,8 +75,9 @@
135 75 return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) );
136 76
137 77 }
138 78
79 +
139 80 /**
140 81 * Returns whether any non-inline forms exist in the options table.
141 82 *
142 83 * @since 2.2.4
@@ -208,11 +149,11 @@
208 149
209 150 }
210 151
211 152 /**
212 - * Outputs a <select> field populated with all forms, based on the given parameters.
153 + * Returns a <select> field populated with all non-inline forms, based on the given parameters.
213 154 *
214 - * @since 2.8.5
155 + * @since 2.3.9
215 156 *
216 157 * @param string $name Name.
217 158 * @param string $id ID.
218 159 * @param bool|array $css_classes <select> CSS class(es).
@@ -219,13 +160,14 @@
219 160 * @param string $selected_option <option> value to mark as selected.
220 161 * @param bool|array $prepend_options <option> elements to prepend before resources.
221 162 * @param bool|array $attributes <select> attributes.
222 163 * @param bool|string|array $description Description.
164 + * @return string HTML Select Field
223 165 */
224 - public function output_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {
166 + public function get_select_field_non_inline( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) {
225 167
226 - $this->output_select_field(
227 - $this->get(),
168 + return $this->get_select_field(
169 + $this->get_non_inline(),
228 170 $name,
229 171 $id,
230 172 $css_classes,
231 173 $selected_option,
@@ -236,67 +178,8 @@
236 178
237 179 }
238 180
239 181 /**
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 182 * Returns a <select> field populated with the resources, based on the given parameters.
300 183 *
301 184 * @since 2.3.9
302 185 *
@@ -377,123 +260,8 @@
377 260
378 261 }
379 262
380 263 /**
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 264 * Returns the HTML/JS markup for the given Form ID.
497 265 *
498 266 * Legacy Forms will return HTML.
499 267 * Current Forms will return a <script> embed string.
@@ -499,13 +267,12 @@
499 267 * Current Forms will return a <script> embed string.
500 268 *
501 269 * @since 1.9.6
502 270 *
503 - * @param int $id Form ID.
504 - * @param int $post_id Post ID that requested the Form.
271 + * @param int $id Form ID.
505 272 * @return WP_Error|string
506 273 */
507 - public function get_html( $id, $post_id = 0 ) {
274 + public function get_html( $id ) {
508 275
509 276 // Cast ID to integer.
510 277 $id = absint( $id );
511 278
@@ -519,26 +286,25 @@
519 286 return new WP_Error(
520 287 'convertkit_resource_forms_get_html',
521 288 sprintf(
522 289 /* translators: ConvertKit Form ID */
523 - __( 'Kit Form ID %s does not exist on Kit.', 'convertkit' ),
290 + __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ),
524 291 $id
525 - ),
526 - 404
292 + )
527 293 );
528 294 }
529 295
530 - // Initialize Settings.
531 - $settings = new ConvertKit_Settings();
532 -
533 296 // 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.
297 + // from forms.convertkit.com.
535 298 if ( ! isset( $this->resources[ $id ]['uid'] ) ) {
299 + // Initialize Settings.
300 + $settings = new ConvertKit_Settings();
301 +
536 302 // Bail if no Access Token is specified in the Plugin Settings.
537 303 if ( ! $settings->has_access_token() ) {
538 304 return new WP_Error(
539 305 'convertkit_resource_forms_get_html',
540 - __( 'Kit Legacy Form could not be fetched as no Access Token specified in Plugin Settings', 'convertkit' )
306 + __( 'ConvertKit Legacy Form could not be fetched as no Access Token specified in Plugin Settings', 'convertkit' )
541 307 );
542 308 }
543 309
544 310 // Initialize the API.
@@ -563,26 +329,16 @@
563 329 // displaying twice.
564 330 if ( $this->resources[ $id ]['format'] !== 'inline' ) {
565 331 add_filter(
566 332 'convertkit_output_scripts_footer',
567 - function ( $scripts ) use ( $id, $post_id, $settings ) {
333 + function ( $scripts ) use ( $id ) {
568 334
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',
335 + $scripts[] = array(
336 + 'async' => true,
337 + 'data-uid' => $this->resources[ $id ]['uid'],
338 + 'src' => $this->resources[ $id ]['embed_js'],
575 339 );
576 340
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 341 return $scripts;
586 342
587 343 }
588 344 );
@@ -611,13 +367,8 @@
611 367 'async' => true,
612 368 'data-uid' => $this->resources[ $id ]['uid'],
613 369 'src' => $this->resources[ $id ]['embed_js'],
614 370 );
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 371
621 372 /**
622 373 * Filter the form <script> key/value pairs immediately before the script is output.
623 374 *