PageRenderTime 77ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/masidev/eapinfo
PHP | 5108 lines | 3127 code | 862 blank | 1119 comment | 568 complexity | 6419200edbad5ce39aac4f575f035bad MD5 | raw 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.newPost' => 'this:wp_newPost',
  38. 'wp.editPost' => 'this:wp_editPost',
  39. 'wp.deletePost' => 'this:wp_deletePost',
  40. 'wp.getPost' => 'this:wp_getPost',
  41. 'wp.getPosts' => 'this:wp_getPosts',
  42. 'wp.newTerm' => 'this:wp_newTerm',
  43. 'wp.editTerm' => 'this:wp_editTerm',
  44. 'wp.deleteTerm' => 'this:wp_deleteTerm',
  45. 'wp.getTerm' => 'this:wp_getTerm',
  46. 'wp.getTerms' => 'this:wp_getTerms',
  47. 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
  48. 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
  49. 'wp.getPage' => 'this:wp_getPage',
  50. 'wp.getPages' => 'this:wp_getPages',
  51. 'wp.newPage' => 'this:wp_newPage',
  52. 'wp.deletePage' => 'this:wp_deletePage',
  53. 'wp.editPage' => 'this:wp_editPage',
  54. 'wp.getPageList' => 'this:wp_getPageList',
  55. 'wp.getAuthors' => 'this:wp_getAuthors',
  56. 'wp.getCategories' => 'this:mw_getCategories', // Alias
  57. 'wp.getTags' => 'this:wp_getTags',
  58. 'wp.newCategory' => 'this:wp_newCategory',
  59. 'wp.deleteCategory' => 'this:wp_deleteCategory',
  60. 'wp.suggestCategories' => 'this:wp_suggestCategories',
  61. 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
  62. 'wp.getCommentCount' => 'this:wp_getCommentCount',
  63. 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
  64. 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
  65. 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
  66. 'wp.getOptions' => 'this:wp_getOptions',
  67. 'wp.setOptions' => 'this:wp_setOptions',
  68. 'wp.getComment' => 'this:wp_getComment',
  69. 'wp.getComments' => 'this:wp_getComments',
  70. 'wp.deleteComment' => 'this:wp_deleteComment',
  71. 'wp.editComment' => 'this:wp_editComment',
  72. 'wp.newComment' => 'this:wp_newComment',
  73. 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
  74. 'wp.getMediaItem' => 'this:wp_getMediaItem',
  75. 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
  76. 'wp.getPostFormats' => 'this:wp_getPostFormats',
  77. 'wp.getPostType' => 'this:wp_getPostType',
  78. 'wp.getPostTypes' => 'this:wp_getPostTypes',
  79. // Blogger API
  80. 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  81. 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
  82. 'blogger.getPost' => 'this:blogger_getPost',
  83. 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
  84. 'blogger.getTemplate' => 'this:blogger_getTemplate',
  85. 'blogger.setTemplate' => 'this:blogger_setTemplate',
  86. 'blogger.newPost' => 'this:blogger_newPost',
  87. 'blogger.editPost' => 'this:blogger_editPost',
  88. 'blogger.deletePost' => 'this:blogger_deletePost',
  89. // MetaWeblog API (with MT extensions to structs)
  90. 'metaWeblog.newPost' => 'this:mw_newPost',
  91. 'metaWeblog.editPost' => 'this:mw_editPost',
  92. 'metaWeblog.getPost' => 'this:mw_getPost',
  93. 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
  94. 'metaWeblog.getCategories' => 'this:mw_getCategories',
  95. 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
  96. // MetaWeblog API aliases for Blogger API
  97. // see http://www.xmlrpc.com/stories/storyReader$2460
  98. 'metaWeblog.deletePost' => 'this:blogger_deletePost',
  99. 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
  100. 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
  101. 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  102. // MovableType API
  103. 'mt.getCategoryList' => 'this:mt_getCategoryList',
  104. 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
  105. 'mt.getPostCategories' => 'this:mt_getPostCategories',
  106. 'mt.setPostCategories' => 'this:mt_setPostCategories',
  107. 'mt.supportedMethods' => 'this:mt_supportedMethods',
  108. 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
  109. 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
  110. 'mt.publishPost' => 'this:mt_publishPost',
  111. // PingBack
  112. 'pingback.ping' => 'this:pingback_ping',
  113. 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
  114. 'demo.sayHello' => 'this:sayHello',
  115. 'demo.addTwoNumbers' => 'this:addTwoNumbers'
  116. );
  117. $this->initialise_blog_option_info();
  118. $this->methods = apply_filters('xmlrpc_methods', $this->methods);
  119. }
  120. function serve_request() {
  121. $this->IXR_Server($this->methods);
  122. }
  123. /**
  124. * Test XMLRPC API by saying, "Hello!" to client.
  125. *
  126. * @since 1.5.0
  127. *
  128. * @param array $args Method Parameters.
  129. * @return string
  130. */
  131. function sayHello($args) {
  132. return 'Hello!';
  133. }
  134. /**
  135. * Test XMLRPC API by adding two numbers for client.
  136. *
  137. * @since 1.5.0
  138. *
  139. * @param array $args Method Parameters.
  140. * @return int
  141. */
  142. function addTwoNumbers($args) {
  143. $number1 = $args[0];
  144. $number2 = $args[1];
  145. return $number1 + $number2;
  146. }
  147. /**
  148. * Check user's credentials.
  149. *
  150. * @since 1.5.0
  151. *
  152. * @param string $user_login User's username.
  153. * @param string $user_pass User's password.
  154. * @return bool Whether authentication passed.
  155. * @deprecated use wp_xmlrpc_server::login
  156. * @see wp_xmlrpc_server::login
  157. */
  158. function login_pass_ok($user_login, $user_pass) {
  159. if ( !get_option( 'enable_xmlrpc' ) ) {
  160. $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') ) );
  161. return false;
  162. }
  163. if (!user_pass_ok($user_login, $user_pass)) {
  164. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  165. return false;
  166. }
  167. return true;
  168. }
  169. /**
  170. * Log user in.
  171. *
  172. * @since 2.8
  173. *
  174. * @param string $username User's username.
  175. * @param string $password User's password.
  176. * @return mixed WP_User object if authentication passed, false otherwise
  177. */
  178. function login($username, $password) {
  179. if ( !get_option( 'enable_xmlrpc' ) ) {
  180. $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') ) );
  181. return false;
  182. }
  183. $user = wp_authenticate($username, $password);
  184. if (is_wp_error($user)) {
  185. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  186. return false;
  187. }
  188. wp_set_current_user( $user->ID );
  189. return $user;
  190. }
  191. /**
  192. * Sanitize string or array of strings for database.
  193. *
  194. * @since 1.5.2
  195. *
  196. * @param string|array $array Sanitize single string or array of strings.
  197. * @return string|array Type matches $array and sanitized for the database.
  198. */
  199. function escape(&$array) {
  200. global $wpdb;
  201. if (!is_array($array)) {
  202. return($wpdb->escape($array));
  203. } else {
  204. foreach ( (array) $array as $k => $v ) {
  205. if ( is_array($v) ) {
  206. $this->escape($array[$k]);
  207. } else if ( is_object($v) ) {
  208. //skip
  209. } else {
  210. $array[$k] = $wpdb->escape($v);
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * Retrieve custom fields for post.
  217. *
  218. * @since 2.5.0
  219. *
  220. * @param int $post_id Post ID.
  221. * @return array Custom fields, if exist.
  222. */
  223. function get_custom_fields($post_id) {
  224. $post_id = (int) $post_id;
  225. $custom_fields = array();
  226. foreach ( (array) has_meta($post_id) as $meta ) {
  227. // Don't expose protected fields.
  228. if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
  229. continue;
  230. $custom_fields[] = array(
  231. "id" => $meta['meta_id'],
  232. "key" => $meta['meta_key'],
  233. "value" => $meta['meta_value']
  234. );
  235. }
  236. return $custom_fields;
  237. }
  238. /**
  239. * Set custom fields for post.
  240. *
  241. * @since 2.5.0
  242. *
  243. * @param int $post_id Post ID.
  244. * @param array $fields Custom fields.
  245. */
  246. function set_custom_fields($post_id, $fields) {
  247. $post_id = (int) $post_id;
  248. foreach ( (array) $fields as $meta ) {
  249. if ( isset($meta['id']) ) {
  250. $meta['id'] = (int) $meta['id'];
  251. $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
  252. if ( isset($meta['key']) ) {
  253. $meta['key'] = stripslashes( $meta['key'] );
  254. if ( $meta['key'] != $pmeta->meta_key )
  255. continue;
  256. $meta['value'] = stripslashes_deep( $meta['value'] );
  257. if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
  258. update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
  259. } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
  260. delete_metadata_by_mid( 'post', $meta['id'] );
  261. }
  262. } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
  263. add_post_meta( $post_id, $meta['key'], $meta['value'] );
  264. }
  265. }
  266. }
  267. /**
  268. * Set up blog options property.
  269. *
  270. * Passes property through 'xmlrpc_blog_options' filter.
  271. *
  272. * @since 2.6.0
  273. */
  274. function initialise_blog_option_info() {
  275. global $wp_version;
  276. $this->blog_options = array(
  277. // Read only options
  278. 'software_name' => array(
  279. 'desc' => __( 'Software Name' ),
  280. 'readonly' => true,
  281. 'value' => 'WordPress'
  282. ),
  283. 'software_version' => array(
  284. 'desc' => __( 'Software Version' ),
  285. 'readonly' => true,
  286. 'value' => $wp_version
  287. ),
  288. 'blog_url' => array(
  289. 'desc' => __( 'Site URL' ),
  290. 'readonly' => true,
  291. 'option' => 'siteurl'
  292. ),
  293. 'image_default_link_type' => array(
  294. 'desc' => __( 'Image default link type' ),
  295. 'readonly' => true,
  296. 'option' => 'image_default_link_type'
  297. ),
  298. 'image_default_size' => array(
  299. 'desc' => __( 'Image default size' ),
  300. 'readonly' => true,
  301. 'option' => 'image_default_size'
  302. ),
  303. 'image_default_align' => array(
  304. 'desc' => __( 'Image default align' ),
  305. 'readonly' => true,
  306. 'option' => 'image_default_align'
  307. ),
  308. 'template' => array(
  309. 'desc' => __( 'Template' ),
  310. 'readonly' => true,
  311. 'option' => 'template'
  312. ),
  313. 'stylesheet' => array(
  314. 'desc' => __( 'Stylesheet' ),
  315. 'readonly' => true,
  316. 'option' => 'stylesheet'
  317. ),
  318. 'post_thumbnail' => array(
  319. 'desc' => __('Post Thumbnail'),
  320. 'readonly' => true,
  321. 'value' => current_theme_supports( 'post-thumbnails' )
  322. ),
  323. // Updatable options
  324. 'time_zone' => array(
  325. 'desc' => __( 'Time Zone' ),
  326. 'readonly' => false,
  327. 'option' => 'gmt_offset'
  328. ),
  329. 'blog_title' => array(
  330. 'desc' => __( 'Site Title' ),
  331. 'readonly' => false,
  332. 'option' => 'blogname'
  333. ),
  334. 'blog_tagline' => array(
  335. 'desc' => __( 'Site Tagline' ),
  336. 'readonly' => false,
  337. 'option' => 'blogdescription'
  338. ),
  339. 'date_format' => array(
  340. 'desc' => __( 'Date Format' ),
  341. 'readonly' => false,
  342. 'option' => 'date_format'
  343. ),
  344. 'time_format' => array(
  345. 'desc' => __( 'Time Format' ),
  346. 'readonly' => false,
  347. 'option' => 'time_format'
  348. ),
  349. 'users_can_register' => array(
  350. 'desc' => __( 'Allow new users to sign up' ),
  351. 'readonly' => false,
  352. 'option' => 'users_can_register'
  353. ),
  354. 'thumbnail_size_w' => array(
  355. 'desc' => __( 'Thumbnail Width' ),
  356. 'readonly' => false,
  357. 'option' => 'thumbnail_size_w'
  358. ),
  359. 'thumbnail_size_h' => array(
  360. 'desc' => __( 'Thumbnail Height' ),
  361. 'readonly' => false,
  362. 'option' => 'thumbnail_size_h'
  363. ),
  364. 'thumbnail_crop' => array(
  365. 'desc' => __( 'Crop thumbnail to exact dimensions' ),
  366. 'readonly' => false,
  367. 'option' => 'thumbnail_crop'
  368. ),
  369. 'medium_size_w' => array(
  370. 'desc' => __( 'Medium size image width' ),
  371. 'readonly' => false,
  372. 'option' => 'medium_size_w'
  373. ),
  374. 'medium_size_h' => array(
  375. 'desc' => __( 'Medium size image height' ),
  376. 'readonly' => false,
  377. 'option' => 'medium_size_h'
  378. ),
  379. 'large_size_w' => array(
  380. 'desc' => __( 'Large size image width' ),
  381. 'readonly' => false,
  382. 'option' => 'large_size_w'
  383. ),
  384. 'large_size_h' => array(
  385. 'desc' => __( 'Large size image height' ),
  386. 'readonly' => false,
  387. 'option' => 'large_size_h'
  388. ),
  389. 'default_comment_status' => array(
  390. 'desc' => __( 'Allow people to post comments on new articles' ),
  391. 'readonly' => false,
  392. 'option' => 'default_comment_status'
  393. ),
  394. 'default_ping_status' => array(
  395. 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks)' ),
  396. 'readonly' => false,
  397. 'option' => 'default_ping_status'
  398. )
  399. );
  400. $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
  401. }
  402. /**
  403. * Retrieve the blogs of the user.
  404. *
  405. * @since 2.6.0
  406. *
  407. * @param array $args Method parameters. Contains:
  408. * - username
  409. * - password
  410. * @return array. Contains:
  411. * - 'isAdmin'
  412. * - 'url'
  413. * - 'blogid'
  414. * - 'blogName'
  415. * - 'xmlrpc' - url of xmlrpc endpoint
  416. */
  417. function wp_getUsersBlogs( $args ) {
  418. global $current_site;
  419. // If this isn't on WPMU then just use blogger_getUsersBlogs
  420. if ( !is_multisite() ) {
  421. array_unshift( $args, 1 );
  422. return $this->blogger_getUsersBlogs( $args );
  423. }
  424. $this->escape( $args );
  425. $username = $args[0];
  426. $password = $args[1];
  427. if ( !$user = $this->login($username, $password) )
  428. return $this->error;
  429. do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
  430. $blogs = (array) get_blogs_of_user( $user->ID );
  431. $struct = array();
  432. foreach ( $blogs as $blog ) {
  433. // Don't include blogs that aren't hosted at this site
  434. if ( $blog->site_id != $current_site->id )
  435. continue;
  436. $blog_id = $blog->userblog_id;
  437. switch_to_blog($blog_id);
  438. $is_admin = current_user_can('manage_options');
  439. $struct[] = array(
  440. 'isAdmin' => $is_admin,
  441. 'url' => get_option( 'home' ) . '/',
  442. 'blogid' => (string) $blog_id,
  443. 'blogName' => get_option( 'blogname' ),
  444. 'xmlrpc' => site_url( 'xmlrpc.php' )
  445. );
  446. restore_current_blog();
  447. }
  448. return $struct;
  449. }
  450. /**
  451. * Checks if the method received at least the minimum number of arguments.
  452. *
  453. * @since 3.4.0
  454. *
  455. * @param string|array $args Sanitize single string or array of strings.
  456. * @param int $count Minimum number of arguments.
  457. * @return boolean if $args contains at least $count arguments.
  458. */
  459. protected function minimum_args( $args, $count ) {
  460. if ( count( $args ) < $count ) {
  461. $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
  462. return false;
  463. }
  464. return true;
  465. }
  466. /**
  467. * Prepares taxonomy data for return in an XML-RPC object.
  468. *
  469. * @access protected
  470. *
  471. * @param object $taxonomy The unprepared taxonomy data
  472. * @param array $fields The subset of taxonomy fields to return
  473. * @return array The prepared taxonomy data
  474. */
  475. protected function _prepare_taxonomy( $taxonomy, $fields ) {
  476. $_taxonomy = array(
  477. 'name' => $taxonomy->name,
  478. 'label' => $taxonomy->label,
  479. 'hierarchical' => (bool) $taxonomy->hierarchical,
  480. 'public' => (bool) $taxonomy->public,
  481. 'show_ui' => (bool) $taxonomy->show_ui,
  482. '_builtin' => (bool) $taxonomy->_builtin,
  483. );
  484. if ( in_array( 'labels', $fields ) )
  485. $_taxonomy['labels'] = (array) $taxonomy->labels;
  486. if ( in_array( 'cap', $fields ) )
  487. $_taxonomy['cap'] = (array) $taxonomy->cap;
  488. if ( in_array( 'object_type', $fields ) )
  489. $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
  490. return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
  491. }
  492. /**
  493. * Prepares term data for return in an XML-RPC object.
  494. *
  495. * @access protected
  496. *
  497. * @param array|object $term The unprepared term data
  498. * @return array The prepared term data
  499. */
  500. protected function _prepare_term( $term ) {
  501. $_term = $term;
  502. if ( ! is_array( $_term) )
  503. $_term = get_object_vars( $_term );
  504. // For Intergers which may be largeer than XMLRPC supports ensure we return strings.
  505. $_term['term_id'] = strval( $_term['term_id'] );
  506. $_term['term_group'] = strval( $_term['term_group'] );
  507. $_term['term_taxonomy_id'] = strval( $_term['term_taxonomy_id'] );
  508. $_term['parent'] = strval( $_term['parent'] );
  509. // Count we are happy to return as an Integer because people really shouldn't use Terms that much.
  510. $_term['count'] = intval( $_term['count'] );
  511. return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
  512. }
  513. /**
  514. * Convert a WordPress date string to an IXR_Date object.
  515. *
  516. * @access protected
  517. *
  518. * @param string $date
  519. * @return IXR_Date
  520. */
  521. protected function _convert_date( $date ) {
  522. if ( $date === '0000-00-00 00:00:00' ) {
  523. return new IXR_Date( '00000000T00:00:00Z' );
  524. }
  525. return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
  526. }
  527. /**
  528. * Convert a WordPress GMT date string to an IXR_Date object.
  529. *
  530. * @access protected
  531. *
  532. * @param string $date_gmt
  533. * @param string $date
  534. * @return IXR_Date
  535. */
  536. protected function _convert_date_gmt( $date_gmt, $date ) {
  537. if ( $date !== '0000-00-00 00:00:00' && $date_gmt === '0000-00-00 00:00:00' ) {
  538. return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
  539. }
  540. return $this->_convert_date( $date_gmt );
  541. }
  542. /**
  543. * Prepares post data for return in an XML-RPC object.
  544. *
  545. * @access protected
  546. *
  547. * @param array $post The unprepared post data
  548. * @param array $fields The subset of post type fields to return
  549. * @return array The prepared post data
  550. */
  551. protected function _prepare_post( $post, $fields ) {
  552. // holds the data for this post. built up based on $fields
  553. $_post = array( 'post_id' => strval( $post['ID'] ) );
  554. // prepare common post fields
  555. $post_fields = array(
  556. 'post_title' => $post['post_title'],
  557. 'post_date' => $this->_convert_date( $post['post_date'] ),
  558. 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
  559. 'post_modified' => $this->_convert_date( $post['post_modified'] ),
  560. 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
  561. 'post_status' => $post['post_status'],
  562. 'post_type' => $post['post_type'],
  563. 'post_name' => $post['post_name'],
  564. 'post_author' => $post['post_author'],
  565. 'post_password' => $post['post_password'],
  566. 'post_excerpt' => $post['post_excerpt'],
  567. 'post_content' => $post['post_content'],
  568. 'post_parent' => strval( $post['post_parent'] ),
  569. 'post_mime_type' => $post['post_mime_type'],
  570. 'link' => post_permalink( $post['ID'] ),
  571. 'guid' => $post['guid'],
  572. 'menu_order' => intval( $post['menu_order'] ),
  573. 'comment_status' => $post['comment_status'],
  574. 'ping_status' => $post['ping_status'],
  575. 'sticky' => ( $post['post_type'] === 'post' && is_sticky( $post['ID'] ) ),
  576. );
  577. // Thumbnail
  578. $post_fields['post_thumbnail'] = array();
  579. $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
  580. if ( $thumbnail_id ) {
  581. $thumbnail_size = current_theme_supports('post-thumbnail') ? 'post-thumbnail' : 'thumbnail';
  582. $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
  583. }
  584. // Consider future posts as published
  585. if ( $post_fields['post_status'] === 'future' )
  586. $post_fields['post_status'] = 'publish';
  587. // Fill in blank post format
  588. $post_fields['post_format'] = get_post_format( $post['ID'] );
  589. if ( empty( $post_fields['post_format'] ) )
  590. $post_fields['post_format'] = 'standard';
  591. // Merge requested $post_fields fields into $_post
  592. if ( in_array( 'post', $fields ) ) {
  593. $_post = array_merge( $_post, $post_fields );
  594. } else {
  595. $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
  596. $_post = array_merge( $_post, $requested_fields );
  597. }
  598. $all_taxonomy_fields = in_array( 'taxonomies', $fields );
  599. if ( $all_taxonomy_fields || in_array( 'terms', $fields ) ) {
  600. $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
  601. $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
  602. $_post['terms'] = array();
  603. foreach ( $terms as $term ) {
  604. $_post['terms'][] = $this->_prepare_term( $term );
  605. }
  606. }
  607. if ( in_array( 'custom_fields', $fields ) )
  608. $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
  609. if ( in_array( 'enclosure', $fields ) ) {
  610. $_post['enclosure'] = array();
  611. $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
  612. if ( ! empty( $enclosures ) ) {
  613. $encdata = explode( "\n", $enclosures[0] );
  614. $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
  615. $_post['enclosure']['length'] = (int) trim( $encdata[1] );
  616. $_post['enclosure']['type'] = trim( $encdata[2] );
  617. }
  618. }
  619. return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
  620. }
  621. /**
  622. * Prepares post data for return in an XML-RPC object.
  623. *
  624. * @access protected
  625. *
  626. * @param object $post_type Post type object
  627. * @param array $fields The subset of post fields to return
  628. * @return array The prepared post type data
  629. */
  630. protected function _prepare_post_type( $post_type, $fields ) {
  631. $_post_type = array(
  632. 'name' => $post_type->name,
  633. 'label' => $post_type->label,
  634. 'hierarchical' => (bool) $post_type->hierarchical,
  635. 'public' => (bool) $post_type->public,
  636. 'show_ui' => (bool) $post_type->show_ui,
  637. '_builtin' => (bool) $post_type->_builtin,
  638. 'has_archive' => (bool) $post_type->has_archive,
  639. 'supports' => get_all_post_type_supports( $post_type->name ),
  640. );
  641. if ( in_array( 'labels', $fields ) ) {
  642. $_post_type['labels'] = (array) $post_type->labels;
  643. }
  644. if ( in_array( 'cap', $fields ) ) {
  645. $_post_type['cap'] = (array) $post_type->cap;
  646. $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
  647. }
  648. if ( in_array( 'menu', $fields ) ) {
  649. $_post_type['menu_position'] = (int) $post_type->menu_position;
  650. $_post_type['menu_icon'] = $post_type->menu_icon;
  651. $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
  652. }
  653. if ( in_array( 'taxonomies', $fields ) )
  654. $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
  655. return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
  656. }
  657. /**
  658. * Prepares media item data for return in an XML-RPC object.
  659. *
  660. * @access protected
  661. *
  662. * @param object $media_item The unprepared media item data
  663. * @param string $thumbnail_size The image size to use for the thumbnail URL
  664. * @return array The prepared media item data
  665. */
  666. protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
  667. $_media_item = array(
  668. 'attachment_id' => strval( $media_item->ID ),
  669. 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
  670. 'parent' => $media_item->post_parent,
  671. 'link' => wp_get_attachment_url( $media_item->ID ),
  672. 'title' => $media_item->post_title,
  673. 'caption' => $media_item->post_excerpt,
  674. 'description' => $media_item->post_content,
  675. 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
  676. );
  677. $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
  678. if ( $thumbnail_src )
  679. $_media_item['thumbnail'] = $thumbnail_src[0];
  680. else
  681. $_media_item['thumbnail'] = $_media_item['link'];
  682. return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
  683. }
  684. /**
  685. * Prepares page data for return in an XML-RPC object.
  686. *
  687. * @access protected
  688. *
  689. * @param object $page The unprepared page data
  690. * @return array The prepared page data
  691. */
  692. protected function _prepare_page( $page ) {
  693. // Get all of the page content and link.
  694. $full_page = get_extended( $page->post_content );
  695. $link = post_permalink( $page->ID );
  696. // Get info the page parent if there is one.
  697. $parent_title = "";
  698. if ( ! empty( $page->post_parent ) ) {
  699. $parent = get_page( $page->post_parent );
  700. $parent_title = $parent->post_title;
  701. }
  702. // Determine comment and ping settings.
  703. $allow_comments = comments_open( $page->ID ) ? 1 : 0;
  704. $allow_pings = pings_open( $page->ID ) ? 1 : 0;
  705. // Format page date.
  706. $page_date = $this->_convert_date( $page->post_date );
  707. $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
  708. // Pull the categories info together.
  709. $categories = array();
  710. foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
  711. $categories[] = get_cat_name( $cat_id );
  712. }
  713. // Get the author info.
  714. $author = get_userdata( $page->post_author );
  715. $page_template = get_page_template_slug( $page->ID );
  716. if ( empty( $page_template ) )
  717. $page_template = 'default';
  718. $_page = array(
  719. 'dateCreated' => $page_date,
  720. 'userid' => $page->post_author,
  721. 'page_id' => $page->ID,
  722. 'page_status' => $page->post_status,
  723. 'description' => $full_page['main'],
  724. 'title' => $page->post_title,
  725. 'link' => $link,
  726. 'permaLink' => $link,
  727. 'categories' => $categories,
  728. 'excerpt' => $page->post_excerpt,
  729. 'text_more' => $full_page['extended'],
  730. 'mt_allow_comments' => $allow_comments,
  731. 'mt_allow_pings' => $allow_pings,
  732. 'wp_slug' => $page->post_name,
  733. 'wp_password' => $page->post_password,
  734. 'wp_author' => $author->display_name,
  735. 'wp_page_parent_id' => $page->post_parent,
  736. 'wp_page_parent_title' => $parent_title,
  737. 'wp_page_order' => $page->menu_order,
  738. 'wp_author_id' => (string) $author->ID,
  739. 'wp_author_display_name' => $author->display_name,
  740. 'date_created_gmt' => $page_date_gmt,
  741. 'custom_fields' => $this->get_custom_fields( $page->ID ),
  742. 'wp_page_template' => $page_template
  743. );
  744. return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
  745. }
  746. /**
  747. * Prepares comment data for return in an XML-RPC object.
  748. *
  749. * @access protected
  750. *
  751. * @param object $comment The unprepared comment data
  752. * @return array The prepared comment data
  753. */
  754. protected function _prepare_comment( $comment ) {
  755. // Format page date.
  756. $comment_date = $this->_convert_date( $comment->comment_date );
  757. $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
  758. if ( '0' == $comment->comment_approved )
  759. $comment_status = 'hold';
  760. else if ( 'spam' == $comment->comment_approved )
  761. $comment_status = 'spam';
  762. else if ( '1' == $comment->comment_approved )
  763. $comment_status = 'approve';
  764. else
  765. $comment_status = $comment->comment_approved;
  766. $_comment = array(
  767. 'date_created_gmt' => $comment_date_gmt,
  768. 'user_id' => $comment->user_id,
  769. 'comment_id' => $comment->comment_ID,
  770. 'parent' => $comment->comment_parent,
  771. 'status' => $comment_status,
  772. 'content' => $comment->comment_content,
  773. 'link' => get_comment_link($comment),
  774. 'post_id' => $comment->comment_post_ID,
  775. 'post_title' => get_the_title($comment->comment_post_ID),
  776. 'author' => $comment->comment_author,
  777. 'author_url' => $comment->comment_author_url,
  778. 'author_email' => $comment->comment_author_email,
  779. 'author_ip' => $comment->comment_author_IP,
  780. 'type' => $comment->comment_type,
  781. );
  782. return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
  783. }
  784. /**
  785. * Create a new post for any registered post type.
  786. *
  787. * @since 3.4.0
  788. *
  789. * @param array $args Method parameters. Contains:
  790. * - int $blog_id
  791. * - string $username
  792. * - string $password
  793. * - array $content_struct
  794. * $content_struct can contain:
  795. * - post_type (default: 'post')
  796. * - post_status (default: 'draft')
  797. * - post_title
  798. * - post_author
  799. * - post_exerpt
  800. * - post_content
  801. * - post_date_gmt | post_date
  802. * - post_format
  803. * - post_password
  804. * - comment_status - can be 'open' | 'closed'
  805. * - ping_status - can be 'open' | 'closed'
  806. * - sticky
  807. * - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
  808. * - custom_fields - array, with each element containing 'key' and 'value'
  809. * - terms - array, with taxonomy names as keys and arrays of term IDs as values
  810. * - terms_names - array, with taxonomy names as keys and arrays of term names as values
  811. * - enclosure
  812. * - any other fields supported by wp_insert_post()
  813. * @return string post_id
  814. */
  815. function wp_newPost( $args ) {
  816. if ( ! $this->minimum_args( $args, 4 ) )
  817. return $this->error;
  818. $this->escape( $args );
  819. $blog_id = (int) $args[0];
  820. $username = $args[1];
  821. $password = $args[2];
  822. $content_struct = $args[3];
  823. if ( ! $user = $this->login( $username, $password ) )
  824. return $this->error;
  825. do_action( 'xmlrpc_call', 'wp.newPost' );
  826. unset( $content_struct['ID'] );
  827. return $this->_insert_post( $user, $content_struct );
  828. }
  829. /**
  830. * Helper method for filtering out elements from an array.
  831. *
  832. * @since 3.4.0
  833. *
  834. * @param int $count Number to compare to one.
  835. */
  836. private function _is_greater_than_one( $count ) {
  837. return $count > 1;
  838. }
  839. /**
  840. * Helper method for wp_newPost and wp_editPost, containing shared logic.
  841. *
  842. * @since 3.4.0
  843. * @uses wp_insert_post()
  844. *
  845. * @param WP_User $user The post author if post_author isn't set in $content_struct.
  846. * @param array $content_struct Post data to insert.
  847. */
  848. protected function _insert_post( $user, $content_struct ) {
  849. $defaults = array( 'post_status' => 'draft', 'post_type' => 'post', 'post_author' => 0,
  850. 'post_password' => '', 'post_excerpt' => '', 'post_content' => '', 'post_title' => '' );
  851. $post_data = wp_parse_args( $content_struct, $defaults );
  852. $post_type = get_post_type_object( $post_data['post_type'] );
  853. if ( ! $post_type )
  854. return new IXR_Error( 403, __( 'Invalid post type' ) );
  855. $update = ! empty( $post_data['ID'] );
  856. if ( $update ) {
  857. if ( ! get_post( $post_data['ID'] ) )
  858. return new IXR_Error( 401, __( 'Invalid post ID.' ) );
  859. if ( ! current_user_can( $post_type->cap->edit_post, $post_data['ID'] ) )
  860. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  861. if ( $post_data['post_type'] != get_post_type( $post_data['ID'] ) )
  862. return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  863. } else {
  864. if ( ! current_user_can( $post_type->cap->edit_posts ) )
  865. return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
  866. }
  867. switch ( $post_data['post_status'] ) {
  868. case 'draft':
  869. case 'pending':
  870. break;
  871. case 'private':
  872. if ( ! current_user_can( $post_type->cap->publish_posts ) )
  873. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type' ) );
  874. break;
  875. case 'publish':
  876. case 'future':
  877. if ( ! current_user_can( $post_type->cap->publish_posts ) )
  878. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type' ) );
  879. break;
  880. default:
  881. $post_data['post_status'] = 'draft';
  882. break;
  883. }
  884. if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) )
  885. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type' ) );
  886. $post_data['post_author'] = absint( $post_data['post_author'] );
  887. if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
  888. if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
  889. return new IXR_Error( 401, __( 'You are not allowed to create posts as this user.' ) );
  890. $author = get_userdata( $post_data['post_author'] );
  891. if ( ! $author )
  892. return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  893. } else {
  894. $post_data['post_author'] = $user->ID;
  895. }
  896. if ( isset( $post_data['comment_status'] ) && $post_data['comment_status'] != 'open' && $post_data['comment_status'] != 'closed' )
  897. unset( $post_data['comment_status'] );
  898. if ( isset( $post_data['ping_status'] ) && $post_data['ping_status'] != 'open' && $post_data['ping_status'] != 'closed' )
  899. unset( $post_data['ping_status'] );
  900. // Do some timestamp voodoo
  901. if ( ! empty( $post_data['post_date_gmt'] ) ) {
  902. // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  903. $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
  904. } elseif ( ! empty( $post_data['post_date'] ) ) {
  905. $dateCreated = $post_data['post_date']->getIso();
  906. }
  907. if ( ! empty( $dateCreated ) ) {
  908. $post_data['post_date'] = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
  909. $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
  910. }
  911. if ( ! isset( $post_data['ID'] ) )
  912. $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
  913. $post_ID = $post_data['ID'];
  914. if ( $post_data['post_type'] == 'post' ) {
  915. // Private and password-protected posts cannot be stickied.
  916. if ( $post_data['post_status'] == 'private' || ! empty( $post_data['post_password'] ) ) {
  917. // Error if the client tried to stick the post, otherwise, silently unstick.
  918. if ( ! empty( $post_data['sticky'] ) )
  919. return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
  920. if ( $update )
  921. unstick_post( $post_ID );
  922. } elseif ( isset( $post_data['sticky'] ) ) {
  923. if ( ! current_user_can( $post_type->cap->edit_others_posts ) )
  924. return new IXR_Error( 401, __( 'Sorry, you are not allowed to stick this post.' ) );
  925. if ( $post_data['sticky'] )
  926. stick_post( $post_ID );
  927. else
  928. unstick_post( $post_ID );
  929. }
  930. }
  931. if ( isset( $post_data['post_thumbnail'] ) ) {
  932. // empty value deletes, non-empty value adds/updates
  933. if ( ! $post_data['post_thumbnail'] )
  934. delete_post_thumbnail( $post_ID );
  935. elseif ( ! set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] ) )
  936. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  937. unset( $content_struct['post_thumbnail'] );
  938. }
  939. if ( isset( $post_data['custom_fields'] ) )
  940. $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
  941. if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
  942. $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
  943. // accumulate term IDs from terms and terms_names
  944. $terms = array();
  945. // first validate the terms specified by ID
  946. if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
  947. $taxonomies = array_keys( $post_data['terms'] );
  948. // validating term ids
  949. foreach ( $taxonomies as $taxonomy ) {
  950. if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
  951. return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  952. if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
  953. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  954. $term_ids = $post_data['terms'][$taxonomy];
  955. foreach ( $term_ids as $term_id ) {
  956. $term = get_term_by( 'id', $term_id, $taxonomy );
  957. if ( ! $term )
  958. return new IXR_Error( 403, __( 'Invalid term ID' ) );
  959. $terms[$taxonomy][] = (int) $term_id;
  960. }
  961. }
  962. }
  963. // now validate terms specified by name
  964. if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
  965. $taxonomies = array_keys( $post_data['terms_names'] );
  966. foreach ( $taxonomies as $taxonomy ) {
  967. if ( ! array_key_exists( $taxonomy , $post_type_taxonomies ) )
  968. return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  969. if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->assign_terms ) )
  970. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  971. // for hierarchical taxonomies, we can't assign a term when multiple terms in the hierarchy share the same name
  972. $ambiguous_terms = array();
  973. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  974. $tax_term_names = get_terms( $taxonomy, array( 'fields' => 'names', 'hide_empty' => false ) );
  975. // count the number of terms with the same name
  976. $tax_term_names_count = array_count_values( $tax_term_names );
  977. // filter out non-ambiguous term names
  978. $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one') );
  979. $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
  980. }
  981. $term_names = $post_data['terms_names'][$taxonomy];
  982. foreach ( $term_names as $term_name ) {
  983. if ( in_array( $term_name, $ambiguous_terms ) )
  984. return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
  985. $term = get_term_by( 'name', $term_name, $taxonomy );
  986. if ( ! $term ) {
  987. // term doesn't exist, so check that the user is allowed to create new terms
  988. if ( ! current_user_can( $post_type_taxonomies[$taxonomy]->cap->edit_terms ) )
  989. return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
  990. // create the new term
  991. $term_info = wp_insert_term( $term_name, $taxonomy );
  992. if ( is_wp_error( $term_info ) )
  993. return new IXR_Error( 500, $term_info->get_error_message() );
  994. $terms[$taxonomy][] = (int) $term_info['term_id'];
  995. } else {
  996. $terms[$taxonomy][] = (int) $term->term_id;
  997. }
  998. }
  999. }
  1000. }
  1001. $post_data['tax_input'] = $terms;
  1002. unset( $post_data['terms'], $post_data['terms_names'] );
  1003. } else {
  1004. // do not allow direct submission of 'tax_input', clients must use 'terms' and/or 'terms_names'
  1005. unset( $post_data['tax_input'], $post_data['post_category'], $post_data['tags_input'] );
  1006. }
  1007. if ( isset( $post_data['post_format'] ) ) {
  1008. $format = set_post_format( $post_ID, $post_data['post_format'] );
  1009. if ( is_wp_error( $format ) )
  1010. return new IXR_Error( 500, $format->get_error_message() );
  1011. unset( $post_data['post_format'] );
  1012. }
  1013. // Handle enclosures
  1014. $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
  1015. $this->add_enclosure_if_new( $post_ID, $enclosure );
  1016. $this->attach_uploads( $post_ID, $post_data['post_content'] );
  1017. $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
  1018. $post_ID = wp_insert_post( $post_data, true );
  1019. if ( is_wp_error( $post_ID ) )
  1020. return new IXR_Error( 500, $post_ID->get_error_message() );
  1021. if ( ! $post_ID )
  1022. return new IXR_Error( 401, __( 'Sorry, your entry could not be posted. Something wrong happened.' ) );
  1023. return strval( $post_ID );
  1024. }
  1025. /**
  1026. * Edit a post for any registered post type.
  1027. *
  1028. * The $content_struct parameter only needs to contain fields that
  1029. * should be changed. All other fields will retain their existing values.
  1030. *
  1031. * @since 3.4.0
  1032. *
  1033. * @param array $args Method parameters. Contains:
  1034. * - int $blog_id
  1035. * - string $username
  1036. * - string $password
  1037. * - int $post_id
  1038. * - array $content_struct
  1039. * @return true on success
  1040. */
  1041. function wp_editPost( $args ) {
  1042. if ( ! $this->minimum_args( $args, 5 ) )
  1043. return $this->error;
  1044. $this->escape( $args );
  1045. $blog_id = (int) $args[0];
  1046. $username = $args[1];
  1047. $password = $args[2];
  1048. $post_id = (int) $args[3];
  1049. $content_struct = $args[4];
  1050. if ( ! $user = $this->login( $username, $password ) )
  1051. return $this->error;
  1052. do_action( 'xmlrpc_call', 'wp.editPost' );
  1053. $post = get_post( $post_id, ARRAY_A );
  1054. if ( empty( $post['ID'] ) )
  1055. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1056. // convert the date field back to IXR form
  1057. $post['post_date'] = $this->_convert_date( $post['post_date'] );
  1058. // ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
  1059. // since _insert_post will ignore the non-GMT date if the GMT date is set
  1060. if ( $post['post_date_gmt'] == '0000-00-00 00:00:00' || isset( $content_struct['post_date'] ) )
  1061. unset( $post['post_date_gmt'] );
  1062. else
  1063. $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
  1064. $this->escape( $post );
  1065. $merged_content_struct = array_merge( $post, $content_struct );
  1066. $retval = $this->_insert_post( $user, $merged_content_struct );
  1067. if ( $retval instanceof IXR_Error )
  1068. return $retval;
  1069. return true;
  1070. }
  1071. /**
  1072. * Delete a post for any registered post type.
  1073. *
  1074. * @since 3.4.0
  1075. *
  1076. * @uses wp_delete_post()
  1077. * @param array $args Method parameters. Contains:
  1078. * - int $blog_id
  1079. * - string $username
  1080. * - string $password
  1081. * - int $post_id
  1082. * @return true on success
  1083. */
  1084. function wp_deletePost( $args ) {
  1085. if ( ! $this->minimum_args( $args, 4 ) )
  1086. return $this->error;
  1087. $this->escape( $args );
  1088. $blog_id = (int) $args[0];
  1089. $username = $args[1];
  1090. $password = $args[2];
  1091. $post_id = (int) $args[3];
  1092. if ( ! $user = $this->login( $username, $password ) )
  1093. return $this->error;
  1094. do_action( 'xmlrpc_call', 'wp.deletePost' );
  1095. $post = wp_get_single_post( $post_id, ARRAY_A );
  1096. if ( empty( $post['ID'] ) )
  1097. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1098. $post_type = get_post_type_object( $post['post_type'] );
  1099. if ( ! current_user_can( $post_type->cap->delete_post, $post_id ) )
  1100. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
  1101. $result = wp_delete_post( $post_id );
  1102. if ( ! $result )
  1103. return new IXR_Error( 500, __( 'The post cannot be deleted.' ) );
  1104. return true;
  1105. }
  1106. /**
  1107. * Retrieve a post.
  1108. *
  1109. * @since 3.4.0
  1110. *
  1111. * The optional $fields parameter specifies what fields will be included
  1112. * in the response array. This should be a list of field names. 'post_id' will
  1113. * always be included in the response regardless of the value of $fields.
  1114. *
  1115. * Instead of, or in addition to, individual field names, conceptual group
  1116. * names can be used to specify multiple fields. The available conceptual
  1117. * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
  1118. * and 'enclosure'.
  1119. *
  1120. * @uses wp_get_single_post()
  1121. * @param array $args Method parameters. Contains:
  1122. * - int $post_id
  1123. * - string $username
  1124. * - string $password
  1125. * - array $fields optional
  1126. * @return array contains (based on $fields parameter):
  1127. * - 'post_id'
  1128. * - 'post_title'
  1129. * - 'post_date'
  1130. * - 'post_date_gmt'
  1131. * - 'post_modified'
  1132. * - 'post_modified_gmt'
  1133. * - 'post_status'
  1134. * - 'post_type'
  1135. * - 'post_name'
  1136. * - 'post_author'
  1137. * - 'post_password'
  1138. * - 'post_excerpt'
  1139. * - 'post_content'
  1140. * - 'link'
  1141. * - 'comment_status'
  1142. * - 'ping_status'
  1143. * - 'sticky'
  1144. * - 'custom_fields'
  1145. * - 'terms'
  1146. * - 'categories'
  1147. * - 'tags'
  1148. * - 'enclosure'
  1149. */
  1150. function wp_getPost( $args ) {
  1151. if ( ! $this->minimum_args( $args, 4 ) )
  1152. return $this->error;
  1153. $this->escape( $args );
  1154. $blog_id = (int) $args[0];
  1155. $username = $args[1];
  1156. $password = $args[2];
  1157. $post_id = (int) $args[3];
  1158. if ( isset( $args[4] ) )
  1159. $fields = $args[4];
  1160. else
  1161. $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
  1162. if ( ! $user = $this->login( $username, $password ) )
  1163. return $this->error;
  1164. do_action( 'xmlrpc_call', 'wp.getPost' );
  1165. $post = wp_get_single_post( $post_id, ARRAY_A );
  1166. if ( empty( $post['ID'] ) )
  1167. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1168. $post_type = get_post_type_object( $post['post_type'] );
  1169. if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) )
  1170. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  1171. return $this->_prepare_post( $post, $fields );
  1172. }
  1173. /**
  1174. * Retrieve posts.
  1175. *
  1176. * @since 3.4.0
  1177. *
  1178. * The optional $filter parameter modifies the query used to retrieve posts.
  1179. * Accepted keys are 'post_type', 'post_status', 'number', 'offset',
  1180. * 'orderby', and 'order'.
  1181. *
  1182. * The optional $fields parameter specifies what fields will be included
  1183. * in the response array.
  1184. *
  1185. * @uses wp_get_recent_posts()
  1186. * @see wp_getPost() for more on $fields
  1187. * @see get_posts() for more on $filter values
  1188. *
  1189. * @param array $args Method parameters. Contains:
  1190. * - int $blog_id
  1191. * - string $username
  1192. * - string $password
  1193. * - array $filter optional
  1194. * - array $fields optional
  1195. * @return array contains a collection of posts.
  1196. */
  1197. function wp_getPosts( $args ) {
  1198. if ( ! $this->minimum_args( $args, 3 ) )
  1199. return $this->error;
  1200. $this->escape( $args );
  1201. $blog_id = (int) $args[0];
  1202. $username = $args[1];
  1203. $password = $args[2];
  1204. $filter = isset( $args[3] ) ? $args[3] : array();
  1205. if ( isset( $args[4] ) )
  1206. $fields = $args[4];
  1207. else
  1208. $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
  1209. if ( ! $user = $this->login( $username, $password ) )
  1210. return $this->error;
  1211. do_action( 'xmlrpc_call', 'wp.getPosts' );
  1212. $query = array();
  1213. if ( isset( $filter['post_type'] ) ) {
  1214. $post_type = get_post_type_object( $filter['post_type'] );
  1215. if ( ! ( (bool) $post_type ) )
  1216. return new IXR_Error( 403, __( 'The post type specified is not valid' ) );
  1217. } else {
  1218. $post_type = get_post_type_object( 'post' );
  1219. }
  1220. if ( ! current_user_can( $post_type->cap->edit_posts ) )
  1221. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type' ));
  1222. $query['post_type'] = $post_type->name;
  1223. if ( isset( $filter['post_status'] ) )
  1224. $query['post_status'] = $filter['post_status'];
  1225. if ( isset( $filter['number'] ) )
  1226. $query['numberposts'] = absint( $filter['number'] );
  1227. if ( isset( $filter['offset'] ) )
  1228. $query['offset'] = absint( $filter['offset'] );
  1229. if ( isset( $filter['orderby'] ) ) {
  1230. $query['orderby'] = $filter['orderby'];
  1231. if ( isset( $filter['order'] ) )
  1232. $query['order'] = $filter['order'];
  1233. }
  1234. $posts_list = wp_get_recent_posts( $query );
  1235. if ( ! $posts_list )
  1236. return array();
  1237. // holds all the posts data
  1238. $struct = array();
  1239. foreach ( $posts_list as $post ) {
  1240. $post_type = get_post_type_object( $post['post_type'] );
  1241. if ( ! current_user_can( $post_type->cap->edit_post, $post['ID'] ) )
  1242. continue;
  1243. $struct[] = $this->_prepare_post( $post, $fields );
  1244. }
  1245. return $struct;
  1246. }
  1247. /**
  1248. * Create a new term.
  1249. *
  1250. * @since 3.4.0
  1251. *
  1252. * @uses wp_insert_term()
  1253. * @param array $args Method parameters. Contains:
  1254. * - int $blog_id
  1255. * - string $username
  1256. * - string $password
  1257. * - array $content_struct
  1258. * The $content_struct must contain:
  1259. * - 'name'
  1260. * - 'taxonomy'
  1261. * Also, it can optionally contain:
  1262. * - 'parent'
  1263. * - 'description'
  1264. * - 'slug'
  1265. * @return string term_id
  1266. */
  1267. function wp_newTerm( $args ) {
  1268. if ( ! $this->minimum_args( $args, 4 ) )
  1269. return $this->error;
  1270. $this->escape( $args );
  1271. $blog_id = (int) $args[0];
  1272. $username = $args[1];
  1273. $password = $args[2];
  1274. $content_struct = $args[3];
  1275. if ( ! $user = $this->login( $username, $password ) )
  1276. return $this->error;
  1277. do_action( 'xmlrpc_call', 'wp.newTerm' );
  1278. if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
  1279. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1280. $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  1281. if ( ! current_user_can( $taxonomy->cap->manage_terms ) )
  1282. return new IXR_Error( 401, __( 'You are not allowed to create terms in this taxonomy.' ) );
  1283. $taxonomy = (array) $taxonomy;
  1284. // hold the data of the term
  1285. $term_data = array();
  1286. $term_data['name'] = trim( $content_struct['name'] );
  1287. if ( empty( $term_data['name'] ) )
  1288. return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  1289. if ( isset( $content_struct['parent'] ) ) {
  1290. if ( ! $taxonomy['hierarchical'] )
  1291. return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
  1292. $parent_term_id = (int) $content_struct['parent'];
  1293. $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
  1294. if ( is_wp_error( $parent_term ) )
  1295. return new IXR_Error( 500, $parent_term->get_error_message() );
  1296. if ( ! $parent_term )
  1297. return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  1298. $term_data['parent'] = $content_struct['parent'];
  1299. }
  1300. if ( isset( $content_struct['description'] ) )
  1301. $term_data['description'] = $content_struct['description'];
  1302. if ( isset( $content_struct['slug'] ) )
  1303. $term_data['slug'] = $content_struct['slug'];
  1304. $term = wp_insert_term( $term_data['name'] , $taxonomy['name'] , $term_data );
  1305. if ( is_wp_error( $term ) )
  1306. return new IXR_Error( 500, $term->get_error_message() );
  1307. if ( ! $term )
  1308. return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) );
  1309. return strval( $term['term_id'] );
  1310. }
  1311. /**
  1312. * Edit a term.
  1313. *
  1314. * @since 3.4.0
  1315. *
  1316. * @uses wp_update_term()
  1317. * @param array $args Method parameters. Contains:
  1318. * - int $blog_id
  1319. * - string $username
  1320. * - string $password
  1321. * - string $term_id
  1322. * - array $content_struct
  1323. * The $content_struct must contain:
  1324. * - 'taxonomy'
  1325. * Also, it can optionally contain:
  1326. * - 'name'
  1327. * - 'parent'
  1328. * - 'description'
  1329. * - 'slug'
  1330. * @return bool True, on success.
  1331. */
  1332. function wp_editTerm( $args ) {
  1333. if ( ! $this->minimum_args( $args, 5 ) )
  1334. return $this->error;
  1335. $this->escape( $args );
  1336. $blog_id = (int) $args[0];
  1337. $username = $args[1];
  1338. $password = $args[2];
  1339. $term_id = (int) $args[3];
  1340. $content_struct = $args[4];
  1341. if ( ! $user = $this->login( $username, $password ) )
  1342. return $this->error;
  1343. do_action( 'xmlrpc_call', 'wp.editTerm' );
  1344. if ( ! taxonomy_exists( $content_struct['taxonomy'] ) )
  1345. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1346. $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  1347. if ( ! current_user_can( $taxonomy->cap->edit_terms ) )
  1348. return new IXR_Error( 401, __( 'You are not allowed to edit terms in this taxonomy.' ) );
  1349. $taxonomy = (array) $taxonomy;
  1350. // hold the data of the term
  1351. $term_data = array();
  1352. $term = get_term( $term_id , $content_struct['taxonomy'] );
  1353. if ( is_wp_error( $term ) )
  1354. return new IXR_Error( 500, $term->get_error_message() );
  1355. if ( ! $term )
  1356. return new IXR_Error( 404, __( 'Invalid term ID' ) );
  1357. if ( isset( $content_struct['name'] ) ) {
  1358. $term_data['name'] = trim( $content_struct['name'] );
  1359. if ( empty( $term_data['name'] ) )
  1360. return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  1361. }
  1362. if ( isset( $content_struct['parent'] ) ) {
  1363. if ( ! $taxonomy['hierarchical'] )
  1364. return new IXR_Error( 403, __( "This taxonomy is not hierarchical so you can't set a parent." ) );
  1365. $parent_term_id = (int) $content_struct['parent'];
  1366. $parent_term = get_term( $parent_term_id , $taxonomy['name'] );
  1367. if ( is_wp_error( $parent_term ) )
  1368. return new IXR_Error( 500, $parent_term->get_error_message() );
  1369. if ( ! $parent_term )
  1370. return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  1371. $term_data['parent'] = $content_struct['parent'];
  1372. }
  1373. if ( isset( $content_struct['description'] ) )
  1374. $term_data['description'] = $content_struct['description'];
  1375. if ( isset( $content_struct['slug'] ) )
  1376. $term_data['slug'] = $content_struct['slug'];
  1377. $term = wp_update_term( $term_id , $taxonomy['name'] , $term_data );
  1378. if ( is_wp_error( $term ) )
  1379. return new IXR_Error( 500, $term->get_error_message() );
  1380. if ( ! $term )
  1381. return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
  1382. return true;
  1383. }
  1384. /**
  1385. * Delete a term.
  1386. *
  1387. * @since 3.4.0
  1388. *
  1389. * @uses wp_delete_term()
  1390. * @param array $args Method parameters. Contains:
  1391. * - int $blog_id
  1392. * - string $username
  1393. * - string $password
  1394. * - string $taxnomy_name
  1395. * - string $term_id
  1396. * @return boolean|IXR_Error If it suceeded true else a reason why not
  1397. */
  1398. function wp_deleteTerm( $args ) {
  1399. if ( ! $this->minimum_args( $args, 5 ) )
  1400. return $this->error;
  1401. $this->escape( $args );
  1402. $blog_id = (int) $args[0];
  1403. $username = $args[1];
  1404. $password = $args[2];
  1405. $taxonomy = $args[3];
  1406. $term_id = (int) $args[4];
  1407. if ( ! $user = $this->login( $username, $password ) )
  1408. return $this->error;
  1409. do_action( 'xmlrpc_call', 'wp.deleteTerm' );
  1410. if ( ! taxonomy_exists( $taxonomy ) )
  1411. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1412. $taxonomy = get_taxonomy( $taxonomy );
  1413. if ( ! current_user_can( $taxonomy->cap->delete_terms ) )
  1414. return new IXR_Error( 401, __( 'You are not allowed to delete terms in this taxonomy.' ) );
  1415. $term = get_term( $term_id, $taxonomy->name );
  1416. if ( is_wp_error( $term ) )
  1417. return new IXR_Error( 500, $term->get_error_message() );
  1418. if ( ! $term )
  1419. return new IXR_Error( 404, __( 'Invalid term ID' ) );
  1420. $result = wp_delete_term( $term_id, $taxonomy->name );
  1421. if ( is_wp_error( $result ) )
  1422. return new IXR_Error( 500, $term->get_error_message() );
  1423. if ( ! $result )
  1424. return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
  1425. return $result;
  1426. }
  1427. /**
  1428. * Retrieve a term.
  1429. *
  1430. * @since 3.4.0
  1431. *
  1432. * @uses get_term()
  1433. * @param array $args Method parameters. Contains:
  1434. * - int $blog_id
  1435. * - string $username
  1436. * - string $password
  1437. * - string $taxonomy
  1438. * - string $term_id
  1439. * @return array contains:
  1440. * - 'term_id'
  1441. * - 'name'
  1442. * - 'slug'
  1443. * - 'term_group'
  1444. * - 'term_taxonomy_id'
  1445. * - 'taxonomy'
  1446. * - 'description'
  1447. * - 'parent'
  1448. * - 'count'
  1449. */
  1450. function wp_getTerm( $args ) {
  1451. if ( ! $this->minimum_args( $args, 5 ) )
  1452. return $this->error;
  1453. $this->escape( $args );
  1454. $blog_id = (int) $args[0];
  1455. $username = $args[1];
  1456. $password = $args[2];
  1457. $taxonomy = $args[3];
  1458. $term_id = (int) $args[4];
  1459. if ( ! $user = $this->login( $username, $password ) )
  1460. return $this->error;
  1461. do_action( 'xmlrpc_call', 'wp.getTerm' );
  1462. if ( ! taxonomy_exists( $taxonomy ) )
  1463. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1464. $taxonomy = get_taxonomy( $taxonomy );
  1465. if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  1466. return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
  1467. $term = get_term( $term_id , $taxonomy->name, ARRAY_A );
  1468. if ( is_wp_error( $term ) )
  1469. return new IXR_Error( 500, $term->get_error_message() );
  1470. if ( ! $term )
  1471. return new IXR_Error( 404, __( 'Invalid term ID' ) );
  1472. return $this->_prepare_term( $term );
  1473. }
  1474. /**
  1475. * Retrieve all terms for a taxonomy.
  1476. *
  1477. * @since 3.4.0
  1478. *
  1479. * The optional $filter parameter modifies the query used to retrieve terms.
  1480. * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
  1481. *
  1482. * @uses get_terms()
  1483. * @param array $args Method parameters. Contains:
  1484. * - int $blog_id
  1485. * - string $username
  1486. * - string $password
  1487. * - string $taxonomy
  1488. * - array $filter optional
  1489. * @return array terms
  1490. */
  1491. function wp_getTerms( $args ) {
  1492. if ( ! $this->minimum_args( $args, 4 ) )
  1493. return $this->error;
  1494. $this->escape( $args );
  1495. $blog_id = (int) $args[0];
  1496. $username = $args[1];
  1497. $password = $args[2];
  1498. $taxonomy = $args[3];
  1499. $filter = isset( $args[4] ) ? $args[4] : array();
  1500. if ( ! $user = $this->login( $username, $password ) )
  1501. return $this->error;
  1502. do_action( 'xmlrpc_call', 'wp.getTerms' );
  1503. if ( ! taxonomy_exists( $taxonomy ) )
  1504. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1505. $taxonomy = get_taxonomy( $taxonomy );
  1506. if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  1507. return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
  1508. $query = array();
  1509. if ( isset( $filter['number'] ) )
  1510. $query['number'] = absint( $filter['number'] );
  1511. if ( isset( $filter['offset'] ) )
  1512. $query['offset'] = absint( $filter['offset'] );
  1513. if ( isset( $filter['orderby'] ) ) {
  1514. $query['orderby'] = $filter['orderby'];
  1515. if ( isset( $filter['order'] ) )
  1516. $query['order'] = $filter['order'];
  1517. }
  1518. if ( isset( $filter['hide_empty'] ) )
  1519. $query['hide_empty'] = $filter['hide_empty'];
  1520. else
  1521. $query['get'] = 'all';
  1522. if ( isset( $filter['search'] ) )
  1523. $query['search'] = $filter['search'];
  1524. $terms = get_terms( $taxonomy->name, $query );
  1525. if ( is_wp_error( $terms ) )
  1526. return new IXR_Error( 500, $terms->get_error_message() );
  1527. $struct = array();
  1528. foreach ( $terms as $term ) {
  1529. $struct[] = $this->_prepare_term( $term );
  1530. }
  1531. return $struct;
  1532. }
  1533. /**
  1534. * Retrieve a taxonomy.
  1535. *
  1536. * @since 3.4.0
  1537. *
  1538. * @uses get_taxonomy()
  1539. * @param array $args Method parameters. Contains:
  1540. * - int $blog_id
  1541. * - string $username
  1542. * - string $password
  1543. * - string $taxonomy
  1544. * @return array (@see get_taxonomy())
  1545. */
  1546. function wp_getTaxonomy( $args ) {
  1547. if ( ! $this->minimum_args( $args, 4 ) )
  1548. return $this->error;
  1549. $this->escape( $args );
  1550. $blog_id = (int) $args[0];
  1551. $username = $args[1];
  1552. $password = $args[2];
  1553. $taxonomy = $args[3];
  1554. if ( isset( $args[4] ) )
  1555. $fields = $args[4];
  1556. else
  1557. $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
  1558. if ( ! $user = $this->login( $username, $password ) )
  1559. return $this->error;
  1560. do_action( 'xmlrpc_call', 'wp.getTaxonomy' );
  1561. if ( ! taxonomy_exists( $taxonomy ) )
  1562. return new IXR_Error( 403, __( 'Invalid taxonomy' ) );
  1563. $taxonomy = get_taxonomy( $taxonomy );
  1564. if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  1565. return new IXR_Error( 401, __( 'You are not allowed to assign terms in this taxonomy.' ) );
  1566. return $this->_prepare_taxonomy( $taxonomy, $fields );
  1567. }
  1568. /**
  1569. * Retrieve all taxonomies.
  1570. *
  1571. * @since 3.4.0
  1572. *
  1573. * @uses get_taxonomies()
  1574. * @param array $args Method parameters. Contains:
  1575. * - int $blog_id
  1576. * - string $username
  1577. * - string $password
  1578. * @return array taxonomies
  1579. */
  1580. function wp_getTaxonomies( $args ) {
  1581. if ( ! $this->minimum_args( $args, 3 ) )
  1582. return $this->error;
  1583. $this->escape( $args );
  1584. $blog_id = (int) $args[0];
  1585. $username = $args[1];
  1586. $password = $args[2];
  1587. $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  1588. if ( isset( $args[4] ) )
  1589. $fields = $args[4];
  1590. else
  1591. $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
  1592. if ( ! $user = $this->login( $username, $password ) )
  1593. return $this->error;
  1594. do_action( 'xmlrpc_call', 'wp.getTaxonomies' );
  1595. $taxonomies = get_taxonomies( $filter, 'objects' );
  1596. // holds all the taxonomy data
  1597. $struct = array();
  1598. foreach ( $taxonomies as $taxonomy ) {
  1599. // capability check for post_types
  1600. if ( ! current_user_can( $taxonomy->cap->assign_terms ) )
  1601. continue;
  1602. $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
  1603. }
  1604. return $struct;
  1605. }
  1606. /**
  1607. * Retrieve page.
  1608. *
  1609. * @since 2.2.0
  1610. *
  1611. * @param array $args Method parameters. Contains:
  1612. * - blog_id
  1613. * - page_id
  1614. * - username
  1615. * - password
  1616. * @return array
  1617. */
  1618. function wp_getPage($args) {
  1619. $this->escape($args);
  1620. $blog_id = (int) $args[0];
  1621. $page_id = (int) $args[1];
  1622. $username = $args[2];
  1623. $password = $args[3];
  1624. if ( !$user = $this->login($username, $password) ) {
  1625. return $this->error;
  1626. }
  1627. $page = get_page($page_id);
  1628. if ( ! $page )
  1629. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1630. if ( !current_user_can( 'edit_page', $page_id ) )
  1631. return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
  1632. do_action('xmlrpc_call', 'wp.getPage');
  1633. // If we found the page then format the data.
  1634. if ( $page->ID && ($page->post_type == 'page') ) {
  1635. return $this->_prepare_page( $page );
  1636. }
  1637. // If the page doesn't exist indicate that.
  1638. else {
  1639. return(new IXR_Error(404, __('Sorry, no such page.')));
  1640. }
  1641. }
  1642. /**
  1643. * Retrieve Pages.
  1644. *
  1645. * @since 2.2.0
  1646. *
  1647. * @param array $args Method parameters. Contains:
  1648. * - blog_id
  1649. * - username
  1650. * - password
  1651. * - num_pages
  1652. * @return array
  1653. */
  1654. function wp_getPages($args) {
  1655. $this->escape($args);
  1656. $blog_id = (int) $args[0];
  1657. $username = $args[1];
  1658. $password = $args[2];
  1659. $num_pages = isset($args[3]) ? (int) $args[3] : 10;
  1660. if ( !$user = $this->login($username, $password) )
  1661. return $this->error;
  1662. if ( !current_user_can( 'edit_pages' ) )
  1663. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  1664. do_action('xmlrpc_call', 'wp.getPages');
  1665. $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
  1666. $num_pages = count($pages);
  1667. // If we have pages, put together their info.
  1668. if ( $num_pages >= 1 ) {
  1669. $pages_struct = array();
  1670. foreach ($pages as $page) {
  1671. if ( current_user_can( 'edit_page', $page->ID ) )
  1672. $pages_struct[] = $this->_prepare_page( $page );
  1673. }
  1674. return($pages_struct);
  1675. }
  1676. // If no pages were found return an error.
  1677. else {
  1678. return(array());
  1679. }
  1680. }
  1681. /**
  1682. * Create new page.
  1683. *
  1684. * @since 2.2.0
  1685. *
  1686. * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
  1687. * @return unknown
  1688. */
  1689. function wp_newPage($args) {
  1690. // Items not escaped here will be escaped in newPost.
  1691. $username = $this->escape($args[1]);
  1692. $password = $this->escape($args[2]);
  1693. $page = $args[3];
  1694. $publish = $args[4];
  1695. if ( !$user = $this->login($username, $password) )
  1696. return $this->error;
  1697. do_action('xmlrpc_call', 'wp.newPage');
  1698. // Mark this as content for a page.
  1699. $args[3]["post_type"] = 'page';
  1700. // Let mw_newPost do all of the heavy lifting.
  1701. return($this->mw_newPost($args));
  1702. }
  1703. /**
  1704. * Delete page.
  1705. *
  1706. * @since 2.2.0
  1707. *
  1708. * @param array $args Method parameters.
  1709. * @return bool True, if success.
  1710. */
  1711. function wp_deletePage($args) {
  1712. $this->escape($args);
  1713. $blog_id = (int) $args[0];
  1714. $username = $args[1];
  1715. $password = $args[2];
  1716. $page_id = (int) $args[3];
  1717. if ( !$user = $this->login($username, $password) )
  1718. return $this->error;
  1719. do_action('xmlrpc_call', 'wp.deletePage');
  1720. // Get the current page based on the page_id and
  1721. // make sure it is a page and not a post.
  1722. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  1723. if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  1724. return(new IXR_Error(404, __('Sorry, no such page.')));
  1725. // Make sure the user can delete pages.
  1726. if ( !current_user_can('delete_page', $page_id) )
  1727. return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
  1728. // Attempt to delete the page.
  1729. $result = wp_delete_post($page_id);
  1730. if ( !$result )
  1731. return(new IXR_Error(500, __('Failed to delete the page.')));
  1732. do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args );
  1733. return(true);
  1734. }
  1735. /**
  1736. * Edit page.
  1737. *
  1738. * @since 2.2.0
  1739. *
  1740. * @param array $args Method parameters.
  1741. * @return unknown
  1742. */
  1743. function wp_editPage($args) {
  1744. // Items not escaped here will be escaped in editPost.
  1745. $blog_id = (int) $args[0];
  1746. $page_id = (int) $this->escape($args[1]);
  1747. $username = $this->escape($args[2]);
  1748. $password = $this->escape($args[3]);
  1749. $content = $args[4];
  1750. $publish = $args[5];
  1751. if ( !$user = $this->login($username, $password) )
  1752. return $this->error;
  1753. do_action('xmlrpc_call', 'wp.editPage');
  1754. // Get the page data and make sure it is a page.
  1755. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  1756. if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  1757. return(new IXR_Error(404, __('Sorry, no such page.')));
  1758. // Make sure the user is allowed to edit pages.
  1759. if ( !current_user_can('edit_page', $page_id) )
  1760. return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
  1761. // Mark this as content for a page.
  1762. $content['post_type'] = 'page';
  1763. // Arrange args in the way mw_editPost understands.
  1764. $args = array(
  1765. $page_id,
  1766. $username,
  1767. $password,
  1768. $content,
  1769. $publish
  1770. );
  1771. // Let mw_editPost do all of the heavy lifting.
  1772. return($this->mw_editPost($args));
  1773. }
  1774. /**
  1775. * Retrieve page list.
  1776. *
  1777. * @since 2.2.0
  1778. *
  1779. * @param array $args Method parameters.
  1780. * @return unknown
  1781. */
  1782. function wp_getPageList($args) {
  1783. global $wpdb;
  1784. $this->escape($args);
  1785. $blog_id = (int) $args[0];
  1786. $username = $args[1];
  1787. $password = $args[2];
  1788. if ( !$user = $this->login($username, $password) )
  1789. return $this->error;
  1790. if ( !current_user_can( 'edit_pages' ) )
  1791. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  1792. do_action('xmlrpc_call', 'wp.getPageList');
  1793. // Get list of pages ids and titles
  1794. $page_list = $wpdb->get_results("
  1795. SELECT ID page_id,
  1796. post_title page_title,
  1797. post_parent page_parent_id,
  1798. post_date_gmt,
  1799. post_date,
  1800. post_status
  1801. FROM {$wpdb->posts}
  1802. WHERE post_type = 'page'
  1803. ORDER BY ID
  1804. ");
  1805. // The date needs to be formatted properly.
  1806. $num_pages = count($page_list);
  1807. for ( $i = 0; $i < $num_pages; $i++ ) {
  1808. $page_list[$i]->dateCreated = $this->_convert_date( $page_list[$i]->post_date );
  1809. $page_list[$i]->date_created_gmt = $this->_convert_date_gmt( $page_list[$i]->post_date_gmt, $page_list[$i]->post_date );
  1810. unset($page_list[$i]->post_date_gmt);
  1811. unset($page_list[$i]->post_date);
  1812. unset($page_list[$i]->post_status);
  1813. }
  1814. return($page_list);
  1815. }
  1816. /**
  1817. * Retrieve authors list.
  1818. *
  1819. * @since 2.2.0
  1820. *
  1821. * @param array $args Method parameters.
  1822. * @return array
  1823. */
  1824. function wp_getAuthors($args) {
  1825. $this->escape($args);
  1826. $blog_id = (int) $args[0];
  1827. $username = $args[1];
  1828. $password = $args[2];
  1829. if ( !$user = $this->login($username, $password) )
  1830. return $this->error;
  1831. if ( !current_user_can('edit_posts') )
  1832. return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
  1833. do_action('xmlrpc_call', 'wp.getAuthors');
  1834. $authors = array();
  1835. foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
  1836. $authors[] = array(
  1837. 'user_id' => $user->ID,
  1838. 'user_login' => $user->user_login,
  1839. 'display_name' => $user->display_name
  1840. );
  1841. }
  1842. return $authors;
  1843. }
  1844. /**
  1845. * Get list of all tags
  1846. *
  1847. * @since 2.7.0
  1848. *
  1849. * @param array $args Method parameters.
  1850. * @return array
  1851. */
  1852. function wp_getTags( $args ) {
  1853. $this->escape( $args );
  1854. $blog_id = (int) $args[0];
  1855. $username = $args[1];
  1856. $password = $args[2];
  1857. if ( !$user = $this->login($username, $password) )
  1858. return $this->error;
  1859. if ( !current_user_can( 'edit_posts' ) )
  1860. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
  1861. do_action( 'xmlrpc_call', 'wp.getKeywords' );
  1862. $tags = array();
  1863. if ( $all_tags = get_tags() ) {
  1864. foreach( (array) $all_tags as $tag ) {
  1865. $struct['tag_id'] = $tag->term_id;
  1866. $struct['name'] = $tag->name;
  1867. $struct['count'] = $tag->count;
  1868. $struct['slug'] = $tag->slug;
  1869. $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
  1870. $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
  1871. $tags[] = $struct;
  1872. }
  1873. }
  1874. return $tags;
  1875. }
  1876. /**
  1877. * Create new category.
  1878. *
  1879. * @since 2.2.0
  1880. *
  1881. * @param array $args Method parameters.
  1882. * @return int Category ID.
  1883. */
  1884. function wp_newCategory($args) {
  1885. $this->escape($args);
  1886. $blog_id = (int) $args[0];
  1887. $username = $args[1];
  1888. $password = $args[2];
  1889. $category = $args[3];
  1890. if ( !$user = $this->login($username, $password) )
  1891. return $this->error;
  1892. do_action('xmlrpc_call', 'wp.newCategory');
  1893. // Make sure the user is allowed to add a category.
  1894. if ( !current_user_can('manage_categories') )
  1895. return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
  1896. // If no slug was provided make it empty so that
  1897. // WordPress will generate one.
  1898. if ( empty($category['slug']) )
  1899. $category['slug'] = '';
  1900. // If no parent_id was provided make it empty
  1901. // so that it will be a top level page (no parent).
  1902. if ( !isset($category['parent_id']) )
  1903. $category['parent_id'] = '';
  1904. // If no description was provided make it empty.
  1905. if ( empty($category["description"]) )
  1906. $category["description"] = "";
  1907. $new_category = array(
  1908. 'cat_name' => $category['name'],
  1909. 'category_nicename' => $category['slug'],
  1910. 'category_parent' => $category['parent_id'],
  1911. 'category_description' => $category['description']
  1912. );
  1913. $cat_id = wp_insert_category($new_category, true);
  1914. if ( is_wp_error( $cat_id ) ) {
  1915. if ( 'term_exists' == $cat_id->get_error_code() )
  1916. return (int) $cat_id->get_error_data();
  1917. else
  1918. return(new IXR_Error(500, __('Sorry, the new category failed.')));
  1919. } elseif ( ! $cat_id ) {
  1920. return(new IXR_Error(500, __('Sorry, the new category failed.')));
  1921. }
  1922. do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args );
  1923. return $cat_id;
  1924. }
  1925. /**
  1926. * Remove category.
  1927. *
  1928. * @since 2.5.0
  1929. *
  1930. * @param array $args Method parameters.
  1931. * @return mixed See {@link wp_delete_term()} for return info.
  1932. */
  1933. function wp_deleteCategory($args) {
  1934. $this->escape($args);
  1935. $blog_id = (int) $args[0];
  1936. $username = $args[1];
  1937. $password = $args[2];
  1938. $category_id = (int) $args[3];
  1939. if ( !$user = $this->login($username, $password) )
  1940. return $this->error;
  1941. do_action('xmlrpc_call', 'wp.deleteCategory');
  1942. if ( !current_user_can('manage_categories') )
  1943. return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
  1944. $status = wp_delete_term( $category_id, 'category' );
  1945. if( true == $status )
  1946. do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args );
  1947. return $status;
  1948. }
  1949. /**
  1950. * Retrieve category list.
  1951. *
  1952. * @since 2.2.0
  1953. *
  1954. * @param array $args Method parameters.
  1955. * @return array
  1956. */
  1957. function wp_suggestCategories($args) {
  1958. $this->escape($args);
  1959. $blog_id = (int) $args[0];
  1960. $username = $args[1];
  1961. $password = $args[2];
  1962. $category = $args[3];
  1963. $max_results = (int) $args[4];
  1964. if ( !$user = $this->login($username, $password) )
  1965. return $this->error;
  1966. if ( !current_user_can( 'edit_posts' ) )
  1967. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
  1968. do_action('xmlrpc_call', 'wp.suggestCategories');
  1969. $category_suggestions = array();
  1970. $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
  1971. foreach ( (array) get_categories($args) as $cat ) {
  1972. $category_suggestions[] = array(
  1973. 'category_id' => $cat->term_id,
  1974. 'category_name' => $cat->name
  1975. );
  1976. }
  1977. return($category_suggestions);
  1978. }
  1979. /**
  1980. * Retrieve comment.
  1981. *
  1982. * @since 2.7.0
  1983. *
  1984. * @param array $args Method parameters.
  1985. * @return array
  1986. */
  1987. function wp_getComment($args) {
  1988. $this->escape($args);
  1989. $blog_id = (int) $args[0];
  1990. $username = $args[1];
  1991. $password = $args[2];
  1992. $comment_id = (int) $args[3];
  1993. if ( !$user = $this->login($username, $password) )
  1994. return $this->error;
  1995. if ( !current_user_can( 'moderate_comments' ) )
  1996. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  1997. do_action('xmlrpc_call', 'wp.getComment');
  1998. if ( ! $comment = get_comment($comment_id) )
  1999. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  2000. return $this->_prepare_comment( $comment );
  2001. }
  2002. /**
  2003. * Retrieve comments.
  2004. *
  2005. * Besides the common blog_id, username, and password arguments, it takes a filter
  2006. * array as last argument.
  2007. *
  2008. * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
  2009. *
  2010. * The defaults are as follows:
  2011. * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
  2012. * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
  2013. * - 'number' - Default is 10. Total number of media items to retrieve.
  2014. * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  2015. *
  2016. * @since 2.7.0
  2017. *
  2018. * @param array $args Method parameters.
  2019. * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
  2020. */
  2021. function wp_getComments($args) {
  2022. $this->escape($args);
  2023. $blog_id = (int) $args[0];
  2024. $username = $args[1];
  2025. $password = $args[2];
  2026. $struct = isset( $args[3] ) ? $args[3] : array();
  2027. if ( !$user = $this->login($username, $password) )
  2028. return $this->error;
  2029. if ( !current_user_can( 'moderate_comments' ) )
  2030. return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
  2031. do_action('xmlrpc_call', 'wp.getComments');
  2032. if ( isset($struct['status']) )
  2033. $status = $struct['status'];
  2034. else
  2035. $status = '';
  2036. $post_id = '';
  2037. if ( isset($struct['post_id']) )
  2038. $post_id = absint($struct['post_id']);
  2039. $offset = 0;
  2040. if ( isset($struct['offset']) )
  2041. $offset = absint($struct['offset']);
  2042. $number = 10;
  2043. if ( isset($struct['number']) )
  2044. $number = absint($struct['number']);
  2045. $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
  2046. $comments_struct = array();
  2047. foreach ( $comments as $comment ) {
  2048. $comments_struct[] = $this->_prepare_comment( $comment );
  2049. }
  2050. return $comments_struct;
  2051. }
  2052. /**
  2053. * Delete a comment.
  2054. *
  2055. * By default, the comment will be moved to the trash instead of deleted.
  2056. * See {@link wp_delete_comment()} for more information on
  2057. * this behavior.
  2058. *
  2059. * @since 2.7.0
  2060. *
  2061. * @param array $args Method parameters. Contains:
  2062. * - blog_id
  2063. * - username
  2064. * - password
  2065. * - comment_id
  2066. * @return mixed {@link wp_delete_comment()}
  2067. */
  2068. function wp_deleteComment($args) {
  2069. $this->escape($args);
  2070. $blog_id = (int) $args[0];
  2071. $username = $args[1];
  2072. $password = $args[2];
  2073. $comment_ID = (int) $args[3];
  2074. if ( !$user = $this->login($username, $password) )
  2075. return $this->error;
  2076. if ( !current_user_can( 'moderate_comments' ) )
  2077. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  2078. if ( ! get_comment($comment_ID) )
  2079. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  2080. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  2081. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  2082. do_action('xmlrpc_call', 'wp.deleteComment');
  2083. $status = wp_delete_comment( $comment_ID );
  2084. if( true == $status )
  2085. do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args );
  2086. return $status;
  2087. }
  2088. /**
  2089. * Edit comment.
  2090. *
  2091. * Besides the common blog_id, username, and password arguments, it takes a
  2092. * comment_id integer and a content_struct array as last argument.
  2093. *
  2094. * The allowed keys in the content_struct array are:
  2095. * - 'author'
  2096. * - 'author_url'
  2097. * - 'author_email'
  2098. * - 'content'
  2099. * - 'date_created_gmt'
  2100. * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
  2101. *
  2102. * @since 2.7.0
  2103. *
  2104. * @param array $args. Contains:
  2105. * - blog_id
  2106. * - username
  2107. * - password
  2108. * - comment_id
  2109. * - content_struct
  2110. * @return bool True, on success.
  2111. */
  2112. function wp_editComment($args) {
  2113. $this->escape($args);
  2114. $blog_id = (int) $args[0];
  2115. $username = $args[1];
  2116. $password = $args[2];
  2117. $comment_ID = (int) $args[3];
  2118. $content_struct = $args[4];
  2119. if ( !$user = $this->login($username, $password) )
  2120. return $this->error;
  2121. if ( !current_user_can( 'moderate_comments' ) )
  2122. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  2123. if ( ! get_comment($comment_ID) )
  2124. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  2125. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  2126. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  2127. do_action('xmlrpc_call', 'wp.editComment');
  2128. if ( isset($content_struct['status']) ) {
  2129. $statuses = get_comment_statuses();
  2130. $statuses = array_keys($statuses);
  2131. if ( ! in_array($content_struct['status'], $statuses) )
  2132. return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  2133. $comment_approved = $content_struct['status'];
  2134. }
  2135. // Do some timestamp voodoo
  2136. if ( !empty( $content_struct['date_created_gmt'] ) ) {
  2137. // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  2138. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  2139. $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  2140. $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  2141. }
  2142. if ( isset($content_struct['content']) )
  2143. $comment_content = $content_struct['content'];
  2144. if ( isset($content_struct['author']) )
  2145. $comment_author = $content_struct['author'];
  2146. if ( isset($content_struct['author_url']) )
  2147. $comment_author_url = $content_struct['author_url'];
  2148. if ( isset($content_struct['author_email']) )
  2149. $comment_author_email = $content_struct['author_email'];
  2150. // We've got all the data -- post it:
  2151. $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
  2152. $result = wp_update_comment($comment);
  2153. if ( is_wp_error( $result ) )
  2154. return new IXR_Error(500, $result->get_error_message());
  2155. if ( !$result )
  2156. return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
  2157. do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args );
  2158. return true;
  2159. }
  2160. /**
  2161. * Create new comment.
  2162. *
  2163. * @since 2.7.0
  2164. *
  2165. * @param array $args Method parameters.
  2166. * @return mixed {@link wp_new_comment()}
  2167. */
  2168. function wp_newComment($args) {
  2169. global $wpdb;
  2170. $this->escape($args);
  2171. $blog_id = (int) $args[0];
  2172. $username = $args[1];
  2173. $password = $args[2];
  2174. $post = $args[3];
  2175. $content_struct = $args[4];
  2176. $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
  2177. $user = $this->login($username, $password);
  2178. if ( !$user ) {
  2179. $logged_in = false;
  2180. if ( $allow_anon && get_option('comment_registration') )
  2181. return new IXR_Error( 403, __( 'You must be registered to comment' ) );
  2182. else if ( !$allow_anon )
  2183. return $this->error;
  2184. } else {
  2185. $logged_in = true;
  2186. }
  2187. if ( is_numeric($post) )
  2188. $post_id = absint($post);
  2189. else
  2190. $post_id = url_to_postid($post);
  2191. if ( ! $post_id )
  2192. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  2193. if ( ! get_post($post_id) )
  2194. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  2195. $comment['comment_post_ID'] = $post_id;
  2196. if ( $logged_in ) {
  2197. $comment['comment_author'] = $wpdb->escape( $user->display_name );
  2198. $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
  2199. $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
  2200. $comment['user_ID'] = $user->ID;
  2201. } else {
  2202. $comment['comment_author'] = '';
  2203. if ( isset($content_struct['author']) )
  2204. $comment['comment_author'] = $content_struct['author'];
  2205. $comment['comment_author_email'] = '';
  2206. if ( isset($content_struct['author_email']) )
  2207. $comment['comment_author_email'] = $content_struct['author_email'];
  2208. $comment['comment_author_url'] = '';
  2209. if ( isset($content_struct['author_url']) )
  2210. $comment['comment_author_url'] = $content_struct['author_url'];
  2211. $comment['user_ID'] = 0;
  2212. if ( get_option('require_name_email') ) {
  2213. if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
  2214. return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
  2215. elseif ( !is_email($comment['comment_author_email']) )
  2216. return new IXR_Error( 403, __( 'A valid email address is required' ) );
  2217. }
  2218. }
  2219. $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
  2220. $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
  2221. do_action('xmlrpc_call', 'wp.newComment');
  2222. $comment_ID = wp_new_comment( $comment );
  2223. do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args );
  2224. return $comment_ID;
  2225. }
  2226. /**
  2227. * Retrieve all of the comment status.
  2228. *
  2229. * @since 2.7.0
  2230. *
  2231. * @param array $args Method parameters.
  2232. * @return array
  2233. */
  2234. function wp_getCommentStatusList($args) {
  2235. $this->escape( $args );
  2236. $blog_id = (int) $args[0];
  2237. $username = $args[1];
  2238. $password = $args[2];
  2239. if ( !$user = $this->login($username, $password) )
  2240. return $this->error;
  2241. if ( !current_user_can( 'moderate_comments' ) )
  2242. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  2243. do_action('xmlrpc_call', 'wp.getCommentStatusList');
  2244. return get_comment_statuses();
  2245. }
  2246. /**
  2247. * Retrieve comment count.
  2248. *
  2249. * @since 2.5.0
  2250. *
  2251. * @param array $args Method parameters.
  2252. * @return array
  2253. */
  2254. function wp_getCommentCount( $args ) {
  2255. $this->escape($args);
  2256. $blog_id = (int) $args[0];
  2257. $username = $args[1];
  2258. $password = $args[2];
  2259. $post_id = (int) $args[3];
  2260. if ( !$user = $this->login($username, $password) )
  2261. return $this->error;
  2262. if ( !current_user_can( 'edit_posts' ) )
  2263. return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
  2264. do_action('xmlrpc_call', 'wp.getCommentCount');
  2265. $count = wp_count_comments( $post_id );
  2266. return array(
  2267. 'approved' => $count->approved,
  2268. 'awaiting_moderation' => $count->moderated,
  2269. 'spam' => $count->spam,
  2270. 'total_comments' => $count->total_comments
  2271. );
  2272. }
  2273. /**
  2274. * Retrieve post statuses.
  2275. *
  2276. * @since 2.5.0
  2277. *
  2278. * @param array $args Method parameters.
  2279. * @return array
  2280. */
  2281. function wp_getPostStatusList( $args ) {
  2282. $this->escape( $args );
  2283. $blog_id = (int) $args[0];
  2284. $username = $args[1];
  2285. $password = $args[2];
  2286. if ( !$user = $this->login($username, $password) )
  2287. return $this->error;
  2288. if ( !current_user_can( 'edit_posts' ) )
  2289. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  2290. do_action('xmlrpc_call', 'wp.getPostStatusList');
  2291. return get_post_statuses();
  2292. }
  2293. /**
  2294. * Retrieve page statuses.
  2295. *
  2296. * @since 2.5.0
  2297. *
  2298. * @param array $args Method parameters.
  2299. * @return array
  2300. */
  2301. function wp_getPageStatusList( $args ) {
  2302. $this->escape( $args );
  2303. $blog_id = (int) $args[0];
  2304. $username = $args[1];
  2305. $password = $args[2];
  2306. if ( !$user = $this->login($username, $password) )
  2307. return $this->error;
  2308. if ( !current_user_can( 'edit_pages' ) )
  2309. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  2310. do_action('xmlrpc_call', 'wp.getPageStatusList');
  2311. return get_page_statuses();
  2312. }
  2313. /**
  2314. * Retrieve page templates.
  2315. *
  2316. * @since 2.6.0
  2317. *
  2318. * @param array $args Method parameters.
  2319. * @return array
  2320. */
  2321. function wp_getPageTemplates( $args ) {
  2322. $this->escape( $args );
  2323. $blog_id = (int) $args[0];
  2324. $username = $args[1];
  2325. $password = $args[2];
  2326. if ( !$user = $this->login($username, $password) )
  2327. return $this->error;
  2328. if ( !current_user_can( 'edit_pages' ) )
  2329. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  2330. $templates = get_page_templates();
  2331. $templates['Default'] = 'default';
  2332. return $templates;
  2333. }
  2334. /**
  2335. * Retrieve blog options.
  2336. *
  2337. * @since 2.6.0
  2338. *
  2339. * @param array $args Method parameters.
  2340. * @return array
  2341. */
  2342. function wp_getOptions( $args ) {
  2343. $this->escape( $args );
  2344. $blog_id = (int) $args[0];
  2345. $username = $args[1];
  2346. $password = $args[2];
  2347. $options = isset( $args[3] ) ? (array) $args[3] : array();
  2348. if ( !$user = $this->login($username, $password) )
  2349. return $this->error;
  2350. // If no specific options where asked for, return all of them
  2351. if ( count( $options ) == 0 )
  2352. $options = array_keys($this->blog_options);
  2353. return $this->_getOptions($options);
  2354. }
  2355. /**
  2356. * Retrieve blog options value from list.
  2357. *
  2358. * @since 2.6.0
  2359. *
  2360. * @param array $options Options to retrieve.
  2361. * @return array
  2362. */
  2363. function _getOptions($options) {
  2364. $data = array();
  2365. foreach ( $options as $option ) {
  2366. if ( array_key_exists( $option, $this->blog_options ) ) {
  2367. $data[$option] = $this->blog_options[$option];
  2368. //Is the value static or dynamic?
  2369. if ( isset( $data[$option]['option'] ) ) {
  2370. $data[$option]['value'] = get_option( $data[$option]['option'] );
  2371. unset($data[$option]['option']);
  2372. }
  2373. }
  2374. }
  2375. return $data;
  2376. }
  2377. /**
  2378. * Update blog options.
  2379. *
  2380. * @since 2.6.0
  2381. *
  2382. * @param array $args Method parameters.
  2383. * @return unknown
  2384. */
  2385. function wp_setOptions( $args ) {
  2386. $this->escape( $args );
  2387. $blog_id = (int) $args[0];
  2388. $username = $args[1];
  2389. $password = $args[2];
  2390. $options = (array) $args[3];
  2391. if ( !$user = $this->login($username, $password) )
  2392. return $this->error;
  2393. if ( !current_user_can( 'manage_options' ) )
  2394. return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
  2395. foreach ( $options as $o_name => $o_value ) {
  2396. $option_names[] = $o_name;
  2397. if ( !array_key_exists( $o_name, $this->blog_options ) )
  2398. continue;
  2399. if ( $this->blog_options[$o_name]['readonly'] == true )
  2400. continue;
  2401. update_option( $this->blog_options[$o_name]['option'], $o_value );
  2402. }
  2403. //Now return the updated values
  2404. return $this->_getOptions($option_names);
  2405. }
  2406. /**
  2407. * Retrieve a media item by ID
  2408. *
  2409. * @since 3.1.0
  2410. *
  2411. * @param array $args Method parameters. Contains:
  2412. * - blog_id
  2413. * - username
  2414. * - password
  2415. * - attachment_id
  2416. * @return array. Associative array containing:
  2417. * - 'date_created_gmt'
  2418. * - 'parent'
  2419. * - 'link'
  2420. * - 'thumbnail'
  2421. * - 'title'
  2422. * - 'caption'
  2423. * - 'description'
  2424. * - 'metadata'
  2425. */
  2426. function wp_getMediaItem($args) {
  2427. $this->escape($args);
  2428. $blog_id = (int) $args[0];
  2429. $username = $args[1];
  2430. $password = $args[2];
  2431. $attachment_id = (int) $args[3];
  2432. if ( !$user = $this->login($username, $password) )
  2433. return $this->error;
  2434. if ( !current_user_can( 'upload_files' ) )
  2435. return new IXR_Error( 403, __( 'You do not have permission to upload files.' ) );
  2436. do_action('xmlrpc_call', 'wp.getMediaItem');
  2437. if ( ! $attachment = get_post($attachment_id) )
  2438. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  2439. return $this->_prepare_media_item( $attachment );
  2440. }
  2441. /**
  2442. * Retrieves a collection of media library items (or attachments)
  2443. *
  2444. * Besides the common blog_id, username, and password arguments, it takes a filter
  2445. * array as last argument.
  2446. *
  2447. * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
  2448. *
  2449. * The defaults are as follows:
  2450. * - 'number' - Default is 5. Total number of media items to retrieve.
  2451. * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  2452. * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  2453. * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
  2454. *
  2455. * @since 3.1.0
  2456. *
  2457. * @param array $args Method parameters. Contains:
  2458. * - blog_id
  2459. * - username
  2460. * - password
  2461. * - filter
  2462. * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
  2463. */
  2464. function wp_getMediaLibrary($args) {
  2465. $this->escape($args);
  2466. $blog_id = (int) $args[0];
  2467. $username = $args[1];
  2468. $password = $args[2];
  2469. $struct = isset( $args[3] ) ? $args[3] : array() ;
  2470. if ( !$user = $this->login($username, $password) )
  2471. return $this->error;
  2472. if ( !current_user_can( 'upload_files' ) )
  2473. return new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
  2474. do_action('xmlrpc_call', 'wp.getMediaLibrary');
  2475. $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
  2476. $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
  2477. $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
  2478. $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
  2479. $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
  2480. $attachments_struct = array();
  2481. foreach ($attachments as $attachment )
  2482. $attachments_struct[] = $this->_prepare_media_item( $attachment );
  2483. return $attachments_struct;
  2484. }
  2485. /**
  2486. * Retrieves a list of post formats used by the site
  2487. *
  2488. * @since 3.1
  2489. *
  2490. * @param array $args Method parameters. Contains:
  2491. * - blog_id
  2492. * - username
  2493. * - password
  2494. * @return array
  2495. */
  2496. function wp_getPostFormats( $args ) {
  2497. $this->escape( $args );
  2498. $blog_id = (int) $args[0];
  2499. $username = $args[1];
  2500. $password = $args[2];
  2501. if ( !$user = $this->login( $username, $password ) )
  2502. return $this->error;
  2503. if ( !current_user_can( 'edit_posts' ) )
  2504. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  2505. do_action( 'xmlrpc_call', 'wp.getPostFormats' );
  2506. $formats = get_post_format_strings();
  2507. # find out if they want a list of currently supports formats
  2508. if ( isset( $args[3] ) && is_array( $args[3] ) ) {
  2509. if ( $args[3]['show-supported'] ) {
  2510. if ( current_theme_supports( 'post-formats' ) ) {
  2511. $supported = get_theme_support( 'post-formats' );
  2512. $data['all'] = $formats;
  2513. $data['supported'] = $supported[0];
  2514. $formats = $data;
  2515. }
  2516. }
  2517. }
  2518. return $formats;
  2519. }
  2520. /**
  2521. * Retrieves a post type
  2522. *
  2523. * @since 3.4.0
  2524. *
  2525. * @uses get_post_type_object()
  2526. * @param array $args Method parameters. Contains:
  2527. * - int $blog_id
  2528. * - string $username
  2529. * - string $password
  2530. * - string $post_type_name
  2531. * - array $fields
  2532. * @return array contains:
  2533. * - 'labels'
  2534. * - 'description'
  2535. * - 'capability_type'
  2536. * - 'cap'
  2537. * - 'map_meta_cap'
  2538. * - 'hierarchical'
  2539. * - 'menu_position'
  2540. * - 'taxonomies'
  2541. * - 'supports'
  2542. */
  2543. function wp_getPostType( $args ) {
  2544. if ( ! $this->minimum_args( $args, 4 ) )
  2545. return $this->error;
  2546. $this->escape( $args );
  2547. $blog_id = (int) $args[0];
  2548. $username = $args[1];
  2549. $password = $args[2];
  2550. $post_type_name = $args[3];
  2551. if ( isset( $args[4] ) )
  2552. $fields = $args[4];
  2553. else
  2554. $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
  2555. if ( !$user = $this->login( $username, $password ) )
  2556. return $this->error;
  2557. do_action( 'xmlrpc_call', 'wp.getPostType' );
  2558. if( ! post_type_exists( $post_type_name ) )
  2559. return new IXR_Error( 403, __( 'Invalid post type' ) );
  2560. $post_type = get_post_type_object( $post_type_name );
  2561. if( ! current_user_can( $post_type->cap->edit_posts ) )
  2562. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post type.' ) );
  2563. return $this->_prepare_post_type( $post_type, $fields );
  2564. }
  2565. /**
  2566. * Retrieves a post types
  2567. *
  2568. * @since 3.4.0
  2569. *
  2570. * @uses get_post_types()
  2571. * @param array $args Method parameters. Contains:
  2572. * - int $blog_id
  2573. * - string $username
  2574. * - string $password
  2575. * - array $filter
  2576. * - array $fields
  2577. * @return array
  2578. */
  2579. function wp_getPostTypes( $args ) {
  2580. if ( ! $this->minimum_args( $args, 3 ) )
  2581. return $this->error;
  2582. $this->escape( $args );
  2583. $blog_id = (int) $args[0];
  2584. $username = $args[1];
  2585. $password = $args[2];
  2586. $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  2587. if ( isset( $args[4] ) )
  2588. $fields = $args[4];
  2589. else
  2590. $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
  2591. if ( ! $user = $this->login( $username, $password ) )
  2592. return $this->error;
  2593. do_action( 'xmlrpc_call', 'wp.getPostTypes' );
  2594. $post_types = get_post_types( $filter, 'objects' );
  2595. $struct = array();
  2596. foreach( $post_types as $post_type ) {
  2597. if( ! current_user_can( $post_type->cap->edit_posts ) )
  2598. continue;
  2599. $struct[$post_type->name] = $this->_prepare_post_type( $post_type, $fields );
  2600. }
  2601. return $struct;
  2602. }
  2603. /* Blogger API functions.
  2604. * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
  2605. */
  2606. /**
  2607. * Retrieve blogs that user owns.
  2608. *
  2609. * Will make more sense once we support multiple blogs.
  2610. *
  2611. * @since 1.5.0
  2612. *
  2613. * @param array $args Method parameters.
  2614. * @return array
  2615. */
  2616. function blogger_getUsersBlogs($args) {
  2617. if ( is_multisite() )
  2618. return $this->_multisite_getUsersBlogs($args);
  2619. $this->escape($args);
  2620. $username = $args[1];
  2621. $password = $args[2];
  2622. if ( !$user = $this->login($username, $password) )
  2623. return $this->error;
  2624. do_action('xmlrpc_call', 'blogger.getUsersBlogs');
  2625. $is_admin = current_user_can('manage_options');
  2626. $struct = array(
  2627. 'isAdmin' => $is_admin,
  2628. 'url' => get_option('home') . '/',
  2629. 'blogid' => '1',
  2630. 'blogName' => get_option('blogname'),
  2631. 'xmlrpc' => site_url( 'xmlrpc.php' )
  2632. );
  2633. return array($struct);
  2634. }
  2635. /**
  2636. * Private function for retrieving a users blogs for multisite setups
  2637. *
  2638. * @access protected
  2639. */
  2640. function _multisite_getUsersBlogs($args) {
  2641. global $current_blog;
  2642. $domain = $current_blog->domain;
  2643. $path = $current_blog->path . 'xmlrpc.php';
  2644. $protocol = is_ssl() ? 'https' : 'http';
  2645. $rpc = new IXR_Client("$protocol://{$domain}{$path}");
  2646. $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
  2647. $blogs = $rpc->getResponse();
  2648. if ( isset($blogs['faultCode']) )
  2649. return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
  2650. if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  2651. return $blogs;
  2652. } else {
  2653. foreach ( (array) $blogs as $blog ) {
  2654. if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
  2655. return array($blog);
  2656. }
  2657. return array();
  2658. }
  2659. }
  2660. /**
  2661. * Retrieve user's data.
  2662. *
  2663. * Gives your client some info about you, so you don't have to.
  2664. *
  2665. * @since 1.5.0
  2666. *
  2667. * @param array $args Method parameters.
  2668. * @return array
  2669. */
  2670. function blogger_getUserInfo($args) {
  2671. $this->escape($args);
  2672. $username = $args[1];
  2673. $password = $args[2];
  2674. if ( !$user = $this->login($username, $password) )
  2675. return $this->error;
  2676. if ( !current_user_can( 'edit_posts' ) )
  2677. return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
  2678. do_action('xmlrpc_call', 'blogger.getUserInfo');
  2679. $struct = array(
  2680. 'nickname' => $user->nickname,
  2681. 'userid' => $user->ID,
  2682. 'url' => $user->user_url,
  2683. 'lastname' => $user->last_name,
  2684. 'firstname' => $user->first_name
  2685. );
  2686. return $struct;
  2687. }
  2688. /**
  2689. * Retrieve post.
  2690. *
  2691. * @since 1.5.0
  2692. *
  2693. * @param array $args Method parameters.
  2694. * @return array
  2695. */
  2696. function blogger_getPost($args) {
  2697. $this->escape($args);
  2698. $post_ID = (int) $args[1];
  2699. $username = $args[2];
  2700. $password = $args[3];
  2701. if ( !$user = $this->login($username, $password) )
  2702. return $this->error;
  2703. $post_data = wp_get_single_post($post_ID, ARRAY_A);
  2704. if ( ! $post_data )
  2705. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  2706. if ( !current_user_can( 'edit_post', $post_ID ) )
  2707. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  2708. do_action('xmlrpc_call', 'blogger.getPost');
  2709. $categories = implode(',', wp_get_post_categories($post_ID));
  2710. $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
  2711. $content .= '<category>'.$categories.'</category>';
  2712. $content .= stripslashes($post_data['post_content']);
  2713. $struct = array(
  2714. 'userid' => $post_data['post_author'],
  2715. 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
  2716. 'content' => $content,
  2717. 'postid' => (string) $post_data['ID']
  2718. );
  2719. return $struct;
  2720. }
  2721. /**
  2722. * Retrieve list of recent posts.
  2723. *
  2724. * @since 1.5.0
  2725. *
  2726. * @param array $args Method parameters.
  2727. * @return array
  2728. */
  2729. function blogger_getRecentPosts($args) {
  2730. $this->escape($args);
  2731. // $args[0] = appkey - ignored
  2732. $blog_ID = (int) $args[1]; /* though we don't use it yet */
  2733. $username = $args[2];
  2734. $password = $args[3];
  2735. if ( isset( $args[4] ) )
  2736. $query = array( 'numberposts' => absint( $args[4] ) );
  2737. else
  2738. $query = array();
  2739. if ( !$user = $this->login($username, $password) )
  2740. return $this->error;
  2741. do_action('xmlrpc_call', 'blogger.getRecentPosts');
  2742. $posts_list = wp_get_recent_posts( $query );
  2743. if ( !$posts_list ) {
  2744. $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  2745. return $this->error;
  2746. }
  2747. foreach ($posts_list as $entry) {
  2748. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  2749. continue;
  2750. $post_date = $this->_convert_date( $entry['post_date'] );
  2751. $categories = implode(',', wp_get_post_categories($entry['ID']));
  2752. $content = '<title>'.stripslashes($entry['post_title']).'</title>';
  2753. $content .= '<category>'.$categories.'</category>';
  2754. $content .= stripslashes($entry['post_content']);
  2755. $struct[] = array(
  2756. 'userid' => $entry['post_author'],
  2757. 'dateCreated' => $post_date,
  2758. 'content' => $content,
  2759. 'postid' => (string) $entry['ID'],
  2760. );
  2761. }
  2762. $recent_posts = array();
  2763. for ( $j=0; $j<count($struct); $j++ ) {
  2764. array_push($recent_posts, $struct[$j]);
  2765. }
  2766. return $recent_posts;
  2767. }
  2768. /**
  2769. * Retrieve blog_filename content.
  2770. *
  2771. * @since 1.5.0
  2772. *
  2773. * @param array $args Method parameters.
  2774. * @return string
  2775. */
  2776. function blogger_getTemplate($args) {
  2777. $this->escape($args);
  2778. $blog_ID = (int) $args[1];
  2779. $username = $args[2];
  2780. $password = $args[3];
  2781. $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
  2782. if ( !$user = $this->login($username, $password) )
  2783. return $this->error;
  2784. do_action('xmlrpc_call', 'blogger.getTemplate');
  2785. if ( !current_user_can('edit_themes') )
  2786. return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
  2787. /* warning: here we make the assumption that the blog's URL is on the same server */
  2788. $filename = get_option('home') . '/';
  2789. $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
  2790. $f = fopen($filename, 'r');
  2791. $content = fread($f, filesize($filename));
  2792. fclose($f);
  2793. /* so it is actually editable with a windows/mac client */
  2794. // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content);
  2795. return $content;
  2796. }
  2797. /**
  2798. * Updates the content of blog_filename.
  2799. *
  2800. * @since 1.5.0
  2801. *
  2802. * @param array $args Method parameters.
  2803. * @return bool True when done.
  2804. */
  2805. function blogger_setTemplate($args) {
  2806. $this->escape($args);
  2807. $blog_ID = (int) $args[1];
  2808. $username = $args[2];
  2809. $password = $args[3];
  2810. $content = $args[4];
  2811. $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
  2812. if ( !$user = $this->login($username, $password) )
  2813. return $this->error;
  2814. do_action('xmlrpc_call', 'blogger.setTemplate');
  2815. if ( !current_user_can('edit_themes') )
  2816. return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
  2817. /* warning: here we make the assumption that the blog's URL is on the same server */
  2818. $filename = get_option('home') . '/';
  2819. $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
  2820. if ($f = fopen($filename, 'w+')) {
  2821. fwrite($f, $content);
  2822. fclose($f);
  2823. } else {
  2824. return new IXR_Error(500, __('Either the file is not writable, or something wrong happened. The file has not been updated.'));
  2825. }
  2826. return true;
  2827. }
  2828. /**
  2829. * Create new post.
  2830. *
  2831. * @since 1.5.0
  2832. *
  2833. * @param array $args Method parameters.
  2834. * @return int
  2835. */
  2836. function blogger_newPost($args) {
  2837. $this->escape($args);
  2838. $blog_ID = (int) $args[1]; /* though we don't use it yet */
  2839. $username = $args[2];
  2840. $password = $args[3];
  2841. $content = $args[4];
  2842. $publish = $args[5];
  2843. if ( !$user = $this->login($username, $password) )
  2844. return $this->error;
  2845. do_action('xmlrpc_call', 'blogger.newPost');
  2846. $cap = ($publish) ? 'publish_posts' : 'edit_posts';
  2847. if ( !current_user_can($cap) )
  2848. return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
  2849. $post_status = ($publish) ? 'publish' : 'draft';
  2850. $post_author = $user->ID;
  2851. $post_title = xmlrpc_getposttitle($content);
  2852. $post_category = xmlrpc_getpostcategory($content);
  2853. $post_content = xmlrpc_removepostdata($content);
  2854. $post_date = current_time('mysql');
  2855. $post_date_gmt = current_time('mysql', 1);
  2856. $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
  2857. $post_ID = wp_insert_post($post_data);
  2858. if ( is_wp_error( $post_ID ) )
  2859. return new IXR_Error(500, $post_ID->get_error_message());
  2860. if ( !$post_ID )
  2861. return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
  2862. $this->attach_uploads( $post_ID, $post_content );
  2863. do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args );
  2864. return $post_ID;
  2865. }
  2866. /**
  2867. * Edit a post.
  2868. *
  2869. * @since 1.5.0
  2870. *
  2871. * @param array $args Method parameters.
  2872. * @return bool true when done.
  2873. */
  2874. function blogger_editPost($args) {
  2875. $this->escape($args);
  2876. $post_ID = (int) $args[1];
  2877. $username = $args[2];
  2878. $password = $args[3];
  2879. $content = $args[4];
  2880. $publish = $args[5];
  2881. if ( !$user = $this->login($username, $password) )
  2882. return $this->error;
  2883. do_action('xmlrpc_call', 'blogger.editPost');
  2884. $actual_post = wp_get_single_post($post_ID,ARRAY_A);
  2885. if ( !$actual_post || $actual_post['post_type'] != 'post' )
  2886. return new IXR_Error(404, __('Sorry, no such post.'));
  2887. $this->escape($actual_post);
  2888. if ( !current_user_can('edit_post', $post_ID) )
  2889. return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
  2890. extract($actual_post, EXTR_SKIP);
  2891. if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
  2892. return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
  2893. $post_title = xmlrpc_getposttitle($content);
  2894. $post_category = xmlrpc_getpostcategory($content);
  2895. $post_content = xmlrpc_removepostdata($content);
  2896. $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
  2897. $result = wp_update_post($postdata);
  2898. if ( !$result )
  2899. return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
  2900. $this->attach_uploads( $ID, $post_content );
  2901. do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args );
  2902. return true;
  2903. }
  2904. /**
  2905. * Remove a post.
  2906. *
  2907. * @since 1.5.0
  2908. *
  2909. * @param array $args Method parameters.
  2910. * @return bool True when post is deleted.
  2911. */
  2912. function blogger_deletePost($args) {
  2913. $this->escape($args);
  2914. $post_ID = (int) $args[1];
  2915. $username = $args[2];
  2916. $password = $args[3];
  2917. $publish = $args[4];
  2918. if ( !$user = $this->login($username, $password) )
  2919. return $this->error;
  2920. do_action('xmlrpc_call', 'blogger.deletePost');
  2921. $actual_post = wp_get_single_post($post_ID,ARRAY_A);
  2922. if ( !$actual_post || $actual_post['post_type'] != 'post' )
  2923. return new IXR_Error(404, __('Sorry, no such post.'));
  2924. if ( !current_user_can('delete_post', $post_ID) )
  2925. return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
  2926. $result = wp_delete_post($post_ID);
  2927. if ( !$result )
  2928. return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
  2929. do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args );
  2930. return true;
  2931. }
  2932. /* MetaWeblog API functions
  2933. * specs on wherever Dave Winer wants them to be
  2934. */
  2935. /**
  2936. * Create a new post.
  2937. *
  2938. * The 'content_struct' argument must contain:
  2939. * - title
  2940. * - description
  2941. * - mt_excerpt
  2942. * - mt_text_more
  2943. * - mt_keywords
  2944. * - mt_tb_ping_urls
  2945. * - categories
  2946. *
  2947. * Also, it can optionally contain:
  2948. * - wp_slug
  2949. * - wp_password
  2950. * - wp_page_parent_id
  2951. * - wp_page_order
  2952. * - wp_author_id
  2953. * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
  2954. * - mt_allow_comments - can be 'open' or 'closed'
  2955. * - mt_allow_pings - can be 'open' or 'closed'
  2956. * - date_created_gmt
  2957. * - dateCreated
  2958. * - wp_post_thumbnail
  2959. *
  2960. * @since 1.5.0
  2961. *
  2962. * @param array $args Method parameters. Contains:
  2963. * - blog_id
  2964. * - username
  2965. * - password
  2966. * - content_struct
  2967. * - publish
  2968. * @return int
  2969. */
  2970. function mw_newPost($args) {
  2971. $this->escape($args);
  2972. $blog_ID = (int) $args[0];
  2973. $username = $args[1];
  2974. $password = $args[2];
  2975. $content_struct = $args[3];
  2976. $publish = isset( $args[4] ) ? $args[4] : 0;
  2977. if ( !$user = $this->login($username, $password) )
  2978. return $this->error;
  2979. do_action('xmlrpc_call', 'metaWeblog.newPost');
  2980. $page_template = '';
  2981. if ( !empty( $content_struct['post_type'] ) ) {
  2982. if ( $content_struct['post_type'] == 'page' ) {
  2983. if ( $publish )
  2984. $cap = 'publish_pages';
  2985. elseif ( isset( $content_struct['page_status'] ) && 'publish' == $content_struct['page_status'] )
  2986. $cap = 'publish_pages';
  2987. else
  2988. $cap = 'edit_pages';
  2989. $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  2990. $post_type = 'page';
  2991. if ( !empty( $content_struct['wp_page_template'] ) )
  2992. $page_template = $content_struct['wp_page_template'];
  2993. } elseif ( $content_struct['post_type'] == 'post' ) {
  2994. if ( $publish )
  2995. $cap = 'publish_posts';
  2996. elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'] )
  2997. $cap = 'publish_posts';
  2998. else
  2999. $cap = 'edit_posts';
  3000. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  3001. $post_type = 'post';
  3002. } else {
  3003. // No other post_type values are allowed here
  3004. return new IXR_Error( 401, __( 'Invalid post type' ) );
  3005. }
  3006. } else {
  3007. if ( $publish )
  3008. $cap = 'publish_posts';
  3009. elseif ( isset( $content_struct['post_status'] ) && 'publish' == $content_struct['post_status'])
  3010. $cap = 'publish_posts';
  3011. else
  3012. $cap = 'edit_posts';
  3013. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  3014. $post_type = 'post';
  3015. }
  3016. if ( !current_user_can( $cap ) )
  3017. return new IXR_Error( 401, $error_message );
  3018. // Check for a valid post format if one was given
  3019. if ( isset( $content_struct['wp_post_format'] ) ) {
  3020. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  3021. if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  3022. return new IXR_Error( 404, __( 'Invalid post format' ) );
  3023. }
  3024. }
  3025. // Let WordPress generate the post_name (slug) unless
  3026. // one has been provided.
  3027. $post_name = "";
  3028. if ( isset($content_struct['wp_slug']) )
  3029. $post_name = $content_struct['wp_slug'];
  3030. // Only use a password if one was given.
  3031. if ( isset($content_struct['wp_password']) )
  3032. $post_password = $content_struct['wp_password'];
  3033. // Only set a post parent if one was provided.
  3034. if ( isset($content_struct['wp_page_parent_id']) )
  3035. $post_parent = $content_struct['wp_page_parent_id'];
  3036. // Only set the menu_order if it was provided.
  3037. if ( isset($content_struct['wp_page_order']) )
  3038. $menu_order = $content_struct['wp_page_order'];
  3039. $post_author = $user->ID;
  3040. // If an author id was provided then use it instead.
  3041. if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
  3042. switch ( $post_type ) {
  3043. case "post":
  3044. if ( !current_user_can('edit_others_posts') )
  3045. return(new IXR_Error(401, __('You are not allowed to post as this user')));
  3046. break;
  3047. case "page":
  3048. if ( !current_user_can('edit_others_pages') )
  3049. return(new IXR_Error(401, __('You are not allowed to create pages as this user')));
  3050. break;
  3051. default:
  3052. return(new IXR_Error(401, __('Invalid post type')));
  3053. break;
  3054. }
  3055. $author = get_userdata( $content_struct['wp_author_id'] );
  3056. if ( ! $author )
  3057. return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  3058. $post_author = $content_struct['wp_author_id'];
  3059. }
  3060. $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
  3061. $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
  3062. $post_status = $publish ? 'publish' : 'draft';
  3063. if ( isset( $content_struct["{$post_type}_status"] ) ) {
  3064. switch ( $content_struct["{$post_type}_status"] ) {
  3065. case 'draft':
  3066. case 'pending':
  3067. case 'private':
  3068. case 'publish':
  3069. $post_status = $content_struct["{$post_type}_status"];
  3070. break;
  3071. default:
  3072. $post_status = $publish ? 'publish' : 'draft';
  3073. break;
  3074. }
  3075. }
  3076. $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
  3077. $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
  3078. $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
  3079. if ( isset($content_struct['mt_allow_comments']) ) {
  3080. if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  3081. switch ( $content_struct['mt_allow_comments'] ) {
  3082. case 'closed':
  3083. $comment_status = 'closed';
  3084. break;
  3085. case 'open':
  3086. $comment_status = 'open';
  3087. break;
  3088. default:
  3089. $comment_status = get_option('default_comment_status');
  3090. break;
  3091. }
  3092. } else {
  3093. switch ( (int) $content_struct['mt_allow_comments'] ) {
  3094. case 0:
  3095. case 2:
  3096. $comment_status = 'closed';
  3097. break;
  3098. case 1:
  3099. $comment_status = 'open';
  3100. break;
  3101. default:
  3102. $comment_status = get_option('default_comment_status');
  3103. break;
  3104. }
  3105. }
  3106. } else {
  3107. $comment_status = get_option('default_comment_status');
  3108. }
  3109. if ( isset($content_struct['mt_allow_pings']) ) {
  3110. if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  3111. switch ( $content_struct['mt_allow_pings'] ) {
  3112. case 'closed':
  3113. $ping_status = 'closed';
  3114. break;
  3115. case 'open':
  3116. $ping_status = 'open';
  3117. break;
  3118. default:
  3119. $ping_status = get_option('default_ping_status');
  3120. break;
  3121. }
  3122. } else {
  3123. switch ( (int) $content_struct['mt_allow_pings'] ) {
  3124. case 0:
  3125. $ping_status = 'closed';
  3126. break;
  3127. case 1:
  3128. $ping_status = 'open';
  3129. break;
  3130. default:
  3131. $ping_status = get_option('default_ping_status');
  3132. break;
  3133. }
  3134. }
  3135. } else {
  3136. $ping_status = get_option('default_ping_status');
  3137. }
  3138. if ( $post_more )
  3139. $post_content = $post_content . '<!--more-->' . $post_more;
  3140. $to_ping = null;
  3141. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  3142. $to_ping = $content_struct['mt_tb_ping_urls'];
  3143. if ( is_array($to_ping) )
  3144. $to_ping = implode(' ', $to_ping);
  3145. }
  3146. // Do some timestamp voodoo
  3147. if ( !empty( $content_struct['date_created_gmt'] ) )
  3148. // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  3149. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  3150. elseif ( !empty( $content_struct['dateCreated']) )
  3151. $dateCreated = $content_struct['dateCreated']->getIso();
  3152. if ( !empty( $dateCreated ) ) {
  3153. $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  3154. $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  3155. } else {
  3156. $post_date = current_time('mysql');
  3157. $post_date_gmt = current_time('mysql', 1);
  3158. }
  3159. $post_category = array();
  3160. if ( isset( $content_struct['categories'] ) ) {
  3161. $catnames = $content_struct['categories'];
  3162. if ( is_array($catnames) ) {
  3163. foreach ($catnames as $cat) {
  3164. $post_category[] = get_cat_ID($cat);
  3165. }
  3166. }
  3167. }
  3168. $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input', 'page_template');
  3169. $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
  3170. // Only posts can be sticky
  3171. if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  3172. if ( $content_struct['sticky'] == true )
  3173. stick_post( $post_ID );
  3174. elseif ( $content_struct['sticky'] == false )
  3175. unstick_post( $post_ID );
  3176. }
  3177. if ( isset($content_struct['custom_fields']) )
  3178. $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  3179. if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
  3180. if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
  3181. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  3182. unset( $content_struct['wp_post_thumbnail'] );
  3183. }
  3184. // Handle enclosures
  3185. $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  3186. $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  3187. $this->attach_uploads( $post_ID, $post_content );
  3188. // Handle post formats if assigned, value is validated earlier
  3189. // in this function
  3190. if ( isset( $content_struct['wp_post_format'] ) )
  3191. wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
  3192. $post_ID = wp_insert_post( $postdata, true );
  3193. if ( is_wp_error( $post_ID ) )
  3194. return new IXR_Error(500, $post_ID->get_error_message());
  3195. if ( !$post_ID )
  3196. return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
  3197. do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args );
  3198. return strval($post_ID);
  3199. }
  3200. function add_enclosure_if_new($post_ID, $enclosure) {
  3201. if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
  3202. $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'];
  3203. $found = false;
  3204. foreach ( (array) get_post_custom($post_ID) as $key => $val) {
  3205. if ($key == 'enclosure') {
  3206. foreach ( (array) $val as $enc ) {
  3207. if ($enc == $encstring) {
  3208. $found = true;
  3209. break 2;
  3210. }
  3211. }
  3212. }
  3213. }
  3214. if (!$found)
  3215. add_post_meta( $post_ID, 'enclosure', $encstring );
  3216. }
  3217. }
  3218. /**
  3219. * Attach upload to a post.
  3220. *
  3221. * @since 2.1.0
  3222. *
  3223. * @param int $post_ID Post ID.
  3224. * @param string $post_content Post Content for attachment.
  3225. */
  3226. function attach_uploads( $post_ID, $post_content ) {
  3227. global $wpdb;
  3228. // find any unattached files
  3229. $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
  3230. if ( is_array( $attachments ) ) {
  3231. foreach ( $attachments as $file ) {
  3232. if ( strpos( $post_content, $file->guid ) !== false )
  3233. $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
  3234. }
  3235. }
  3236. }
  3237. /**
  3238. * Edit a post.
  3239. *
  3240. * @since 1.5.0
  3241. *
  3242. * @param array $args Method parameters.
  3243. * @return bool True on success.
  3244. */
  3245. function mw_editPost($args) {
  3246. $this->escape($args);
  3247. $post_ID = (int) $args[0];
  3248. $username = $args[1];
  3249. $password = $args[2];
  3250. $content_struct = $args[3];
  3251. $publish = isset( $args[4] ) ? $args[4] : 0;
  3252. if ( ! $user = $this->login($username, $password) )
  3253. return $this->error;
  3254. do_action('xmlrpc_call', 'metaWeblog.editPost');
  3255. $postdata = wp_get_single_post( $post_ID, ARRAY_A );
  3256. // If there is no post data for the give post id, stop
  3257. // now and return an error. Other wise a new post will be
  3258. // created (which was the old behavior).
  3259. if ( ! $postdata || empty( $postdata[ 'ID' ] ) )
  3260. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3261. if ( ! current_user_can( 'edit_post', $post_ID ) )
  3262. return new IXR_Error( 401, __( 'Sorry, you do not have the right to edit this post.' ) );
  3263. // Use wp.editPost to edit post types other than post and page.
  3264. if ( ! in_array( $postdata[ 'post_type' ], array( 'post', 'page' ) ) )
  3265. return new IXR_Error( 401, __( 'Invalid post type' ) );
  3266. // Thwart attempt to change the post type.
  3267. if ( ! empty( $content_struct[ 'post_type' ] ) && ( $content_struct['post_type'] != $postdata[ 'post_type' ] ) )
  3268. return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  3269. // Check for a valid post format if one was given
  3270. if ( isset( $content_struct['wp_post_format'] ) ) {
  3271. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  3272. if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  3273. return new IXR_Error( 404, __( 'Invalid post format' ) );
  3274. }
  3275. }
  3276. $this->escape($postdata);
  3277. extract($postdata, EXTR_SKIP);
  3278. // Let WordPress manage slug if none was provided.
  3279. $post_name = "";
  3280. $post_name = $postdata['post_name'];
  3281. if ( isset($content_struct['wp_slug']) )
  3282. $post_name = $content_struct['wp_slug'];
  3283. // Only use a password if one was given.
  3284. if ( isset($content_struct['wp_password']) )
  3285. $post_password = $content_struct['wp_password'];
  3286. // Only set a post parent if one was given.
  3287. if ( isset($content_struct['wp_page_parent_id']) )
  3288. $post_parent = $content_struct['wp_page_parent_id'];
  3289. // Only set the menu_order if it was given.
  3290. if ( isset($content_struct['wp_page_order']) )
  3291. $menu_order = $content_struct['wp_page_order'];
  3292. if ( ! empty( $content_struct['wp_page_template'] ) && 'page' == $post_type )
  3293. $page_template = $content_struct['wp_page_template'];
  3294. $post_author = $postdata['post_author'];
  3295. // Only set the post_author if one is set.
  3296. if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
  3297. switch ( $post_type ) {
  3298. case 'post':
  3299. if ( !current_user_can('edit_others_posts') )
  3300. return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
  3301. break;
  3302. case 'page':
  3303. if ( !current_user_can('edit_others_pages') )
  3304. return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
  3305. break;
  3306. default:
  3307. return(new IXR_Error(401, __('Invalid post type')));
  3308. break;
  3309. }
  3310. $post_author = $content_struct['wp_author_id'];
  3311. }
  3312. if ( isset($content_struct['mt_allow_comments']) ) {
  3313. if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  3314. switch ( $content_struct['mt_allow_comments'] ) {
  3315. case 'closed':
  3316. $comment_status = 'closed';
  3317. break;
  3318. case 'open':
  3319. $comment_status = 'open';
  3320. break;
  3321. default:
  3322. $comment_status = get_option('default_comment_status');
  3323. break;
  3324. }
  3325. } else {
  3326. switch ( (int) $content_struct['mt_allow_comments'] ) {
  3327. case 0:
  3328. case 2:
  3329. $comment_status = 'closed';
  3330. break;
  3331. case 1:
  3332. $comment_status = 'open';
  3333. break;
  3334. default:
  3335. $comment_status = get_option('default_comment_status');
  3336. break;
  3337. }
  3338. }
  3339. }
  3340. if ( isset($content_struct['mt_allow_pings']) ) {
  3341. if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  3342. switch ( $content_struct['mt_allow_pings'] ) {
  3343. case 'closed':
  3344. $ping_status = 'closed';
  3345. break;
  3346. case 'open':
  3347. $ping_status = 'open';
  3348. break;
  3349. default:
  3350. $ping_status = get_option('default_ping_status');
  3351. break;
  3352. }
  3353. } else {
  3354. switch ( (int) $content_struct["mt_allow_pings"] ) {
  3355. case 0:
  3356. $ping_status = 'closed';
  3357. break;
  3358. case 1:
  3359. $ping_status = 'open';
  3360. break;
  3361. default:
  3362. $ping_status = get_option('default_ping_status');
  3363. break;
  3364. }
  3365. }
  3366. }
  3367. if ( isset( $content_struct['title'] ) )
  3368. $post_title = $content_struct['title'];
  3369. if ( isset( $content_struct['description'] ) )
  3370. $post_content = $content_struct['description'];
  3371. $post_category = array();
  3372. if ( isset( $content_struct['categories'] ) ) {
  3373. $catnames = $content_struct['categories'];
  3374. if ( is_array($catnames) ) {
  3375. foreach ($catnames as $cat) {
  3376. $post_category[] = get_cat_ID($cat);
  3377. }
  3378. }
  3379. }
  3380. if ( isset( $content_struct['mt_excerpt'] ) )
  3381. $post_excerpt = $content_struct['mt_excerpt'];
  3382. $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
  3383. $post_status = $publish ? 'publish' : 'draft';
  3384. if ( isset( $content_struct["{$post_type}_status"] ) ) {
  3385. switch( $content_struct["{$post_type}_status"] ) {
  3386. case 'draft':
  3387. case 'pending':
  3388. case 'private':
  3389. case 'publish':
  3390. $post_status = $content_struct["{$post_type}_status"];
  3391. break;
  3392. default:
  3393. $post_status = $publish ? 'publish' : 'draft';
  3394. break;
  3395. }
  3396. }
  3397. $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
  3398. if ( ('publish' == $post_status) ) {
  3399. if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
  3400. return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
  3401. else if ( !current_user_can('publish_posts') )
  3402. return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
  3403. }
  3404. if ( $post_more )
  3405. $post_content = $post_content . "<!--more-->" . $post_more;
  3406. $to_ping = null;
  3407. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  3408. $to_ping = $content_struct['mt_tb_ping_urls'];
  3409. if ( is_array($to_ping) )
  3410. $to_ping = implode(' ', $to_ping);
  3411. }
  3412. // Do some timestamp voodoo
  3413. if ( !empty( $content_struct['date_created_gmt'] ) )
  3414. // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  3415. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  3416. elseif ( !empty( $content_struct['dateCreated']) )
  3417. $dateCreated = $content_struct['dateCreated']->getIso();
  3418. if ( !empty( $dateCreated ) ) {
  3419. $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  3420. $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  3421. } else {
  3422. $post_date = $postdata['post_date'];
  3423. $post_date_gmt = $postdata['post_date_gmt'];
  3424. }
  3425. // We've got all the data -- post it:
  3426. $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
  3427. $result = wp_update_post($newpost, true);
  3428. if ( is_wp_error( $result ) )
  3429. return new IXR_Error(500, $result->get_error_message());
  3430. if ( !$result )
  3431. return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
  3432. // Only posts can be sticky
  3433. if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  3434. if ( $content_struct['sticky'] == true )
  3435. stick_post( $post_ID );
  3436. elseif ( $content_struct['sticky'] == false )
  3437. unstick_post( $post_ID );
  3438. }
  3439. if ( isset($content_struct['custom_fields']) )
  3440. $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  3441. if ( isset ( $content_struct['wp_post_thumbnail'] ) ) {
  3442. // empty value deletes, non-empty value adds/updates
  3443. if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
  3444. delete_post_thumbnail( $post_ID );
  3445. } else {
  3446. if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false )
  3447. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  3448. }
  3449. unset( $content_struct['wp_post_thumbnail'] );
  3450. }
  3451. // Handle enclosures
  3452. $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  3453. $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  3454. $this->attach_uploads( $ID, $post_content );
  3455. // Handle post formats if assigned, validation is handled
  3456. // earlier in this function
  3457. if ( isset( $content_struct['wp_post_format'] ) )
  3458. wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
  3459. do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args );
  3460. return true;
  3461. }
  3462. /**
  3463. * Retrieve post.
  3464. *
  3465. * @since 1.5.0
  3466. *
  3467. * @param array $args Method parameters.
  3468. * @return array
  3469. */
  3470. function mw_getPost($args) {
  3471. $this->escape($args);
  3472. $post_ID = (int) $args[0];
  3473. $username = $args[1];
  3474. $password = $args[2];
  3475. if ( !$user = $this->login($username, $password) )
  3476. return $this->error;
  3477. $postdata = wp_get_single_post($post_ID, ARRAY_A);
  3478. if ( ! $postdata )
  3479. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3480. if ( !current_user_can( 'edit_post', $post_ID ) )
  3481. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  3482. do_action('xmlrpc_call', 'metaWeblog.getPost');
  3483. if ($postdata['post_date'] != '') {
  3484. $post_date = $this->_convert_date( $postdata['post_date'] );
  3485. $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
  3486. $post_modified = $this->_convert_date( $postdata['post_modified'] );
  3487. $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
  3488. $categories = array();
  3489. $catids = wp_get_post_categories($post_ID);
  3490. foreach($catids as $catid)
  3491. $categories[] = get_cat_name($catid);
  3492. $tagnames = array();
  3493. $tags = wp_get_post_tags( $post_ID );
  3494. if ( !empty( $tags ) ) {
  3495. foreach ( $tags as $tag )
  3496. $tagnames[] = $tag->name;
  3497. $tagnames = implode( ', ', $tagnames );
  3498. } else {
  3499. $tagnames = '';
  3500. }
  3501. $post = get_extended($postdata['post_content']);
  3502. $link = post_permalink($postdata['ID']);
  3503. // Get the author info.
  3504. $author = get_userdata($postdata['post_author']);
  3505. $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
  3506. $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
  3507. // Consider future posts as published
  3508. if ( $postdata['post_status'] === 'future' )
  3509. $postdata['post_status'] = 'publish';
  3510. // Get post format
  3511. $post_format = get_post_format( $post_ID );
  3512. if ( empty( $post_format ) )
  3513. $post_format = 'standard';
  3514. $sticky = false;
  3515. if ( is_sticky( $post_ID ) )
  3516. $sticky = true;
  3517. $enclosure = array();
  3518. foreach ( (array) get_post_custom($post_ID) as $key => $val) {
  3519. if ($key == 'enclosure') {
  3520. foreach ( (array) $val as $enc ) {
  3521. $encdata = explode("\n", $enc);
  3522. $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
  3523. $enclosure['length'] = (int) trim($encdata[1]);
  3524. $enclosure['type'] = trim($encdata[2]);
  3525. break 2;
  3526. }
  3527. }
  3528. }
  3529. $resp = array(
  3530. 'dateCreated' => $post_date,
  3531. 'userid' => $postdata['post_author'],
  3532. 'postid' => $postdata['ID'],
  3533. 'description' => $post['main'],
  3534. 'title' => $postdata['post_title'],
  3535. 'link' => $link,
  3536. 'permaLink' => $link,
  3537. // commented out because no other tool seems to use this
  3538. // 'content' => $entry['post_content'],
  3539. 'categories' => $categories,
  3540. 'mt_excerpt' => $postdata['post_excerpt'],
  3541. 'mt_text_more' => $post['extended'],
  3542. 'wp_more_text' => $post['more_text'],
  3543. 'mt_allow_comments' => $allow_comments,
  3544. 'mt_allow_pings' => $allow_pings,
  3545. 'mt_keywords' => $tagnames,
  3546. 'wp_slug' => $postdata['post_name'],
  3547. 'wp_password' => $postdata['post_password'],
  3548. 'wp_author_id' => (string) $author->ID,
  3549. 'wp_author_display_name' => $author->display_name,
  3550. 'date_created_gmt' => $post_date_gmt,
  3551. 'post_status' => $postdata['post_status'],
  3552. 'custom_fields' => $this->get_custom_fields($post_ID),
  3553. 'wp_post_format' => $post_format,
  3554. 'sticky' => $sticky,
  3555. 'date_modified' => $post_modified,
  3556. 'date_modified_gmt' => $post_modified_gmt
  3557. );
  3558. if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
  3559. $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
  3560. return $resp;
  3561. } else {
  3562. return new IXR_Error(404, __('Sorry, no such post.'));
  3563. }
  3564. }
  3565. /**
  3566. * Retrieve list of recent posts.
  3567. *
  3568. * @since 1.5.0
  3569. *
  3570. * @param array $args Method parameters.
  3571. * @return array
  3572. */
  3573. function mw_getRecentPosts($args) {
  3574. $this->escape($args);
  3575. $blog_ID = (int) $args[0];
  3576. $username = $args[1];
  3577. $password = $args[2];
  3578. if ( isset( $args[3] ) )
  3579. $query = array( 'numberposts' => absint( $args[3] ) );
  3580. else
  3581. $query = array();
  3582. if ( !$user = $this->login($username, $password) )
  3583. return $this->error;
  3584. do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
  3585. $posts_list = wp_get_recent_posts( $query );
  3586. if ( !$posts_list )
  3587. return array();
  3588. foreach ($posts_list as $entry) {
  3589. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  3590. continue;
  3591. $post_date = $this->_convert_date( $entry['post_date'] );
  3592. $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  3593. $post_modified = $this->_convert_date( $entry['post_modified'] );
  3594. $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
  3595. $categories = array();
  3596. $catids = wp_get_post_categories($entry['ID']);
  3597. foreach( $catids as $catid )
  3598. $categories[] = get_cat_name($catid);
  3599. $tagnames = array();
  3600. $tags = wp_get_post_tags( $entry['ID'] );
  3601. if ( !empty( $tags ) ) {
  3602. foreach ( $tags as $tag ) {
  3603. $tagnames[] = $tag->name;
  3604. }
  3605. $tagnames = implode( ', ', $tagnames );
  3606. } else {
  3607. $tagnames = '';
  3608. }
  3609. $post = get_extended($entry['post_content']);
  3610. $link = post_permalink($entry['ID']);
  3611. // Get the post author info.
  3612. $author = get_userdata($entry['post_author']);
  3613. $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
  3614. $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
  3615. // Consider future posts as published
  3616. if ( $entry['post_status'] === 'future' )
  3617. $entry['post_status'] = 'publish';
  3618. // Get post format
  3619. $post_format = get_post_format( $entry['ID'] );
  3620. if ( empty( $post_format ) )
  3621. $post_format = 'standard';
  3622. $struct[] = array(
  3623. 'dateCreated' => $post_date,
  3624. 'userid' => $entry['post_author'],
  3625. 'postid' => (string) $entry['ID'],
  3626. 'description' => $post['main'],
  3627. 'title' => $entry['post_title'],
  3628. 'link' => $link,
  3629. 'permaLink' => $link,
  3630. // commented out because no other tool seems to use this
  3631. // 'content' => $entry['post_content'],
  3632. 'categories' => $categories,
  3633. 'mt_excerpt' => $entry['post_excerpt'],
  3634. 'mt_text_more' => $post['extended'],
  3635. 'wp_more_text' => $post['more_text'],
  3636. 'mt_allow_comments' => $allow_comments,
  3637. 'mt_allow_pings' => $allow_pings,
  3638. 'mt_keywords' => $tagnames,
  3639. 'wp_slug' => $entry['post_name'],
  3640. 'wp_password' => $entry['post_password'],
  3641. 'wp_author_id' => (string) $author->ID,
  3642. 'wp_author_display_name' => $author->display_name,
  3643. 'date_created_gmt' => $post_date_gmt,
  3644. 'post_status' => $entry['post_status'],
  3645. 'custom_fields' => $this->get_custom_fields($entry['ID']),
  3646. 'wp_post_format' => $post_format,
  3647. 'date_modified' => $post_modified,
  3648. 'date_modified_gmt' => $post_modified_gmt
  3649. );
  3650. $entry_index = count( $struct ) - 1;
  3651. $struct[ $entry_index ][ 'wp_post_thumbnail' ] = get_post_thumbnail_id( $entry['ID'] );
  3652. }
  3653. $recent_posts = array();
  3654. for ( $j=0; $j<count($struct); $j++ ) {
  3655. array_push($recent_posts, $struct[$j]);
  3656. }
  3657. return $recent_posts;
  3658. }
  3659. /**
  3660. * Retrieve the list of categories on a given blog.
  3661. *
  3662. * @since 1.5.0
  3663. *
  3664. * @param array $args Method parameters.
  3665. * @return array
  3666. */
  3667. function mw_getCategories($args) {
  3668. $this->escape($args);
  3669. $blog_ID = (int) $args[0];
  3670. $username = $args[1];
  3671. $password = $args[2];
  3672. if ( !$user = $this->login($username, $password) )
  3673. return $this->error;
  3674. if ( !current_user_can( 'edit_posts' ) )
  3675. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  3676. do_action('xmlrpc_call', 'metaWeblog.getCategories');
  3677. $categories_struct = array();
  3678. if ( $cats = get_categories(array('get' => 'all')) ) {
  3679. foreach ( $cats as $cat ) {
  3680. $struct['categoryId'] = $cat->term_id;
  3681. $struct['parentId'] = $cat->parent;
  3682. $struct['description'] = $cat->name;
  3683. $struct['categoryDescription'] = $cat->description;
  3684. $struct['categoryName'] = $cat->name;
  3685. $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
  3686. $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
  3687. $categories_struct[] = $struct;
  3688. }
  3689. }
  3690. return $categories_struct;
  3691. }
  3692. /**
  3693. * Uploads a file, following your settings.
  3694. *
  3695. * Adapted from a patch by Johann Richard.
  3696. *
  3697. * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
  3698. *
  3699. * @since 1.5.0
  3700. *
  3701. * @param array $args Method parameters.
  3702. * @return array
  3703. */
  3704. function mw_newMediaObject($args) {
  3705. global $wpdb;
  3706. $blog_ID = (int) $args[0];
  3707. $username = $wpdb->escape($args[1]);
  3708. $password = $wpdb->escape($args[2]);
  3709. $data = $args[3];
  3710. $name = sanitize_file_name( $data['name'] );
  3711. $type = $data['type'];
  3712. $bits = $data['bits'];
  3713. if ( !$user = $this->login($username, $password) )
  3714. return $this->error;
  3715. do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
  3716. if ( !current_user_can('upload_files') ) {
  3717. $this->error = new IXR_Error( 401, __( 'You do not have permission to upload files.' ) );
  3718. return $this->error;
  3719. }
  3720. if ( $upload_err = apply_filters( 'pre_upload_error', false ) )
  3721. return new IXR_Error(500, $upload_err);
  3722. if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
  3723. // Get postmeta info on the object.
  3724. $old_file = $wpdb->get_row("
  3725. SELECT ID
  3726. FROM {$wpdb->posts}
  3727. WHERE post_title = '{$name}'
  3728. AND post_type = 'attachment'
  3729. ");
  3730. // Delete previous file.
  3731. wp_delete_attachment($old_file->ID);
  3732. // Make sure the new name is different by pre-pending the
  3733. // previous post id.
  3734. $filename = preg_replace('/^wpid\d+-/', '', $name);
  3735. $name = "wpid{$old_file->ID}-{$filename}";
  3736. }
  3737. $upload = wp_upload_bits($name, null, $bits);
  3738. if ( ! empty($upload['error']) ) {
  3739. $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
  3740. return new IXR_Error(500, $errorString);
  3741. }
  3742. // Construct the attachment array
  3743. // attach to post_id 0
  3744. $post_id = 0;
  3745. $attachment = array(
  3746. 'post_title' => $name,
  3747. 'post_content' => '',
  3748. 'post_type' => 'attachment',
  3749. 'post_parent' => $post_id,
  3750. 'post_mime_type' => $type,
  3751. 'guid' => $upload[ 'url' ]
  3752. );
  3753. // Save the data
  3754. $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
  3755. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  3756. do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args );
  3757. $struct = array(
  3758. 'id' => strval( $id ),
  3759. 'file' => $name,
  3760. 'url' => $upload[ 'url' ],
  3761. 'type' => $type
  3762. );
  3763. return apply_filters( 'wp_handle_upload', $struct, 'upload' );
  3764. }
  3765. /* MovableType API functions
  3766. * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
  3767. */
  3768. /**
  3769. * Retrieve the post titles of recent posts.
  3770. *
  3771. * @since 1.5.0
  3772. *
  3773. * @param array $args Method parameters.
  3774. * @return array
  3775. */
  3776. function mt_getRecentPostTitles($args) {
  3777. $this->escape($args);
  3778. $blog_ID = (int) $args[0];
  3779. $username = $args[1];
  3780. $password = $args[2];
  3781. if ( isset( $args[3] ) )
  3782. $query = array( 'numberposts' => absint( $args[3] ) );
  3783. else
  3784. $query = array();
  3785. if ( !$user = $this->login($username, $password) )
  3786. return $this->error;
  3787. do_action('xmlrpc_call', 'mt.getRecentPostTitles');
  3788. $posts_list = wp_get_recent_posts( $query );
  3789. if ( !$posts_list ) {
  3790. $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  3791. return $this->error;
  3792. }
  3793. $struct = array();
  3794. foreach ($posts_list as $entry) {
  3795. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  3796. continue;
  3797. $post_date = $this->_convert_date( $entry['post_date'] );
  3798. $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  3799. $struct[] = array(
  3800. 'dateCreated' => $post_date,
  3801. 'userid' => $entry['post_author'],
  3802. 'postid' => (string) $entry['ID'],
  3803. 'title' => $entry['post_title'],
  3804. 'post_status' => $entry['post_status'],
  3805. 'date_created_gmt' => $post_date_gmt
  3806. );
  3807. }
  3808. $recent_posts = array();
  3809. for ( $j=0; $j<count($struct); $j++ ) {
  3810. array_push($recent_posts, $struct[$j]);
  3811. }
  3812. return $recent_posts;
  3813. }
  3814. /**
  3815. * Retrieve list of all categories on blog.
  3816. *
  3817. * @since 1.5.0
  3818. *
  3819. * @param array $args Method parameters.
  3820. * @return array
  3821. */
  3822. function mt_getCategoryList($args) {
  3823. $this->escape($args);
  3824. $blog_ID = (int) $args[0];
  3825. $username = $args[1];
  3826. $password = $args[2];
  3827. if ( !$user = $this->login($username, $password) )
  3828. return $this->error;
  3829. if ( !current_user_can( 'edit_posts' ) )
  3830. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  3831. do_action('xmlrpc_call', 'mt.getCategoryList');
  3832. $categories_struct = array();
  3833. if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
  3834. foreach ( $cats as $cat ) {
  3835. $struct['categoryId'] = $cat->term_id;
  3836. $struct['categoryName'] = $cat->name;
  3837. $categories_struct[] = $struct;
  3838. }
  3839. }
  3840. return $categories_struct;
  3841. }
  3842. /**
  3843. * Retrieve post categories.
  3844. *
  3845. * @since 1.5.0
  3846. *
  3847. * @param array $args Method parameters.
  3848. * @return array
  3849. */
  3850. function mt_getPostCategories($args) {
  3851. $this->escape($args);
  3852. $post_ID = (int) $args[0];
  3853. $username = $args[1];
  3854. $password = $args[2];
  3855. if ( !$user = $this->login($username, $password) )
  3856. return $this->error;
  3857. if ( ! get_post( $post_ID ) )
  3858. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3859. if ( !current_user_can( 'edit_post', $post_ID ) )
  3860. return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
  3861. do_action('xmlrpc_call', 'mt.getPostCategories');
  3862. $categories = array();
  3863. $catids = wp_get_post_categories(intval($post_ID));
  3864. // first listed category will be the primary category
  3865. $isPrimary = true;
  3866. foreach ( $catids as $catid ) {
  3867. $categories[] = array(
  3868. 'categoryName' => get_cat_name($catid),
  3869. 'categoryId' => (string) $catid,
  3870. 'isPrimary' => $isPrimary
  3871. );
  3872. $isPrimary = false;
  3873. }
  3874. return $categories;
  3875. }
  3876. /**
  3877. * Sets categories for a post.
  3878. *
  3879. * @since 1.5.0
  3880. *
  3881. * @param array $args Method parameters.
  3882. * @return bool True on success.
  3883. */
  3884. function mt_setPostCategories($args) {
  3885. $this->escape($args);
  3886. $post_ID = (int) $args[0];
  3887. $username = $args[1];
  3888. $password = $args[2];
  3889. $categories = $args[3];
  3890. if ( !$user = $this->login($username, $password) )
  3891. return $this->error;
  3892. do_action('xmlrpc_call', 'mt.setPostCategories');
  3893. if ( ! get_post( $post_ID ) )
  3894. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3895. if ( !current_user_can('edit_post', $post_ID) )
  3896. return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
  3897. foreach ( $categories as $cat ) {
  3898. $catids[] = $cat['categoryId'];
  3899. }
  3900. wp_set_post_categories($post_ID, $catids);
  3901. return true;
  3902. }
  3903. /**
  3904. * Retrieve an array of methods supported by this server.
  3905. *
  3906. * @since 1.5.0
  3907. *
  3908. * @param array $args Method parameters.
  3909. * @return array
  3910. */
  3911. function mt_supportedMethods($args) {
  3912. do_action('xmlrpc_call', 'mt.supportedMethods');
  3913. $supported_methods = array();
  3914. foreach ( $this->methods as $key => $value ) {
  3915. $supported_methods[] = $key;
  3916. }
  3917. return $supported_methods;
  3918. }
  3919. /**
  3920. * Retrieve an empty array because we don't support per-post text filters.
  3921. *
  3922. * @since 1.5.0
  3923. *
  3924. * @param array $args Method parameters.
  3925. */
  3926. function mt_supportedTextFilters($args) {
  3927. do_action('xmlrpc_call', 'mt.supportedTextFilters');
  3928. return apply_filters('xmlrpc_text_filters', array());
  3929. }
  3930. /**
  3931. * Retrieve trackbacks sent to a given post.
  3932. *
  3933. * @since 1.5.0
  3934. *
  3935. * @param array $args Method parameters.
  3936. * @return mixed
  3937. */
  3938. function mt_getTrackbackPings($args) {
  3939. global $wpdb;
  3940. $post_ID = intval($args);
  3941. do_action('xmlrpc_call', 'mt.getTrackbackPings');
  3942. $actual_post = wp_get_single_post($post_ID, ARRAY_A);
  3943. if ( !$actual_post )
  3944. return new IXR_Error(404, __('Sorry, no such post.'));
  3945. $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
  3946. if ( !$comments )
  3947. return array();
  3948. $trackback_pings = array();
  3949. foreach ( $comments as $comment ) {
  3950. if ( 'trackback' == $comment->comment_type ) {
  3951. $content = $comment->comment_content;
  3952. $title = substr($content, 8, (strpos($content, '</strong>') - 8));
  3953. $trackback_pings[] = array(
  3954. 'pingTitle' => $title,
  3955. 'pingURL' => $comment->comment_author_url,
  3956. 'pingIP' => $comment->comment_author_IP
  3957. );
  3958. }
  3959. }
  3960. return $trackback_pings;
  3961. }
  3962. /**
  3963. * Sets a post's publish status to 'publish'.
  3964. *
  3965. * @since 1.5.0
  3966. *
  3967. * @param array $args Method parameters.
  3968. * @return int
  3969. */
  3970. function mt_publishPost($args) {
  3971. $this->escape($args);
  3972. $post_ID = (int) $args[0];
  3973. $username = $args[1];
  3974. $password = $args[2];
  3975. if ( !$user = $this->login($username, $password) )
  3976. return $this->error;
  3977. do_action('xmlrpc_call', 'mt.publishPost');
  3978. $postdata = wp_get_single_post($post_ID, ARRAY_A);
  3979. if ( ! $postdata )
  3980. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3981. if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
  3982. return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
  3983. $postdata['post_status'] = 'publish';
  3984. // retain old cats
  3985. $cats = wp_get_post_categories($post_ID);
  3986. $postdata['post_category'] = $cats;
  3987. $this->escape($postdata);
  3988. $result = wp_update_post($postdata);
  3989. return $result;
  3990. }
  3991. /* PingBack functions
  3992. * specs on www.hixie.ch/specs/pingback/pingback
  3993. */
  3994. /**
  3995. * Retrieves a pingback and registers it.
  3996. *
  3997. * @since 1.5.0
  3998. *
  3999. * @param array $args Method parameters.
  4000. * @return array
  4001. */
  4002. function pingback_ping($args) {
  4003. global $wpdb;
  4004. do_action('xmlrpc_call', 'pingback.ping');
  4005. $this->escape($args);
  4006. $pagelinkedfrom = $args[0];
  4007. $pagelinkedto = $args[1];
  4008. $title = '';
  4009. $pagelinkedfrom = str_replace('&amp;', '&', $pagelinkedfrom);
  4010. $pagelinkedto = str_replace('&amp;', '&', $pagelinkedto);
  4011. $pagelinkedto = str_replace('&', '&amp;', $pagelinkedto);
  4012. // Check if the page linked to is in our site
  4013. $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
  4014. if ( !$pos1 )
  4015. return new IXR_Error(0, __('Is there no link to us?'));
  4016. // let's find which post is linked to
  4017. // FIXME: does url_to_postid() cover all these cases already?
  4018. // if so, then let's use it and drop the old code.
  4019. $urltest = parse_url($pagelinkedto);
  4020. if ( $post_ID = url_to_postid($pagelinkedto) ) {
  4021. $way = 'url_to_postid()';
  4022. } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
  4023. // the path defines the post_ID (archives/p/XXXX)
  4024. $blah = explode('/', $match[0]);
  4025. $post_ID = (int) $blah[1];
  4026. $way = 'from the path';
  4027. } elseif ( preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
  4028. // the querystring defines the post_ID (?p=XXXX)
  4029. $blah = explode('=', $match[0]);
  4030. $post_ID = (int) $blah[1];
  4031. $way = 'from the querystring';
  4032. } elseif ( isset($urltest['fragment']) ) {
  4033. // an #anchor is there, it's either...
  4034. if ( intval($urltest['fragment']) ) {
  4035. // ...an integer #XXXX (simplest case)
  4036. $post_ID = (int) $urltest['fragment'];
  4037. $way = 'from the fragment (numeric)';
  4038. } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
  4039. // ...a post id in the form 'post-###'
  4040. $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
  4041. $way = 'from the fragment (post-###)';
  4042. } elseif ( is_string($urltest['fragment']) ) {
  4043. // ...or a string #title, a little more complicated
  4044. $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
  4045. $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
  4046. if (! ($post_ID = $wpdb->get_var($sql)) ) {
  4047. // returning unknown error '0' is better than die()ing
  4048. return new IXR_Error(0, '');
  4049. }
  4050. $way = 'from the fragment (title)';
  4051. }
  4052. } else {
  4053. // TODO: Attempt to extract a post ID from the given URL
  4054. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  4055. }
  4056. $post_ID = (int) $post_ID;
  4057. $post = get_post($post_ID);
  4058. if ( !$post ) // Post_ID not found
  4059. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  4060. if ( $post_ID == url_to_postid($pagelinkedfrom) )
  4061. return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
  4062. // Check if pings are on
  4063. if ( !pings_open($post) )
  4064. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  4065. // Let's check that the remote site didn't already pingback this entry
  4066. if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
  4067. return new IXR_Error( 48, __( 'The pingback has already been registered.' ) );
  4068. // very stupid, but gives time to the 'from' server to publish !
  4069. sleep(1);
  4070. // Let's check the remote site
  4071. $linea = wp_remote_fopen( $pagelinkedfrom );
  4072. if ( !$linea )
  4073. return new IXR_Error(16, __('The source URL does not exist.'));
  4074. $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
  4075. // Work around bug in strip_tags():
  4076. $linea = str_replace('<!DOC', '<DOC', $linea);
  4077. $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
  4078. $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
  4079. preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
  4080. $title = $matchtitle[1];
  4081. if ( empty( $title ) )
  4082. return new IXR_Error(32, __('We cannot find a title on that page.'));
  4083. $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
  4084. $p = explode( "\n\n", $linea );
  4085. $preg_target = preg_quote($pagelinkedto, '|');
  4086. foreach ( $p as $para ) {
  4087. if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
  4088. preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
  4089. // If the URL isn't in a link context, keep looking
  4090. if ( empty($context) )
  4091. continue;
  4092. // We're going to use this fake tag to mark the context in a bit
  4093. // the marker is needed in case the link text appears more than once in the paragraph
  4094. $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
  4095. // prevent really long link text
  4096. if ( strlen($context[1]) > 100 )
  4097. $context[1] = substr($context[1], 0, 100) . '...';
  4098. $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
  4099. $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
  4100. $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
  4101. $excerpt = trim($excerpt);
  4102. $preg_marker = preg_quote($marker, '|');
  4103. $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
  4104. $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
  4105. break;
  4106. }
  4107. }
  4108. if ( empty($context) ) // Link to target not found
  4109. return new IXR_Error(17, __('The source URL does not contain a link to the target URL, and so cannot be used as a source.'));
  4110. $pagelinkedfrom = str_replace('&', '&amp;', $pagelinkedfrom);
  4111. $context = '[...] ' . esc_html( $excerpt ) . ' [...]';
  4112. $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
  4113. $comment_post_ID = (int) $post_ID;
  4114. $comment_author = $title;
  4115. $comment_author_email = '';
  4116. $this->escape($comment_author);
  4117. $comment_author_url = $pagelinkedfrom;
  4118. $comment_content = $context;
  4119. $this->escape($comment_content);
  4120. $comment_type = 'pingback';
  4121. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
  4122. $comment_ID = wp_new_comment($commentdata);
  4123. do_action('pingback_post', $comment_ID);
  4124. return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
  4125. }
  4126. /**
  4127. * Retrieve array of URLs that pingbacked the given URL.
  4128. *
  4129. * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
  4130. *
  4131. * @since 1.5.0
  4132. *
  4133. * @param array $args Method parameters.
  4134. * @return array
  4135. */
  4136. function pingback_extensions_getPingbacks($args) {
  4137. global $wpdb;
  4138. do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
  4139. $this->escape($args);
  4140. $url = $args;
  4141. $post_ID = url_to_postid($url);
  4142. if ( !$post_ID ) {
  4143. // We aren't sure that the resource is available and/or pingback enabled
  4144. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  4145. }
  4146. $actual_post = wp_get_single_post($post_ID, ARRAY_A);
  4147. if ( !$actual_post ) {
  4148. // No such post = resource not found
  4149. return new IXR_Error(32, __('The specified target URL does not exist.'));
  4150. }
  4151. $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
  4152. if ( !$comments )
  4153. return array();
  4154. $pingbacks = array();
  4155. foreach ( $comments as $comment ) {
  4156. if ( 'pingback' == $comment->comment_type )
  4157. $pingbacks[] = $comment->comment_author_url;
  4158. }
  4159. return $pingbacks;
  4160. }
  4161. }