admin
1 year ago
emails
1 year ago
fields
1 year ago
functions
1 year ago
providers
1 year ago
templates
1 year ago
class-db.php
1 year ago
class-fields.php
1 year ago
class-form.php
1 year ago
class-install.php
1 year ago
class-process.php
1 year ago
class-providers.php
1 year ago
class-templates.php
1 year ago
class-widget.php
1 year ago
deprecated.php
1 year ago
functions-list.php
1 year ago
functions.php
1 year ago
integrations.php
1 year ago
class-widget.php
183 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 | |
| 70 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 71 | |
| 72 | // Title. |
| 73 | if ( ! empty( $instance['title'] ) ) { |
| 74 | echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; // phpcs:ignore |
| 75 | } |
| 76 | |
| 77 | // Form. |
| 78 | if ( ! empty( $instance['form_id'] ) ) { |
| 79 | wpforms()->obj( 'frontend' )->output( absint( $instance['form_id'] ), $instance['show_title'], $instance['show_desc'] ); |
| 80 | } |
| 81 | |
| 82 | echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Deal with the settings when they are saved by the admin. Here is |
| 87 | * where any validation should be dealt with. |
| 88 | * |
| 89 | * @since 1.0.2 |
| 90 | * |
| 91 | * @param array $new_instance An array of new settings as submitted by the admin. |
| 92 | * @param array $old_instance An array of the previous settings. |
| 93 | * |
| 94 | * @return array The validated and (if necessary) amended settings |
| 95 | */ |
| 96 | public function update( $new_instance, $old_instance ) { |
| 97 | |
| 98 | $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 99 | $new_instance['form_id'] = ! empty( $new_instance['form_id'] ) ? (int) $new_instance['form_id'] : 0; |
| 100 | $new_instance['show_title'] = isset( $new_instance['show_title'] ) && $new_instance['show_title'] ? '1' : false; |
| 101 | $new_instance['show_desc'] = isset( $new_instance['show_desc'] ) && $new_instance['show_desc'] ? '1' : false; |
| 102 | |
| 103 | return $new_instance; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Display the form for this widget on the Widgets page of the WP Admin area. |
| 108 | * |
| 109 | * @since 1.0.2 |
| 110 | * |
| 111 | * @param array $instance An array of the current settings for this widget. |
| 112 | */ |
| 113 | public function form( $instance ) { |
| 114 | |
| 115 | // Merge with defaults. |
| 116 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 117 | ?> |
| 118 | <p> |
| 119 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 120 | <?php echo esc_html( _x( 'Title:', 'Widget', 'wpforms-lite' ) ); ?> |
| 121 | </label> |
| 122 | <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"/> |
| 123 | </p> |
| 124 | <p> |
| 125 | <label for="<?php echo esc_attr( $this->get_field_id( 'form_id' ) ); ?>"> |
| 126 | <?php echo esc_html( _x( 'Form:', 'Widget', 'wpforms-lite' ) ); ?> |
| 127 | </label> |
| 128 | <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' ) ); ?>"> |
| 129 | <?php |
| 130 | $forms = wpforms()->obj( 'form' )->get(); |
| 131 | |
| 132 | if ( ! empty( $forms ) ) { |
| 133 | echo '<option value="" selected disabled>' . esc_html_x( 'Select your form', 'Widget', 'wpforms-lite' ) . '</option>'; |
| 134 | |
| 135 | foreach ( $forms as $form ) { |
| 136 | echo '<option value="' . esc_attr( $form->ID ) . '" ' . selected( $instance['form_id'], $form->ID, false ) . '>' . esc_html( $form->post_title ) . '</option>'; |
| 137 | } |
| 138 | } else { |
| 139 | echo '<option value="">' . esc_html_x( 'No forms', 'Widget', 'wpforms-lite' ) . '</option>'; |
| 140 | } |
| 141 | ?> |
| 142 | </select> |
| 143 | </p> |
| 144 | <p> |
| 145 | <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'] ); ?>> |
| 146 | <label for="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>"> |
| 147 | <?php echo esc_html( _x( 'Display form name', 'Widget', 'wpforms-lite' ) ); ?> |
| 148 | </label> |
| 149 | <br> |
| 150 | <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'] ); ?>> |
| 151 | <label for="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>"> |
| 152 | <?php echo esc_html( _x( 'Display form description', 'Widget', 'wpforms-lite' ) ); ?> |
| 153 | </label> |
| 154 | </p> |
| 155 | <?php |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Register the WPForms widget. |
| 161 | * |
| 162 | * @since 1.0.2 |
| 163 | */ |
| 164 | function wpforms_register_widgets() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks |
| 165 | |
| 166 | // Hide legacy widget. This filter is available from WP 5.8. |
| 167 | // phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.PHPDocHooks.RequiredHookDocumentation |
| 168 | add_filter( |
| 169 | 'widget_types_to_hide_from_legacy_widget_block', |
| 170 | static function ( $widget_types ) { |
| 171 | |
| 172 | $widget_types[] = 'wpforms-widget'; |
| 173 | |
| 174 | return $widget_types; |
| 175 | } |
| 176 | ); |
| 177 | // phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName, WPForms.Comments.PHPDocHooks.RequiredHookDocumentation |
| 178 | |
| 179 | register_widget( 'WPForms_Widget' ); |
| 180 | } |
| 181 | |
| 182 | add_action( 'widgets_init', 'wpforms_register_widgets' ); |
| 183 |