PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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