PluginProbe
Crowdsignal Dashboard – Polls, Surveys & more / 1.0
Crowdsignal Dashboard – Polls, Surveys & more v1.0
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 1.0, at polldaddy.php

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