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