admin
5 months ago
emails
1 year ago
fields
5 months ago
functions
5 months ago
providers
10 months ago
templates
10 months ago
class-db.php
1 year ago
class-fields.php
1 year ago
class-form.php
8 months ago
class-install.php
1 year ago
class-process.php
5 months ago
class-providers.php
1 year ago
class-templates.php
2 years ago
class-widget.php
1 year ago
deprecated.php
8 months ago
functions-list.php
3 years ago
functions.php
11 months ago
integrations.php
1 year ago
class-widget.php
193 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * WPForms widget. |
| 9 | * |
| 10 | * @since 1.0.2 |
| 11 | */ |
| 12 | class WPForms_Widget extends WP_Widget { |
| 13 | |
| 14 | /** |
| 15 | * Hold widget settings defaults, populated in constructor. |
| 16 | * |
| 17 | * @since 1.0.2 |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $defaults; |
| 22 | |
| 23 | /** |
| 24 | * Constructor. |
| 25 | * |
| 26 | * @since 1.0.2 |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | |
| 30 | // Widget defaults. |
| 31 | $this->defaults = [ |
| 32 | 'title' => '', |
| 33 | 'form_id' => '', |
| 34 | 'show_title' => false, |
| 35 | 'show_desc' => false, |
| 36 | ]; |
| 37 | |
| 38 | // Widget Slug. |
| 39 | $widget_slug = 'wpforms-widget'; |
| 40 | |
| 41 | // Widget basics. |
| 42 | $widget_ops = [ |
| 43 | 'classname' => $widget_slug, |
| 44 | 'description' => esc_html_x( 'Display a form.', 'Widget', 'wpforms-lite' ), |
| 45 | 'show_instance_in_rest' => false, |
| 46 | ]; |
| 47 | |
| 48 | // Widget controls. |
| 49 | $control_ops = [ |
| 50 | 'id_base' => $widget_slug, |
| 51 | ]; |
| 52 | |
| 53 | // Load widget. |
| 54 | parent::__construct( $widget_slug, esc_html_x( 'WPForms', 'Widget', 'wpforms-lite' ), $widget_ops, $control_ops ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Output the HTML for this widget. |
| 59 | * |
| 60 | * @since 1.0.2 |
| 61 | * |
| 62 | * @param array $args An array of standard parameters for widgets in this theme. |
| 63 | * @param array $instance An array of settings for this widget instance. |
| 64 | */ |
| 65 | public function widget( $args, $instance ) { |
| 66 | |
| 67 | // Merge with defaults. |
| 68 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 69 | $args = wp_parse_args( |
| 70 | $args, |
| 71 | [ |
| 72 | 'before_widget' => '', |
| 73 | 'after_widget' => '', |
| 74 | 'before_title' => '', |
| 75 | 'after_title' => '', |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 80 | echo $args['before_widget']; |
| 81 | |
| 82 | if ( ! empty( $instance['title'] ) ) { |
| 83 | // phpcs:ignore |
| 84 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; |
| 85 | } |
| 86 | |
| 87 | if ( ! empty( $instance['form_id'] ) ) { |
| 88 | wpforms()->obj( 'frontend' )->output( absint( $instance['form_id'] ), (bool) $instance['show_title'], (bool) $instance['show_desc'] ); |
| 89 | } |
| 90 | |
| 91 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 92 | echo $args['after_widget']; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Deal with the settings when they are saved by the admin. Here is |
| 97 | * where any validation should be dealt with. |
| 98 | * |
| 99 | * @since 1.0.2 |
| 100 | * |
| 101 | * @param array $new_instance An array of new settings as submitted by the admin. |
| 102 | * @param array $old_instance An array of the previous settings. |
| 103 | * |
| 104 | * @return array The validated and (if necessary) amended settings |
| 105 | */ |
| 106 | public function update( $new_instance, $old_instance ) { |
| 107 | |
| 108 | $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 109 | $new_instance['form_id'] = ! empty( $new_instance['form_id'] ) ? (int) $new_instance['form_id'] : 0; |
| 110 | $new_instance['show_title'] = isset( $new_instance['show_title'] ) && $new_instance['show_title'] ? '1' : false; |
| 111 | $new_instance['show_desc'] = isset( $new_instance['show_desc'] ) && $new_instance['show_desc'] ? '1' : false; |
| 112 | |
| 113 | return $new_instance; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Display the form for this widget on the Widgets page of the WP Admin area. |
| 118 | * |
| 119 | * @since 1.0.2 |
| 120 | * |
| 121 | * @param array $instance An array of the current settings for this widget. |
| 122 | */ |
| 123 | public function form( $instance ) { |
| 124 | |
| 125 | // Merge with defaults. |
| 126 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 127 | ?> |
| 128 | <p> |
| 129 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 130 | <?php echo esc_html( _x( 'Title:', 'Widget', 'wpforms-lite' ) ); ?> |
| 131 | </label> |
| 132 | <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat"/> |
| 133 | </p> |
| 134 | <p> |
| 135 | <label for="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>"> |
| 136 | <?php echo esc_html( _x( 'Form:', 'Widget', 'wpforms-lite' ) ); ?> |
| 137 | </label> |
| 138 | <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'form_id' ) ); ?>"> |
| 139 | <?php |
| 140 | $forms = wpforms()->obj( 'form' )->get(); |
| 141 | |
| 142 | if ( ! empty( $forms ) ) { |
| 143 | echo '<option value="" selected disabled>' . esc_html_x( 'Select your form', 'Widget', 'wpforms-lite' ) . '</option>'; |
| 144 | |
| 145 | foreach ( $forms as $form ) { |
| 146 | echo '<option value="' . esc_attr( $form->ID ) . '" ' . selected( $instance['form_id'], $form->ID, false ) . '>' . esc_html( $form->post_title ) . '</option>'; |
| 147 | } |
| 148 | } else { |
| 149 | echo '<option value="">' . esc_html_x( 'No forms', 'Widget', 'wpforms-lite' ) . '</option>'; |
| 150 | } |
| 151 | ?> |
| 152 | </select> |
| 153 | </p> |
| 154 | <p> |
| 155 | <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_title' ) ); ?>" <?php checked( '1', $instance['show_title'] ); ?>> |
| 156 | <label for="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>"> |
| 157 | <?php echo esc_html( _x( 'Display form name', 'Widget', 'wpforms-lite' ) ); ?> |
| 158 | </label> |
| 159 | <br> |
| 160 | <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_desc' ) ); ?>" <?php checked( '1', $instance['show_desc'] ); ?>> |
| 161 | <label for="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>"> |
| 162 | <?php echo esc_html( _x( 'Display form description', 'Widget', 'wpforms-lite' ) ); ?> |
| 163 | </label> |
| 164 | </p> |
| 165 | <?php |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Register the WPForms widget. |
| 171 | * |
| 172 | * @since 1.0.2 |
| 173 | */ |
| 174 | function wpforms_register_widgets() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks |
| 175 | |
| 176 | // Hide legacy widget. This filter is available from WP 5.8. |
| 177 | // phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.PHPDocHooks.RequiredHookDocumentation |
| 178 | add_filter( |
| 179 | 'widget_types_to_hide_from_legacy_widget_block', |
| 180 | static function ( $widget_types ) { |
| 181 | |
| 182 | $widget_types[] = 'wpforms-widget'; |
| 183 | |
| 184 | return $widget_types; |
| 185 | } |
| 186 | ); |
| 187 | // phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.PHPDocHooks.RequiredHookDocumentation |
| 188 | |
| 189 | register_widget( 'WPForms_Widget' ); |
| 190 | } |
| 191 | |
| 192 | add_action( 'widgets_init', 'wpforms_register_widgets' ); |
| 193 |