PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/aqge/deptandashboard
PHP | 3616 lines | 2159 code | 659 blank | 798 comment | 417 complexity | a874f84709dac86e97ad6429ee47de91 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1

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

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