PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.0
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.0
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 / includes / class-convertkit-resource-forms.php

class-convertkit-resource-forms.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.2.0, at includes/class-convertkit-resource-forms.php

140 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Forms Resource class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Reads ConvertKit Forms from the options table, and refreshes
11 * ConvertKit Forms data stored locally from the API.
12 *
13 * @since 1.9.6
14 */
15 class ConvertKit_Resource_Forms extends ConvertKit_Resource {
16
17 /**
18 * Holds the Settings Key that stores site wide ConvertKit settings
19 *
20 * @var string
21 */
22 public $settings_name = 'convertkit_forms';
23
24 /**
25 * The type of resource
26 *
27 * @var string
28 */
29 public $type = 'forms';
30
31 /**
32 * Constructor.
33 *
34 * @since 1.9.8.4
35 *
36 * @param bool|string $context Context.
37 */
38 public function __construct( $context = false ) {
39
40 // Initialize the API if the API Key and Secret have been defined in the Plugin Settings.
41 $settings = new ConvertKit_Settings();
42 if ( $settings->has_api_key_and_secret() ) {
43 $this->api = new ConvertKit_API(
44 $settings->get_api_key(),
45 $settings->get_api_secret(),
46 $settings->debug_enabled(),
47 $context
48 );
49 }
50
51 // Call parent initialization function.
52 parent::init();
53
54 }
55
56 /**
57 * Returns the HTML/JS markup for the given Form ID.
58 *
59 * Legacy Forms will return HTML.
60 * Current Forms will return a <script> embed string.
61 *
62 * @since 1.9.6
63 *
64 * @param int $id Form ID.
65 * @return WP_Error|string
66 */
67 public function get_html( $id ) {
68
69 // Cast ID to integer.
70 $id = absint( $id );
71
72 // Bail if the resources are a WP_Error.
73 if ( is_wp_error( $this->resources ) ) {
74 return $this->resources;
75 }
76
77 // Bail if the resource doesn't exist.
78 if ( ! isset( $this->resources[ $id ] ) ) {
79 return new WP_Error(
80 'convertkit_resource_forms_get_html',
81 sprintf(
82 /* translators: ConvertKit Form ID */
83 __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ),
84 $id
85 )
86 );
87 }
88
89 // If no uid is present in the Form API data, this is a legacy form that's served by directly fetching the HTML
90 // from forms.convertkit.com.
91 if ( ! isset( $this->resources[ $id ]['uid'] ) ) {
92 // Initialize Settings.
93 $settings = new ConvertKit_Settings();
94
95 // Bail if no API Key is specified in the Plugin Settings.
96 if ( ! $settings->has_api_key() ) {
97 return new WP_Error(
98 'convertkit_resource_forms_get_html',
99 __( 'ConvertKit Legacy Form could not be fetched as no API Key specified in Plugin Settings', 'convertkit' )
100 );
101 }
102
103 // Initialize the API.
104 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' );
105
106 // Return Legacy Form HTML.
107 return $api->get_form_html( $id );
108 }
109
110 // If the form's format is not an inline form, add the inline script before the closing </body> tag.
111 // This prevents a modal form's overlay being constrained by the WordPress Theme's styles,
112 // and accidentally embedding the same non-inline form twice, which would result in e.g. the same modal form
113 // displaying twice.
114 if ( $this->resources[ $id ]['format'] !== 'inline' ) {
115 add_filter(
116 'convertkit_output_scripts_footer',
117 function( $scripts ) use ( $id ) {
118
119 $scripts[] = array(
120 'async' => true,
121 'data-uid' => $this->resources[ $id ]['uid'],
122 'src' => $this->resources[ $id ]['embed_js'],
123 );
124
125 return $scripts;
126
127 }
128 );
129
130 // Don't return a script for output, as it'll be output in the site's footer.
131 return '';
132 }
133
134 // If here, return Form <script> embed now, as we want the inline form to display at this specific point of the content.
135 return '<script async data-uid="' . esc_attr( $this->resources[ $id ]['uid'] ) . '" src="' . esc_attr( $this->resources[ $id ]['embed_js'] ) . '"></script>';
136
137 }
138
139 }
140