admin
10 years ago
emails
10 years ago
fields
10 years ago
templates
10 years ago
class-db.php
10 years ago
class-fields.php
10 years ago
class-form.php
10 years ago
class-frontend.php
10 years ago
class-install.php
10 years ago
class-logging.php
10 years ago
class-preview.php
10 years ago
class-process.php
10 years ago
class-smart-tags.php
10 years ago
class-templates.php
10 years ago
class-widget.php
10 years ago
functions.php
10 years ago
class-widget.php
142 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WPForms widget. |
| 4 | * |
| 5 | * @package WPForms |
| 6 | * @author WPForms |
| 7 | * @since 1.0.2 |
| 8 | * @license GPL-2.0+ |
| 9 | * @copyright Copyright (c) 2016, WPForms LLC |
| 10 | */ |
| 11 | class WPForms_Widget extends WP_Widget { |
| 12 | |
| 13 | /** |
| 14 | * Holds widget settings defaults, populated in constructor. |
| 15 | * |
| 16 | * @since 1.0.2 |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $defaults; |
| 20 | |
| 21 | /** |
| 22 | * Constructor |
| 23 | * |
| 24 | * @since 1.0.2 |
| 25 | */ |
| 26 | function __construct() { |
| 27 | |
| 28 | // widget defaults |
| 29 | $this->defaults = array( |
| 30 | 'title' => '', |
| 31 | 'form_id' => '', |
| 32 | 'show_title' => false, |
| 33 | 'show_desc' => false, |
| 34 | ); |
| 35 | |
| 36 | // Widget Slug |
| 37 | $widget_slug = 'wpforms-widget'; |
| 38 | |
| 39 | // widget basics |
| 40 | $widget_ops = array( |
| 41 | 'classname' => $widget_slug, |
| 42 | 'description' => 'Display a form.' |
| 43 | ); |
| 44 | |
| 45 | // widget controls |
| 46 | $control_ops = array( |
| 47 | 'id_base' => $widget_slug, |
| 48 | ); |
| 49 | |
| 50 | // load widget |
| 51 | parent::__construct( $widget_slug, 'WPForms', $widget_ops, $control_ops ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Outputs the HTML for this widget. |
| 56 | * |
| 57 | * @since 1.0.2 |
| 58 | * @param array $args An array of standard parameters for widgets in this theme |
| 59 | * @param array $instance An array of settings for this widget instance |
| 60 | */ |
| 61 | function widget( $args, $instance ) { |
| 62 | |
| 63 | extract( $args ); |
| 64 | |
| 65 | // Merge with defaults |
| 66 | $instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 67 | |
| 68 | echo $before_widget; |
| 69 | |
| 70 | // Title |
| 71 | if ( !empty( $instance['title'] ) ) { |
| 72 | echo $before_title . apply_filters( 'widget_title' , $instance['title'] ) . $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 $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 | * @param array $new_instance An array of new settings as submitted by the admin |
| 89 | * @param array $old_instance An array of the previous settings |
| 90 | * @return array The validated and (if necessary) amended settings |
| 91 | */ |
| 92 | function update( $new_instance, $old_instance ) { |
| 93 | |
| 94 | $new_instance['title'] = strip_tags( $new_instance['title'] ); |
| 95 | $new_instance['form_id'] = absint( $new_instance['form_id'] ); |
| 96 | $new_instance['show_title'] = isset( $new_instance['show_title'] ) ? '1' : false; |
| 97 | $new_instance['show_desc'] = isset( $new_instance['show_desc'] ) ? '1' : false; |
| 98 | |
| 99 | return $new_instance; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
| 104 | * |
| 105 | * @since 1.0.2 |
| 106 | * @param array $instance An array of the current settings for this widget |
| 107 | */ |
| 108 | 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 $this->get_field_id( 'title' ); ?>">Title:</label> |
| 115 | <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" /> |
| 116 | </p> |
| 117 | <p> |
| 118 | <label for="<?php echo $this->get_field_id( 'form_id' ); ?>">Form:</label> |
| 119 | <select id="<?php echo $this->get_field_id( 'form_id' ); ?>" name="<?php echo $this->get_field_name( 'form_id' ); ?>" class="widefat"> |
| 120 | <?php |
| 121 | $forms = wpforms()->form->get(); |
| 122 | if ( !empty( $forms ) ) { |
| 123 | foreach ( $forms as $form ) { |
| 124 | printf( '<option value="%d" %s>%s</option>', $form->ID, selected( $instance['form_id'], $form->ID, false ), esc_html( $form->post_title ) ); |
| 125 | } |
| 126 | } else { |
| 127 | printf( '<option value="">%s</option>', __( 'No forms', 'wpforms' ) ); |
| 128 | } |
| 129 | ?> |
| 130 | </select> |
| 131 | </p> |
| 132 | <p> |
| 133 | <input type="checkbox" id="<?php echo $this->get_field_id( 'show_title' ); ?>" name="<?php echo $this->get_field_name( 'show_title' ); ?>" <?php checked( '1', $instance['show_title'] ); ?>> |
| 134 | <label for="<?php echo $this->get_field_id( 'show_title' ); ?>"><?php _e( 'Display form title', 'wpforms' ); ?></label> |
| 135 | <br> |
| 136 | <input type="checkbox" id="<?php echo $this->get_field_id( 'show_desc' ); ?>" name="<?php echo $this->get_field_name( 'show_desc' ); ?>" <?php checked( '1', $instance['show_desc'] ); ?>> |
| 137 | <label for="<?php echo $this->get_field_id( 'show_desc' ); ?>"><?php _e( 'Display form description', 'wpforms' ); ?></label> |
| 138 | </p> |
| 139 | <?php |
| 140 | } |
| 141 | } |
| 142 | add_action( 'widgets_init', create_function( '', "register_widget('WPForms_Widget');" ) ); |