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