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

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

153 lines 4.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 * Registers a ConvertKit Form Widget with WordPress' widgets functionality.
15 *
16 * Since 1.9.7.6, the ConvertKit Form Block can be used on WordPress 5.8+ sites
17 * that make use of the block editor for Widgets at Apperance > Widgets, and therefore
18 * on WordPress 5.8+, this widget will appear as a 'legacy' widget in WordPress.
19 *
20 * It's retained as not all users may be on WordPress 5.8+, and users may already
21 * have this widget configured on their site.
22 *
23 * @author ConvertKit
24 * @version 1.4.3
25 */
26 class CK_Widget_Form extends WP_Widget {
27
28 /**
29 * Constructor.
30 */
31 public function __construct() {
32
33 parent::__construct(
34 'convertkit_form',
35 __( 'ConvertKit Form (Legacy Widget)', 'convertkit' ),
36 array(
37 'classname' => 'convertkit widget_convertkit_form',
38 'description' => __( 'Display a ConvertKit form.', 'convertkit' ),
39 'customize_selective_refresh' => true,
40 )
41 );
42
43 }
44
45 /**
46 * Outputs the settings update form.
47 *
48 * @since 1.4.3
49 *
50 * @param array $instance Current settings.
51 * @return string Default return is 'noform'
52 */
53 public function form( $instance ) {
54
55 $forms = new ConvertKit_Resource_Forms( 'output_form' );
56
57 // Bail if no Forms exist.
58 if ( ! $forms->exist() ) {
59 ?>
60 <p>
61 <?php esc_html_e( 'To display a ConvertKit Form, at least one form must be defined in your ConvertKit Account.', 'convertkit' ); ?>
62 </p>
63 <?php
64 }
65
66 // If the widget's settings are not defined, set them now to avoid undefined index errors.
67 if ( ! array_key_exists( 'title', $instance ) ) {
68 $instance['title'] = '';
69 }
70 if ( ! array_key_exists( 'form', $instance ) ) {
71 $instance['form'] = '';
72 }
73 ?>
74 <p>
75 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'convertkit' ); ?></label>
76 <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php esc_attr( $instance['title'] ); ?>" />
77 </p>
78 <p>
79 <label for="<?php echo esc_attr( $this->get_field_id( 'form' ) ); ?>"><?php esc_html_e( 'Form', 'convertkit' ); ?></label>
80 <select name="<?php echo esc_attr( $this->get_field_name( 'form' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'form' ) ); ?>" size="1">
81 <?php
82 foreach ( $forms->get() as $form ) {
83 ?>
84 <option value="<?php echo esc_attr( $form['id'] ); ?>"<?php selected( $form['id'], $instance['form'] ); ?>><?php echo esc_attr( $form['name'] ); ?></option>
85 <?php
86 }
87 ?>
88 </select>
89 </p>
90 <?php
91
92 return '';
93
94 }
95
96 /**
97 * Output widget.
98 *
99 * @see WP_Widget
100 *
101 * @param array $args Widget arguments.
102 * @param array $instance Widget settings.
103 */
104 public function widget( $args, $instance ) {
105
106 // Bail if no form is defined.
107 if ( ! isset( $instance['form'] ) ) {
108 return;
109 }
110
111 // Get Form.
112 $forms = new ConvertKit_Resource_Forms( 'output_form' );
113 $form = $forms->get_html( $instance['form'] );
114
115 // Bail if the Form has an error.
116 if ( is_wp_error( $form ) ) {
117 return;
118 }
119
120 // Output Form.
121 // $args already escaped as supplied by WordPress, so we don't need to escape them again.
122 // phpcs:disable WordPress.Security.EscapeOutput
123 echo $args['before_widget'];
124 if ( $instance['title'] ) {
125 echo $args['before_title'];
126 echo esc_attr( $instance['title'] );
127 echo $args['after_title'];
128 }
129 echo $form;
130 echo $args['after_widget'];
131 // phpcs:enable
132
133 }
134
135 /**
136 * Updates a particular instance of a widget.
137 *
138 * @see WP_Widget->update
139 * @param array $new_instance Updated widget settings.
140 * @param array $old_instance Original widget settings.
141 * @return array
142 */
143 public function update( $new_instance, $old_instance ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
144
145 return array(
146 'title' => sanitize_text_field( $new_instance['title'] ),
147 'form' => sanitize_text_field( $new_instance['form'] ),
148 );
149
150 }
151
152 }
153