PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.6.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.6.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 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.6.3, at includes/class-ck-widget-form.php

304 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 $tag = '<script async data-uid="' . $forms[ $form_id ]['uid'] . '" src="' . $forms[ $form_id ]['embed_js'] . '"></script>';
137 echo $tag;
138 } else {
139 // old form
140 $url = add_query_arg( array(
141 'api_key' => WP_ConvertKit::get_api_key(),
142 'v' => WP_ConvertKit::get_forms_version(),
143 ),
144 'https://forms.convertkit.com/' . $form_id . '.html'
145 );
146
147 $form_markup = $api->get_resource( $url );
148
149 if ( $api && ! is_wp_error( $api ) ) {
150 ob_start();
151
152 $this->widget_start( $args, $instance );
153 echo $form_markup;
154 $this->widget_end( $args );
155
156 $content = ob_get_clean();
157
158 echo $content;
159 }
160 }
161 }
162
163 /**
164 * Outputs the settings update form.
165 *
166 * @param array $instance Widget settings.
167 * @return null
168 */
169 public function form( $instance ) {
170
171 if ( empty( $this->settings ) ) {
172 return;
173 }
174
175 foreach ( $this->settings as $key => $setting ) {
176
177 $class = isset( $setting['class'] ) ? $setting['class'] : '';
178 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
179
180 switch ( $setting['type'] ) {
181
182 case 'text' :
183 ?>
184 <p>
185 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
186 <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 ); ?>" />
187 </p>
188 <?php
189 break;
190
191 case 'number' :
192 ?>
193 <p>
194 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
195 <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 ); ?>" />
196 </p>
197 <?php
198 break;
199
200 case 'select' :
201 if ( empty( $setting['options'] ) ) {
202 $query_args = array(
203 'page' => WP_ConvertKit::SETTINGS_PAGE_SLUG,
204 );
205 $settings_page_url = add_query_arg( $query_args, admin_url( 'options-general.php' ) );
206 ?>
207 <p><?php echo __( 'No forms were returned from ConvertKit.','convertkit' ); ?></p>
208 <?php /* translators: 1: settings page url */ ?>
209 <p><?php echo sprintf( __( 'Please check the <a href="%s">settings</a>.','convertkit' ), $settings_page_url ); ?></p>
210 <?php
211 } else {
212 ?>
213 <p>
214 <label
215 for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
216 <select class="widefat <?php echo esc_attr( $class ); ?>"
217 id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
218 name="<?php echo $this->get_field_name( $key ); ?>">
219 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
220 <option
221 value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value['name'] ); ?></option>
222 <?php endforeach; ?>
223 </select>
224 </p>
225 <?php
226 }
227 break;
228
229 case 'textarea' :
230 ?>
231 <p>
232 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
233 <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>
234 <?php if ( isset( $setting['desc'] ) ) : ?>
235 <small><?php echo esc_html( $setting['desc'] ); ?></small>
236 <?php endif; ?>
237 </p>
238 <?php
239 break;
240
241 case 'checkbox' :
242 ?>
243 <p>
244 <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 ); ?> />
245 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
246 </p>
247 <?php
248 break;
249 } // End switch().
250 } // End foreach().
251 }
252
253 /**
254 * Updates a particular instance of a widget.
255 *
256 * @see WP_Widget->update
257 * @param array $new_instance Updated widget settings.
258 * @param array $old_instance Original widget settings.
259 * @return array
260 */
261 public function update( $new_instance, $old_instance ) {
262
263 $instance = $old_instance;
264
265 if ( empty( $this->settings ) ) {
266 return $instance;
267 }
268
269 // Loop settings and get values to save.
270 foreach ( $this->settings as $key => $setting ) {
271 if ( ! isset( $setting['type'] ) ) {
272 continue;
273 }
274
275 // Format the value based on settings type.
276 switch ( $setting['type'] ) {
277 case 'number' :
278 $instance[ $key ] = absint( $new_instance[ $key ] );
279
280 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
281 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
282 }
283
284 if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
285 $instance[ $key ] = min( $instance[ $key ], $setting['max'] );
286 }
287 break;
288 case 'textarea' :
289 $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
290 break;
291 case 'checkbox' :
292 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
293 break;
294 default:
295 $instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
296 break;
297 }
298 }
299
300 return $instance;
301 }
302
303 }
304