PluginProbe
Crowdsignal Dashboard – Polls, Surveys & more / 0.9
Crowdsignal Dashboard – Polls, Surveys & more v0.9
3.1.8 3.1.7 trunk 0.8 0.9 1.0 1.2 1.3 1.4 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.10 1.8.2 All 97 releases
polldaddy / polldaddy.php

polldaddy.php in Crowdsignal Dashboard – Polls, Surveys & more 0.9, at polldaddy.php

1,102 lines 35.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Plugin Name: PollDaddy Polls
5 Description: Create and manage PollDaddy polls in WordPress
6 Author: Automattic, Inc.
7 Author URL: http://automattic.com/
8 Version: 0.9
9 */
10
11 // You can hardcode your PollDaddy PartnerGUID (API Key) here
12 //define( 'WP_POLLDADDY__PARTNERGUID', '12345...' );
13
14 if ( !defined( 'WP_POLLDADDY__CLASS' ) )
15 define( 'WP_POLLDADDY__CLASS', 'WP_PollDaddy' );
16
17 if ( !defined( 'WP_POLLDADDY__POLLDADDY_CLIENT_PATH' ) )
18 define( 'WP_POLLDADDY__POLLDADDY_CLIENT_PATH', dirname( __FILE__ ) . '/polldaddy-client.php' );
19
20 // TODO: when user changes PollDaddy password, userCode changes
21 class WP_PollDaddy {
22 var $errors;
23 var $polldaddy_client_class = 'PollDaddy_Client';
24 var $base_url = false;
25 var $version = '0.8';
26
27 function admin_menu() {
28 global $current_user;
29
30 $this->errors = new WP_Error;
31
32 if ( !$this->base_url )
33 $this->base_url = plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ) . '/';
34
35 if ( !defined( 'WP_POLLDADDY__PARTNERGUID' ) ) {
36 $guid = get_option( 'polldaddy_api_key' );
37 if ( !$guid || !is_string( $guid ) )
38 $guid = false;
39 define( 'WP_POLLDADDY__PARTNERGUID', $guid );
40 }
41
42 if ( !WP_POLLDADDY__PARTNERGUID ) {
43 if ( function_exists( 'add_object_page' ) ) // WP 2.7+
44 $hook = add_object_page( __( 'Polls' ), __( 'Polls' ), 'edit_posts', 'polls', array( &$this, 'api_key_page' ), '/wp-content/admin-plugins/polldaddy/polldaddy.png' );
45 else
46 $hook = add_management_page( __( 'Polls' ), __( 'Polls' ), 'edit_posts', 'polls', array( &$this, 'api_key_page' ) );
47 add_action( "load-$hook", array( &$this, 'api_key_page_load' ) );
48 if ( empty( $_GET['page'] ) || 'polls' != $_GET['page'] )
49 add_action( 'admin_notices', create_function( '', 'echo "<div class=\"error\"><p>" . sprintf( "You need to <a href=\"%s\">input your PollDaddy.com account details</a>.", "edit.php?page=polls" ) . "</p></div>";' ) );
50 return false;
51 }
52
53 if ( !defined( 'WP_POLLDADDY__USERCODE' ) ) {
54 define( 'WP_POLLDADDY__USERCODE',
55 isset( $current_user->data->polldaddy_account ) && is_string( $current_user->data->polldaddy_account ) ?
56 $current_user->data->polldaddy_account :
57 false
58 );
59 }
60
61 if ( function_exists( 'add_object_page' ) ) // WP 2.7+
62 $hook = add_object_page( __( 'Polls' ), __( 'Polls' ), 'edit_posts', 'polls', array( &$this, 'management_page' ), '/wp-content/admin-plugins/polldaddy/polldaddy.png' );
63 else
64 $hook = add_management_page( __( 'Polls' ), __( 'Polls' ), 'edit_posts', 'polls', array( &$this, 'management_page' ) );
65 add_action( "load-$hook", array( &$this, 'management_page_load' ) );
66
67 // Hack-a-lack-a
68 add_submenu_page( 'polls', __( 'Edit Polls' ), __( 'Edit' ), 'edit_posts', 'polls' ); //, array( &$this, 'management_page' ) );
69 add_submenu_page( 'polls', __( 'Add New Poll' ), __( 'Add New' ), 'edit_posts', 'polls&action=create-poll', array( &$this, 'management_page' ) );
70
71 add_action( 'media_buttons', array( &$this, 'media_buttons' ) );
72 }
73
74 function api_key_page_load() {
75 if ( 'post' != strtolower( $_SERVER['REQUEST_METHOD'] ) || empty( $_POST['action'] ) || 'account' != $_POST['action'] )
76 return false;
77
78 check_admin_referer( 'polldaddy-account' );
79
80 $polldaddy_email = stripslashes( $_POST['polldaddy_email'] );
81 $polldaddy_password = stripslashes( $_POST['polldaddy_password'] );
82
83 if ( !$polldaddy_email )
84 $this->errors->add( 'polldaddy_email', __( 'Email address required' ) );
85
86 if ( !$polldaddy_password )
87 $this->errors->add( 'polldaddy_password', __( 'Password required' ) );
88
89 if ( $this->errors->get_error_codes() )
90 return false;
91
92 $details = array(
93 'uName' => get_bloginfo( 'name' ),
94 'uEmail' => $polldaddy_email,
95 'uPass' => $polldaddy_password
96 );
97 if ( function_exists( 'wp_remote_post' ) ) { // WP 2.7+
98 $polldaddy_api_key = wp_remote_post( 'http://api.polldaddy.com/key/new.aspx', array(
99 'body' => $details
100 ) );
101 } else {
102 $fp = fsockopen(
103 'api.polldaddy.com',
104 80,
105 $err_num,
106 $err_str,
107 3
108 );
109
110 if ( !$fp ) {
111 $this->errors->add( 'connect', __( "Can't connect to PollDaddy.com" ) );
112 return false;
113 }
114
115 if ( function_exists( 'stream_set_timeout' ) )
116 stream_set_timeout( $fp, 3 );
117
118 global $wp_version;
119
120 $request_body = http_build_query( $details, null, '&' );
121
122 $request = "POST /key/new.aspx HTTP/1.0\r\n";
123 $request .= "Host: api.polldaddy.com\r\n";
124 $request .= "User-agent: WordPress/$wp_version\r\n";
125 $request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
126 $request .= 'Content-Length: ' . strlen( $request_body ) . "\r\n";
127
128 fwrite( $fp, "$request\r\n$request_body" );
129
130 $response = '';
131 while ( !feof( $fp ) )
132 $response .= fread( $fp, 4096 );
133 fclose( $fp );
134 list($headers, $polldaddy_api_key) = explode( "\r\n\r\n", $response, 2 );
135 }
136
137 if ( !$polldaddy_api_key ) {
138 $this->errors->add( 'polldaddy_password', __( 'Invalid Account' ) );
139 return false;
140 }
141
142 update_option( 'polldaddy_api_key', $polldaddy_api_key );
143
144 require_once WP_POLLDADDY__POLLDADDY_CLIENT_PATH;
145
146 $polldaddy = new $this->polldaddy_client_class( $polldaddy_api_key );
147
148 if ( $polldaddy_account = $polldaddy->GetUserCode( 0 ) ) {
149 update_usermeta( $GLOBALS['user_ID'], 'polldaddy_account', $polldaddy_account );
150 } else {
151 $this->parse_errors( $polldaddy );
152 $this->errors->add( 'GetUserCode', __( 'Account could not be accessed. Are your email address and password correct?' ) );
153 return false;
154 }
155
156 return true;
157 }
158
159 function parse_errors( &$polldaddy ) {
160 if ( $polldaddy->errors )
161 foreach ( $polldaddy->errors as $code => $error )
162 $this->errors->add( $code, $error );
163 if ( isset( $this->errors->errors[4] ) ) {
164 $this->errors->errors[4] = array( sprintf( __( 'Obsolete PollDaddy User API Key: <a href="%s">Sign in again to re-authenticate</a>' ), add_query_arg( array( 'action' => 'signup', 'reaction' => empty( $_GET['action'] ) ? false : $_GET['action'] ) ) ) );
165 $this->errors->add_data( true, 4 );
166 }
167 }
168
169 function print_errors() {
170 if ( !$error_codes = $this->errors->get_error_codes() )
171 return;
172 ?>
173
174 <div class="error">
175
176 <?php
177
178 foreach ( $error_codes as $error_code ) :
179 foreach ( $this->errors->get_error_messages( $error_code ) as $error_message ) :
180 ?>
181
182 <p><?php echo $this->errors->get_error_data( $error_code ) ? $error_message : wp_specialchars( $error_message ); ?></p>
183
184 <?php
185 endforeach;
186 endforeach;
187 ?>
188
189 </div>
190 <br class="clear" />
191
192 <?php
193 }
194
195 function api_key_page() {
196
197 ?>
198
199 <div class="wrap">
200
201 <h2><?php _e( 'PollDaddy Account' ); ?></h2>
202
203 <p><?php printf( __('Before you can use the PollDaddy plugin, you need to enter your <a href="%s">PollDaddy.com</a> account details.' ), 'http://polldaddy.com/' ); ?></p>
204
205 <form action="" method="post">
206 <table class="form-table">
207 <tbody>
208 <tr class="form-field form-required">
209 <th valign="top" scope="row">
210 <label for="polldaddy-email">PollDaddy Email Address</label>
211 </th>
212 <td>
213 <input type="text" name="polldaddy_email" id="polldaddy-email" aria-required="true" size="40" />
214 </td>
215 </tr>
216 <tr class="form-field form-required">
217 <th valign="top" scope="row">
218 <label for="polldaddy-password">PollDaddy Password</label>
219 </th>
220 <td>
221 <input type="password" name="polldaddy_password" id="polldaddy-password" aria-required="true" size="40" />
222 </td>
223 </tr>
224 </tbody>
225 </table>
226 <p class="submit">
227 <?php wp_nonce_field( 'polldaddy-account' ); ?>
228 <input type="hidden" name="action" value="account" />
229 <input type="hidden" name="account" value="import" />
230 <input type="submit" value="<?php echo attribute_escape( __( 'Submit' ) ); ?>" />
231 </p>
232 </form>
233 </div>
234
235 <?php
236 }
237
238 function media_buttons() {
239 $title = __( 'Add Poll' );
240 echo "<a href='admin.php?page=polls&amp;iframe&amp;TB_iframe=true' id='add_poll' class='thickbox' title='$title'><img src='{$this->base_url}polldaddy.png' alt='$title' /></a>";
241 }
242
243 function management_page_load() {
244 wp_reset_vars( array( 'action', 'poll' ) );
245 global $action, $poll;
246
247 if ( !WP_POLLDADDY__USERCODE )
248 $action = 'signup';
249
250 require_once WP_POLLDADDY__POLLDADDY_CLIENT_PATH;
251
252 wp_enqueue_script( 'polls', "{$this->base_url}polldaddy.js", array( 'jquery', 'jquery-ui-sortable' ), $this->version );
253 wp_enqueue_script( 'admin-forms' );
254 add_thickbox();
255
256 wp_enqueue_style( 'polls', "{$this->base_url}polldaddy.css", array( 'global', 'wp-admin' ), $this->version );
257 add_action( 'admin_body_class', array( &$this, 'admin_body_class' ) );
258
259 add_action( 'admin_notices', array( &$this, 'management_page_notices' ) );
260
261 $query_args = array();
262
263 $is_POST = 'post' == strtolower( $_SERVER['REQUEST_METHOD'] );
264
265 switch ( $action ) :
266 case 'signup' : // sign up for first time
267 case 'account' : // reauthenticate
268 if ( !$is_POST )
269 return;
270
271 check_admin_referer( 'polldaddy-account' );
272
273 if ( $new_args = $this->management_page_load_signup() )
274 $query_args = array_merge( $query_args, $new_args );
275 if ( $this->errors->get_error_codes() )
276 return false;
277
278 wp_reset_vars( array( 'action' ) );
279 if ( !empty( $_GET['reaction'] ) )
280 $query_args['action'] = $_GET['reaction'];
281 elseif ( !empty( $_GET['action'] ) && 'signup' != $_GET['action'] )
282 $query_args['action'] = $_GET['action'];
283 else
284 $query_args['action'] = false;
285 break;
286 case 'delete' :
287 global $current_user;
288
289 if ( empty( $poll ) )
290 return;
291
292 if ( is_array( $poll ) )
293 check_admin_referer( 'delete-poll_bulk' );
294 else
295 check_admin_referer( "delete-poll_$poll" );
296
297 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
298
299 foreach ( (array) $_REQUEST['poll'] as $poll_id ) {
300 $poll_object = $polldaddy->GetPoll( $poll );
301
302 if ( !$this->can_edit( $poll_object ) ) {
303 $this->errors->add( 'permission', __( 'You are not allowed to delete this poll.' ) );
304 return false;
305 }
306
307 $polldaddy->reset();
308
309 // Send Poll Author credentials
310 if ( !empty( $poll_object->_owner ) && $current_user->ID != $poll_object->_owner ) {
311 if ( !$userCode = get_usermeta( $poll_object->_owner, 'polldaddy_account' ) ) {
312 $this->errors->add( 'no_usercode', __( 'Invalid Poll Author' ) );
313 }
314 $polldaddy->userCode = $userCode;
315 }
316
317 $polldaddy->DeletePoll( $poll_id );
318 $polldaddy->reset();
319 }
320
321 $query_args['message'] = 'deleted';
322 $query_args['deleted'] = count( (array) $poll );
323 break;
324 case 'edit-poll' : // TODO: use polldaddy_poll
325 if ( !$is_POST || !$poll = (int) $poll )
326 return;
327
328 check_admin_referer( "edit-poll_$poll" );
329
330 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
331
332 $poll_object = $polldaddy->GetPoll( $poll );
333
334 if ( !$this->can_edit( $poll_object ) ) {
335 $this->errors->add( 'permission', __( 'You are not allowed to edit this poll.' ) );
336 return false;
337 }
338
339 // Send Poll Author credentials
340 if ( !empty( $poll_object->_owner ) && $current_user->ID != $poll_object->_owner ) {
341 if ( !$userCode = get_usermeta( $poll_object->_owner, 'polldaddy_account' ) ) {
342 $this->errors->add( 'no_usercode', __( 'Invalid Poll Author' ) );
343 }
344 $polldaddy->userCode = $userCode;
345 }
346
347 $this->parse_errors( $polldaddy );
348
349 if ( !$poll_object )
350 $this->errors->add( 'GetPoll', __( 'Poll not found' ) );
351
352 if ( $this->errors->get_error_codes() )
353 return false;
354
355 $poll_data = get_object_vars( $poll_object );
356 foreach ( $poll_data as $key => $value )
357 if ( '_' === $key[0] )
358 unset( $poll_data[$key] );
359
360 foreach ( array( 'multipleChoice', 'randomiseAnswers', 'otherAnswer' ) as $option ) {
361 if ( isset( $_POST[$option] ) && $_POST[$option] )
362 $poll_data[$option] = 'yes';
363 else
364 $poll_data[$option] = 'no';
365 }
366
367 $blocks = array( 'off', 'cookie', 'cookieIP' );
368 if ( isset( $_POST['blockRepeatVotersType'] ) && in_array( $_POST['blockRepeatVotersType'], $blocks ) )
369 $poll_data['blockRepeatVotersType'] = $_POST['blockRepeatVotersType'];
370
371 $results = array( 'show', 'percent', 'hide' );
372 if ( isset( $_POST['resultsType'] ) && in_array( $_POST['resultsType'], $results ) )
373 $poll_data['resultsType'] = $_POST['resultsType'];
374 $poll_data['question'] = stripslashes( $_POST['question'] );
375
376 if ( empty( $_POST['answer'] ) || !is_array( $_POST['answer'] ) )
377 $this->errors->add( 'answer', __( 'Invalid answers' ) );
378
379 $answers = array();
380 foreach ( $_POST['answer'] as $answer_id => $answer ) {
381 if ( !$answer = trim( stripslashes( $answer ) ) )
382 continue;
383
384 if ( is_numeric( $answer_id ) )
385 $answers[] = polldaddy_poll_answer( $answer, $answer_id );
386 else
387 $answers[] = polldaddy_poll_answer( $answer );
388 }
389
390 if ( 2 > count( $answers ) )
391 $this->errors->add( 'answer', __( 'You must include at least 2 answers' ) );
392
393 if ( $this->errors->get_error_codes() )
394 return false;
395
396 $poll_data['answers'] = $answers;
397 $poll_data['styleID'] = $_POST['styleID'];
398
399 $polldaddy->reset();
400
401 $update_response = $polldaddy->UpdatePoll( $poll, $poll_data );
402
403 $this->parse_errors( $polldaddy );
404
405 if ( !$update_response )
406 $this->errors->add( 'UpdatePoll', __( 'Poll could not be updated' ) );
407
408 if ( $this->errors->get_error_codes() )
409 return false;
410
411 $query_args['message'] = 'updated';
412 if ( isset($_POST['iframe']) )
413 $query_args['iframe'] = '';
414 break;
415 case 'create-poll' :
416 if ( !$is_POST )
417 return;
418
419 check_admin_referer( 'create-poll' );
420
421 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
422
423 $answers = array();
424 foreach ( $_POST['answer'] as $answer )
425 if ( $answer = trim( stripslashes( $answer ) ) )
426 $answers[] = polldaddy_poll_answer( $answer );
427 if ( !$answers )
428 return false;
429
430 $poll_data = _polldaddy_poll_defaults();
431 foreach ( $poll_data as $key => $value )
432 if ( isset($_POST[$key]) )
433 $poll_data[$key] = stripslashes( $_POST[$key] );
434
435 $poll_data['answers'] = $answers;
436
437 $poll = $polldaddy->CreatePoll( $poll_data );
438 $this->parse_errors( $polldaddy );
439
440 if ( !$poll || empty( $poll->_id ) )
441 $this->errors->add( 'CreatePoll', __( 'Poll could not be created' ) );
442
443 if ( $this->errors->get_error_codes() )
444 return false;
445
446 $query_args['message'] = 'created';
447 $query_args['action'] = 'edit-poll';
448 $query_args['poll'] = $poll->_id;
449 if ( isset($_POST['iframe']) )
450 $query_args['iframe'] = '';
451 break;
452 default :
453 return;
454 endswitch;
455
456 wp_redirect( add_query_arg( $query_args, wp_get_referer() ) );
457 exit;
458 }
459
460 function management_page_load_signup() {
461 switch ( $_POST['account'] ) :
462 case 'import' :
463 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID );
464 $email = trim( stripslashes( $_POST['polldaddy_email'] ) );
465 $password = trim( stripslashes( $_POST['polldaddy_password'] ) );
466
467 if ( !is_email( $email ) )
468 $this->errors->add( 'polldaddy_email', __( 'Email address required' ) );
469
470 if ( !$password )
471 $this->errors->add( 'polldaddy_password', __( 'Password required' ) );
472
473 if ( $this->errors->get_error_codes() )
474 return false;
475
476 if ( $polldaddy_account = $polldaddy->Initiate( $email, $password, $GLOBALS['user_ID'] ) ) {
477 update_usermeta( $GLOBALS['user_ID'], 'polldaddy_account', $polldaddy_account );
478 } else {
479 $this->parse_errors( $polldaddy );
480 $this->errors->add( 'import-account', __( 'Account could not be imported. Are your email address and password correct?' ) );
481 return false;
482 }
483 break;
484 default :
485 return;
486 endswitch;
487 }
488
489 function admin_body_class( $class ) {
490 if ( isset( $_GET['iframe'] ) )
491 $class .= 'poll-preview-iframe ';
492 if ( isset( $_GET['TB_iframe'] ) )
493 $class .= 'poll-preview-iframe-editor ';
494 return $class;
495 }
496
497 function management_page_notices() {
498 $message = false;
499 switch ( (string) @$_GET['message'] ) :
500 case 'deleted' :
501 $deleted = (int) $_GET['deleted'];
502 if ( 1 == $deleted )
503 $message = __( 'Poll deleted.' );
504 else
505 $message = sprintf( __ngettext( '%s Poll Deleted.', '%s Polls Deleted.', $deleted ), number_format_i18n( $deleted ) );
506 break;
507 case 'updated' :
508 $message = __( 'Poll updated.' );
509 break;
510 case 'created' :
511 $message = __( 'Poll created.' );
512 if ( isset( $_GET['iframe'] ) )
513 $message .= ' <input type="button" class="button polldaddy-send-to-editor" value="' . attribute_escape( __( 'Send to Editor' ) ) . '" />';
514 break;
515 endswitch;
516
517 $is_POST = 'post' == strtolower( $_SERVER['REQUEST_METHOD'] );
518
519 if ( $is_POST ) {
520 switch ( $GLOBALS['action'] ) :
521 case 'create-poll' :
522 $message = __( 'Error: An error has occurred; Poll not created.' );
523 break;
524 case 'edit-poll' :
525 $message = __( 'Error: An error has occurred; Poll not updated.' );
526 break;
527 case 'account' :
528 if ( 'import' == $_POST['account'] )
529 $message = __( 'Error: An error has occurred; Account could not be imported. Perhaps your email address or password is incorrect?' );
530 else
531 $message = __( 'Error: An error has occurred; Account could not be created.' );
532 break;
533 endswitch;
534 }
535
536 if ( !$message )
537 return;
538 ?>
539 <div class='updated'><p><?php echo $message; ?></p></div>
540 <?php
541 $this->print_errors();
542 }
543
544 function management_page() {
545 global $action, $poll;
546 $poll = (int) $poll;
547
548 ?>
549
550 <div class="wrap" id="manage-polls">
551
552 <?php
553 switch ( $action ) :
554 case 'signup' :
555 case 'account' :
556 $this->signup();
557 break;
558 case 'preview' :
559 ?>
560
561 <h2 id="preview-header"><?php printf( __( 'Poll Preview (<a href="%s">Edit Poll</a>, <a href="%s">List Polls</a>)' ),
562 clean_url( add_query_arg( array( 'action' => 'edit', 'poll' => $poll, 'message' => false ) ) ),
563 clean_url( add_query_arg( array( 'action' => false, 'poll' => $poll, 'message' => false ) ) )
564 ); ?></h2>
565
566 <?php
567 echo do_shortcode( "[polldaddy poll=$poll]" );
568 break;
569 case 'results' :
570 ?>
571
572 <h2><?php printf( __( 'Poll Results (<a href="%s">Edit</a>)' ), clean_url( add_query_arg( array( 'action' => 'edit', 'poll' => $poll, 'message' => false ) ) ) ); ?></h2>
573
574 <?php
575 $this->poll_results_page( $poll );
576 break;
577 case 'edit' :
578 case 'edit-poll' :
579 ?>
580
581 <h2><?php printf( __('Edit Poll (<a href="%s">List Polls</a>)'), clean_url( add_query_arg( array( 'action' => false, 'poll' => $poll, 'message' => false ) ) ) ); ?></h2>
582
583 <?php
584
585 $this->poll_edit_form( $poll );
586 break;
587 case 'create-poll' :
588 ?>
589
590 <h2><?php printf( __('Create Poll (<a href="%s">List Polls</a>)'), clean_url( add_query_arg( array( 'action' => false, 'poll' => $poll, 'message' => false ) ) ) ); ?></h2>
591
592 <?php
593 $this->poll_edit_form();
594 break;
595 default :
596
597 ?>
598
599 <h2 id="poll-list-header"><?php printf( __( 'Polls (<a href="%s">Add New</a>)' ), clean_url( add_query_arg( array(
600 'action' => 'create-poll',
601 'poll' => false,
602 'message' => false
603 ) ) ) ); ?></h2>
604
605 <?php
606 $this->polls_table( isset( $_GET['view'] ) && 'user' == $_GET['view'] ? 'user' : 'blog' );
607 endswitch;
608 ?>
609
610 </div>
611
612 <?php
613
614 }
615
616 function polls_table( $view = 'blog' ) {
617 $page = absint($_GET['paged']);
618 if ( !$page )
619 $page = 1;
620 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
621 if ( 'user' == $view )
622 $polls_object = $polldaddy->listPolls( ( $page - 1 ) * 10 + 1, $page * 10 );
623 else
624 $polls_object = $polldaddy->listPollsByBlog( ( $page - 1 ) * 10 + 1, $page * 10 );
625 $this->parse_errors( $polldaddy );
626 $this->print_errors();
627 $polls = & $polls_object->poll;
628 $total_polls = $polls_object->_total;
629 $class = '';
630
631 $page_links = paginate_links( array(
632 'base' => add_query_arg( 'paged', '%#%' ),
633 'format' => '',
634 'total' => ceil( $total_polls / 10 ),
635 'current' => $page
636 ) );
637
638 ?>
639
640 <ul class="subsubsub">
641 <li><a href="<?php echo clean_url( add_query_arg( array( 'view' => false, 'paged' => false ) ) ); ?>"<?php if ( 'blog' == $view ) echo ' class="current"'; ?>><?php _e( "All Blog's Polls" ); ?></a> | </li>
642 <li><a href="<?php echo clean_url( add_query_arg( array( 'view' => 'user', 'paged' => false ) ) ); ?>"<?php if ( 'user' == $view ) echo ' class="current"'; ?>><?php _e( "All My Polls" ); ?></a></li>
643 </ul>
644 <form method="post" action="">
645 <div class="tablenav">
646 <div class="alignleft">
647 <select name="action">
648 <option selected="selected" value=""><?php _e( 'Actions' ); ?></option>
649 <option value="delete"><?php _e( 'Delete' ); ?></option>
650 </select>
651 <input class="button-secondary action" type="submit" name="doaction" value="<?php _e( 'Apply' ); ?>" />
652 <?php wp_nonce_field( 'delete-poll_bulk' ); ?>
653 </div>
654 <div class="tablenav-pages"><?php echo $page_links; ?></div>
655 </div>
656 <br class="clear" />
657 <table class="widefat">
658 <thead>
659 <tr>
660 <th id="cb" class="manage-column column-cb check-column" scope="col" /><input type="checkbox" /></th>
661 <th id="title" class="manage-column column-title" scope="col">Poll</th>
662 <th id="votes" class="manage-column column-vote" scope="col">Votes</th>
663 <th id="date" class="manage-column column-date" scope="col">Created</th>
664 </tr>
665 </thead>
666 <tbody>
667
668 <?php
669 if ( $polls ) :
670 foreach ( $polls as $poll ) :
671 $poll_id = (int) $poll->_id;
672
673 if ( $this->can_edit( $poll ) )
674 $edit_link = clean_url( add_query_arg( array( 'action' => 'edit', 'poll' => $poll_id, 'message' => false ) ) );
675 else
676 $edit_link = false;
677
678 $class = $class ? '' : ' class="alternate"';
679 $results_link = clean_url( add_query_arg( array( 'action' => 'results', 'poll' => $poll_id, 'message' => false ) ) );
680 $delete_link = clean_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete', 'poll' => $poll_id, 'message' => false ) ), "delete-poll_$poll_id" ) );
681 $preview_link = clean_url( add_query_arg( array( 'action' => 'preview', 'poll' => $poll_id, 'message' => false ) ) ); //, 'iframe' => '', 'TB_iframe' => 'true' ) ) );
682 list($poll_time) = explode( '.', $poll->_created );
683 $poll_time = strtotime( $poll_time );
684 ?>
685
686 <tr<?php echo $class; ?>>
687 <th class="check-column" scope="row"><input type="checkbox" value="<?php echo (int) $poll_id; ?>" name="poll[]" /></th>
688 <td class="post-title column-title">
689 <?php if ( $edit_link ) : ?>
690 <strong><a class="row-title" href="<?php echo $edit_link; ?>"><?php echo wp_specialchars( $poll->___content ); ?></a></strong>
691 <span class="edit"><a href="<?php echo $edit_link; ?>"><?php _e( 'Edit' ); ?></a> | </span>
692 <?php else : ?>
693 <strong><?php echo wp_specialchars( $poll->___content ); ?></strong>
694 <?php endif; ?>
695
696 <span class="results"><a href="<?php echo $results_link; ?>"><?php _e( 'Results' ); ?></a> | </span>
697 <span class="delete"><a class="delete-poll delete" href="<?php echo $delete_link; ?>"><?php _e( 'Delete' ); ?></a> | </span>
698 <?php if ( isset( $_GET['iframe'] ) ) : ?>
699 <span class="view"><a href="<?php echo $preview_link; ?>"><?php _e( 'Preview' ); ?></a> | </span>
700 <span class="editor">
701 <a href="#" class="polldaddy-send-to-editor"><?php _e( 'Send to editor' ); ?></a>
702 <input type="hidden" class="polldaddy-poll-id hack" value="<?php echo (int) $poll_id; ?>" /> |
703 </span>
704 <?php else : ?>
705 <span class="view"><a class="thickbox" href="<?php echo $preview_link; ?>"><?php _e( 'Preview' ); ?></a> | </span>
706 <?php endif; ?>
707 <span class="shortcode"><a href="#" class="polldaddy-show-shortcode"><?php _e( 'HTML code' ); ?></a></span>
708 </td>
709 <td class="poll-votes column-vote"><?php echo number_format_i18n( $poll->_responses ); ?></td>
710 <td class="date column-date"><abbr title="<?php echo date( __('Y/m/d g:i:s A'), $poll_time ); ?>"><?php echo date( __('Y/m/d'), $poll_time ); ?></abbr></td>
711 </tr>
712 <tr class="polldaddy-shortcode-row" style="display: none;">
713 <td colspan="4">
714 <h4><?php _e( 'Shortcode' ); ?></h4>
715 <pre>[polldaddy poll=<?php echo (int) $poll_id; ?>]</pre>
716
717 <h4><?php _e( 'JavaScript' ); ?></h4>
718 <pre>&lt;script type="text/javascript" language="javascript"
719 src="http://static.polldaddy.com/p/<?php echo (int) $poll_id; ?>.js"&gt;&lt;/script&gt;
720 &lt;noscript&gt;
721 &lt;a href="http://answers.polldaddy.com/poll/1000076/"&gt;<?php echo wp_specialchars( $poll->___content ); ?>&lt;/a&gt;&lt;br/&gt;
722 &lt;span style="font:9px;"&gt;(&lt;a href="http://www.polldaddy.com"&gt;polls&lt;/a&gt;)&lt;/span&gt;
723 &lt;/noscript&gt;</pre>
724 </td>
725 </tr>
726
727 <?php
728 endforeach;
729 elseif ( $total_polls ) : // $polls
730 ?>
731
732 <tr>
733 <td colspan="4"><?php printf( __( 'What are you doing here? <a href="%s">Go back</a>.' ), clean_url( add_query_arg( 'paged', false ) ) ); ?></td>
734 </tr>
735
736 <?php
737 else : // $polls
738 ?>
739
740 <tr>
741 <td colspan="4"><?php printf( __( 'No polls yet. <a href="%s">Create one</a>' ), clean_url( add_query_arg( array( 'action' => 'create-poll' ) ) ) ); ?></td>
742 </tr>
743 <?php endif; // $polls ?>
744
745 </tbody>
746 </table>
747 </form>
748 <div class="tablenav">
749 <div class="tablenav-pages"><?php echo $page_links; ?></div>
750 </div>
751 <br class="clear" />
752
753 <?php
754 }
755
756 function poll_edit_form( $poll_id = 0 ) {
757 $poll_id = (int) $poll_id;
758
759 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
760
761 $is_POST = 'post' == strtolower( $_SERVER['REQUEST_METHOD'] );
762
763 if ( $poll_id ) {
764 $poll = $polldaddy->GetPoll( $poll_id );
765 $this->parse_errors( $polldaddy );
766 if ( !$this->can_edit( $poll ) ) {
767 $this->errors->add( 'permission', __( 'You are not allowed to edit this poll.' ) );
768 }
769 } else {
770 $poll = polldaddy_poll( array(), null, false );
771 }
772
773 $question = $is_POST ? attribute_escape( stripslashes( $_POST['question'] ) ) : attribute_escape( $poll->question );
774
775 $this->print_errors();
776 ?>
777
778 <form action="" method="post">
779 <div id="poststuff"><div id="post-body" class="has-sidebar">
780
781 <div class="inner-sidebar" id="side-info-column">
782 <div id="submitdiv" class="postbox">
783 <h3><?php _e( 'Publish' ); ?></h3>
784 <div class="inside">
785 <div id="major-publishing-actions">
786 <p id="publishing-action">
787 <?php wp_nonce_field( $poll_id ? "edit-poll_$poll_id" : 'create-poll' ); ?>
788 <input type="hidden" name="action" value="<?php echo $poll_id ? 'edit-poll' : 'create-poll'; ?>" />
789 <input type="hidden" class="polldaddy-poll-id" name="poll" value="<?php echo $poll_id; ?>" />
790 <input type="submit" class="button-primary" value="<?php echo attribute_escape( __( 'Save Poll' ) ); ?>" />
791
792 <?php if ( isset( $_GET['iframe'] ) && $poll_id ) : ?>
793
794 <input type="button" class="button polldaddy-send-to-editor" value="<?php echo attribute_escape( __( 'Send to Editor' ) ); ?>" />
795
796 <?php endif; ?>
797
798 </p>
799 <br class="clear" />
800 </div>
801 </div>
802 </div>
803
804 <div class="postbox">
805 <h3><?php _e( 'Poll results' ); ?></h3>
806 <div class="inside">
807 <ul class="poll-options">
808
809 <?php
810 foreach ( array( 'show' => __( 'Show results to voters' ), 'percent' => __( 'Only show percentages' ), 'hide' => __( 'Hide all results' ) ) as $value => $label ) :
811 if ( $is_POST )
812 $checked = $value === $_POST['resultsType'] ? ' checked="checked"' : '';
813 else
814 $checked = $value === $poll->resultsType ? ' checked="checked"' : '';
815 ?>
816
817 <li>
818 <label for="resultsType-<?php echo $value; ?>"><input type="radio"<?php echo $checked; ?> value="<?php echo $value; ?>" name="resultsType" id="resultsType-<?php echo $value; ?>" /> <?php echo wp_specialchars( $label ); ?></label>
819 </li>
820
821 <?php endforeach; ?>
822
823 </ul>
824 </div>
825 </div>
826
827 <div class="postbox">
828 <h3><?php _e( 'Block repeat voters' ); ?></h3>
829 <div class="inside">
830 <ul class="poll-options">
831
832 <?php
833 foreach ( array( 'off' => __( "Don't block repeat voters" ), 'cookie' => __( 'Block by cookie (recommended)' ), 'cookieIP' => __( 'Block by cookie and by IP address' ) ) as $value => $label ) :
834 if ( $is_POST )
835 $checked = $value === $_POST['blockRepeatVotersType'] ? ' checked="checked"' : '';
836 else
837 $checked = $value === $poll->blockRepeatVotersType ? ' checked="checked"' : '';
838 ?>
839
840 <li>
841 <label for="blockRepeatVotersType-<?php echo $value; ?>"><input type="radio"<?php echo $checked; ?> value="<?php echo $value; ?>" name="blockRepeatVotersType" id="blockRepeatVotersType-<?php echo $value; ?>" /> <?php echo wp_specialchars( $label ); ?></label>
842 </li>
843
844 <?php endforeach; ?>
845
846 </ul>
847 <p>Note: Blocking by cookie and IP address can be problematic for some voters.</p>
848 </div>
849 </div>
850 </div>
851
852
853 <div id="post-body-content" class="has-sidebar-content">
854
855 <div id="titlediv">
856 <div id="titlewrap">
857 <input type="text" autocomplete="off" id="title" value="<?php echo $question; ?>" tabindex="1" size="30" name="question" />
858 </div>
859 </div>
860
861 <div id="answersdiv" class="postbox">
862 <h3><?php _e( 'Answers' ); ?></h3>
863
864 <div id="answerswrap" class="inside">
865 <ul id="answers">
866 <?php
867 $a = 0;
868 $answers = array();
869 if ( $is_POST && $_POST['answer'] ) {
870 foreach( $_POST['answer'] as $answer_id => $answer )
871 $answers[attribute_escape($answer_id)] = attribute_escape( stripslashes($answer) );
872 } elseif ( isset( $poll->answers->answer ) ) {
873 foreach ( $poll->answers->answer as $answer )
874 $answers[(int) $answer->_id] = attribute_escape( $answer->___content );
875 }
876
877 foreach ( $answers as $answer_id => $answer ) :
878 $a++;
879 $delete_link = clean_url( wp_nonce_url( add_query_arg( array( 'action' => 'delete-answer', 'poll' => $poll_id, 'answer' => $answer_id, 'message' => false ) ), "delete-answer_$answer_id" ) );
880 ?>
881
882 <li>
883 <span class="handle" title="<?php echo attribute_escape( 'click and drag to move' ); ?>">&#x2195;</span>
884 <div><input type="text" autocomplete="off" id="answer-<?php echo $answer_id; ?>" value="<?php echo $answer; ?>" tabindex="2" size="30" name="answer[<?php echo $answer_id; ?>]" /></div>
885 <a href="<?php echo $delete_link; ?>" class="delete-answer delete" title="<?php echo attribute_escape( 'delete this answer' ); ?>">&times;</a>
886 </li>
887
888 <?php
889 endforeach;
890
891 while ( 3 - $a > 0 ) :
892 $a++;
893 ?>
894
895 <li>
896 <span class="handle" title="<?php echo attribute_escape( 'click and drag to move' ); ?>">&#x2195;</span>
897 <div><input type="text" autocomplete="off" value="" tabindex="2" size="30" name="answer[new<?php echo $a; ?>]" /></div>
898 <a href="#" class="delete-answer delete" title="<?php echo attribute_escape( 'delete this answer' ); ?>">&times;</a>
899 </li>
900
901 <?php
902 endwhile;
903 ?>
904
905 </ul>
906
907 <p id="add-answer-holder">
908 <button class="button"><?php echo wp_specialchars( __( 'Add another' ) ); ?></button>
909 </p>
910
911 <ul id="answer-options">
912
913 <?php
914 foreach ( array( 'multipleChoice' => __( 'Multiple choice' ), 'randomiseAnswers' => __( 'Randomize answer order' ), 'otherAnswer' => __( 'Allow other answers' ) ) as $option => $label ) :
915 if ( $is_POST )
916 $checked = 'yes' === $_POST[$option] ? ' checked="checked"' : '';
917 else
918 $checked = 'yes' === $poll->$option ? ' checked="checked"' : '';
919 ?>
920
921 <li>
922 <label for="<?php echo $option; ?>"><input type="checkbox"<?php echo $checked; ?> value="yes" id="<?php echo $option; ?>" name="<?php echo $option; ?>" /> <?php echo wp_specialchars( $label ); ?></label>
923 </li>
924
925 <?php endforeach; ?>
926
927 </ul>
928 </div>
929 </div>
930
931 <div id="design" class="postbox">
932
933 <?php $style_ID = (int) ( $is_POST ? $_POST['styleID'] : $poll->styleID ); ?>
934
935 <h3><?php _e( 'Design' ); ?></h3>
936
937 <div class="inside">
938 <div class="hide-if-no-js">
939 <a class="alignleft" href="#previous">&#171;</a>
940 <a class="alignright" href="#next">&#187;</a>
941 <img src="http://polldaddy.com/Images/polls/<?php echo $style_ID; ?>.gif" />
942 <img class="hide-if-js" src="http://polldaddy.com/Images/polls/<?php echo 1 + $style_ID; ?>.gif" />
943 </div>
944
945 <p class="hide-if-js" id="no-js-styleID">
946 <select name="styleID">
947
948 <?php
949 $options = array(
950 0 => 'Grey Plastic Standard',
951 1 => 'White Plastic Standard',
952 2 => 'Black Plastic Standard',
953 3 => 'Simple Grey',
954 4 => 'Simple White',
955 5 => 'Simple Dark',
956 6 => 'Thinking 1',
957 7 => 'Thinking 2',
958 8 => 'Manga',
959 9 => 'Working 1',
960 10 => 'Working 2',
961 11 => 'SideBar Narrow (Dark)',
962 12 => 'SideBar Narrow (Light)',
963 13 => 'SideBar Narrow (Grey)',
964 14 => 'Skulls',
965 15 => 'Music',
966 16 => 'Sunset',
967 17 => 'Pink Butterflies',
968 18 => 'Map'
969 );
970 foreach ( $options as $styleID => $label ) :
971 $selected = $styleID == $style_ID ? ' selected="selected"' : '';
972 ?>
973 <option value="<?php echo (int) $styleID; ?>"<?php echo $selected; ?>><?php echo wp_specialchars( $label ); ?></option>
974 <?php endforeach; ?>
975
976 </select>
977 </p>
978 </div>
979 </div>
980
981 </div>
982 </div></div>
983 </form>
984 <br class="clear" />
985
986 <?php
987 }
988
989 function poll_results_page( $poll_id ) {
990 $polldaddy = new $this->polldaddy_client_class( WP_POLLDADDY__PARTNERGUID, WP_POLLDADDY__USERCODE );
991
992 $results = $polldaddy->GetPollResults( $poll_id );
993 ?>
994
995 <table class="poll-results widefat">
996 <thead>
997 <tr>
998 <th scope="col" class="column-title"><?php _e( 'Answer' ); ?></th>
999 <th scope="col" class="column-vote"><?php _e( 'Votes' ); ?></th>
1000 </tr>
1001 </thead>
1002 <tbody>
1003
1004 <?php
1005 $class = '';
1006 foreach ( $results->answers as $answer ) :
1007 $class = $class ? '' : ' class="alternate"';
1008 $content = $results->others && 'Other answer...' === $answer->___content ? sprintf( __( 'Other (<a href="%s">see below</a>)' ), '#other-answers-results' ) : wp_specialchars( $answer->___content );
1009
1010 ?>
1011
1012 <tr<?php echo $class; ?>>
1013 <th scope="row" class="column-title"><?php echo $content; ?></th>
1014 <td class="column-vote">
1015 <div class="result-holder">
1016 <span class="result-bar" style="width: <?php echo number_format( $answer->_percent, 2 ); ?>%;">&nbsp;</span>
1017 <span class="result-total alignleft"><?php echo number_format_i18n( $answer->_total ); ?></span>
1018 <span class="result-percent alignright"><?php echo number_format_i18n( $answer->_percent ); ?>%</span>
1019 </div>
1020 </td>
1021 </tr>
1022 <?php
1023 endforeach;
1024 ?>
1025
1026 </tbody>
1027 </table>
1028
1029 <?php
1030
1031 if ( !$results->others )
1032 return;
1033 ?>
1034
1035 <table id="other-answers-results" class="poll-others widefat">
1036 <thead>
1037 <tr>
1038 <th scope="col" class="column-title"><?php _e( 'Other Answer' ); ?></th>
1039 <th scope="col" class="column-vote"><?php _e( 'Votes' ); ?></th>
1040 </tr>
1041 </thead>
1042 <tbody>
1043
1044 <?php
1045 $class = '';
1046 $others = array_count_values( $results->others );
1047 arsort( $others );
1048 foreach ( $others as $other => $freq ) :
1049 $class = $class ? '' : ' class="alternate"';
1050 ?>
1051
1052 <tr<?php echo $class; ?>>
1053 <th scope="row" class="column-title"><?php echo wp_specialchars( $other ); ?></th>
1054 <td class="column-vote"><?php echo number_format_i18n( $freq ); ?></td>
1055 </tr>
1056 <?php
1057 endforeach;
1058 ?>
1059
1060 </tbody>
1061 </table>
1062
1063 <?php
1064 }
1065
1066 function signup() {
1067 return $this->api_key_page();
1068 }
1069
1070 function can_edit( &$poll ) {
1071 global $current_user;
1072 if ( empty( $poll->_owner ) )
1073 return true;
1074
1075 if ( $current_user->ID == $poll->_owner )
1076 return true;
1077
1078 return current_user_can( 'edit_others_posts' );
1079 }
1080 }
1081
1082 function polldaddy_loader() {
1083 global $polldaddy_object;
1084 $polldaddy_class = WP_POLLDADDY__CLASS;
1085 $polldaddy_object = new $polldaddy_class;
1086 add_action( 'admin_menu', array( &$polldaddy_object, 'admin_menu' ) );
1087 }
1088
1089 add_action( 'init', 'polldaddy_loader' );
1090
1091 function polldaddy_shortcode($atts, $content=null) {
1092 extract(shortcode_atts(array(
1093 'poll' => 'empty',
1094 ), $atts));
1095
1096 $poll = (int) $poll;
1097
1098 return "<script type='text/javascript' language='javascript' charset='utf-8' src='http://s3.polldaddy.com/p/$poll.js'></script><noscript> <a href='http://answers.polldaddy.com/poll/$poll/'>View Poll</a></noscript>";
1099 }
1100
1101 add_shortcode('polldaddy', 'polldaddy_shortcode');
1102