-
Notifications
You must be signed in to change notification settings - Fork 12
/
Widget.php
100 lines (83 loc) · 3.42 KB
/
Widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Copyright (c) 2012 Oliver Seidel (email : oliver.seidel @ deliciousdays.com)
* Copyright (c) 2014-2017 Bastian Germann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Cforms2;
class Widget extends \WP_Widget {
public function __construct() {
parent::__construct(
'cforms', __('cformsII', 'cforms2'), array(
'description' => __('Add any cforms form to your sidebar', 'cforms2')
), array('width' => 200, 'height' => 200)
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget($args, $instance) {
$no = ($instance['form'] == '1') ? '' : $instance['form'];
echo $args['before_widget'];
if (!empty($instance['title']))
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
insert_cform($no);
echo $args['after_widget'];
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form($instance) {
// stored data
$title = isset($instance['title']) ? $instance['title'] : '';
$form = isset($instance['form']) ? $instance['form'] : null;
$opt = '';
$forms = count(FormSettings::forms());
for ($i = 1; $i <= $forms; $i++) {
$no = ($i == 1) ? '' : ($i);
$selected = ( $i == $form ) ? ' selected="selected"' : '';
$name = stripslashes(FormSettings::form($no)->name());
$name = (strlen($name) > 40) ? substr($name, 0, 40) . '…' : $name;
$opt .= '<option value="' . $i . '"' . $selected . '>' . $name . '</option>';
}
echo '<label for="' . $this->get_field_id('title') . '">' . __('Title', 'cforms2') . ':</label>' .
'<input type="text" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="' . esc_attr($title) . '" /><br />';
echo '<label for="' . $this->get_field_id('form') . '">' . __('Form', 'cforms2') . ':</label>' .
'<select id="' . $this->get_field_id('form') . '" name="' . $this->get_field_name('form') . '">' . $opt . '</select>';
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update($new_instance, $old_instance) {
$check = array('title', 'form');
$instance = array();
foreach ($check as $value) {
if (empty($new_instance[$value])) {
$instance[$value] = '';
} else {
$instance[$value] = strip_tags($new_instance[$value]);
}
}
return $instance;
}
}