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