1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Widget_Form extends WP_Widget
  7: {
  8:       9:  10: 
 11:     protected $repository;
 12: 
 13:      14:  15: 
 16:     protected $controller;
 17: 
 18:      19:  20: 
 21:     protected $defaults;
 22: 
 23:     public function __construct()
 24:     {
 25:         $this->repository = Quform::getService('repository');
 26:         $this->controller = Quform::getService('formController');
 27: 
 28:         $this->defaults = array(
 29:             'title' => '',
 30:             'id' => '',
 31:             'show_title' => true,
 32:             'show_description' => true
 33:         );
 34: 
 35:         $options = array(
 36:             'description' => __('Display one of your created forms.', 'quform'),
 37:             'classname'   => 'quform-widget'
 38:         );
 39: 
 40:         parent::__construct('quform-widget', Quform::getPluginName(), $options);
 41:     }
 42: 
 43:      44:  45:  46:  47:  48: 
 49:     public function widget($args, $instance)
 50:     {
 51:         $instance = wp_parse_args((array) $instance, $this->defaults);
 52: 
 53:         echo $args['before_widget'];
 54: 
 55:         $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
 56: 
 57:         if (Quform::isNonEmptyString($title)) {
 58:             echo $args['before_title'] . $title . $args['after_title'];
 59:         }
 60: 
 61:         $options = array(
 62:             'show_title' => $instance['show_title'],
 63:             'show_description' => $instance['show_description'],
 64:             'id' => $instance['id']
 65:         );
 66: 
 67:         echo $this->controller->form($options);
 68: 
 69:         echo $args['after_widget'];
 70:     }
 71: 
 72:      73:  74:  75:  76:  77:  78: 
 79:     public function update($new_instance, $old_instance)
 80:     {
 81:         $instance = $old_instance;
 82:         $instance['title'] = sanitize_text_field($new_instance['title']);
 83:         $instance['id'] = is_numeric($new_instance['id']) ? (int) $new_instance['id'] : '';
 84:         $instance['show_title'] = ! empty($new_instance['show_title']);
 85:         $instance['show_description'] = ! empty($new_instance['show_description']);
 86: 
 87:         return $instance;
 88:     }
 89: 
 90:      91:  92:  93:  94:  95: 
 96:     public function form($instance)
 97:     {
 98:         $instance = wp_parse_args((array) $instance, $this->defaults);
 99:         $orderBy = get_user_meta(get_current_user_id(), 'quform_forms_order_by', true);
100:         $order = get_user_meta(get_current_user_id(), 'quform_forms_order', true);
101:         $forms = $this->repository->formsToSelectArray(null, $orderBy, $order);
102: 
103:         if ( ! count($forms)) {
104:             if (current_user_can('quform_add_forms')) {
105:                 
106:                 printf('<p>' . esc_html__('No forms yet, %1$sclick here to create one%2$s.', 'quform') . '</p>', '<a href="' . esc_url(admin_url('admin.php?page=quform.forms&sp=add')) . '">', '</a>');
107:             } else {
108:                 echo '<p>' . esc_html__('No forms yet.', 'quform') . '</p>';
109:             }
110:             return;
111:         }
112:         ?>
113:         <p>
114:             <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'quform'); ?></label>
115:             <input
116:                 type="text"
117:                 class="widefat"
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 Quform::escape($instance['title']); ?>">
121:             <br>
122:             <small><?php esc_html_e('The title of the widget.', 'quform'); ?></small>
123:         </p>
124:         <p>
125:             <label for="<?php echo esc_attr($this->get_field_id('id')); ?>"><?php esc_html_e('Form:', 'quform'); ?></label>
126:             <select
127:                 id="<?php echo esc_attr($this->get_field_id('id')); ?>"
128:                 name="<?php echo esc_attr($this->get_field_name('id')); ?>"
129:                 class="widefat">
130:                 <option value=""><?php esc_html_e('Select a form', 'quform'); ?></option>
131:                 <?php foreach ($forms as $id => $name) : ?>
132:                     <option value="<?php echo Quform::escape($id); ?>" <?php selected($instance['id'], $id); ?>><?php echo Quform::escape($name); ?></option>
133:                 <?php endforeach; ?>
134:             </select>
135:         </p>
136:         <p>
137:             <input
138:                 type="checkbox"
139:                 id="<?php echo esc_attr($this->get_field_id('show_title')); ?>"
140:                 name="<?php echo esc_attr($this->get_field_name('show_title')); ?>"
141:                 value="1"
142:                 <?php checked($instance['show_title']); ?>>
143:             <label for="<?php echo esc_attr($this->get_field_id('show_title')); ?>"><?php esc_html_e('Show form title', 'quform'); ?></label>
144:             <br>
145:             <input
146:                 type="checkbox"
147:                 id="<?php echo esc_attr($this->get_field_id('show_description')); ?>"
148:                 name="<?php echo esc_attr($this->get_field_name('show_description')); ?>"
149:                 value="1"
150:                 <?php checked($instance['show_description']); ?>>
151:             <label for="<?php echo esc_attr($this->get_field_id('show_description')); ?>"><?php esc_html_e('Show form description', 'quform'); ?></label>
152:         </p>
153:         <?php
154:     }
155: 
156:     157: 158: 
159:     public static function register()
160:     {
161:         register_widget(__CLASS__);
162:     }
163: }
164: