PageRenderTime 85ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

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