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

155 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'] ); ?>>
85 <?php echo esc_attr( $form['name'] ); ?>
86 </option>
87 <?php
88 }
89 ?>
90 </select>
91 </p>
92 <?php
93
94 return '';
95
96 }
97
98 /**
99 * Output widget.
100 *
101 * @see WP_Widget
102 *
103 * @param array $args Widget arguments.
104 * @param array $instance Widget settings.
105 */
106 public function widget( $args, $instance ) {
107
108 // Bail if no form is defined.
109 if ( ! isset( $instance['form'] ) ) {
110 return;
111 }
112
113 // Get Form.
114 $forms = new ConvertKit_Resource_Forms( 'output_form' );
115 $form = $forms->get_html( $instance['form'] );
116
117 // Bail if the Form has an error.
118 if ( is_wp_error( $form ) ) {
119 return;
120 }
121
122 // Output Form.
123 // $args already escaped as supplied by WordPress, so we don't need to escape them again.
124 // phpcs:disable WordPress.Security.EscapeOutput
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