PluginProbe
Crowdsignal Dashboard – Polls, Surveys & more / 0.8
Crowdsignal Dashboard – Polls, Surveys & more v0.8
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.8, at polldaddy.php

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