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

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

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