PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.0
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.0
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 2.3.2 All 195 releases
convertkit / includes / class-ck-widget-form.php

class-ck-widget-form.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 1.9.0, at includes/class-ck-widget-form.php

306 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Form Widget class
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Form Widget.
15 *
16 * @author ConvertKit
17 * @category Widgets
18 * @version 1.0.0
19 * @extends WP_Widget
20 */
21 class CK_Widget_Form extends WP_Widget {
22
23 /**
24 * CSS class.
25 *
26 * @var string
27 */
28 public $widget_cssclass;
29
30 /**
31 * Widget description.
32 *
33 * @var string
34 */
35 public $widget_description;
36
37 /**
38 * Widget ID.
39 *
40 * @var string
41 */
42 public $widget_id;
43
44 /**
45 * Widget name.
46 *
47 * @var string
48 */
49 public $widget_name;
50
51 /**
52 * Settings.
53 *
54 * @var array
55 */
56 public $settings;
57
58 /**
59 * Constructor.
60 */
61 public function __construct() {
62 $this->widget_cssclass = 'convertkit widget_convertkit_form';
63 $this->widget_description = __( 'Display a ConvertKit form.', 'convertkit' );
64 $this->widget_id = 'convertkit_form';
65 $this->widget_name = __( 'ConvertKit Form', 'convertkit' );
66
67 $forms = get_option( 'convertkit_forms', array() );
68
69 $this->settings = array(
70 'title' => array(
71 'type' => 'text',
72 'std' => __( 'ConvertKit Form', 'convertkit' ),
73 'label' => __( 'Title', 'convertkit' ),
74 ),
75 'form' => array(
76 'type' => 'select',
77 'std' => '',
78 'label' => __( 'Form', 'convertkit' ),
79 'options' => $forms,
80 ),
81 );
82
83 $widget_ops = array(
84 'classname' => $this->widget_cssclass,
85 'description' => $this->widget_description,
86 'customize_selective_refresh' => true,
87 );
88
89 parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
90
91 }
92
93 /**
94 * Output the html at the start of a widget.
95 *
96 * @param array $args Widget arguments.
97 * @param array $instance Widget settings.
98 */
99 public function widget_start( $args, $instance ) {
100 echo $args['before_widget'];
101 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
102 if ( $title ) {
103 echo $args['before_title'] . $title . $args['after_title'];
104 }
105 }
106
107 /**
108 * Output the html at the end of a widget.
109 *
110 * @param array $args After widget setting.
111 */
112 public function widget_end( $args ) {
113 echo $args['after_widget'];
114 }
115
116 /**
117 * Output widget.
118 *
119 * @see WP_Widget
120 *
121 * @param array $args Widget arguments.
122 * @param array $instance Widget settings.
123 */
124 public function widget( $args, $instance ) {
125
126 if ( ! isset( $instance['form'] ) ) {
127 return;
128 }
129
130 $api = WP_ConvertKit::get_api();
131 $form_id = $instance['form'];
132 $forms = get_option( 'convertkit_forms' );
133
134 if ( isset( $forms[ $form_id ]['uid'] ) ) {
135 // new form
136 $this->widget_start( $args, $instance );
137 $tag = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
138 echo $tag;
139 $this->widget_end( $args );
140 } else {
141 // old form
142 $url = add_query_arg( array(
143 'api_key' => WP_ConvertKit::get_api_key(),
144 'v' => WP_ConvertKit::get_forms_version(),
145 ),
146 'https://forms.convertkit.com/' . $form_id . '.html'
147 );
148
149 $form_markup = $api->get_resource( $url );
150
151 if ( $api && ! is_wp_error( $api ) ) {
152 ob_start();
153
154 $this->widget_start( $args, $instance );
155 echo $form_markup;
156 $this->widget_end( $args );
157
158 $content = ob_get_clean();
159
160 echo $content;
161 }
162 }
163 }
164
165 /**
166 * Outputs the settings update form.
167 *
168 * @param array $instance Widget settings.
169 * @return null
170 */
171 public function form( $instance ) {
172
173 if ( empty( $this->settings ) ) {
174 return;
175 }
176
177 foreach ( $this->settings as $key => $setting ) {
178
179 $class = isset( $setting['class'] ) ? $setting['class'] : '';
180 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
181
182 switch ( $setting['type'] ) {
183
184 case 'text' :
185 ?>
186 <p>
187 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
188 <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
189 </p>
190 <?php
191 break;
192
193 case 'number' :
194 ?>
195 <p>
196 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
197 <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" />
198 </p>
199 <?php
200 break;
201
202 case 'select' :
203 if ( empty( $setting['options'] ) ) {
204 $query_args = array(
205 'page' => WP_ConvertKit::SETTINGS_PAGE_SLUG,
206 );
207 $settings_page_url = add_query_arg( $query_args, admin_url( 'options-general.php' ) );
208 ?>
209 <p><?php echo __( 'No forms were returned from ConvertKit.','convertkit' ); ?></p>
210 <?php /* translators: 1: settings page url */ ?>
211 <p><?php echo sprintf( __( 'Please check the <a href="%s">settings</a>.','convertkit' ), $settings_page_url ); ?></p>
212 <?php
213 } else {
214 ?>
215 <p>
216 <label
217 for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
218 <select class="widefat <?php echo esc_attr( $class ); ?>"
219 id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
220 name="<?php echo $this->get_field_name( $key ); ?>">
221 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
222 <option
223 value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value['name'] ); ?></option>
224 <?php endforeach; ?>
225 </select>
226 </p>
227 <?php
228 }
229 break;
230
231 case 'textarea' :
232 ?>
233 <p>
234 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
235 <textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea>
236 <?php if ( isset( $setting['desc'] ) ) : ?>
237 <small><?php echo esc_html( $setting['desc'] ); ?></small>
238 <?php endif; ?>
239 </p>
240 <?php
241 break;
242
243 case 'checkbox' :
244 ?>
245 <p>
246 <input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> />
247 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
248 </p>
249 <?php
250 break;
251 } // End switch().
252 } // End foreach().
253 }
254
255 /**
256 * Updates a particular instance of a widget.
257 *
258 * @see WP_Widget->update
259 * @param array $new_instance Updated widget settings.
260 * @param array $old_instance Original widget settings.
261 * @return array
262 */
263 public function update( $new_instance, $old_instance ) {
264
265 $instance = $old_instance;
266
267 if ( empty( $this->settings ) ) {
268 return $instance;
269 }
270
271 // Loop settings and get values to save.
272 foreach ( $this->settings as $key => $setting ) {
273 if ( ! isset( $setting['type'] ) ) {
274 continue;
275 }
276
277 // Format the value based on settings type.
278 switch ( $setting['type'] ) {
279 case 'number' :
280 $instance[ $key ] = absint( $new_instance[ $key ] );
281
282 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
283 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
284 }
285
286 if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
287 $instance[ $key ] = min( $instance[ $key ], $setting['max'] );
288 }
289 break;
290 case 'textarea' :
291 $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
292 break;
293 case 'checkbox' :
294 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
295 break;
296 default:
297 $instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
298 break;
299 }
300 }
301
302 return $instance;
303 }
304
305 }
306