PageRenderTime 73ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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