1: <?php
2:
3: 4: 5:
6: class Quform_Widget_Popup extends WP_Widget
7: {
8: 9: 10:
11: protected $repository;
12:
13: 14: 15:
16: protected $controller;
17:
18: 19: 20:
21: protected $options;
22:
23: 24: 25:
26: protected $defaults;
27:
28: public function __construct()
29: {
30: $this->repository = Quform::getService('repository');
31: $this->controller = Quform::getService('formController');
32: $this->options = Quform::getService('options');
33:
34: $this->defaults = array(
35: 'title' => '',
36: 'id' => '',
37: 'content' => '',
38: 'show_title' => true,
39: 'show_description' => true,
40: 'width' => '',
41: 'options' => ''
42: );
43:
44: $options = array(
45: 'description' => __('Display one of your created forms in a popup.', 'quform'),
46: 'classname' => 'quform-popup-widget'
47: );
48:
49:
50: parent::__construct('quform-popup-widget', sprintf(_x('%s Popup', 'popup widget name', 'quform'), Quform::getPluginName()), $options);
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function widget($args, $instance)
60: {
61: $instance = wp_parse_args((array) $instance, $this->defaults);
62:
63: echo $args['before_widget'];
64:
65: $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
66:
67: if (Quform::isNonEmptyString($title)) {
68: echo $args['before_title'] . $title . $args['after_title'];
69: }
70:
71: $options = array(
72: 'id' => $instance['id'],
73: 'popup' => true,
74: 'content' => $instance['content'],
75: 'show_title' => $instance['show_title'],
76: 'show_description' => $instance['show_description'],
77: 'width' => $instance['width'],
78: 'options' => $instance['options']
79: );
80:
81: echo $this->controller->form($options);
82:
83: echo $args['after_widget'];
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: public function update($new_instance, $old_instance)
94: {
95: $instance = $old_instance;
96: $instance['title'] = sanitize_text_field($new_instance['title']);
97: $instance['id'] = is_numeric($new_instance['id']) ? (int) $new_instance['id'] : '';
98: $instance['content'] = wp_kses_post($new_instance['content']);
99: $instance['show_title'] = ! empty($new_instance['show_title']);
100: $instance['show_description'] = ! empty($new_instance['show_description']);
101: $instance['width'] = sanitize_text_field($new_instance['width']);
102: $instance['options'] = wp_kses_post($new_instance['options']);
103:
104: $this->options->set('popupEnabled', true);
105:
106: return $instance;
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function form($instance)
116: {
117: $instance = wp_parse_args((array) $instance, $this->defaults);
118: $orderBy = get_user_meta(get_current_user_id(), 'quform_forms_order_by', true);
119: $order = get_user_meta(get_current_user_id(), 'quform_forms_order', true);
120: $forms = $this->repository->formsToSelectArray(null, $orderBy, $order);
121:
122: if ( ! count($forms)) {
123: if (current_user_can('quform_add_forms')) {
124:
125: 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>');
126: } else {
127: echo '<p>' . esc_html__('No forms yet.', 'quform') . '</p>';
128: }
129: return;
130: }
131: ?>
132: <p>
133: <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'quform'); ?></label>
134: <input
135: type="text"
136: class="widefat"
137: id="<?php echo esc_attr($this->get_field_id('title')); ?>"
138: name="<?php echo esc_attr($this->get_field_name('title')); ?>"
139: value="<?php echo Quform::escape($instance['title']); ?>">
140: <br>
141: <small><?php esc_html_e('The title of the widget.', 'quform'); ?></small>
142: </p>
143: <p>
144: <label for="<?php echo esc_attr($this->get_field_id('id')); ?>"><?php esc_html_e('Form:', 'quform'); ?></label>
145: <select
146: id="<?php echo esc_attr($this->get_field_id('id')); ?>"
147: name="<?php echo esc_attr($this->get_field_name('id')); ?>"
148: class="widefat">
149: <option value=""><?php esc_html_e('Select a form', 'quform'); ?></option>
150: <?php foreach ($forms as $id => $name) : ?>
151: <option value="<?php echo Quform::escape($id); ?>" <?php selected($instance['id'], $id); ?>><?php echo Quform::escape($name); ?></option>
152: <?php endforeach; ?>
153: </select>
154: </p>
155: <p>
156: <label for="<?php echo esc_attr($this->get_field_id('content')); ?>"><?php esc_html_e('Content:', 'quform'); ?></label>
157: <textarea
158: id="<?php echo esc_attr($this->get_field_id('content')); ?>"
159: name="<?php echo esc_attr($this->get_field_name('content')); ?>"
160: class="widefat"><?php echo Quform::escape($instance['content']); ?></textarea>
161: <br>
162: <small><?php esc_html_e('The text or HTML to trigger the popup, shortcodes can also be used.', 'quform'); ?></small>
163: </p>
164: <p>
165: <input
166: type="checkbox"
167: id="<?php echo esc_attr($this->get_field_id('show_title')); ?>"
168: name="<?php echo esc_attr($this->get_field_name('show_title')); ?>"
169: value="1"
170: <?php checked($instance['show_title']); ?>>
171: <label for="<?php echo esc_attr($this->get_field_id('show_title')); ?>"><?php esc_html_e('Show form title', 'quform'); ?></label>
172: <br>
173: <input
174: type="checkbox"
175: id="<?php echo esc_attr($this->get_field_id('show_description')); ?>"
176: name="<?php echo esc_attr($this->get_field_name('show_description')); ?>"
177: value="1"
178: <?php checked($instance['show_description']); ?>>
179: <label for="<?php echo esc_attr($this->get_field_id('show_description')); ?>"><?php esc_html_e('Show form description', 'quform'); ?></label>
180: </p>
181: <p>
182: <label for="<?php echo esc_attr($this->get_field_id('width')); ?>"><?php esc_html_e('Width:', 'quform'); ?></label>
183: <input
184: type="text"
185: class="widefat"
186: id="<?php echo esc_attr($this->get_field_id('width')); ?>"
187: name="<?php echo esc_attr($this->get_field_name('width')); ?>"
188: value="<?php echo Quform::escape($instance['width']); ?>">
189: <br>
190: <small><?php esc_html_e('The width of the popup, any CSS width or number is accepted.', 'quform'); ?></small>
191: </p>
192: <p>
193: <label for="<?php echo esc_attr($this->get_field_id('options')); ?>"><?php esc_html_e('Options:', 'quform'); ?></label>
194: <input
195: type="text"
196: class="widefat"
197: id="<?php echo esc_attr($this->get_field_id('options')); ?>"
198: name="<?php echo esc_attr($this->get_field_name('options')); ?>"
199: value="<?php echo Quform::escape($instance['options']); ?>">
200: <br>
201: <small>
202: <?php
203: printf(
204:
205: esc_html__('JSON encoded options to pass to the popup script, %1$sexamples%2$s.', 'quform'),
206: '<a href="https://support.themecatcher.net/quform-wordpress-v2/guides/customization/popup-script-options" target="_blank">',
207: '</a>'
208: );
209: ?>
210: </small>
211: </p>
212: <?php
213: }
214:
215: 216: 217:
218: public static function register()
219: {
220: register_widget(__CLASS__);
221: }
222: }
223: