PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/class-wp-xmlrpc-server.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 3527 lines | 2150 code | 654 blank | 723 comment | 414 complexity | edc7906d7dbdc82c80e5860250fa8e8d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * XML-RPC protocol support for WordPress
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * WordPress XMLRPC server implementation.
  9. *
  10. * Implements compatability for Blogger API, MetaWeblog API, MovableType, and
  11. * pingback. Additional WordPress API for managing comments, pages, posts,
  12. * options, etc.
  13. *
  14. * Since WordPress 2.6.0, WordPress XMLRPC server can be disabled in the
  15. * administration panels.
  16. *
  17. * @package WordPress
  18. * @subpackage Publishing
  19. * @since 1.5.0
  20. */
  21. class wp_xmlrpc_server extends IXR_Server {
  22. /**
  23. * Register all of the XMLRPC methods that XMLRPC server understands.
  24. *
  25. * PHP4 constructor and sets up server and method property. Passes XMLRPC
  26. * methods through the 'xmlrpc_methods' filter to allow plugins to extend
  27. * or replace XMLRPC methods.
  28. *
  29. * @since 1.5.0
  30. *
  31. * @return wp_xmlrpc_server
  32. */
  33. function wp_xmlrpc_server() {
  34. $this->methods = array(
  35. // WordPress API
  36. 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
  37. 'wp.getPage' => 'this:wp_getPage',
  38. 'wp.getPages' => 'this:wp_getPages',
  39. 'wp.newPage' => 'this:wp_newPage',
  40. 'wp.deletePage' => 'this:wp_deletePage',
  41. 'wp.editPage' => 'this:wp_editPage',
  42. 'wp.getPageList' => 'this:wp_getPageList',
  43. 'wp.getAuthors' => 'this:wp_getAuthors',
  44. 'wp.getCategories' => 'this:mw_getCategories', // Alias
  45. 'wp.getTags' => 'this:wp_getTags',
  46. 'wp.newCategory' => 'this:wp_newCategory',
  47. 'wp.deleteCategory' => 'this:wp_deleteCategory',
  48. 'wp.suggestCategories' => 'this:wp_suggestCategories',
  49. 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
  50. 'wp.getCommentCount' => 'this:wp_getCommentCount',
  51. 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
  52. 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
  53. 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
  54. 'wp.getOptions' => 'this:wp_getOptions',
  55. 'wp.setOptions' => 'this:wp_setOptions',
  56. 'wp.getComment' => 'this:wp_getComment',
  57. 'wp.getComments' => 'this:wp_getComments',
  58. 'wp.deleteComment' => 'this:wp_deleteComment',
  59. 'wp.editComment' => 'this:wp_editComment',
  60. 'wp.newComment' => 'this:wp_newComment',
  61. 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
  62. 'wp.getMediaItem' => 'this:wp_getMediaItem',
  63. 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
  64. 'wp.getPostFormats' => 'this:wp_getPostFormats',
  65. // Blogger API
  66. 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  67. 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
  68. 'blogger.getPost' => 'this:blogger_getPost',
  69. 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
  70. 'blogger.getTemplate' => 'this:blogger_getTemplate',
  71. 'blogger.setTemplate' => 'this:blogger_setTemplate',
  72. 'blogger.newPost' => 'this:blogger_newPost',
  73. 'blogger.editPost' => 'this:blogger_editPost',
  74. 'blogger.deletePost' => 'this:blogger_deletePost',
  75. // MetaWeblog API (with MT extensions to structs)
  76. 'metaWeblog.newPost' => 'this:mw_newPost',
  77. 'metaWeblog.editPost' => 'this:mw_editPost',
  78. 'metaWeblog.getPost' => 'this:mw_getPost',
  79. 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
  80. 'metaWeblog.getCategories' => 'this:mw_getCategories',
  81. 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
  82. // MetaWeblog API aliases for Blogger API
  83. // see http://www.xmlrpc.com/stories/storyReader$2460
  84. 'metaWeblog.deletePost' => 'this:blogger_deletePost',
  85. 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
  86. 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
  87. 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  88. // MovableType API
  89. 'mt.getCategoryList' => 'this:mt_getCategoryList',
  90. 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
  91. 'mt.getPostCategories' => 'this:mt_getPostCategories',
  92. 'mt.setPostCategories' => 'this:mt_setPostCategories',
  93. 'mt.supportedMethods' => 'this:mt_supportedMethods',
  94. 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
  95. 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
  96. 'mt.publishPost' => 'this:mt_publishPost',
  97. // PingBack
  98. 'pingback.ping' => 'this:pingback_ping',
  99. 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
  100. 'demo.sayHello' => 'this:sayHello',
  101. 'demo.addTwoNumbers' => 'this:addTwoNumbers'
  102. );
  103. $this->initialise_blog_option_info( );
  104. $this->methods = apply_filters('xmlrpc_methods', $this->methods);
  105. }
  106. function serve_request() {
  107. $this->IXR_Server($this->methods);
  108. }
  109. /**
  110. * Test XMLRPC API by saying, "Hello!" to client.
  111. *
  112. * @since 1.5.0
  113. *
  114. * @param array $args Method Parameters.
  115. * @return string
  116. */
  117. function sayHello($args) {
  118. return 'Hello!';
  119. }
  120. /**
  121. * Test XMLRPC API by adding two numbers for client.
  122. *
  123. * @since 1.5.0
  124. *
  125. * @param array $args Method Parameters.
  126. * @return int
  127. */
  128. function addTwoNumbers($args) {
  129. $number1 = $args[0];
  130. $number2 = $args[1];
  131. return $number1 + $number2;
  132. }
  133. /**
  134. * Check user's credentials.
  135. *
  136. * @since 1.5.0
  137. *
  138. * @param string $user_login User's username.
  139. * @param string $user_pass User's password.
  140. * @return bool Whether authentication passed.
  141. * @deprecated use wp_xmlrpc_server::login
  142. * @see wp_xmlrpc_server::login
  143. */
  144. function login_pass_ok($user_login, $user_pass) {
  145. if ( !get_option( 'enable_xmlrpc' ) ) {
  146. $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
  147. return false;
  148. }
  149. if (!user_pass_ok($user_login, $user_pass)) {
  150. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Log user in.
  157. *
  158. * @since 2.8
  159. *
  160. * @param string $username User's username.
  161. * @param string $password User's password.
  162. * @return mixed WP_User object if authentication passed, false otherwise
  163. */
  164. function login($username, $password) {
  165. if ( !get_option( 'enable_xmlrpc' ) ) {
  166. $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
  167. return false;
  168. }
  169. $user = wp_authenticate($username, $password);
  170. if (is_wp_error($user)) {
  171. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  172. return false;
  173. }
  174. wp_set_current_user( $user->ID );
  175. return $user;
  176. }
  177. /**
  178. * Sanitize string or array of strings for database.
  179. *
  180. * @since 1.5.2
  181. *
  182. * @param string|array $array Sanitize single string or array of strings.
  183. * @return string|array Type matches $array and sanitized for the database.
  184. */
  185. function escape(&$array) {
  186. global $wpdb;
  187. if (!is_array($array)) {
  188. return($wpdb->escape($array));
  189. } else {
  190. foreach ( (array) $array as $k => $v ) {
  191. if ( is_array($v) ) {
  192. $this->escape($array[$k]);
  193. } else if ( is_object($v) ) {
  194. //skip
  195. } else {
  196. $array[$k] = $wpdb->escape($v);
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * Retrieve custom fields for post.
  203. *
  204. * @since 2.5.0
  205. *
  206. * @param int $post_id Post ID.
  207. * @return array Custom fields, if exist.
  208. */
  209. function get_custom_fields($post_id) {
  210. $post_id = (int) $post_id;
  211. $custom_fields = array();
  212. foreach ( (array) has_meta($post_id) as $meta ) {
  213. // Don't expose protected fields.
  214. if ( strpos($meta['meta_key'], '_wp_') === 0 ) {
  215. continue;
  216. }
  217. $custom_fields[] = array(
  218. "id" => $meta['meta_id'],
  219. "key" => $meta['meta_key'],
  220. "value" => $meta['meta_value']
  221. );
  222. }
  223. return $custom_fields;
  224. }
  225. /**
  226. * Set custom fields for post.
  227. *
  228. * @since 2.5.0
  229. *
  230. * @param int $post_id Post ID.
  231. * @param array $fields Custom fields.
  232. */
  233. function set_custom_fields($post_id, $fields) {
  234. $post_id = (int) $post_id;
  235. foreach ( (array) $fields as $meta ) {
  236. if ( isset($meta['id']) ) {
  237. $meta['id'] = (int) $meta['id'];
  238. if ( isset($meta['key']) ) {
  239. update_meta($meta['id'], $meta['key'], $meta['value']);
  240. }
  241. else {
  242. delete_meta($meta['id']);
  243. }
  244. }
  245. else {
  246. $_POST['metakeyinput'] = $meta['key'];
  247. $_POST['metavalue'] = $meta['value'];
  248. add_meta($post_id);
  249. }
  250. }
  251. }
  252. /**
  253. * Set up blog options property.
  254. *
  255. * Passes property through 'xmlrpc_blog_options' filter.
  256. *
  257. * @since 2.6.0
  258. */
  259. function initialise_blog_option_info( ) {
  260. global $wp_version;
  261. $this->blog_options = array(
  262. // Read only options
  263. 'software_name' => array(
  264. 'desc' => __( 'Software Name' ),
  265. 'readonly' => true,
  266. 'value' => 'WordPress'
  267. ),
  268. 'software_version' => array(
  269. 'desc' => __( 'Software Version' ),
  270. 'readonly' => true,
  271. 'value' => $wp_version
  272. ),
  273. 'blog_url' => array(
  274. 'desc' => __( 'Site URL' ),
  275. 'readonly' => true,
  276. 'option' => 'siteurl'
  277. ),
  278. // Updatable options
  279. 'time_zone' => array(
  280. 'desc' => __( 'Time Zone' ),
  281. 'readonly' => false,
  282. 'option' => 'gmt_offset'
  283. ),
  284. 'blog_title' => array(
  285. 'desc' => __( 'Site Title' ),
  286. 'readonly' => false,
  287. 'option' => 'blogname'
  288. ),
  289. 'blog_tagline' => array(
  290. 'desc' => __( 'Site Tagline' ),
  291. 'readonly' => false,
  292. 'option' => 'blogdescription'
  293. ),
  294. 'date_format' => array(
  295. 'desc' => __( 'Date Format' ),
  296. 'readonly' => false,
  297. 'option' => 'date_format'
  298. ),
  299. 'time_format' => array(
  300. 'desc' => __( 'Time Format' ),
  301. 'readonly' => false,
  302. 'option' => 'time_format'
  303. ),
  304. 'users_can_register' => array(
  305. 'desc' => __( 'Allow new users to sign up' ),
  306. 'readonly' => false,
  307. 'option' => 'users_can_register'
  308. ),
  309. 'thumbnail_size_w' => array(
  310. 'desc' => __( 'Thumbnail Width' ),
  311. 'readonly' => false,
  312. 'option' => 'thumbnail_size_w'
  313. ),
  314. 'thumbnail_size_h' => array(
  315. 'desc' => __( 'Thumbnail Height' ),
  316. 'readonly' => false,
  317. 'option' => 'thumbnail_size_h'
  318. ),
  319. 'thumbnail_crop' => array(
  320. 'desc' => __( 'Crop thumbnail to exact dimensions' ),
  321. 'readonly' => false,
  322. 'option' => 'thumbnail_crop'
  323. ),
  324. 'medium_size_w' => array(
  325. 'desc' => __( 'Medium size image width' ),
  326. 'readonly' => false,
  327. 'option' => 'medium_size_w'
  328. ),
  329. 'medium_size_h' => array(
  330. 'desc' => __( 'Medium size image height' ),
  331. 'readonly' => false,
  332. 'option' => 'medium_size_h'
  333. ),
  334. 'large_size_w' => array(
  335. 'desc' => __( 'Large size image width' ),
  336. 'readonly' => false,
  337. 'option' => 'large_size_w'
  338. ),
  339. 'large_size_h' => array(
  340. 'desc' => __( 'Large size image height' ),
  341. 'readonly' => false,
  342. 'option' => 'large_size_h'
  343. )
  344. );
  345. $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
  346. }
  347. /**
  348. * Retrieve the blogs of the user.
  349. *
  350. * @since 2.6.0
  351. *
  352. * @param array $args Method parameters.
  353. * @return array
  354. */
  355. function wp_getUsersBlogs( $args ) {
  356. global $current_site;
  357. // If this isn't on WPMU then just use blogger_getUsersBlogs
  358. if ( !is_multisite() ) {
  359. array_unshift( $args, 1 );
  360. return $this->blogger_getUsersBlogs( $args );
  361. }
  362. $this->escape( $args );
  363. $username = $args[0];
  364. $password = $args[1];
  365. if ( !$user = $this->login($username, $password) )
  366. return $this->error;
  367. do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
  368. $blogs = (array) get_blogs_of_user( $user->ID );
  369. $struct = array( );
  370. foreach ( $blogs as $blog ) {
  371. // Don't include blogs that aren't hosted at this site
  372. if ( $blog->site_id != $current_site->id )
  373. continue;
  374. $blog_id = $blog->userblog_id;
  375. switch_to_blog($blog_id);
  376. $is_admin = current_user_can('manage_options');
  377. $struct[] = array(
  378. 'isAdmin' => $is_admin,
  379. 'url' => get_option( 'home' ) . '/',
  380. 'blogid' => (string) $blog_id,
  381. 'blogName' => get_option( 'blogname' ),
  382. 'xmlrpc' => site_url( 'xmlrpc.php' )
  383. );
  384. restore_current_blog( );
  385. }
  386. return $struct;
  387. }
  388. /**
  389. * Retrieve page.
  390. *
  391. * @since 2.2.0
  392. *
  393. * @param array $args Method parameters.
  394. * @return array
  395. */
  396. function wp_getPage($args) {
  397. $this->escape($args);
  398. $blog_id = (int) $args[0];
  399. $page_id = (int) $args[1];
  400. $username = $args[2];
  401. $password = $args[3];
  402. if ( !$user = $this->login($username, $password) ) {
  403. return $this->error;
  404. }
  405. if ( !current_user_can( 'edit_page', $page_id ) )
  406. return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
  407. do_action('xmlrpc_call', 'wp.getPage');
  408. // Lookup page info.
  409. $page = get_page($page_id);
  410. // If we found the page then format the data.
  411. if ( $page->ID && ($page->post_type == "page") ) {
  412. // Get all of the page content and link.
  413. $full_page = get_extended($page->post_content);
  414. $link = post_permalink($page->ID);
  415. // Get info the page parent if there is one.
  416. $parent_title = "";
  417. if ( !empty($page->post_parent) ) {
  418. $parent = get_page($page->post_parent);
  419. $parent_title = $parent->post_title;
  420. }
  421. // Determine comment and ping settings.
  422. $allow_comments = comments_open($page->ID) ? 1 : 0;
  423. $allow_pings = pings_open($page->ID) ? 1 : 0;
  424. // Format page date.
  425. $page_date = mysql2date("Ymd\TH:i:s", $page->post_date, false);
  426. $page_date_gmt = mysql2date("Ymd\TH:i:s", $page->post_date_gmt, false);
  427. // For drafts use the GMT version of the date
  428. if ( $page->post_status == 'draft' )
  429. $page_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page->post_date ), 'Ymd\TH:i:s' );
  430. // Pull the categories info together.
  431. $categories = array();
  432. foreach ( wp_get_post_categories($page->ID) as $cat_id ) {
  433. $categories[] = get_cat_name($cat_id);
  434. }
  435. // Get the author info.
  436. $author = get_userdata($page->post_author);
  437. $page_template = get_post_meta( $page->ID, '_wp_page_template', true );
  438. if ( empty( $page_template ) )
  439. $page_template = 'default';
  440. $page_struct = array(
  441. "dateCreated" => new IXR_Date($page_date),
  442. "userid" => $page->post_author,
  443. "page_id" => $page->ID,
  444. "page_status" => $page->post_status,
  445. "description" => $full_page["main"],
  446. "title" => $page->post_title,
  447. "link" => $link,
  448. "permaLink" => $link,
  449. "categories" => $categories,
  450. "excerpt" => $page->post_excerpt,
  451. "text_more" => $full_page["extended"],
  452. "mt_allow_comments" => $allow_comments,
  453. "mt_allow_pings" => $allow_pings,
  454. "wp_slug" => $page->post_name,
  455. "wp_password" => $page->post_password,
  456. "wp_author" => $author->display_name,
  457. "wp_page_parent_id" => $page->post_parent,
  458. "wp_page_parent_title" => $parent_title,
  459. "wp_page_order" => $page->menu_order,
  460. "wp_author_id" => $author->ID,
  461. "wp_author_display_name" => $author->display_name,
  462. "date_created_gmt" => new IXR_Date($page_date_gmt),
  463. "custom_fields" => $this->get_custom_fields($page_id),
  464. "wp_page_template" => $page_template
  465. );
  466. return($page_struct);
  467. }
  468. // If the page doesn't exist indicate that.
  469. else {
  470. return(new IXR_Error(404, __("Sorry, no such page.")));
  471. }
  472. }
  473. /**
  474. * Retrieve Pages.
  475. *
  476. * @since 2.2.0
  477. *
  478. * @param array $args Method parameters.
  479. * @return array
  480. */
  481. function wp_getPages($args) {
  482. $this->escape($args);
  483. $blog_id = (int) $args[0];
  484. $username = $args[1];
  485. $password = $args[2];
  486. $num_pages = isset($args[3]) ? (int) $args[3] : 10;
  487. if ( !$user = $this->login($username, $password) )
  488. return $this->error;
  489. if ( !current_user_can( 'edit_pages' ) )
  490. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  491. do_action('xmlrpc_call', 'wp.getPages');
  492. $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
  493. $num_pages = count($pages);
  494. // If we have pages, put together their info.
  495. if ( $num_pages >= 1 ) {
  496. $pages_struct = array();
  497. for ( $i = 0; $i < $num_pages; $i++ ) {
  498. $page = wp_xmlrpc_server::wp_getPage(array(
  499. $blog_id, $pages[$i]->ID, $username, $password
  500. ));
  501. $pages_struct[] = $page;
  502. }
  503. return($pages_struct);
  504. }
  505. // If no pages were found return an error.
  506. else {
  507. return(array());
  508. }
  509. }
  510. /**
  511. * Create new page.
  512. *
  513. * @since 2.2.0
  514. *
  515. * @param array $args Method parameters.
  516. * @return unknown
  517. */
  518. function wp_newPage($args) {
  519. // Items not escaped here will be escaped in newPost.
  520. $username = $this->escape($args[1]);
  521. $password = $this->escape($args[2]);
  522. $page = $args[3];
  523. $publish = $args[4];
  524. if ( !$user = $this->login($username, $password) )
  525. return $this->error;
  526. do_action('xmlrpc_call', 'wp.newPage');
  527. // Make sure the user is allowed to add new pages.
  528. if ( !current_user_can("publish_pages") )
  529. return(new IXR_Error(401, __("Sorry, you cannot add new pages.")));
  530. // Mark this as content for a page.
  531. $args[3]["post_type"] = "page";
  532. // Let mw_newPost do all of the heavy lifting.
  533. return($this->mw_newPost($args));
  534. }
  535. /**
  536. * Delete page.
  537. *
  538. * @since 2.2.0
  539. *
  540. * @param array $args Method parameters.
  541. * @return bool True, if success.
  542. */
  543. function wp_deletePage($args) {
  544. $this->escape($args);
  545. $blog_id = (int) $args[0];
  546. $username = $args[1];
  547. $password = $args[2];
  548. $page_id = (int) $args[3];
  549. if ( !$user = $this->login($username, $password) )
  550. return $this->error;
  551. do_action('xmlrpc_call', 'wp.deletePage');
  552. // Get the current page based on the page_id and
  553. // make sure it is a page and not a post.
  554. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  555. if ( !$actual_page || ($actual_page["post_type"] != "page") )
  556. return(new IXR_Error(404, __("Sorry, no such page.")));
  557. // Make sure the user can delete pages.
  558. if ( !current_user_can("delete_page", $page_id) )
  559. return(new IXR_Error(401, __("Sorry, you do not have the right to delete this page.")));
  560. // Attempt to delete the page.
  561. $result = wp_delete_post($page_id);
  562. if ( !$result )
  563. return(new IXR_Error(500, __("Failed to delete the page.")));
  564. return(true);
  565. }
  566. /**
  567. * Edit page.
  568. *
  569. * @since 2.2.0
  570. *
  571. * @param array $args Method parameters.
  572. * @return unknown
  573. */
  574. function wp_editPage($args) {
  575. // Items not escaped here will be escaped in editPost.
  576. $blog_id = (int) $args[0];
  577. $page_id = (int) $this->escape($args[1]);
  578. $username = $this->escape($args[2]);
  579. $password = $this->escape($args[3]);
  580. $content = $args[4];
  581. $publish = $args[5];
  582. if ( !$user = $this->login($username, $password) )
  583. return $this->error;
  584. do_action('xmlrpc_call', 'wp.editPage');
  585. // Get the page data and make sure it is a page.
  586. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  587. if ( !$actual_page || ($actual_page["post_type"] != "page") )
  588. return(new IXR_Error(404, __("Sorry, no such page.")));
  589. // Make sure the user is allowed to edit pages.
  590. if ( !current_user_can("edit_page", $page_id) )
  591. return(new IXR_Error(401, __("Sorry, you do not have the right to edit this page.")));
  592. // Mark this as content for a page.
  593. $content["post_type"] = "page";
  594. // Arrange args in the way mw_editPost understands.
  595. $args = array(
  596. $page_id,
  597. $username,
  598. $password,
  599. $content,
  600. $publish
  601. );
  602. // Let mw_editPost do all of the heavy lifting.
  603. return($this->mw_editPost($args));
  604. }
  605. /**
  606. * Retrieve page list.
  607. *
  608. * @since 2.2.0
  609. *
  610. * @param array $args Method parameters.
  611. * @return unknown
  612. */
  613. function wp_getPageList($args) {
  614. global $wpdb;
  615. $this->escape($args);
  616. $blog_id = (int) $args[0];
  617. $username = $args[1];
  618. $password = $args[2];
  619. if ( !$user = $this->login($username, $password) )
  620. return $this->error;
  621. if ( !current_user_can( 'edit_pages' ) )
  622. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  623. do_action('xmlrpc_call', 'wp.getPageList');
  624. // Get list of pages ids and titles
  625. $page_list = $wpdb->get_results("
  626. SELECT ID page_id,
  627. post_title page_title,
  628. post_parent page_parent_id,
  629. post_date_gmt,
  630. post_date,
  631. post_status
  632. FROM {$wpdb->posts}
  633. WHERE post_type = 'page'
  634. ORDER BY ID
  635. ");
  636. // The date needs to be formated properly.
  637. $num_pages = count($page_list);
  638. for ( $i = 0; $i < $num_pages; $i++ ) {
  639. $post_date = mysql2date("Ymd\TH:i:s", $page_list[$i]->post_date, false);
  640. $post_date_gmt = mysql2date("Ymd\TH:i:s", $page_list[$i]->post_date_gmt, false);
  641. $page_list[$i]->dateCreated = new IXR_Date($post_date);
  642. $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);
  643. // For drafts use the GMT version of the date
  644. if ( $page_list[$i]->post_status == 'draft' ) {
  645. $page_list[$i]->date_created_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page_list[$i]->post_date ), 'Ymd\TH:i:s' );
  646. $page_list[$i]->date_created_gmt = new IXR_Date( $page_list[$i]->date_created_gmt );
  647. }
  648. unset($page_list[$i]->post_date_gmt);
  649. unset($page_list[$i]->post_date);
  650. unset($page_list[$i]->post_status);
  651. }
  652. return($page_list);
  653. }
  654. /**
  655. * Retrieve authors list.
  656. *
  657. * @since 2.2.0
  658. *
  659. * @param array $args Method parameters.
  660. * @return array
  661. */
  662. function wp_getAuthors($args) {
  663. $this->escape($args);
  664. $blog_id = (int) $args[0];
  665. $username = $args[1];
  666. $password = $args[2];
  667. if ( !$user = $this->login($username, $password) )
  668. return $this->error;
  669. if ( !current_user_can("edit_posts") )
  670. return(new IXR_Error(401, __("Sorry, you cannot edit posts on this site.")));
  671. do_action('xmlrpc_call', 'wp.getAuthors');
  672. $authors = array();
  673. foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
  674. $authors[] = array(
  675. "user_id" => $user->ID,
  676. "user_login" => $user->user_login,
  677. "display_name" => $user->display_name
  678. );
  679. }
  680. return $authors;
  681. }
  682. /**
  683. * Get list of all tags
  684. *
  685. * @since 2.7
  686. *
  687. * @param array $args Method parameters.
  688. * @return array
  689. */
  690. function wp_getTags( $args ) {
  691. $this->escape( $args );
  692. $blog_id = (int) $args[0];
  693. $username = $args[1];
  694. $password = $args[2];
  695. if ( !$user = $this->login($username, $password) )
  696. return $this->error;
  697. if ( !current_user_can( 'edit_posts' ) )
  698. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
  699. do_action( 'xmlrpc_call', 'wp.getKeywords' );
  700. $tags = array( );
  701. if ( $all_tags = get_tags() ) {
  702. foreach( (array) $all_tags as $tag ) {
  703. $struct['tag_id'] = $tag->term_id;
  704. $struct['name'] = $tag->name;
  705. $struct['count'] = $tag->count;
  706. $struct['slug'] = $tag->slug;
  707. $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
  708. $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
  709. $tags[] = $struct;
  710. }
  711. }
  712. return $tags;
  713. }
  714. /**
  715. * Create new category.
  716. *
  717. * @since 2.2.0
  718. *
  719. * @param array $args Method parameters.
  720. * @return int Category ID.
  721. */
  722. function wp_newCategory($args) {
  723. $this->escape($args);
  724. $blog_id = (int) $args[0];
  725. $username = $args[1];
  726. $password = $args[2];
  727. $category = $args[3];
  728. if ( !$user = $this->login($username, $password) )
  729. return $this->error;
  730. do_action('xmlrpc_call', 'wp.newCategory');
  731. // Make sure the user is allowed to add a category.
  732. if ( !current_user_can("manage_categories") )
  733. return(new IXR_Error(401, __("Sorry, you do not have the right to add a category.")));
  734. // If no slug was provided make it empty so that
  735. // WordPress will generate one.
  736. if ( empty($category["slug"]) )
  737. $category["slug"] = "";
  738. // If no parent_id was provided make it empty
  739. // so that it will be a top level page (no parent).
  740. if ( !isset($category["parent_id"]) )
  741. $category["parent_id"] = "";
  742. // If no description was provided make it empty.
  743. if ( empty($category["description"]) )
  744. $category["description"] = "";
  745. $new_category = array(
  746. "cat_name" => $category["name"],
  747. "category_nicename" => $category["slug"],
  748. "category_parent" => $category["parent_id"],
  749. "category_description" => $category["description"]
  750. );
  751. $cat_id = wp_insert_category($new_category, true);
  752. if ( is_wp_error( $cat_id ) ) {
  753. if ( 'term_exists' == $cat_id->get_error_code() )
  754. return (int) $cat_id->get_error_data();
  755. else
  756. return(new IXR_Error(500, __("Sorry, the new category failed.")));
  757. } elseif ( ! $cat_id ) {
  758. return(new IXR_Error(500, __("Sorry, the new category failed.")));
  759. }
  760. return($cat_id);
  761. }
  762. /**
  763. * Remove category.
  764. *
  765. * @since 2.5.0
  766. *
  767. * @param array $args Method parameters.
  768. * @return mixed See {@link wp_delete_term()} for return info.
  769. */
  770. function wp_deleteCategory($args) {
  771. $this->escape($args);
  772. $blog_id = (int) $args[0];
  773. $username = $args[1];
  774. $password = $args[2];
  775. $category_id = (int) $args[3];
  776. if ( !$user = $this->login($username, $password) )
  777. return $this->error;
  778. do_action('xmlrpc_call', 'wp.deleteCategory');
  779. if ( !current_user_can("manage_categories") )
  780. return new IXR_Error( 401, __( "Sorry, you do not have the right to delete a category." ) );
  781. return wp_delete_term( $category_id, 'category' );
  782. }
  783. /**
  784. * Retrieve category list.
  785. *
  786. * @since 2.2.0
  787. *
  788. * @param array $args Method parameters.
  789. * @return array
  790. */
  791. function wp_suggestCategories($args) {
  792. $this->escape($args);
  793. $blog_id = (int) $args[0];
  794. $username = $args[1];
  795. $password = $args[2];
  796. $category = $args[3];
  797. $max_results = (int) $args[4];
  798. if ( !$user = $this->login($username, $password) )
  799. return $this->error;
  800. if ( !current_user_can( 'edit_posts' ) )
  801. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
  802. do_action('xmlrpc_call', 'wp.suggestCategories');
  803. $category_suggestions = array();
  804. $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
  805. foreach ( (array) get_categories($args) as $cat ) {
  806. $category_suggestions[] = array(
  807. "category_id" => $cat->term_id,
  808. "category_name" => $cat->name
  809. );
  810. }
  811. return($category_suggestions);
  812. }
  813. /**
  814. * Retrieve comment.
  815. *
  816. * @since 2.7.0
  817. *
  818. * @param array $args Method parameters.
  819. * @return array
  820. */
  821. function wp_getComment($args) {
  822. $this->escape($args);
  823. $blog_id = (int) $args[0];
  824. $username = $args[1];
  825. $password = $args[2];
  826. $comment_id = (int) $args[3];
  827. if ( !$user = $this->login($username, $password) )
  828. return $this->error;
  829. if ( !current_user_can( 'moderate_comments' ) )
  830. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  831. do_action('xmlrpc_call', 'wp.getComment');
  832. if ( ! $comment = get_comment($comment_id) )
  833. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  834. // Format page date.
  835. $comment_date = mysql2date("Ymd\TH:i:s", $comment->comment_date, false);
  836. $comment_date_gmt = mysql2date("Ymd\TH:i:s", $comment->comment_date_gmt, false);
  837. if ( '0' == $comment->comment_approved )
  838. $comment_status = 'hold';
  839. else if ( 'spam' == $comment->comment_approved )
  840. $comment_status = 'spam';
  841. else if ( '1' == $comment->comment_approved )
  842. $comment_status = 'approve';
  843. else
  844. $comment_status = $comment->comment_approved;
  845. $link = get_comment_link($comment);
  846. $comment_struct = array(
  847. "date_created_gmt" => new IXR_Date($comment_date_gmt),
  848. "user_id" => $comment->user_id,
  849. "comment_id" => $comment->comment_ID,
  850. "parent" => $comment->comment_parent,
  851. "status" => $comment_status,
  852. "content" => $comment->comment_content,
  853. "link" => $link,
  854. "post_id" => $comment->comment_post_ID,
  855. "post_title" => get_the_title($comment->comment_post_ID),
  856. "author" => $comment->comment_author,
  857. "author_url" => $comment->comment_author_url,
  858. "author_email" => $comment->comment_author_email,
  859. "author_ip" => $comment->comment_author_IP,
  860. "type" => $comment->comment_type,
  861. );
  862. return $comment_struct;
  863. }
  864. /**
  865. * Retrieve comments.
  866. *
  867. * @since 2.7.0
  868. *
  869. * @param array $args Method parameters.
  870. * @return array
  871. */
  872. function wp_getComments($args) {
  873. $raw_args = $args;
  874. $this->escape($args);
  875. $blog_id = (int) $args[0];
  876. $username = $args[1];
  877. $password = $args[2];
  878. $struct = $args[3];
  879. if ( !$user = $this->login($username, $password) )
  880. return $this->error;
  881. if ( !current_user_can( 'moderate_comments' ) )
  882. return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
  883. do_action('xmlrpc_call', 'wp.getComments');
  884. if ( isset($struct['status']) )
  885. $status = $struct['status'];
  886. else
  887. $status = '';
  888. $post_id = '';
  889. if ( isset($struct['post_id']) )
  890. $post_id = absint($struct['post_id']);
  891. $offset = 0;
  892. if ( isset($struct['offset']) )
  893. $offset = absint($struct['offset']);
  894. $number = 10;
  895. if ( isset($struct['number']) )
  896. $number = absint($struct['number']);
  897. $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
  898. $num_comments = count($comments);
  899. if ( ! $num_comments )
  900. return array();
  901. $comments_struct = array();
  902. for ( $i = 0; $i < $num_comments; $i++ ) {
  903. $comment = wp_xmlrpc_server::wp_getComment(array(
  904. $raw_args[0], $raw_args[1], $raw_args[2], $comments[$i]->comment_ID,
  905. ));
  906. $comments_struct[] = $comment;
  907. }
  908. return $comments_struct;
  909. }
  910. /**
  911. * Remove comment.
  912. *
  913. * @since 2.7.0
  914. *
  915. * @param array $args Method parameters.
  916. * @return mixed {@link wp_delete_comment()}
  917. */
  918. function wp_deleteComment($args) {
  919. $this->escape($args);
  920. $blog_id = (int) $args[0];
  921. $username = $args[1];
  922. $password = $args[2];
  923. $comment_ID = (int) $args[3];
  924. if ( !$user = $this->login($username, $password) )
  925. return $this->error;
  926. if ( !current_user_can( 'moderate_comments' ) )
  927. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  928. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  929. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  930. do_action('xmlrpc_call', 'wp.deleteComment');
  931. if ( ! get_comment($comment_ID) )
  932. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  933. return wp_delete_comment($comment_ID);
  934. }
  935. /**
  936. * Edit comment.
  937. *
  938. * @since 2.7.0
  939. *
  940. * @param array $args Method parameters.
  941. * @return bool True, on success.
  942. */
  943. function wp_editComment($args) {
  944. $this->escape($args);
  945. $blog_id = (int) $args[0];
  946. $username = $args[1];
  947. $password = $args[2];
  948. $comment_ID = (int) $args[3];
  949. $content_struct = $args[4];
  950. if ( !$user = $this->login($username, $password) )
  951. return $this->error;
  952. if ( !current_user_can( 'moderate_comments' ) )
  953. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  954. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  955. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  956. do_action('xmlrpc_call', 'wp.editComment');
  957. if ( ! get_comment($comment_ID) )
  958. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  959. if ( isset($content_struct['status']) ) {
  960. $statuses = get_comment_statuses();
  961. $statuses = array_keys($statuses);
  962. if ( ! in_array($content_struct['status'], $statuses) )
  963. return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  964. $comment_approved = $content_struct['status'];
  965. }
  966. // Do some timestamp voodoo
  967. if ( !empty( $content_struct['date_created_gmt'] ) ) {
  968. $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  969. $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  970. $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  971. }
  972. if ( isset($content_struct['content']) )
  973. $comment_content = $content_struct['content'];
  974. if ( isset($content_struct['author']) )
  975. $comment_author = $content_struct['author'];
  976. if ( isset($content_struct['author_url']) )
  977. $comment_author_url = $content_struct['author_url'];
  978. if ( isset($content_struct['author_email']) )
  979. $comment_author_email = $content_struct['author_email'];
  980. // We've got all the data -- post it:
  981. $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
  982. $result = wp_update_comment($comment);
  983. if ( is_wp_error( $result ) )
  984. return new IXR_Error(500, $result->get_error_message());
  985. if ( !$result )
  986. return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
  987. return true;
  988. }
  989. /**
  990. * Create new comment.
  991. *
  992. * @since 2.7.0
  993. *
  994. * @param array $args Method parameters.
  995. * @return mixed {@link wp_new_comment()}
  996. */
  997. function wp_newComment($args) {
  998. global $wpdb;
  999. $this->escape($args);
  1000. $blog_id = (int) $args[0];
  1001. $username = $args[1];
  1002. $password = $args[2];
  1003. $post = $args[3];
  1004. $content_struct = $args[4];
  1005. $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
  1006. $user = $this->login($username, $password);
  1007. if ( !$user ) {
  1008. $logged_in = false;
  1009. if ( $allow_anon && get_option('comment_registration') )
  1010. return new IXR_Error( 403, __( 'You must be registered to comment' ) );
  1011. else if ( !$allow_anon )
  1012. return $this->error;
  1013. } else {
  1014. $logged_in = true;
  1015. }
  1016. if ( is_numeric($post) )
  1017. $post_id = absint($post);
  1018. else
  1019. $post_id = url_to_postid($post);
  1020. if ( ! $post_id )
  1021. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1022. if ( ! get_post($post_id) )
  1023. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1024. $comment['comment_post_ID'] = $post_id;
  1025. if ( $logged_in ) {
  1026. $comment['comment_author'] = $wpdb->escape( $user->display_name );
  1027. $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
  1028. $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
  1029. $comment['user_ID'] = $user->ID;
  1030. } else {
  1031. $comment['comment_author'] = '';
  1032. if ( isset($content_struct['author']) )
  1033. $comment['comment_author'] = $content_struct['author'];
  1034. $comment['comment_author_email'] = '';
  1035. if ( isset($content_struct['author_email']) )
  1036. $comment['comment_author_email'] = $content_struct['author_email'];
  1037. $comment['comment_author_url'] = '';
  1038. if ( isset($content_struct['author_url']) )
  1039. $comment['comment_author_url'] = $content_struct['author_url'];
  1040. $comment['user_ID'] = 0;
  1041. if ( get_option('require_name_email') ) {
  1042. if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
  1043. return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
  1044. elseif ( !is_email($comment['comment_author_email']) )
  1045. return new IXR_Error( 403, __( 'A valid email address is required' ) );
  1046. }
  1047. }
  1048. $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
  1049. $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
  1050. do_action('xmlrpc_call', 'wp.newComment');
  1051. return wp_new_comment($comment);
  1052. }
  1053. /**
  1054. * Retrieve all of the comment status.
  1055. *
  1056. * @since 2.7.0
  1057. *
  1058. * @param array $args Method parameters.
  1059. * @return array
  1060. */
  1061. function wp_getCommentStatusList($args) {
  1062. $this->escape( $args );
  1063. $blog_id = (int) $args[0];
  1064. $username = $args[1];
  1065. $password = $args[2];
  1066. if ( !$user = $this->login($username, $password) )
  1067. return $this->error;
  1068. if ( !current_user_can( 'moderate_comments' ) )
  1069. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1070. do_action('xmlrpc_call', 'wp.getCommentStatusList');
  1071. return get_comment_statuses( );
  1072. }
  1073. /**
  1074. * Retrieve comment count.
  1075. *
  1076. * @since 2.5.0
  1077. *
  1078. * @param array $args Method parameters.
  1079. * @return array
  1080. */
  1081. function wp_getCommentCount( $args ) {
  1082. $this->escape($args);
  1083. $blog_id = (int) $args[0];
  1084. $username = $args[1];
  1085. $password = $args[2];
  1086. $post_id = (int) $args[3];
  1087. if ( !$user = $this->login($username, $password) )
  1088. return $this->error;
  1089. if ( !current_user_can( 'edit_posts' ) )
  1090. return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
  1091. do_action('xmlrpc_call', 'wp.getCommentCount');
  1092. $count = wp_count_comments( $post_id );
  1093. return array(
  1094. "approved" => $count->approved,
  1095. "awaiting_moderation" => $count->moderated,
  1096. "spam" => $count->spam,
  1097. "total_comments" => $count->total_comments
  1098. );
  1099. }
  1100. /**
  1101. * Retrieve post statuses.
  1102. *
  1103. * @since 2.5.0
  1104. *
  1105. * @param array $args Method parameters.
  1106. * @return array
  1107. */
  1108. function wp_getPostStatusList( $args ) {
  1109. $this->escape( $args );
  1110. $blog_id = (int) $args[0];
  1111. $username = $args[1];
  1112. $password = $args[2];
  1113. if ( !$user = $this->login($username, $password) )
  1114. return $this->error;
  1115. if ( !current_user_can( 'edit_posts' ) )
  1116. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1117. do_action('xmlrpc_call', 'wp.getPostStatusList');
  1118. return get_post_statuses( );
  1119. }
  1120. /**
  1121. * Retrieve page statuses.
  1122. *
  1123. * @since 2.5.0
  1124. *
  1125. * @param array $args Method parameters.
  1126. * @return array
  1127. */
  1128. function wp_getPageStatusList( $args ) {
  1129. $this->escape( $args );
  1130. $blog_id = (int) $args[0];
  1131. $username = $args[1];
  1132. $password = $args[2];
  1133. if ( !$user = $this->login($username, $password) )
  1134. return $this->error;
  1135. if ( !current_user_can( 'edit_pages' ) )
  1136. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1137. do_action('xmlrpc_call', 'wp.getPageStatusList');
  1138. return get_page_statuses( );
  1139. }
  1140. /**
  1141. * Retrieve page templates.
  1142. *
  1143. * @since 2.6.0
  1144. *
  1145. * @param array $args Method parameters.
  1146. * @return array
  1147. */
  1148. function wp_getPageTemplates( $args ) {
  1149. $this->escape( $args );
  1150. $blog_id = (int) $args[0];
  1151. $username = $args[1];
  1152. $password = $args[2];
  1153. if ( !$user = $this->login($username, $password) )
  1154. return $this->error;
  1155. if ( !current_user_can( 'edit_pages' ) )
  1156. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1157. $templates = get_page_templates( );
  1158. $templates['Default'] = 'default';
  1159. return $templates;
  1160. }
  1161. /**
  1162. * Retrieve blog options.
  1163. *
  1164. * @since 2.6.0
  1165. *
  1166. * @param array $args Method parameters.
  1167. * @return array
  1168. */
  1169. function wp_getOptions( $args ) {
  1170. $this->escape( $args );
  1171. $blog_id = (int) $args[0];
  1172. $username = $args[1];
  1173. $password = $args[2];
  1174. $options = isset( $args[3] ) ? (array) $args[3] : array();
  1175. if ( !$user = $this->login($username, $password) )
  1176. return $this->error;
  1177. // If no specific options where asked for, return all of them
  1178. if ( count( $options ) == 0 )
  1179. $options = array_keys($this->blog_options);
  1180. return $this->_getOptions($options);
  1181. }
  1182. /**
  1183. * Retrieve blog options value from list.
  1184. *
  1185. * @since 2.6.0
  1186. *
  1187. * @param array $options Options to retrieve.
  1188. * @return array
  1189. */
  1190. function _getOptions($options) {
  1191. $data = array( );
  1192. foreach ( $options as $option ) {
  1193. if ( array_key_exists( $option, $this->blog_options ) ) {
  1194. $data[$option] = $this->blog_options[$option];
  1195. //Is the value static or dynamic?
  1196. if ( isset( $data[$option]['option'] ) ) {
  1197. $data[$option]['value'] = get_option( $data[$option]['option'] );
  1198. unset($data[$option]['option']);
  1199. }
  1200. }
  1201. }
  1202. return $data;
  1203. }
  1204. /**
  1205. * Update blog options.
  1206. *
  1207. * @since 2.6.0
  1208. *
  1209. * @param array $args Method parameters.
  1210. * @return unknown
  1211. */
  1212. function wp_setOptions( $args ) {
  1213. $this->escape( $args );
  1214. $blog_id = (int) $args[0];
  1215. $username = $args[1];
  1216. $password = $args[2];
  1217. $options = (array) $args[3];
  1218. if ( !$user = $this->login($username, $password) )
  1219. return $this->error;
  1220. if ( !current_user_can( 'manage_options' ) )
  1221. return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
  1222. foreach ( $options as $o_name => $o_value ) {
  1223. $option_names[] = $o_name;
  1224. if ( !array_key_exists( $o_name, $this->blog_options ) )
  1225. continue;
  1226. if ( $this->blog_options[$o_name]['readonly'] == true )
  1227. continue;
  1228. update_option( $this->blog_options[$o_name]['option'], $o_value );
  1229. }
  1230. //Now return the updated values
  1231. return $this->_getOptions($option_names);
  1232. }
  1233. /**
  1234. * Retrieve a media item by ID
  1235. *
  1236. * @since 3.1.0
  1237. *
  1238. * @param array $args Method parameters. Contains:
  1239. * - blog_id
  1240. * - username
  1241. * - password
  1242. * - attachment_id
  1243. * @return array. Assocciative array containing:
  1244. * - 'date_created_gmt'
  1245. * - 'parent'
  1246. * - 'link'
  1247. * - 'thumbnail'
  1248. * - 'title'
  1249. * - 'caption'
  1250. * - 'description'
  1251. * - 'metadata'
  1252. */
  1253. function wp_getMediaItem($args) {
  1254. $this->escape($args);
  1255. $blog_id = (int) $args[0];
  1256. $username = $args[1];
  1257. $password = $args[2];
  1258. $attachment_id = (int) $args[3];
  1259. if ( !$user = $this->login($username, $password) )
  1260. return $this->error;
  1261. if ( !current_user_can( 'upload_files' ) )
  1262. return new IXR_Error( 403, __( 'You are not allowed to upload files on this site.' ) );
  1263. do_action('xmlrpc_call', 'wp.getMediaItem');
  1264. if ( ! $attachment = get_post($attachment_id) )
  1265. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  1266. // Format page date.
  1267. $attachment_date = mysql2date("Ymd\TH:i:s", $attachment->post_date, false);
  1268. $attachment_date_gmt = mysql2date("Ymd\TH:i:s", $attachment->post_date_gmt, false);
  1269. $link = wp_get_attachment_url($attachment->ID);
  1270. $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID);
  1271. $attachment_struct = array(
  1272. "date_created_gmt" => new IXR_Date($attachment_date_gmt),
  1273. "parent" => $attachment->post_parent,
  1274. "link" => $link,
  1275. "thumbnail" => $thumbnail_link,
  1276. "title" => $attachment->post_title,
  1277. "caption" => $attachment->post_excerpt,
  1278. "description" => $attachment->post_content,
  1279. "metadata" => wp_get_attachment_metadata($attachment->ID),
  1280. );
  1281. return $attachment_struct;
  1282. }
  1283. /**
  1284. * Retrieves a collection of media library items (or attachments)
  1285. *
  1286. * Besides the common blog_id, username, and password arguments, it takes a filter
  1287. * array as last argument.
  1288. *
  1289. * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
  1290. *
  1291. * The defaults are as follows:
  1292. * - 'number' - Default is 5. Total number of media items to retrieve.
  1293. * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  1294. * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  1295. * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
  1296. *
  1297. * @since 3.1.0
  1298. *
  1299. * @param array $args Method parameters. Contains:
  1300. * - blog_id
  1301. * - username
  1302. * - password
  1303. * - filter
  1304. * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
  1305. */
  1306. function wp_getMediaLibrary($args) {
  1307. $raw_args = $args;
  1308. $this->escape($args);
  1309. $blog_id = (int) $args[0];
  1310. $username = $args[1];
  1311. $password = $args[2];
  1312. $struct = isset( $args[3] ) ? $args[3] : array() ;
  1313. if ( !$user = $this->login($username, $password) )
  1314. return $this->error;
  1315. if ( !current_user_can( 'upload_files' ) )
  1316. return new IXR_Error( 401, __( 'Sorry, you cannot upload files.' ) );
  1317. do_action('xmlrpc_call', 'wp.getMediaLibrary');
  1318. $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
  1319. $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
  1320. $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
  1321. $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
  1322. $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
  1323. $num_attachments = count($attachments);
  1324. if ( ! $num_attachments )
  1325. return array();
  1326. $attachments_struct = array();
  1327. foreach ($attachments as $attachment )
  1328. $attachments_struct[] = $this->wp_getMediaItem( array( $raw_args[0], $raw_args[1], $raw_args[2], $attachment->ID ) );
  1329. return $attachments_struct;
  1330. }
  1331. /**
  1332. * Retrives a list of post formats used by the site
  1333. *
  1334. * @since 3.1
  1335. *
  1336. * @param array $args Method parameters. Contains:
  1337. * - blog_id
  1338. * - username
  1339. * - password
  1340. * @return array
  1341. */
  1342. function wp_getPostFormats( $args ) {
  1343. $this->escape( $args );
  1344. $blog_id = (int) $args[0];
  1345. $username = $args[1];
  1346. $password = $args[2];
  1347. if ( !$user = $this->login( $username, $password ) )
  1348. return $this->error;
  1349. do_action( 'xmlrpc_call', 'wp.getPostFormats' );
  1350. return get_post_format_strings();
  1351. }
  1352. /* Blogger API functions.
  1353. * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
  1354. */
  1355. /**
  1356. * Retrieve blogs that user owns.
  1357. *
  1358. * Will make more sense once we support multiple blogs.
  1359. *
  1360. * @since 1.5.0
  1361. *
  1362. * @param array $args Method parameters.
  1363. * @return array
  1364. */
  1365. function blogger_getUsersBlogs($args) {
  1366. if ( is_multisite() )
  1367. return $this->_multisite_getUsersBlogs($args);
  1368. $this->escape($args);
  1369. $username = $args[1];
  1370. $password = $args[2];
  1371. if ( !$user = $this->login($username, $password) )
  1372. return $this->error;
  1373. do_action('xmlrpc_call', 'blogger.getUsersBlogs');
  1374. $is_admin = current_user_can('manage_options');
  1375. $struct = array(
  1376. 'isAdmin' => $is_admin,
  1377. 'url' => get_option('home') . '/',
  1378. 'blogid' => '1',
  1379. 'blogName' => get_option('blogname'),
  1380. 'xmlrpc' => site_url( 'xmlrpc.php' )
  1381. );
  1382. return array($struct);
  1383. }
  1384. /**
  1385. * Private function for retrieving a users blogs for multisite setups
  1386. *
  1387. * @access protected
  1388. */
  1389. function _multisite_getUsersBlogs($args) {
  1390. global $current_blog;
  1391. $domain = $current_blog->domain;
  1392. $path = $current_blog->path . 'xmlrpc.php';
  1393. $protocol = is_ssl() ? 'https' : 'http';
  1394. $rpc = new IXR_Client("$protocol://{$domain}{$path}");
  1395. $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
  1396. $blogs = $rpc->getResponse();
  1397. if ( isset($blogs['faultCode']) )
  1398. return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
  1399. if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  1400. return $blogs;
  1401. } else {
  1402. foreach ( (array) $blogs as $blog ) {
  1403. if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
  1404. return array($blog);
  1405. }
  1406. return array();
  1407. }
  1408. }
  1409. /**
  1410. * Retrieve user's data.
  1411. *
  1412. * Gives your client some info about you, so you don't have to.
  1413. *
  1414. * @since 1.5.0
  1415. *
  1416. * @param array $args Method parameters.
  1417. * @return array
  1418. */
  1419. function blogger_getUserInfo($args) {
  1420. $this->escape($args);
  1421. $username = $args[1];
  1422. $password = $args[2];
  1423. if ( !$user = $this->login($username, $password) )
  1424. return $this->error;
  1425. if ( !current_user_can( 'edit_posts' ) )
  1426. return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
  1427. do_action('xmlrpc_call', 'blogger.getUserInfo');
  1428. $struct = array(
  1429. 'nickname' => $user->nickname,
  1430. 'userid' => $user->ID,
  1431. 'url' => $user->user_url,
  1432. 'lastname' => $user->last_name,
  1433. 'firstname' => $user->first_name
  1434. );
  1435. return $struct;
  1436. }
  1437. /**
  1438. * Retrieve post.
  1439. *
  1440. * @since 1.5.0
  1441. *
  1442. * @param array $args Method parameters.
  1443. * @return array
  1444. */
  1445. function blogger_getPost($args) {
  1446. $this->escape($args);
  1447. $post_ID = (int) $args[1];
  1448. $username = $args[2];
  1449. $password = $args[3];
  1450. if ( !$user = $this->login($username, $password) )
  1451. return $this->error;
  1452. if ( !current_user_can( 'edit_post', $post_ID ) )
  1453. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  1454. do_action('xmlrpc_call', 'blogger.getPost');
  1455. $post_data = wp_get_single_post($post_ID, ARRAY_A);
  1456. $categories = implode(',', wp_get_post_categories($post_ID));
  1457. $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
  1458. $content .= '<category>'.$categories.'</category>';
  1459. $content .= stripslashes($post_data['post_content']);
  1460. $struct = array(
  1461. 'userid' => $post_data['post_author'],
  1462. 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'], false)),
  1463. 'content' => $content,
  1464. 'postid' => (string) $post_data['ID']
  1465. );
  1466. return $struct;
  1467. }
  1468. /**
  1469. * Retrieve list of recent posts.
  1470. *
  1471. * @since 1.5.0
  1472. *
  1473. * @param array $args Method parameters.
  1474. * @return array
  1475. */
  1476. function blogger_getRecentPosts($args) {
  1477. $this->escape($args);
  1478. // $args[0] = appkey - ignored
  1479. $blog_ID = (int) $args[1]; /* though we don't use it yet */
  1480. $username = $args[2];
  1481. $password = $args[3];
  1482. if ( isset( $args[4] ) )
  1483. $query = array( 'numberposts' => absint( $args[4] ) );
  1484. else
  1485. $query = array();
  1486. if ( !$user = $this->login($username, $password) )
  1487. return $this->error;
  1488. do_action('xmlrpc_call', 'blogger.getRecentPosts');
  1489. $posts_list = wp_get_recent_posts( $query );
  1490. if ( !$posts_list ) {
  1491. $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  1492. return $this->error;
  1493. }
  1494. foreach ($posts_list as $entry) {
  1495. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  1496. continue;
  1497. $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
  1498. $categories = implode(',', wp_get_post_categories($entry['ID']));
  1499. $content = '<title>'.stripslashes($entry['post_title']).'</title>';
  1500. $content .= '<category>'.$categories.'</category>';
  1501. $content .= stripslashes($entry['post_content']);
  1502. $struct[] = array(
  1503. 'userid' => $entry['post_author'],
  1504. 'dateCreated' => new IXR_Date($post_date),
  1505. 'content' => $content,
  1506. 'postid' => (string) $entry['ID'],
  1507. );
  1508. }
  1509. $recent_posts = array();
  1510. for ( $j=0; $j<count($struct); $j++ ) {
  1511. array_push($recent_posts, $struct[$j]);
  1512. }
  1513. return $recent_posts;
  1514. }
  1515. /**
  1516. * Retrieve blog_filename content.
  1517. *
  1518. * @since 1.5.0
  1519. *
  1520. * @param array $args Method para…

Large files files are truncated, but you can click here to view the full file