PageRenderTime 59ms CodeModel.GetById 2ms app.highlight 48ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/polldaddy/polldaddy-client.php

https://bitbucket.org/derekeder/thoughtyouknew.us
PHP | 1316 lines | 825 code | 233 blank | 258 comment | 183 complexity | 2bad140d1fe33d88e71cf897de282f04 MD5 | raw file
   1<?php
   2
   3require_once dirname( __FILE__ ) . '/polldaddy-xml.php';
   4
   5class api_client {
   6	var $polldaddy_url = 'http://api.polldaddy.com/';
   7	var $partnerGUID;
   8	var $userCode;
   9	var $admin = 0;
  10	var $version = '1.0';
  11
  12	var $request = null;
  13	var $response = null;
  14	var $request_xml = '';
  15	var $response_xml = '';	
  16	
  17	var $requests = array();
  18	var $responses = array();
  19
  20	var $errors = array();
  21
  22	function api_client( $partnerGUID = '', $userCode = null ) {
  23		$this->partnerGUID = $partnerGUID;
  24		$this->userCode = $userCode;
  25	}
  26
  27	function send_request() {
  28		$this->request_xml  = "<?xml version='1.0' encoding='utf-8' ?>\n";
  29		$this->request_xml .= $this->request->xml( 'all' );
  30		
  31		$this->requests[] = $this->request_xml;
  32
  33		if ( function_exists( 'wp_remote_post' ) ) {
  34			$response = wp_remote_post( $this->polldaddy_url, array(
  35				'headers' => array( 'Content-Type' => 'text/xml; charset=utf-8', 'Content-Length' => strlen( $this->request_xml ) ),
  36				'user-agent' => 'PollDaddy PHP Client/0.1',
  37				'body' => $this->request_xml
  38			) );
  39			if ( !$response || is_wp_error( $response ) ) {
  40				$errors[-1] = "Can't connect";
  41				return false;
  42			}
  43			$this->response_xml = wp_remote_retrieve_body( $response );
  44		} else {
  45			$parsed = parse_url( $this->polldaddy_url );
  46
  47			if ( !isset( $parsed['host'] ) && !isset( $parsed['scheme'] ) ) {
  48				$errors[-1] = 'Invalid API URL';
  49				return false;
  50			}
  51			
  52			$fp = fsockopen(
  53				$parsed['host'],
  54				$parsed['scheme'] == 'ssl' || $parsed['scheme'] == 'https' && extension_loaded('openssl') ? 443 : 80,
  55				$err_num,
  56				$err_str,
  57				3
  58			);
  59
  60			if ( !$fp ) {
  61				$errors[-1] = "Can't connect";
  62				return false;
  63			}
  64
  65			if ( function_exists( 'stream_set_timeout' ) )
  66				stream_set_timeout( $fp, 3 );
  67
  68			if ( !isset( $parsed['path']) || !$path = $parsed['path'] . ( isset($parsed['query']) ? '?' . $parsed['query'] : '' ) )
  69				$path = '/';
  70
  71			$request  = "POST $path HTTP/1.0\r\n";
  72			$request .= "Host: {$parsed['host']}\r\n";
  73			$request .= "User-agent: PollDaddy PHP Client/0.1\r\n";
  74			$request .= "Content-Type: text/xml; charset=utf-8\r\n";
  75			$request .= 'Content-Length: ' . strlen( $this->request_xml ) . "\r\n";
  76
  77			fwrite( $fp, "$request\r\n$this->request_xml" );
  78
  79			$response = '';
  80			while ( !feof( $fp ) )
  81				$response .= fread( $fp, 4096 );
  82			fclose( $fp );
  83
  84
  85			if ( !$response ) {
  86				$errors[-2] = 'No Data';
  87			}
  88
  89			list($headers, $this->response_xml) = explode( "\r\n\r\n", $response, 2 );
  90		}
  91		
  92		$this->responses[] = $this->response_xml;
  93
  94		$parser = new PollDaddy_XML_Parser( $this->response_xml );
  95		$this->response =& $parser->objects[0];
  96		if ( isset( $this->response->errors->error ) ) {
  97			if ( !is_array( $this->response->errors->error ) )
  98				$this->response->errors->error = array( $this->response->errors->error );
  99			foreach ( $this->response->errors->error as $error )
 100				$this->errors[$error->_id] = $error->___content;
 101		}
 102	}
 103
 104	function response_part( $pos ) {
 105		if ( !isset( $this->response->demands->demand ) )
 106			return false;
 107
 108		if ( is_array( $this->response->demands->demand ) ) {
 109			if ( isset( $this->response->demands->demand[$pos] ) )
 110				return $this->response->demands->demand[$pos];
 111			return false;
 112		}
 113
 114		if ( 0 === $pos )
 115			return $this->response->demands->demand;
 116
 117		return false;
 118	}
 119
 120	function add_request( $demand, $object = null ) {
 121		if ( !is_a( $this->request, 'PollDaddy_Request' ) )
 122			$this->request = new PollDaddy_Request( array(
 123				'userCode' => $this->userCode,
 124				'demands' => new PollDaddy_Demands( array( 'demand' => array() ) )
 125			), array(
 126				'version' => $this->version,
 127				'admin' => $this->admin,
 128				'partnerGUID' => $this->partnerGUID
 129			) );
 130
 131		if ( is_a( $object, 'Ghetto_XML_Object' ) )
 132			$args = array( $object->___name => &$object );
 133		elseif ( is_array( $object ) )
 134			$args =& $object;
 135		else
 136			$args = null;
 137
 138		$this->request->demands->demand[] = new PollDaddy_Demand( $args, array( 'id' => $demand ) );
 139
 140		return count( $this->request->demands->demand ) - 1;
 141	}
 142
 143	function reset() {
 144		$this->request = null;
 145		$this->response = null;
 146		$this->request_xml = '';
 147		$this->response_xml = '';
 148		$this->errors = array();
 149	}
 150
 151/* pdInitiate: Initiate API "connection" */
 152
 153	/**
 154	 * @param string $Email
 155	 * @param string $Password
 156	 * @param int $partnerUserID
 157	 * @return string|false PollDaddy userCode or false on failure
 158	 */
 159	function initiate( $Email, $Password, $partnerUserID ) {
 160		$this->request = new PollDaddy_Initiate( compact( 'Email', 'Password' ), array( 'partnerGUID' => $this->partnerGUID, 'partnerUserID' => $partnerUserID ) );
 161		$this->send_request();
 162		if ( isset( $this->response->userCode ) )
 163			return $this->response->userCode;
 164		return false;
 165	}
 166
 167
 168/* pdAccess: API Access Control */
 169
 170	/**
 171	 * @param string $partnerUserID
 172	 * @return string|false PollDaddy userCode or false on failure
 173	 */
 174	function get_usercode( $partnerUserID ) {
 175		$this->request = new PollDaddy_Access( array(
 176//			'demands' => new PollDaddy_Demands( array( 'demand' => new PollDaddy_Demand( null, array( 'id' => __FUNCTION__ ) ) ) )
 177			'demands' => new PollDaddy_Demands( array( 'demand' => new PollDaddy_Demand( null, array( 'id' => 'getusercode' ) ) ) )
 178		), array(
 179			'partnerGUID' => $this->partnerGUID,
 180			'partnerUserID' => $partnerUserID
 181		) );
 182		$this->send_request();
 183		if ( isset( $this->response->userCode ) )
 184			return $this->response->userCode;
 185		return false;
 186	}
 187
 188	// Not Implemented
 189	function remove_usercode() {
 190		return false;
 191	}
 192
 193	/**
 194	 * @see polldaddy_account()
 195	 * @param int $partnerUserID
 196	 * @param array $args polldaddy_account() args
 197	 * @return string|false PollDaddy userCode or false on failure
 198	 */
 199	function create_account( $partnerUserID, $args ) {
 200		if ( !$account = polldaddy_account( $args ) )
 201			return false;
 202
 203		$this->request = new PollDaddy_Access( array(
 204//			'demands' => new PollDaddy_Demands( array( 'demand' => new PollDaddy_Demand( compact( 'account' ), array( 'id' => __FUNCTION__ ) ) ) )
 205			'demands' => new PollDaddy_Demands( array( 'demand' => new PollDaddy_Demand( compact( 'account' ), array( 'id' => 'createaccount' ) ) ) )
 206		), array(
 207			'partnerGUID' => $this->partnerGUID,
 208			'partnerUserID' => $partnerUserID
 209		) );
 210		$this->send_request();
 211		if ( isset( $this->response->userCode ) )
 212			return $this->response->userCode;
 213		return false;
 214	}
 215
 216function sync_rating( ){
 217          $pos = $this->add_request( 'syncrating', new PollDaddy_Rating( null , null ) );
 218  
 219          $this->send_request();
 220  
 221          $demand = $this->response_part( $pos );
 222  
 223          if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating ) ){
 224                  return $demand->rating;
 225          }
 226  
 227          return false;
 228  
 229  }
 230
 231/* pdRequest: Request API Objects */
 232
 233  /* Accounts */
 234	/**
 235	 * @return object|false PollDaddy Account or false on failure
 236	 */
 237	function get_account() {
 238//		$pos = $this->add_request( __FUNCTION__ );
 239		$pos = $this->add_request( 'getaccount' );
 240		$this->send_request();
 241		$r = $this->response_part( $pos );
 242		if ( isset( $r->account ) && !is_null( $r->account->email ) )
 243			return $r->account;
 244		return false;
 245	}
 246
 247	/**
 248	 * @see polldaddy_account()
 249	 * @param array $args polldaddy_account() args
 250	 * @return string|false PollDaddy userCode or false on failure
 251	 */
 252	function update_account( $args ) {
 253		if ( !$account = polldaddy_account( $args ) )
 254			return false;
 255
 256//		$this->add_request( __FUNCTION__, $account );
 257		$this->add_request( 'updateaccount', $account );
 258		$this->send_request();
 259		if ( isset( $this->response->userCode ) )
 260			return $this->response->userCode;
 261		return false;
 262	}
 263
 264  /* Polls */
 265	/**
 266	 * @return array|false Array of PollDaddy Polls or false on failure
 267	 */
 268	function get_polls( $start = 0, $end = 0 ) {
 269		$start = (int) $start;
 270		$end = (int) $end;
 271		if ( !$start && !$end )
 272//			$pos = $this->add_request( __FUNCTION__ );
 273			$pos = $this->add_request( 'getpolls' );
 274		else
 275//			$pos = $this->add_request( __FUNCTION__, new PollDaddy_List( null, compact( 'start', 'end' ) ) );
 276			$pos = $this->add_request( 'getpolls', new PollDaddy_List( null, compact( 'start', 'end' ) ) );
 277		$this->send_request();
 278		$r = $this->response_part( $pos );
 279		if ( isset( $r->polls ) ) {
 280			if ( isset( $r->polls->poll ) ) {
 281				if ( !is_array( $r->polls->poll ) )
 282					$r->polls->poll = array( $r->polls->poll );
 283			}
 284			return $r->polls;
 285		}
 286		return false;
 287	}
 288
 289	/**
 290	 * @return array|false Array of PollDaddy Polls or false on failure
 291	 */
 292	function get_polls_by_parent_id( $start = 0, $end = 0, $id = null ) {
 293		$start = (int) $start;
 294		$end = (int) $end;
 295		if ( !is_numeric( $id ) )
 296			$id = $GLOBALS['blog_id'];
 297
 298		if ( !$start && !$end )
 299//			$pos = $this->add_request( __FUNCTION__ );
 300			$pos = $this->add_request( 'getpolls', compact( 'id' ) );
 301		else
 302//			$pos = $this->add_request( __FUNCTION__, new PollDaddy_List( null, compact( 'start', 'end', 'id' ) ) );
 303			$pos = $this->add_request( 'getpolls', new PollDaddy_List( null, compact( 'start', 'end', 'id' ) ) );
 304		$this->send_request();
 305		$r = $this->response_part( $pos );
 306		if ( isset( $r->polls ) ) {
 307			if ( isset( $r->polls->poll ) ) {
 308				if ( !is_array( $r->polls->poll ) )
 309					$r->polls->poll = array( $r->polls->poll );
 310			}
 311			return $r->polls;
 312		}
 313		return false;
 314	}
 315
 316	/**
 317	 * @param int $id PollDaddy Poll ID
 318	 * @return array|false PollDaddy Poll or false on failure
 319	 */
 320	function get_poll( $id ) {
 321		if ( !$id = (int) $id )
 322			return false;
 323
 324//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll( null, compact( 'id' ) ) );
 325		$pos = $this->add_request( 'getpoll', new PollDaddy_Poll( null, compact( 'id' ) ) );
 326		$this->send_request();
 327
 328		$demand = $this->response_part( $pos );
 329		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->poll ) && !is_null( $demand->poll->question ) ) {
 330			if ( isset( $demand->poll->answers->answer ) && !is_array( $demand->poll->answers->answer ) ) {
 331				if ( $demand->poll->answers->answer )
 332					$demand->poll->answers->answer = array( $demand->poll->answers->answer );
 333				else
 334					$demand->poll->answers->answer = array();
 335			}
 336			return $demand->poll;
 337		}
 338		return false;
 339	}
 340
 341	/**
 342	 * @param int $id PollDaddy Poll ID
 343	 * @return array|false PollDaddy Poll or false on failure
 344	 */
 345	function build_poll( $id ) {
 346		if ( !$id = (int) $id )
 347			return false;
 348
 349//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll( null, compact( 'id' ) ) );
 350		$pos = $this->add_request( 'buildpoll', new PollDaddy_Poll( null, compact( 'id' ) ) );
 351		$this->send_request();
 352
 353		$demand = $this->response_part( $pos );
 354		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->poll ) && !is_null( $demand->poll->question ) ) {
 355			if ( isset( $demand->poll->answers->answer ) && !is_array( $demand->poll->answers->answer ) ) {
 356				if ( $demand->poll->answers->answer )
 357					$demand->poll->answers->answer = array( $demand->poll->answers->answer );
 358				else
 359					$demand->poll->answers->answer = array();
 360			}
 361			return $demand->poll;
 362		}
 363		return false;
 364	}
 365	
 366	/**
 367	 * @param int $id PollDaddy Poll ID
 368	 * @return bool success
 369	 */
 370	function delete_poll( $id ) {
 371		if ( !$id = (int) $id )
 372			return false;
 373
 374//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll( null, compact( 'id' ) ) );
 375		$pos = $this->add_request( 'deletepoll', new PollDaddy_Poll( null, compact( 'id' ) ) );
 376		$this->send_request();
 377
 378		return empty( $this->errors );
 379	}
 380
 381	/**
 382	 * @param int $id PollDaddy Poll ID
 383	 * @return bool success
 384	 */
 385	function open_poll( $id ) {
 386		if ( !$id = (int) $id )
 387			return false;
 388
 389//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll( null, compact( 'id' ) ) );
 390		$pos = $this->add_request( 'openpoll', new PollDaddy_Poll( null, compact( 'id' ) ) );
 391		$this->send_request();
 392
 393		return empty( $this->errors );
 394	}
 395	
 396	/**
 397	 * @param int $id PollDaddy Poll ID
 398	 * @return bool success
 399	 */
 400	function close_poll( $id ) {
 401		if ( !$id = (int) $id )
 402			return false;
 403
 404//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll( null, compact( 'id' ) ) );
 405		$pos = $this->add_request( 'closepoll', new PollDaddy_Poll( null, compact( 'id' ) ) );
 406		$this->send_request();
 407
 408		return empty( $this->errors );
 409	}
 410
 411	/**
 412	 * @see polldaddy_poll()
 413	 * @param array $args polldaddy_poll() args
 414	 * @return array|false PollDaddy Poll or false on failure
 415	 */
 416	function create_poll( $args = null ) {
 417		if ( !$poll = polldaddy_poll( $args ) )
 418			return false;
 419//		$pos = $this->add_request( __FUNCTION__, $poll );
 420		$pos = $this->add_request( 'createpoll', $poll );
 421		$this->send_request();
 422		if ( !$demand = $this->response_part( $pos ) )
 423			return $demand;
 424		if ( !isset( $demand->poll ) )
 425			return false;
 426		return $demand->poll;
 427
 428	}
 429
 430	/**
 431	 * @see polldaddy_poll()
 432	 * @param int $id PollDaddy Poll ID
 433	 * @param array $args polldaddy_poll() args
 434	 * @return array|false PollDaddy Poll or false on failure
 435	 */
 436	function update_poll( $id, $args = null ) {
 437		if ( !$id = (int) $id )
 438			return false;
 439
 440		if ( !$poll = polldaddy_poll( $args, $id ) )
 441			return false;
 442
 443//		$pos = $this->add_request( __FUNCTION__, $poll );
 444		$pos = $this->add_request( 'updatepoll', $poll );
 445		$this->send_request();
 446		if ( !$demand = $this->response_part( $pos ) )
 447			return $demand;
 448		if ( !isset( $demand->poll ) )
 449			return false;
 450		return $demand->poll;
 451	} 
 452
 453	/**
 454	 * @see polldaddy_poll()
 455	 * @param int $id PollDaddy Folder ID
 456	 * @param array $args polldaddy_poll() args
 457	 * @return false on failure
 458	 */
 459	function update_poll_defaults( $folderID,  $args = null ) {
 460		$folderID = (int) $folderID;
 461       
 462		if ( !$poll = new PollDaddy_Poll( $args, compact( 'folderID' ) ) )
 463			return false;
 464
 465//		$pos = $this->add_request( __FUNCTION__, $poll );
 466		$pos = $this->add_request( 'updatepolldefaults', $poll );
 467		$this->send_request();
 468		return empty( $this->errors );
 469	}
 470
 471  /* Poll Results */
 472	/**
 473	 * @param int $id PollDaddy Poll ID
 474	 * @return array|false PollDaddy Result or false on failure
 475	 */
 476	function get_poll_results( $id ) {
 477		if ( !$id = (int) $id )
 478			return false;
 479			
 480			$start = 0;
 481			$end = 2;
 482
 483//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll_Result( null, compact( 'id' ) ) );
 484		$pos = $this->add_request( 'getpollresults', new PollDaddy_Poll( null, compact( 'id' ) ) );
 485		//Optionally if you want to list other answers... 
 486		//$pos = $this->add_request( 'getpollresults', new PollDaddy_List( null, compact( 'id', 'start', 'end' ) ) );
 487		$this->send_request();
 488
 489		$demand = $this->response_part( $pos );
 490		
 491		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->result ) ) {
 492			$answers = $others = array();
 493			if ( isset( $demand->result->answers ) ) {
 494				if ( isset( $demand->result->answers->answer ) ) {
 495					if ( is_array( $demand->result->answers->answer ) )
 496						$answers = $demand->result->answers->answer;
 497					else
 498						$answers = array( $demand->result->answers->answer );
 499				}
 500			}
 501			if ( isset( $demand->result->otherAnswers ) ) {
 502				if ( isset( $demand->result->otherAnswers->otherAnswer ) ) {
 503					if ( is_array( $demand->result->otherAnswers->otherAnswer ) )
 504						$others = $demand->result->otherAnswers->otherAnswer;
 505					else
 506						$others = array( $demand->result->otherAnswers->otherAnswer );
 507				}
 508			}
 509			return (object) compact( 'answers', 'others' );
 510		}
 511		return false;
 512	}
 513
 514	/**
 515	 * @param int $id PollDaddy Poll ID
 516	 * @return bool success
 517	 */
 518	function reset_poll_results( $id ) {
 519		if ( !$id = (int) $id )
 520			return false;
 521
 522//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Poll_Result( null, compact( 'id' ) ) );
 523		$pos = $this->add_request( 'resetpollresults', new PollDaddy_Poll( null, compact( 'id' ) ) );
 524		$this->send_request();
 525
 526		return empty( $this->errors );
 527	}
 528
 529  /* Poll Comments */
 530	/**
 531	 * @param int $id PollDaddy Poll ID
 532	 * @return array|false PollDaddy Comments or false on failure
 533	 */
 534	function get_poll_comments( $id ) {
 535		if ( !$id = (int) $id )
 536			return false;
 537
 538//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Comments( null, compact( 'id' ) ) );
 539		$pos = $this->add_request( 'getpollcomments', new PollDaddy_Poll( null, compact( 'id' ) ) );
 540		$this->send_request();
 541
 542		$demand = $this->response_part( $pos );
 543		
 544		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->comments ) ) {
 545			if ( isset( $demand->comments->comment ) && !is_array( $demand->comments->comment ) ) {
 546				if ( $demand->comments->comment )
 547					$demand->comments->comment = array( $demand->comments->comment );
 548				else
 549					$demand->comments->comment = array();
 550			}
 551			return $demand->comments;
 552		}
 553		return false;
 554	}
 555
 556	/**
 557	 * @see polldaddy_comment()
 558	 * @param array $args polldaddy_comment() args
 559	 * @return bool success
 560	 */
 561	function moderate_comment( $id, $args = null ) {
 562		if ( !$id = (int) $id )
 563			return false;
 564			
 565		if ( !$comment = polldaddy_comment( $args, $id ) )
 566			return false;
 567
 568//		$this->add_request( __FUNCTION__, new PollDaddy_Comments( $comments ) );
 569		$this->add_request( 'moderatecomment', $comment);
 570		$this->send_request();
 571		
 572		return empty( $this->errors );	
 573	}
 574
 575	/* Languages */
 576		/**
 577		 * @return array|false PollDaddy Languages or false on failure
 578		 */
 579	function get_languages() {
 580
 581//		$pos = $this->add_request( __FUNCTION__, null );
 582		$pos = $this->add_request( 'getlanguages', null );
 583		$this->send_request();
 584
 585		$demand = $this->response_part( $pos );
 586		
 587		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->languages ) ) {
 588			if ( isset( $demand->languages->language ) && !is_array( $demand->languages->language ) ) {
 589				if ( $demand->languages->language )
 590					$demand->languages->language  = array( $demand->languages->language );
 591				else
 592					$demand->languages->language  = array();
 593			}
 594			return $demand->languages->language;
 595		}
 596		return false;
 597	}
 598
 599   /* Language Packs */
 600	/**
 601	 * @return array|false PollDaddy Packs or false on failure
 602	 */
 603	function get_packs() {
 604
 605//		$pos = $this->add_request( __FUNCTION__, null );
 606		$pos = $this->add_request( 'getpacks', null );
 607		$this->send_request();
 608
 609		$demand = $this->response_part( $pos );
 610		if ( isset( $demand->packs ) ) {
 611			if ( isset( $demand->packs->pack ) ) {
 612				if ( !is_array( $demand->packs->pack ) )
 613					$demand->packs->pack = array( $demand->packs->pack );
 614			}
 615			return $demand->packs;
 616		}
 617		return false;
 618	}
 619	
 620	/**
 621	 * @param int $id PollDaddy Pack ID
 622	 * @return array|false PollDaddy Pack or false on failure
 623	 */
 624	function get_pack( $id ) {
 625		if ( !$id = (int) $id )
 626			return false;
 627
 628//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Pack( null, compact( 'id' ) ) );
 629		$pos = $this->add_request( 'getpack', new PollDaddy_Pack( null, compact( 'id' ) ) );
 630		$this->send_request();
 631
 632		$demand = $this->response_part( $pos );
 633		
 634		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->pack ) ) {
 635			return $demand->pack;
 636		}
 637		return false;
 638	}
 639
 640	/**
 641	 * @param int $id PollDaddy Pack ID
 642	 * @return bool success
 643	 */
 644	function delete_pack( $id ) {
 645		if ( !$id = (int) $id )
 646			return false;
 647
 648//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Pack( null, compact( 'id' ) ) );
 649		$pos = $this->add_request( 'deletepack', new PollDaddy_Pack( null, compact( 'id' ) ) );
 650		$this->send_request();
 651
 652		return empty( $this->errors );
 653	}
 654
 655	/**
 656	 * @see polldaddy_pack()
 657	 * @param array $args polldaddy_pack() args
 658	 * @return array|false PollDaddy Pack or false on failure
 659	 */
 660	function create_pack( $args = null ) {
 661		if ( !$pack = polldaddy_pack( $args ) )
 662			return false;
 663//		$pos = $this->add_request( __FUNCTION__, $pack );
 664		$pos = $this->add_request( 'createpack', $pack );
 665		$this->send_request();
 666		if ( !$demand = $this->response_part( $pos ) )
 667			return $demand;
 668		if ( !isset( $demand->pack ) )
 669			return false;
 670		return $demand->pack;
 671	}
 672
 673	/**
 674	 * @see polldaddy_pack()
 675	 * @param int $id PollDaddy Pack ID
 676	 * @param array $args polldaddy_pack() args
 677	 * @return array|false PollDaddy Pack or false on failure
 678	 */
 679	function update_pack( $id, $args = null ) {
 680		if ( !$id = (int) $id )
 681			return false;
 682
 683		if ( !$pack = polldaddy_pack( $args, $id ) )
 684			return false;
 685
 686//		$pos = $this->add_request( __FUNCTION__, $pack );
 687		$pos = $this->add_request( 'updatepack', $pack );
 688		$this->send_request();
 689		return $this->response_part( $pos );
 690	}
 691
 692   /* Styles */
 693	/**
 694	 * @return array|false PollDaddy Styles or false on failure
 695	 */
 696	function get_styles() {
 697
 698//		$pos = $this->add_request( __FUNCTION__, null );
 699		$pos = $this->add_request( 'getstyles', null );
 700		$this->send_request();
 701
 702		$demand = $this->response_part( $pos );
 703		if ( isset( $demand->styles ) ) {
 704			if ( isset( $demand->styles->style ) ) {
 705				if ( !is_array( $demand->styles->style ) )
 706					$demand->styles->style = array( $demand->styles->style );
 707			}
 708			return $demand->styles;
 709		}
 710		return false;
 711	}
 712
 713	/**
 714	 * @param int $id PollDaddy Style ID
 715	 * @return array|false PollDaddy Style or false on failure
 716	 */
 717	function get_style( $id ) {
 718		if ( !$id = (int) $id )
 719			return false;
 720
 721	//	$pos = $this->add_request( __FUNCTION__, new PollDaddy_Style( null, compact( 'id' ) ) );
 722		$pos = $this->add_request( 'getstyle', new PollDaddy_Style( null, compact( 'id' ) ) );
 723		$this->send_request();
 724
 725		$demand = $this->response_part( $pos );
 726
 727		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->style ) ) {
 728			return $demand->style;
 729		}
 730		return false;
 731	}
 732
 733	/**
 734	 * @param int $id PollDaddy Style ID
 735	 * @return bool success
 736	 */
 737	function delete_style( $id ) {
 738		if ( !$id = (int) $id )
 739			return false;
 740
 741//		$pos = $this->add_request( __FUNCTION__, new PollDaddy_Style( null, compact( 'id' ) ) );
 742		$pos = $this->add_request( 'deletestyle', new PollDaddy_Style( null, compact( 'id' ) ) );
 743		$this->send_request();
 744
 745		return empty( $this->errors );
 746	}
 747
 748	/**
 749	 * @see polldaddy_style()
 750	 * @param array $args polldaddy_style() args
 751	 * @return array|false PollDaddy Style or false on failure
 752	 */
 753	function create_style( $args = null ) {
 754		if ( !$style = polldaddy_style( $args ) )
 755			return false;
 756//		$pos = $this->add_request( __FUNCTION__, $style );
 757		$pos = $this->add_request( 'createstyle', $style );
 758		$this->send_request();
 759		if ( !$demand = $this->response_part( $pos ) )
 760			return $demand;
 761		if ( !isset( $demand->style ) )
 762			return false;
 763		return $demand->style;
 764	}
 765
 766	/**
 767	 * @see polldaddy_style()
 768	 * @param int $id PollDaddy Style ID
 769	 * @param array $args polldaddy_style() args
 770	 * @return array|false PollDaddy Style or false on failure
 771	 */
 772	function update_style( $id, $args = null ) {
 773		if ( !$id = (int) $id )
 774			return false;
 775
 776		if ( !$style = polldaddy_style( $args, $id ) )
 777			return false;
 778
 779//		$pos = $this->add_request( __FUNCTION__, $style );
 780		$pos = $this->add_request( 'updatestyle', $style );
 781		$this->send_request();
 782		if ( !$demand = $this->response_part( $pos ) )
 783			return $demand;
 784		if ( !isset( $demand->style ) )
 785			return false;
 786		return $demand->style;
 787	}
 788
 789	function get_rating( $id ){
 790		if ( !$id = (int) $id )
 791			return false;
 792		
 793		$pos = $this->add_request( 'getrating', new PollDaddy_Rating( null , compact( 'id' ) ) );
 794
 795		$this->send_request();
 796
 797		$demand = $this->response_part( $pos );
 798
 799		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating ) ){
 800			return $demand->rating;
 801		}
 802			
 803		return false;
 804		
 805	}
 806	
 807	function update_rating( $id, $settings, $type ){
 808
 809	    if ( !$id = (int) $id )
 810	        return false;
 811
 812	    $pos = $this->add_request( 'updaterating', new PollDaddy_Rating( compact( 'settings'  ) , compact( 'id', 'type' ) ) );
 813
 814	    $this->send_request();
 815
 816	    $demand = $this->response_part( $pos );
 817
 818	    if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating ) ){
 819	        return $demand->rating;
 820	    }
 821
 822	    return false;
 823
 824	}
 825
 826    /* Create Rating 
 827	 * @param string $name PollDaddy rating name
 828	 * @param string $type PollDaddy rating type
 829	 * @return array|false PollDaddy Result or false on failure
 830	 */
 831
 832    function create_rating( $name, $type ){
 833
 834	    $pos = $this->add_request( 'createrating', new PollDaddy_Rating( compact( 'name'  ) , compact( 'type' ) ) );
 835
 836	    $this->send_request();
 837
 838	    $demand = $this->response_part( $pos );
 839
 840	    if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating ) ){
 841	        return $demand->rating;
 842	    }
 843
 844	    return false;
 845
 846    }
 847	
 848	
 849	/* Rating Results */
 850	/**
 851	 * @param int $id PollDaddy Poll ID
 852	 * @param string $period Rating period
 853	 * @param int $start paging start
 854	 * @param int $end paging end
 855	 * @return array|false PollDaddy Rating Result or false on failure
 856	 */
 857	function get_rating_results( $id, $period = '90', $start = 0, $end = 2 ) {
 858		if ( !$id = (int) $id )
 859			return false;
 860
 861		$pos = $this->add_request( 'getratingresults', new PollDaddy_List( compact( 'period' ) , compact( 'id', 'start', 'end' ) ) );
 862
 863		$this->send_request();
 864
 865		$demand = $this->response_part( $pos );
 866
 867		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating_result ) ) {
 868			if ( isset( $demand->rating_result->ratings ) ) {
 869				if ( isset( $demand->rating_result->ratings->rating ) ) {
 870					if ( !is_array( $demand->rating_result->ratings->rating ) )
 871						$demand->rating_result->ratings->rating = array( $demand->rating_result->ratings->rating );
 872				}
 873				return $demand->rating_result->ratings;
 874			}
 875		}
 876		return false;
 877	}
 878	
 879	function delete_rating_result( $id, $uid = '' ){
 880		if ( !$id = (int) $id )
 881			return false;
 882		
 883		$pos = $this->add_request( 'deleteratingresult', new PollDaddy_Rating( compact( 'uid' ) , compact( 'id' ) ) );
 884
 885		$this->send_request();
 886
 887		$demand = $this->response_part( $pos );
 888
 889		if ( is_a( $demand, 'Ghetto_XML_Object' ) && isset( $demand->rating ) ){
 890			return $demand->rating;
 891		}
 892			
 893		return false;
 894		
 895	}
 896	
 897	function get_xml(){
 898		return array( 'REQUEST' => $this->request_xml, 'RESPONSE' => $this->response_xml );
 899	}
 900}
 901
 902function &polldaddy_activity( $act ) {
 903	if ( !is_string( $act ) || !$act )
 904		return false;
 905
 906	$obj = new PollDaddy_Activity( $act );
 907	
 908	return $obj; 
 909}
 910
 911/**
 912 * @param int $id
 913 * @param string $title
 914 * @param string $css
 915 */
 916function &polldaddy_style( $args = null, $id = null, $_require_data = true ) {
 917	$false = false;
 918	if ( is_a( $args, 'PollDaddy_Style' ) ) {
 919		if ( is_null( $id ) )
 920			return $args;
 921		if ( !$id = (int) $id )
 922			return $false;
 923		$args->_id = $id;
 924		return $args;
 925	}
 926
 927	$defaults = _polldaddy_style_defaults();
 928	$retro = 0;
 929	
 930	if ( !is_null( $args ) ) {
 931		$args = wp_parse_args( $args, $defaults );
 932
 933		//if ( $_require_data ) {}
 934		
 935		$retro = (int) $args['retro'];
 936			
 937		if ( is_null( $id ) )
 938			$id = $args['id'];
 939		unset( $args['id'] );
 940	}
 941
 942	$obj = new PollDaddy_Style( $args, compact( 'id', 'retro' ) );
 943	
 944	return $obj; 
 945}
 946
 947function _polldaddy_style_defaults() {
 948	return array(
 949		'id' => null,
 950		'title' => false,
 951		'css' => false,
 952		'retro' => 0
 953	);
 954}
 955
 956/**
 957 * @param int $id
 958 * @param string $title
 959 * @param array $phrases
 960 */
 961function &polldaddy_pack( $args = null, $id = null, $_require_data = true ) {
 962	$false = false;
 963	if ( is_a( $args, 'PollDaddy_Pack' ) ) {
 964		if ( is_null( $id ) )
 965			return $args;
 966		if ( !$id = (int) $id )
 967			return $false;
 968		$args->_id = $id;
 969		return $args;
 970	}
 971
 972	$defaults = _polldaddy_pack_defaults();
 973	$retro = 0;
 974	
 975	if ( !is_null( $args ) ) {
 976		$args = wp_parse_args( $args, $defaults );
 977
 978		//if ( $_require_data ) {}
 979		
 980		$retro = (int) $args['retro'];
 981
 982		$args['pack'] = new Custom_Pack( $args['pack'] );
 983			
 984		if ( is_null( $id ) )
 985			$id = $args['id'];
 986		unset( $args['id'] );
 987	}
 988
 989	$obj = new PollDaddy_Pack( $args, compact( 'id', 'retro' ) );
 990	
 991	return $obj; 
 992}
 993
 994function _polldaddy_pack_defaults() {
 995	return array(
 996		'id' => null,
 997		'retro' => 0,
 998		'pack' => array()
 999	);
1000}
1001
1002function _polldaddy_pack_phrases_defaults() {
1003	return array(
1004		'id' => null,
1005		'type' => null,
1006		'title' => false,
1007		'phrase' => array()
1008	);
1009}
1010
1011function &polldaddy_custom_phrase( $phrase, $phraseID = null ) {
1012	if ( !is_string( $phrase ) || !$phrase )
1013		return false;
1014
1015	$obj = new Custom_Pack_Phrase( $phrase, compact( 'phraseID' ) );
1016	
1017	return $obj;
1018}
1019
1020function polldaddy_email( $args = null, $id = null, $_require_data = true ) {
1021	if ( is_a( $args, 'PollDaddy_Email' ) ) {
1022		if ( is_null( $id ) )
1023			return $args;
1024		if ( !$id = (int) $id )
1025			return $false;
1026		$args->_id = $id;
1027		return $args;
1028	}
1029
1030	$defaults = array();
1031
1032	if ( !is_null( $args ) ) {
1033		$args = wp_parse_args( $args, $defaults );
1034
1035		if ( $_require_data ) {
1036			if ( !isset( $args['address'] ) || !is_string( $args['address'] ) )
1037				return false;
1038		}
1039
1040		// Check email is an email address
1041		if ( preg_match( '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $args['address'], $matches ) == 0 )
1042			return false;
1043	}
1044
1045	return new PollDaddy_Email( $args, compact( 'id' ) );
1046}
1047
1048/**
1049 * @param string $partnerUserID
1050 * @param string $userName
1051 * @param string $email
1052 * @param string $password
1053 * @param string $firstName
1054 * @param string $lastName
1055 * @param string $countryCode
1056 * @param string $gender
1057 * @param string $yearOfBirth
1058 * @param string $websiteURL
1059 * @param string $avatarURL
1060 * @param string $bio
1061 */
1062function &polldaddy_account( $args = null ) {
1063	$false = false;
1064	if ( is_a( $args, 'PollDaddy_Account' ) )
1065		return $args;
1066
1067	$defaults = _polldaddy_account_defaults();
1068
1069	$args = wp_parse_args( $args, $defaults );
1070
1071	foreach ( array( 'userName', 'email' ) as $required )
1072		if ( !is_string( $args[$required] ) || !$args[$required] )
1073			return $false;
1074
1075	$obj = new PollDaddy_Account( $args );
1076	
1077	return $obj;
1078}
1079
1080function _polldaddy_account_defaults() {
1081	return array(
1082		'userName' => false,
1083		'email' => false,
1084		'password' => false,
1085		'firstName' => false,
1086		'lastName' => false,
1087		'countryCode' => 'nn',
1088		'gender' => 'male',
1089		'yearOfBirth' => 1901,
1090		'websiteURL' => false,
1091		'avatarURL' => false,
1092		'bio' => false
1093	);
1094}
1095
1096function &polldaddy_poll( $args = null, $id = null, $_require_data = true ) {
1097	$false = false;
1098	if ( is_a( $args, 'PollDaddy_Poll' ) ) {
1099		if ( is_null( $id ) )
1100			return $args;
1101		if ( !$id = (int) $id )
1102			return $false;
1103		$args->_id = $id;
1104		return $args;
1105	}
1106
1107	$defaults = _polldaddy_poll_defaults();
1108
1109	if ( !is_null( $args ) ) {
1110		$args = wp_parse_args( $args, $defaults );
1111		$args['parentID'] = (int) $args['parentID'];
1112
1113		if ( $_require_data ) {
1114			if ( !is_string( $args['question'] ) || !$args['question'] )
1115				return $false;
1116
1117			if ( !is_array($args['answers']) || !$args['answers'] )
1118				return $false;
1119		}
1120
1121		foreach ( array( 'multipleChoice', 'randomiseAnswers', 'otherAnswer', 'makePublic', 'closePoll', 'closePollNow', 'sharing' ) as $bool ) {
1122			if ( 'no' !== $args[$bool] && 'yes' !== $args[$bool] )
1123				$args[$bool] = $defaults[$bool];
1124		}
1125
1126		foreach ( array( 'styleID', 'packID', 'folderID', 'languageID', 'choices', 'blockExpiration' ) as $int )
1127			if ( !is_numeric( $args[$int] ) )
1128				$args[$bool] = $defaults[$int];
1129
1130		if ( !in_array( $args['resultsType'], array( 'show', 'percent', 'hide' ) ) )
1131			$args['resultsType'] = $defaults['resultsType'];
1132
1133		if ( !in_array( $args['blockRepeatVotersType'], array( 'off', 'cookie', 'cookieip' ) ) )
1134			$args['blockRepeatVotersType'] = $defaults['blockRepeatVotersType'];
1135
1136		if ( !in_array( $args['comments'], array( 'off', 'allow', 'moderate' ) ) )
1137			$args['comments'] = $defaults['comments'];
1138
1139		if ( is_numeric( $args['closeDate'] ) )
1140			$args['closeDate'] = gmdate( 'Y-m-d H:i:s', $args['closeDate'] );
1141		if ( !$args['closeDate'] )
1142			$args['closeDate'] = gmdate( 'Y-m-d H:i:s' );
1143
1144		$args['answers'] = new PollDaddy_Poll_Answers( array( 'answer' => $args['answers'] ) );
1145
1146		if ( is_null( $id ) )
1147			$id = $args['id'];
1148		unset( $args['id'] );
1149	}
1150	
1151	$obj = new PollDaddy_Poll( $args, compact( 'id' ) );
1152
1153	return $obj;
1154}
1155
1156function _polldaddy_poll_defaults() {
1157	return array(
1158		'id' => null,
1159		'question' => false,
1160		'multipleChoice' => 'no',
1161		'randomiseAnswers' => 'no',
1162		'otherAnswer' => 'no',
1163		'resultsType' => 'show',
1164		'blockRepeatVotersType' => 'cookie',
1165		'blockExpiration' => 0,
1166		'comments' => 'allow',
1167		'makePublic' => 'yes',
1168		'closePoll' => 'no',
1169		'closePollNow' => 'no',
1170		'sharing' => 'yes',
1171		'closeDate' => gmdate( 'Y-m-d H:i:s' ),
1172		'styleID' => 0,
1173		'packID' => 0,
1174		'folderID' => 0,
1175		'languageID' => _polldaddy_poll_default_language_id(),
1176		'parentID' => (int) $GLOBALS['blog_id'],
1177		'mediaCode' => '',
1178		'mediaType' => 0,
1179		'choices' => 0,
1180		'answers' => array()
1181	);
1182}
1183
1184/**
1185 * @param int $id
1186 * @param int $type
1187 */
1188function &polldaddy_comment( $args = null, $id = null ) {
1189	$defaults = _polldaddy_comment_defaults();
1190
1191	$atts = wp_parse_args( $args, $defaults );
1192	
1193	$obj = new PollDaddy_Comment( null, $atts );
1194	
1195	return $obj; 
1196}
1197
1198function _polldaddy_comment_defaults() {
1199	return array(
1200		'id' => null,
1201		'method' => 0
1202	);
1203}
1204
1205/**
1206 * @param int $id
1207 * @param array $comment
1208 */
1209function &polldaddy_comments( $args = null, $id = null ) {
1210	$false = false;
1211	if ( is_a( $args, 'PollDaddy_Comments' ) )
1212		return $args;
1213
1214	$defaults = _polldaddy_comments_defaults();
1215
1216	$args = wp_parse_args( $args, $defaults );
1217
1218	if ( is_null( $id ) )
1219		$id = $args['id'];
1220	unset( $args['id'] );
1221	
1222	$obj = new PollDaddy_Comments( $args, compact( 'id' ) );
1223	
1224	return $obj; 
1225}
1226
1227function _polldaddy_comments_defaults() {
1228	return array(
1229		'id' => null,
1230		'comment' => array()
1231	);
1232}
1233
1234if ( !function_exists( '_polldaddy_poll_default_language_id' ) ) :
1235function _polldaddy_poll_default_language_id() {
1236	return 1;
1237}
1238endif;
1239
1240function &polldaddy_poll_answer( $args, $id = null ) {
1241	if ( !is_string( $args['text'] ) || !$args['text'] )
1242		return false;
1243		
1244	$answer = new PollDaddy_Poll_Answer( $args, compact( 'id' ) );
1245
1246	return $answer;
1247}
1248
1249if ( !function_exists( 'wp_parse_args' ) ) :
1250/**
1251 * Merge user defined arguments into defaults array.
1252 *
1253 * This function is used throughout WordPress to allow for both string or array
1254 * to be merged into another array.
1255 *
1256 * @since 2.2.0
1257 *
1258 * @param string|array $args Value to merge with $defaults
1259 * @param array $defaults Array that serves as the defaults.
1260 * @return array Merged user defined values with defaults.
1261 */
1262function wp_parse_args( $args, $defaults = '' ) {
1263	if ( is_object( $args ) )
1264		$r = get_object_vars( $args );
1265	elseif ( is_array( $args ) )
1266		$r =& $args;
1267	else
1268		wp_parse_str( $args, $r );
1269
1270	if ( is_array( $defaults ) )
1271		return array_merge( $defaults, $r );
1272	return $r;
1273}
1274endif;
1275
1276if ( !function_exists( 'wp_parse_str' ) ) :
1277/**
1278 * Parses a string into variables to be stored in an array.
1279 *
1280 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
1281 * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
1282 *
1283 * @since 2.2.1
1284 * @uses apply_filters() for the 'wp_parse_str' filter.
1285 *
1286 * @param string $string The string to be parsed.
1287 * @param array $array Variables will be stored in this array.
1288 */
1289function wp_parse_str( $string, &$array ) {
1290	parse_str( $string, $array );
1291	if ( get_magic_quotes_gpc() )
1292		$array = stripslashes_deep( $array );
1293	return $array;
1294
1295	$array = apply_filters( 'wp_parse_str', $array );
1296}
1297endif;
1298
1299if ( !function_exists( 'stripslashes_deep' ) ) :
1300/**
1301 * Navigates through an array and removes slashes from the values.
1302 *
1303 * If an array is passed, the array_map() function causes a callback to pass the
1304 * value back to the function. The slashes from this value will removed.
1305 *
1306 * @since 2.0.0
1307 *
1308 * @param array|string $value The array or string to be striped.
1309 * @return array|string Stripped array (or string in the callback).
1310 */
1311function stripslashes_deep($value) {
1312	$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
1313	return $value;
1314}
1315endif;
1316?>