| 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 |
<?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 |
// phpcs:disable WordPress.Security.EscapeOutput |
| 124 |
echo $args['before_widget']; |
| 125 |
if ( $instance['title'] ) { |
| 126 |
echo $args['before_title']; |
| 127 |
echo esc_attr( $instance['title'] ); |
| 128 |
echo $args['after_title']; |
| 129 |
} |
| 130 |
echo $form; |
| 131 |
echo $args['after_widget']; |
| 132 |
// phpcs:enable |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Updates a particular instance of a widget. |
| 138 |
* |
| 139 |
* @see WP_Widget->update |
| 140 |
* @param array $new_instance Updated widget settings. |
| 141 |
* @param array $old_instance Original widget settings. |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function update( $new_instance, $old_instance ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter |
| 145 |
|
| 146 |
return array( |
| 147 |
'title' => sanitize_text_field( $new_instance['title'] ), |
| 148 |
'form' => sanitize_text_field( $new_instance['form'] ), |
| 149 |
); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|