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

155 lines 4.0 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 __( 'Kit Form (Legacy Widget)', 'convertkit' ),
36 array(
37 'classname' => 'convertkit widget_convertkit_form',
38 'description' => __( 'Display a Kit 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 Kit Form, at least one form must be defined in your Kit 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 <?php
81 echo $forms->get_select_field_all( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
82 esc_attr( $this->get_field_name( 'form' ) ),
83 esc_attr( $this->get_field_id( 'form' ) ),
84 array(
85 'widefat',
86 ),
87 esc_attr( $instance['form'] )
88 );
89 ?>
90 </p>
91 <?php
92
93 return '';
94
95 }
96
97 /**
98 * Output widget.
99 *
100 * @see WP_Widget
101 *
102 * @param array $args Widget arguments.
103 * @param array $instance Widget settings.
104 */
105 public function widget( $args, $instance ) {
106
107 // Bail if no form is defined.
108 if ( ! isset( $instance['form'] ) ) {
109 return;
110 }
111
112 // Get Form.
113 $forms = new ConvertKit_Resource_Forms( 'output_form' );
114 $form = $forms->get_html( $instance['form'] );
115
116 // Bail if the Form has an error.
117 if ( is_wp_error( $form ) ) {
118 return;
119 }
120
121 // Output Form.
122 // $args already escaped as supplied by WordPress, so we don't need to escape them again.
123 // $form could be a script or legacy form with varying HTML, so we don't want to escape it.
124 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
125 echo $args['before_widget'];
126 if ( $instance['title'] ) {
127 echo $args['before_title'];
128 echo esc_attr( $instance['title'] );
129 echo $args['after_title'];
130 }
131 echo $form;
132 echo $args['after_widget'];
133 // phpcs:enable
134
135 }
136
137 /**
138 * Updates a particular instance of a widget.
139 *
140 * @see WP_Widget->update
141 * @param array $new_instance Updated widget settings.
142 * @param array $old_instance Original widget settings.
143 * @return array
144 */
145 public function update( $new_instance, $old_instance ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
146
147 return array(
148 'title' => sanitize_text_field( $new_instance['title'] ),
149 'form' => sanitize_text_field( $new_instance['form'] ),
150 );
151
152 }
153
154 }
155