PageRenderTime 83ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

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