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

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

313 lines 8.1 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 $this->settings = array(
67 'title' => array(
68 'type' => 'text',
69 'std' => __( 'ConvertKit Form', 'convertkit' ),
70 'label' => __( 'Title', 'convertkit' ),
71 ),
72 'form' => array(
73 'type' => 'select',
74 'std' => '',
75 'label' => __( 'Form', 'convertkit' ),
76 'options' => $this->get_forms(),
77 ),
78 );
79
80 $widget_ops = array(
81 'classname' => $this->widget_cssclass,
82 'description' => $this->widget_description,
83 'customize_selective_refresh' => true,
84 );
85
86 parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
87
88 }
89
90 /**
91 * Output the html at the start of a widget.
92 *
93 * @param array $args Widget arguments.
94 * @param array $instance Widget settings.
95 */
96 public function widget_start( $args, $instance ) {
97 echo $args['before_widget'];
98 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
99 if ( $title ) {
100 echo $args['before_title'] . $title . $args['after_title'];
101 }
102 }
103
104 /**
105 * Output the html at the end of a widget.
106 *
107 * @param array $args After widget setting.
108 */
109 public function widget_end( $args ) {
110 echo $args['after_widget'];
111 }
112
113 /**
114 * Output widget.
115 *
116 * @see WP_Widget
117 *
118 * @param array $args Widget arguments.
119 * @param array $instance Widget settings.
120 */
121 public function widget( $args, $instance ) {
122
123 if ( ! isset( $instance['form'] ) ) {
124 return;
125 }
126
127 $api = WP_ConvertKit::get_api();
128 $form_id = $instance['form'];
129
130 $url = add_query_arg( array(
131 'api_key' => WP_ConvertKit::get_api_key(),
132 'v' => WP_ConvertKit::get_forms_version(),
133 ),
134 'https://forms.convertkit.com/' . $form_id . '.html'
135 );
136
137 $form_markup = $api->get_resource( $url );
138
139 if ( $api && ! is_wp_error( $api ) ) {
140 ob_start();
141
142 $this->widget_start( $args, $instance );
143 echo $form_markup;
144 $this->widget_end( $args );
145
146 $content = ob_get_clean();
147
148 echo $content;
149 }
150 }
151
152 /**
153 * Outputs the settings update form.
154 *
155 * @param array $instance Widget settings.
156 * @return null
157 */
158 public function form( $instance ) {
159
160 if ( empty( $this->settings ) ) {
161 return;
162 }
163
164 foreach ( $this->settings as $key => $setting ) {
165
166 $class = isset( $setting['class'] ) ? $setting['class'] : '';
167 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
168
169 switch ( $setting['type'] ) {
170
171 case 'text' :
172 ?>
173 <p>
174 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
175 <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 ); ?>" />
176 </p>
177 <?php
178 break;
179
180 case 'number' :
181 ?>
182 <p>
183 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
184 <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 ); ?>" />
185 </p>
186 <?php
187 break;
188
189 case 'select' :
190 if ( empty( $setting['options'] ) ) {
191 $query_args = array(
192 'page' => WP_ConvertKit::SETTINGS_PAGE_SLUG,
193 );
194 $settings_page_url = add_query_arg( $query_args, admin_url( 'options-general.php' ) );
195 ?>
196 <p><?php echo __( 'No forms were returned from ConvertKit.','convertkit' ); ?></p>
197 <?php /* translators: 1: settings page url */ ?>
198 <p><?php echo sprintf( __( 'Please check the <a href="%s">settings</a>.','convertkit' ), $settings_page_url ); ?></p>
199 <?php
200 } else {
201 ?>
202 <p>
203 <label
204 for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
205 <select class="widefat <?php echo esc_attr( $class ); ?>"
206 id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
207 name="<?php echo $this->get_field_name( $key ); ?>">
208 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
209 <option
210 value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
211 <?php endforeach; ?>
212 </select>
213 </p>
214 <?php
215 }
216 break;
217
218 case 'textarea' :
219 ?>
220 <p>
221 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
222 <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>
223 <?php if ( isset( $setting['desc'] ) ) : ?>
224 <small><?php echo esc_html( $setting['desc'] ); ?></small>
225 <?php endif; ?>
226 </p>
227 <?php
228 break;
229
230 case 'checkbox' :
231 ?>
232 <p>
233 <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 ); ?> />
234 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
235 </p>
236 <?php
237 break;
238 } // End switch().
239 } // End foreach().
240 }
241
242 /**
243 * Updates a particular instance of a widget.
244 *
245 * @see WP_Widget->update
246 * @param array $new_instance Updated widget settings.
247 * @param array $old_instance Original widget settings.
248 * @return array
249 */
250 public function update( $new_instance, $old_instance ) {
251
252 $instance = $old_instance;
253
254 if ( empty( $this->settings ) ) {
255 return $instance;
256 }
257
258 // Loop settings and get values to save.
259 foreach ( $this->settings as $key => $setting ) {
260 if ( ! isset( $setting['type'] ) ) {
261 continue;
262 }
263
264 // Format the value based on settings type.
265 switch ( $setting['type'] ) {
266 case 'number' :
267 $instance[ $key ] = absint( $new_instance[ $key ] );
268
269 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
270 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
271 }
272
273 if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
274 $instance[ $key ] = min( $instance[ $key ], $setting['max'] );
275 }
276 break;
277 case 'textarea' :
278 $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
279 break;
280 case 'checkbox' :
281 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
282 break;
283 default:
284 $instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
285 break;
286 }
287 }
288
289 return $instance;
290 }
291
292 /**
293 * Get Forms from API
294 *
295 * @return array
296 */
297 public function get_forms() {
298
299 $api = WP_ConvertKit::get_api();
300 $forms_array = array();
301
302 if ( $api && ! is_wp_error( $api ) ) {
303 $forms = $api->get_resources( 'forms' );
304 foreach ( $forms as $form ) {
305 $forms_array[ $form['id'] ] = $form['name'];
306 }
307 }
308
309 return $forms_array;
310 }
311
312 }
313