-
Notifications
You must be signed in to change notification settings - Fork 11
/
bp-polls.php
316 lines (254 loc) · 10 KB
/
bp-polls.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Plugin Name: BP Polls
* Plugin URI: http://buddypress.org
* Description: Add polls feature to your buddypress site
* Author: Themekraft
* Version: 1.2.1
* Author URI: http://themekraft.com
*/
add_action('bp_init', 'vpl_init');
register_activation_hook(__FILE__, 'vpl_activation');
function vpl_init() {
global $bp,$wpdb;
load_plugin_textdomain( 'bp_polls', false, dirname( plugin_basename( __FILE__ ) ) . '/langs/' );
define( VPL_DEBUG, false );
define( VPL_ROOT_URL, plugins_url('', __FILE__));
define( VPL_ROOT_PATH, plugin_dir_path(__FILE__));
define( VPL_TABLE_POLLS, $wpdb->prefix . "vpl_polls");
define( VPL_TABLE_QUESTIONS, $wpdb->prefix . "vpl_questions");
define( VPL_TABLE_ANSWERS, $wpdb->prefix . "vpl_answers");
define( VPL_TABLE_VOTES, $wpdb->prefix . "vpl_votes");
define( VPL_TABLE_TAXONOMY, $wpdb->prefix . "vpl_taxonomy");
define( VPL_TABLE_POLL_TAXONOMY, $wpdb->prefix . "vpl_poll_taxonomy");
define( VPL_COMPONENT_SLUG, 'polls');
define( VPL_COMPONENT_NAME, __('Polls', 'bp_polls') );
define( VPL_GROUP_MODULE, 'group');
define( VPL_USER_MODULE, 'user');
define( VPL_ALL_POLLS_PAGE, 'all_polls_page');
define( VPL_ALL_POLLS_MODULE, 'all_polls');
define( VPL_DATE_FORMATE, get_option('date_format'));
define( VPL_TIME_FORMATE, get_option('time_format'));
add_filter('bp_directory_pages', 'vpl_add_admin_page');
//Define Constants that varias for users and groups
include_once('components/defines_functions.php');
define( VPL_CURRENT_MODULE, vpl_get_current_module() ); // user or group
define( VPL_CURRENT_COMPONENT, vpl_get_current_component() ); // expect for 'polls'
// Url to poll component ( in groups - groups/single/polls; in porfile members/username/polls/ )
define( VPL_CURRENT_COMPONENT_URL, vpl_get_current_component_url() );
// Load Polls view Class
include_once('components/core/Poll_Extension_View.php');
// Load style and scripts
include_once('components/styles_and_scripts.php');
add_action('wp_head','vpl_poll_print_js_translation');
add_action('admin_menu','vpl_poll_menu');
// Ajax functions
include_once('components/ajax.php');
//Add shortcode
add_shortcode('bp-polls','vpl_poll_shortcode');
//Add shortcode
add_shortcode('bp-poll','vpl_poll_single_shortcode');
// Add polls to user menu ( control panel also )
include_once('components/register_user_polls.php');
define( VPL_CURRENT_ACTION, vpl_get_current_action() ); // [ list, view, edit, new ]
if( VPL_CURRENT_MODULE ) {
//This function Looking for suitable submitted forms and launches
//appropriate processing functions from 'components/processing_POST.php'
if( !empty($_POST) ) {
include_once('components/processing_POST.php');
vpl_processing_post();
}
if( VPL_CURRENT_MODULE != VPL_ALL_POLLS_MODULE ) {
if( VPL_CURRENT_MODULE == VPL_GROUP_MODULE ) {
// Need register group extension when we in groups component
include_once('components/register_group_polls.php');
vpl_register_group_polls();
}
// Display Polls content
if( VPL_CURRENT_COMPONENT == VPL_COMPONENT_SLUG ) {
// Display polls functions
include_once('components/display_polls.php');
}
} else {
// Load Polls view Class
add_filter('bp_located_template', 'vpl_load_template_filter', 10, 2);
add_filter('wp_title', 'vpl_add_title');
global $view;
$view = new Poll_Extension_View( VPL_CURRENT_MODULE, VPL_CURRENT_ACTION , array() );
bp_core_load_template('polls/all_polls');
}
}
}
function vpl_add_title($title){
global $post;
$title = $post->post_title.' '.$title;
return $title;
}
/**
* Plugin Activation function
*/
function vpl_activation() {
include_once('components/install.php');
vpl_create_tables();
}
function vpl_poll_shortcode($atts){
$view = new Poll_Extension_View( VPL_CURRENT_MODULE, VPL_CURRENT_ACTION , array());
$view->avaible_polls();
}
function vpl_poll_single_shortcode($atts){
$poll_id = $atts['id'];
if( $poll_id ) {
$view = new Poll_Extension_View( 'shortcode', 'view' , array($poll_id));
$view->set_group_id(0);
$view->display();
}
}
/**
* Return permission of current user to modify polls
*/
function vpl_is_user_can_modify_polls() {
global $bp, $current_user;
// current_user_can('administrator') );
// $bp->is_item_admin );
return $bp->is_item_admin;
}
function vpl_is_user_can_create_polls() {
global $bp;
if( VPL_GROUP_MODULE == VPL_CURRENT_MODULE ) {
if( $bp->groups->current_group->is_member ) {
return true;
}
}
return false;
}
function vpl_add_admin_page($pages) {
$pages[VPL_ALL_POLLS_MODULE] = VPL_COMPONENT_NAME;
return $pages;
}
function vpl_load_template_filter($found_template, $templates) {
if( VPL_CURRENT_MODULE ) {
foreach ((array) $templates as $template) {
if (file_exists(STYLESHEETPATH . '/' . $template))
$filtered_templates[] = STYLESHEETPATH . '/' . $template;
else
$filtered_templates[] = dirname(__FILE__) . '/templates/' . $template;
}
$found_template = $filtered_templates[0];
return apply_filters('vpl_load_template_filter', $found_template);
}else {
return $found_template;
}
}
function vpl_is_debug() {
if( vpl_is_user_can_modify_polls() && VPL_DEBUG == true )
return true;
else
return false;
}
function vpl_js_date_format( $php_date_str = '') {
$js_date_str = 'M d, yy';
if( !$php_date_str ){
$php_date_str = VPL_DATE_FORMATE;
}
$dates = array(
'F j, Y' => 'MM d, yy',
'Y/m/d' => 'yy/mm/dd',
'm/d/Y' => 'mm/dd/yy',
'd/m/Y' => 'dd/mm/yy',
'd.m.Y' => 'dd.mm.yy',
'j. F Y' => 'dd. MM yy'
);
if( isset($dates[$php_date_str])) {
$js_date_str = $dates[$php_date_str];
}
return $js_date_str;
}
function vpl_js_time_format(){
$js_time_str = 'hh:mm';
return $js_time_str;
}
function vpl_poll_print_js_translation() {
?>
<script>
// Translation function
function __e(str){
if( VPL_TRANS[str] ) {
return VPL_TRANS[str];
} else {
return str;
}
}
var VPL_TRANS = [];
VPL_TRANS['Please enter the Poll name'] = '<?php _e('Please enter the Poll name','bp_polls')?>';
VPL_TRANS['Start date can not be empty'] = '<?php _e('Start date can not be empty','bp_polls')?>';
VPL_TRANS['End date can not be empty'] = '<?php _e('End date can not be empty','bp_polls')?>';
VPL_TRANS['Start date must be less than the end date'] = '<?php _e('Start date must be less than the end date','bp_polls')?>';
VPL_TRANS['Poll must have at least one question'] = '<?php _e('Poll must have at least one question','bp_polls')?>';
VPL_TRANS['Missed question text for Question #'] = '<?php _e('Missed question text for Question #','bp_polls')?>';
VPL_TRANS['Dating Poll must have at least 2 dates'] = '<?php _e('Dating Poll must have at least 2 dates','bp_polls')?>';
VPL_TRANS['Question #'] = '<?php _e('Question #','bp_polls')?>';
VPL_TRANS['must have at least 2 answers'] = '<?php _e('must have at least 2 answers','bp_polls')?>';
VPL_TRANS['Missed start for date #'] = '<?php _e('Missed start for date #','bp_polls')?>';
VPL_TRANS['Missed end for date #'] = '<?php _e('Missed end for date #','bp_polls')?>';
VPL_TRANS['The startdate for answer'] = '<?php _e('The startdate for answer','bp_polls')?>';
VPL_TRANS['have to be before the enddate'] = '<?php _e('have to be before the enddate','bp_polls')?>';
VPL_TRANS['A start and an end date for answer'] = '<?php _e('A start and an end date for answer','bp_polls')?>';
VPL_TRANS['have to be filled in'] = '<?php _e('have to be filled in','bp_polls')?>';
VPL_TRANS['Missed answer text for Question #'] = '<?php _e('Missed answer text for Question #','bp_polls')?>';
VPL_TRANS['Answer #'] = '<?php _e('Answer #','bp_polls')?>';
VPL_TRANS['Are you sure want delete this answer?'] = '<?php _e('Are you sure want delete this answer?','bp_polls')?>';
VPL_TRANS['Are you sure want delete this date?'] = '<?php _e('Are you sure want delete this date?','bp_polls')?>';
VPL_TRANS['Are you sure want delete this question?'] = '<?php _e('Are you sure want delete this question?','bp_polls')?>';
VPL_TRANS['Date Title'] = '<?php _e('Poll Name','bp_polls')?>';
VPL_TRANS['Poll Name'] = '<?php _e('Poll Name','bp_polls')?>';
VPL_TRANS['Total'] = '<?php _e('Total','bp_polls')?>';
VPL_TRANS['votes'] = '<?php _e('votes','bp_polls')?>';
VPL_TRANS['Thank you for the vote! It was successfully saved.'] = '<?php _e('Thank you for the vote! It was successfully saved.','bp_polls')?>';
VPL_TRANS['Invites have been sent successfully'] = '<?php _e('Invites have been sent successfully','bp_polls')?>';
VPL_TRANS['Are you sure want to proceed? This action will clear all votes statistic.'] = '<?php _e('Are you sure want to proceed? This action will clear all votes statistic.','bp_polls')?>';
//timepicker
VPL_TRANS['Choose Time'] = '<?php _e('Choose Time','bp_polls')?>';
VPL_TRANS['Now'] = '<?php _e('Now','bp_polls')?>';
VPL_TRANS['Done'] = '<?php _e('Done','bp_polls')?>';
VPL_TRANS['Time'] = '<?php _e('Time','bp_polls')?>';
VPL_TRANS['Hour'] = '<?php _e('Hour','bp_polls')?>';
VPL_TRANS['Minute'] = '<?php _e('Minute','bp_polls')?>';
</script>
<?php
}
function vpl_get_status_name( $status ) {
$translated = '';
switch($status){
case 'open':
$translated = __('Open', 'bp_polls');
break;
case 'closed':
$translated = __('Closed', 'bp_polls');
break;
case 'draft':
$translated = __('Draft', 'bp_polls');
break;
}
return $translated;
}
function vpl_poll_menu(){
add_options_page('BP Polls', 'BP Polls', 'manage_options', 'vpl-bp-polls', 'vpl_menu_options');
}
function vpl_menu_options(){
if( $_POST['save']){
if( $_POST['invite_all'] == '1') {
update_option('vpl_invite_all', 1);
} else {
update_option('vpl_invite_all', 0);
}
}
?>
<div class="wrap">
<h2><?php _e('BP Polls Options', 'bp_polls')?></h2>
<form action="" method="post">
<label><input name="invite_all" type="checkbox" <?php checked(1, get_option('vpl_invite_all') ); ?> value="1" /> <?php _e('Enable inviting all users to poll.', 'bp_polls')?> </label><br/><br/>
<input name="save" type="submit" value="Save" class="button-primary"/>
</form>
</div>
<?php
}