forked from lesterchan/wp-polls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polls-add.php
208 lines (205 loc) · 11 KB
/
polls-add.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
### Check Whether User Can Manage Polls
if(!current_user_can('manage_polls')) {
die('Access Denied');
}
### Poll Manager
$base_name = plugin_basename('wp-polls/polls-manager.php');
$base_page = 'admin.php?page='.$base_name;
### Form Processing
if(!empty($_POST['do'])) {
// Decide What To Do
switch($_POST['do']) {
// Add Poll
case __('Add Poll', 'wp-polls'):
check_admin_referer('wp-polls_add-poll');
// Poll Question
$pollq_question = wp_kses_post( trim( $_POST['pollq_question'] ) );
if( ! empty( $pollq_question ) ) {
// Poll Start Date
$timestamp_sql = '';
$pollq_timestamp_day = (int) sanitize_key($_POST['pollq_timestamp_day']);
$pollq_timestamp_month = (int) sanitize_key($_POST['pollq_timestamp_month']);
$pollq_timestamp_year = (int) sanitize_key($_POST['pollq_timestamp_year']);
$pollq_timestamp_hour = (int) sanitize_key($_POST['pollq_timestamp_hour']);
$pollq_timestamp_minute = (int) sanitize_key($_POST['pollq_timestamp_minute']);
$pollq_timestamp_second = (int) sanitize_key($_POST['pollq_timestamp_second']);
$pollq_timestamp = gmmktime($pollq_timestamp_hour, $pollq_timestamp_minute, $pollq_timestamp_second, $pollq_timestamp_month, $pollq_timestamp_day, $pollq_timestamp_year);
if ($pollq_timestamp > current_time('timestamp')) {
$pollq_active = -1;
} else {
$pollq_active = 1;
}
// Poll End Date
$pollq_expiry_no = isset( $_POST['pollq_expiry_no'] ) ? (int) sanitize_key($_POST['pollq_expiry_no']) : 0;
if ($pollq_expiry_no === 1) {
$pollq_expiry = '';
} else {
$pollq_expiry_day = (int) sanitize_key($_POST['pollq_expiry_day']);
$pollq_expiry_month = (int) sanitize_key($_POST['pollq_expiry_month']);
$pollq_expiry_year = (int) sanitize_key($_POST['pollq_expiry_year']);
$pollq_expiry_hour = (int) sanitize_key($_POST['pollq_expiry_hour']);
$pollq_expiry_minute = (int) sanitize_key($_POST['pollq_expiry_minute']);
$pollq_expiry_second = (int) sanitize_key($_POST['pollq_expiry_second']);
$pollq_expiry = gmmktime($pollq_expiry_hour, $pollq_expiry_minute, $pollq_expiry_second, $pollq_expiry_month, $pollq_expiry_day, $pollq_expiry_year);
if ($pollq_expiry <= current_time('timestamp')) {
$pollq_active = 0;
}
}
// Mutilple Poll
$pollq_multiple_yes = (int) sanitize_key($_POST['pollq_multiple_yes']);
$pollq_multiple = 0;
if ($pollq_multiple_yes === 1) {
$pollq_multiple = (int) sanitize_key($_POST['pollq_multiple']);
} else {
$pollq_multiple = 0;
}
// Insert Poll
$add_poll_question = $wpdb->insert(
$wpdb->pollsq,
array(
'pollq_question' => $pollq_question,
'pollq_timestamp' => $pollq_timestamp,
'pollq_totalvotes' => 0,
'pollq_active' => $pollq_active,
'pollq_expiry' => $pollq_expiry,
'pollq_multiple' => $pollq_multiple,
'pollq_totalvoters' => 0
),
array(
'%s',
'%s',
'%d',
'%d',
'%s',
'%d',
'%d'
)
);
if ( ! $add_poll_question ) {
$text .= '<p style="color: red;">' . sprintf(__('Error In Adding Poll \'%s\'.', 'wp-polls'), $pollq_question) . '</p>';
}
// Add Poll Answers
$polla_answers = $_POST['polla_answers'];
$polla_qid = (int) $wpdb->insert_id;
foreach ($polla_answers as $polla_answer) {
$polla_answer = wp_kses_post( trim( $polla_answer ) );
if( ! empty( $polla_answer ) ) {
$add_poll_answers = $wpdb->insert(
$wpdb->pollsa,
array(
'polla_qid' => $polla_qid,
'polla_answers' => $polla_answer,
'polla_votes' => 0
),
array(
'%d',
'%s',
'%d'
)
);
if ( ! $add_poll_answers ) {
$text .= '<p style="color: red;">' . sprintf(__('Error In Adding Poll\'s Answer \'%s\'.', 'wp-polls'), $polla_answer) . '</p>';
}
} else {
$text .= '<p style="color: red;">' . __( 'Poll\'s Answer is empty.', 'wp-polls' ) . '</p>';
}
}
// Update Lastest Poll ID To Poll Options
$latest_pollid = polls_latest_id();
$update_latestpoll = update_option('poll_latestpoll', $latest_pollid);
// If poll starts in the future use the correct poll ID
$latest_pollid = ( $latest_pollid < $polla_qid ) ? $polla_qid : $latest_pollid;
if ( empty( $text ) ) {
$text = '<p style="color: green;">' . sprintf( __( 'Poll \'%s\' (ID: %s) added successfully. Embed this poll with the shortcode: %s or go back to <a href="%s">Manage Polls</a>', 'wp-polls' ), $pollq_question, $latest_pollid, '<input type="text" value=\'[poll id="' . $latest_pollid . '"]\' readonly="readonly" size="10" />', $base_page ) . '</p>';
} else {
if( $add_poll_question ) {
$text .= '<p style="color: green;">' . sprintf( __( 'Poll \'%s\' (ID: %s) (Shortcode: %s) added successfully, but there are some errors with the Poll\'s Answers. Embed this poll with the shortcode: %s or go back to <a href="%s">Manage Polls</a>', 'wp-polls' ), $pollq_question, $latest_pollid, '<input type="text" value=\'[poll id="' . $latest_pollid . '"]\' readonly="readonly" size="10" />' ) .'</p>';
}
}
do_action( 'wp_polls_add_poll', $latest_pollid );
cron_polls_place();
} else {
$text .= '<p style="color: red;">' . __( 'Poll Question is empty.', 'wp-polls' ) . '</p>';
}
break;
}
}
### Add Poll Form
$poll_noquestion = 2;
$count = 0;
?>
<?php if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade">'.removeslashes($text).'</div>'; } ?>
<form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>">
<?php wp_nonce_field('wp-polls_add-poll'); ?>
<div class="wrap">
<h2><?php _e('Add Poll', 'wp-polls'); ?></h2>
<!-- Poll Question -->
<h3><?php _e('Poll Question', 'wp-polls'); ?></h3>
<table class="form-table">
<tr>
<th width="20%" scope="row" valign="top"><?php _e('Question', 'wp-polls') ?></th>
<td width="80%"><input type="text" size="70" name="pollq_question" value="" /></td>
</tr>
</table>
<!-- Poll Answers -->
<h3><?php _e('Poll Answers', 'wp-polls'); ?></h3>
<table class="form-table">
<tfoot>
<tr>
<td width="20%"> </td>
<td width="80%"><input type="button" value="<?php _e('Add Answer', 'wp-polls') ?>" onclick="add_poll_answer_add();" class="button" /></td>
</tr>
</tfoot>
<tbody id="poll_answers">
<?php
for($i = 1; $i <= $poll_noquestion; $i++) {
echo "<tr id=\"poll-answer-$i\">\n";
echo "<th width=\"20%\" scope=\"row\" valign=\"top\">".sprintf(__('Answer %s', 'wp-polls'), number_format_i18n($i))."</th>\n";
echo "<td width=\"80%\"><input type=\"text\" size=\"50\" maxlength=\"200\" name=\"polla_answers[]\" /> <input type=\"button\" value=\"".__('Remove', 'wp-polls')."\" onclick=\"remove_poll_answer_add(".$i.");\" class=\"button\" /></td>\n";
echo "</tr>\n";
$count++;
}
?>
</tbody>
</table>
<!-- Poll Multiple Answers -->
<h3><?php _e('Poll Multiple Answers', 'wp-polls') ?></h3>
<table class="form-table">
<tr>
<th width="40%" scope="row" valign="top"><?php _e('Allows Users To Select More Than One Answer?', 'wp-polls'); ?></th>
<td width="60%">
<select name="pollq_multiple_yes" id="pollq_multiple_yes" size="1" onchange="check_pollq_multiple();">
<option value="0"><?php _e('No', 'wp-polls'); ?></option>
<option value="1"><?php _e('Yes', 'wp-polls'); ?></option>
</select>
</td>
</tr>
<tr>
<th width="40%" scope="row" valign="top"><?php _e('Maximum Number Of Selected Answers Allowed?', 'wp-polls') ?></th>
<td width="60%">
<select name="pollq_multiple" id="pollq_multiple" size="1" disabled="disabled">
<?php
for($i = 1; $i <= $poll_noquestion; $i++) {
echo "<option value=\"$i\">".number_format_i18n($i)."</option>\n";
}
?>
</select>
</td>
</tr>
</table>
<!-- Poll Start/End Date -->
<h3><?php _e('Poll Start/End Date', 'wp-polls'); ?></h3>
<table class="form-table">
<tr>
<th width="20%" scope="row" valign="top"><?php _e('Start Date/Time', 'wp-polls') ?></th>
<td width="80%"><?php poll_timestamp(current_time('timestamp')); ?></td>
</tr>
<tr>
<th width="20%" scope="row" valign="top"><?php _e('End Date/Time', 'wp-polls') ?></th>
<td width="80%"><input type="checkbox" name="pollq_expiry_no" id="pollq_expiry_no" value="1" checked="checked" onclick="check_pollexpiry();" /> <label for="pollq_expiry_no"><?php _e('Do NOT Expire This Poll', 'wp-polls'); ?></label><?php poll_timestamp(current_time('timestamp'), 'pollq_expiry', 'none'); ?></td>
</tr>
</table>
<p style="text-align: center;"><input type="submit" name="do" value="<?php _e('Add Poll', 'wp-polls'); ?>" class="button-primary" /> <input type="button" name="cancel" value="<?php _e('Cancel', 'wp-polls'); ?>" class="button" onclick="javascript:history.go(-1)" /></p>
</div>
</form>