PageRenderTime 69ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/squinlan/twolittlebirdsbakery
PHP | 3616 lines | 2159 code | 659 blank | 798 comment | 417 complexity | a874f84709dac86e97ad6429ee47de91 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.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. * Since WordPress 2.6.0, WordPress XMLRPC server can be disabled in the
  15. * administration panels.
  16. *
  17. * @package WordPress
  18. * @subpackage Publishing
  19. * @since 1.5.0
  20. */
  21. class wp_xmlrpc_server extends IXR_Server {
  22. /**
  23. * Register all of the XMLRPC methods that XMLRPC server understands.
  24. *
  25. * Sets up server and method property. Passes XMLRPC
  26. * methods through the 'xmlrpc_methods' filter to allow plugins to extend
  27. * or replace XMLRPC methods.
  28. *
  29. * @since 1.5.0
  30. *
  31. * @return wp_xmlrpc_server
  32. */
  33. function __construct() {
  34. $this->methods = array(
  35. // WordPress API
  36. 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
  37. 'wp.getPage' => 'this:wp_getPage',
  38. 'wp.getPages' => 'this:wp_getPages',
  39. 'wp.newPage' => 'this:wp_newPage',
  40. 'wp.deletePage' => 'this:wp_deletePage',
  41. 'wp.editPage' => 'this:wp_editPage',
  42. 'wp.getPageList' => 'this:wp_getPageList',
  43. 'wp.getAuthors' => 'this:wp_getAuthors',
  44. 'wp.getCategories' => 'this:mw_getCategories', // Alias
  45. 'wp.getTags' => 'this:wp_getTags',
  46. 'wp.newCategory' => 'this:wp_newCategory',
  47. 'wp.deleteCategory' => 'this:wp_deleteCategory',
  48. 'wp.suggestCategories' => 'this:wp_suggestCategories',
  49. 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias
  50. 'wp.getCommentCount' => 'this:wp_getCommentCount',
  51. 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
  52. 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
  53. 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
  54. 'wp.getOptions' => 'this:wp_getOptions',
  55. 'wp.setOptions' => 'this:wp_setOptions',
  56. 'wp.getComment' => 'this:wp_getComment',
  57. 'wp.getComments' => 'this:wp_getComments',
  58. 'wp.deleteComment' => 'this:wp_deleteComment',
  59. 'wp.editComment' => 'this:wp_editComment',
  60. 'wp.newComment' => 'this:wp_newComment',
  61. 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
  62. 'wp.getMediaItem' => 'this:wp_getMediaItem',
  63. 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
  64. 'wp.getPostFormats' => 'this:wp_getPostFormats',
  65. // Blogger API
  66. 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  67. 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
  68. 'blogger.getPost' => 'this:blogger_getPost',
  69. 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
  70. 'blogger.getTemplate' => 'this:blogger_getTemplate',
  71. 'blogger.setTemplate' => 'this:blogger_setTemplate',
  72. 'blogger.newPost' => 'this:blogger_newPost',
  73. 'blogger.editPost' => 'this:blogger_editPost',
  74. 'blogger.deletePost' => 'this:blogger_deletePost',
  75. // MetaWeblog API (with MT extensions to structs)
  76. 'metaWeblog.newPost' => 'this:mw_newPost',
  77. 'metaWeblog.editPost' => 'this:mw_editPost',
  78. 'metaWeblog.getPost' => 'this:mw_getPost',
  79. 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
  80. 'metaWeblog.getCategories' => 'this:mw_getCategories',
  81. 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
  82. // MetaWeblog API aliases for Blogger API
  83. // see http://www.xmlrpc.com/stories/storyReader$2460
  84. 'metaWeblog.deletePost' => 'this:blogger_deletePost',
  85. 'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
  86. 'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
  87. 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  88. // MovableType API
  89. 'mt.getCategoryList' => 'this:mt_getCategoryList',
  90. 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
  91. 'mt.getPostCategories' => 'this:mt_getPostCategories',
  92. 'mt.setPostCategories' => 'this:mt_setPostCategories',
  93. 'mt.supportedMethods' => 'this:mt_supportedMethods',
  94. 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
  95. 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
  96. 'mt.publishPost' => 'this:mt_publishPost',
  97. // PingBack
  98. 'pingback.ping' => 'this:pingback_ping',
  99. 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
  100. 'demo.sayHello' => 'this:sayHello',
  101. 'demo.addTwoNumbers' => 'this:addTwoNumbers'
  102. );
  103. $this->initialise_blog_option_info( );
  104. $this->methods = apply_filters('xmlrpc_methods', $this->methods);
  105. }
  106. function serve_request() {
  107. $this->IXR_Server($this->methods);
  108. }
  109. /**
  110. * Test XMLRPC API by saying, "Hello!" to client.
  111. *
  112. * @since 1.5.0
  113. *
  114. * @param array $args Method Parameters.
  115. * @return string
  116. */
  117. function sayHello($args) {
  118. return 'Hello!';
  119. }
  120. /**
  121. * Test XMLRPC API by adding two numbers for client.
  122. *
  123. * @since 1.5.0
  124. *
  125. * @param array $args Method Parameters.
  126. * @return int
  127. */
  128. function addTwoNumbers($args) {
  129. $number1 = $args[0];
  130. $number2 = $args[1];
  131. return $number1 + $number2;
  132. }
  133. /**
  134. * Check user's credentials.
  135. *
  136. * @since 1.5.0
  137. *
  138. * @param string $user_login User's username.
  139. * @param string $user_pass User's password.
  140. * @return bool Whether authentication passed.
  141. * @deprecated use wp_xmlrpc_server::login
  142. * @see wp_xmlrpc_server::login
  143. */
  144. function login_pass_ok($user_login, $user_pass) {
  145. if ( !get_option( 'enable_xmlrpc' ) ) {
  146. $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
  147. return false;
  148. }
  149. if (!user_pass_ok($user_login, $user_pass)) {
  150. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Log user in.
  157. *
  158. * @since 2.8
  159. *
  160. * @param string $username User's username.
  161. * @param string $password User's password.
  162. * @return mixed WP_User object if authentication passed, false otherwise
  163. */
  164. function login($username, $password) {
  165. if ( !get_option( 'enable_xmlrpc' ) ) {
  166. $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site. An admin user can enable them at %s'), admin_url('options-writing.php') ) );
  167. return false;
  168. }
  169. $user = wp_authenticate($username, $password);
  170. if (is_wp_error($user)) {
  171. $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  172. return false;
  173. }
  174. wp_set_current_user( $user->ID );
  175. return $user;
  176. }
  177. /**
  178. * Sanitize string or array of strings for database.
  179. *
  180. * @since 1.5.2
  181. *
  182. * @param string|array $array Sanitize single string or array of strings.
  183. * @return string|array Type matches $array and sanitized for the database.
  184. */
  185. function escape(&$array) {
  186. global $wpdb;
  187. if (!is_array($array)) {
  188. return($wpdb->escape($array));
  189. } else {
  190. foreach ( (array) $array as $k => $v ) {
  191. if ( is_array($v) ) {
  192. $this->escape($array[$k]);
  193. } else if ( is_object($v) ) {
  194. //skip
  195. } else {
  196. $array[$k] = $wpdb->escape($v);
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * Retrieve custom fields for post.
  203. *
  204. * @since 2.5.0
  205. *
  206. * @param int $post_id Post ID.
  207. * @return array Custom fields, if exist.
  208. */
  209. function get_custom_fields($post_id) {
  210. $post_id = (int) $post_id;
  211. $custom_fields = array();
  212. foreach ( (array) has_meta($post_id) as $meta ) {
  213. // Don't expose protected fields.
  214. if ( ! current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
  215. continue;
  216. $custom_fields[] = array(
  217. "id" => $meta['meta_id'],
  218. "key" => $meta['meta_key'],
  219. "value" => $meta['meta_value']
  220. );
  221. }
  222. return $custom_fields;
  223. }
  224. /**
  225. * Set custom fields for post.
  226. *
  227. * @since 2.5.0
  228. *
  229. * @param int $post_id Post ID.
  230. * @param array $fields Custom fields.
  231. */
  232. function set_custom_fields($post_id, $fields) {
  233. $post_id = (int) $post_id;
  234. foreach ( (array) $fields as $meta ) {
  235. if ( isset($meta['id']) ) {
  236. $meta['id'] = (int) $meta['id'];
  237. $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
  238. $meta['value'] = stripslashes_deep( $meta['value'] );
  239. if ( isset($meta['key']) ) {
  240. $meta['key'] = stripslashes( $meta['key'] );
  241. if ( $meta['key'] != $pmeta->meta_key )
  242. continue;
  243. if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) )
  244. update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
  245. } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
  246. delete_metadata_by_mid( 'post', $meta['id'] );
  247. }
  248. } elseif ( current_user_can( 'add_post_meta', $post_id, stripslashes( $meta['key'] ) ) ) {
  249. add_post_meta( $post_id, $meta['key'], $meta['value'] );
  250. }
  251. }
  252. }
  253. /**
  254. * Set up blog options property.
  255. *
  256. * Passes property through 'xmlrpc_blog_options' filter.
  257. *
  258. * @since 2.6.0
  259. */
  260. function initialise_blog_option_info( ) {
  261. global $wp_version;
  262. $this->blog_options = array(
  263. // Read only options
  264. 'software_name' => array(
  265. 'desc' => __( 'Software Name' ),
  266. 'readonly' => true,
  267. 'value' => 'WordPress'
  268. ),
  269. 'software_version' => array(
  270. 'desc' => __( 'Software Version' ),
  271. 'readonly' => true,
  272. 'value' => $wp_version
  273. ),
  274. 'blog_url' => array(
  275. 'desc' => __( 'Site URL' ),
  276. 'readonly' => true,
  277. 'option' => 'siteurl'
  278. ),
  279. // Updatable options
  280. 'time_zone' => array(
  281. 'desc' => __( 'Time Zone' ),
  282. 'readonly' => false,
  283. 'option' => 'gmt_offset'
  284. ),
  285. 'blog_title' => array(
  286. 'desc' => __( 'Site Title' ),
  287. 'readonly' => false,
  288. 'option' => 'blogname'
  289. ),
  290. 'blog_tagline' => array(
  291. 'desc' => __( 'Site Tagline' ),
  292. 'readonly' => false,
  293. 'option' => 'blogdescription'
  294. ),
  295. 'date_format' => array(
  296. 'desc' => __( 'Date Format' ),
  297. 'readonly' => false,
  298. 'option' => 'date_format'
  299. ),
  300. 'time_format' => array(
  301. 'desc' => __( 'Time Format' ),
  302. 'readonly' => false,
  303. 'option' => 'time_format'
  304. ),
  305. 'users_can_register' => array(
  306. 'desc' => __( 'Allow new users to sign up' ),
  307. 'readonly' => false,
  308. 'option' => 'users_can_register'
  309. ),
  310. 'thumbnail_size_w' => array(
  311. 'desc' => __( 'Thumbnail Width' ),
  312. 'readonly' => false,
  313. 'option' => 'thumbnail_size_w'
  314. ),
  315. 'thumbnail_size_h' => array(
  316. 'desc' => __( 'Thumbnail Height' ),
  317. 'readonly' => false,
  318. 'option' => 'thumbnail_size_h'
  319. ),
  320. 'thumbnail_crop' => array(
  321. 'desc' => __( 'Crop thumbnail to exact dimensions' ),
  322. 'readonly' => false,
  323. 'option' => 'thumbnail_crop'
  324. ),
  325. 'medium_size_w' => array(
  326. 'desc' => __( 'Medium size image width' ),
  327. 'readonly' => false,
  328. 'option' => 'medium_size_w'
  329. ),
  330. 'medium_size_h' => array(
  331. 'desc' => __( 'Medium size image height' ),
  332. 'readonly' => false,
  333. 'option' => 'medium_size_h'
  334. ),
  335. 'large_size_w' => array(
  336. 'desc' => __( 'Large size image width' ),
  337. 'readonly' => false,
  338. 'option' => 'large_size_w'
  339. ),
  340. 'large_size_h' => array(
  341. 'desc' => __( 'Large size image height' ),
  342. 'readonly' => false,
  343. 'option' => 'large_size_h'
  344. )
  345. );
  346. $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
  347. }
  348. /**
  349. * Retrieve the blogs of the user.
  350. *
  351. * @since 2.6.0
  352. *
  353. * @param array $args Method parameters. Contains:
  354. * - username
  355. * - password
  356. * @return array. Contains:
  357. * - 'isAdmin'
  358. * - 'url'
  359. * - 'blogid'
  360. * - 'blogName'
  361. * - 'xmlrpc' - url of xmlrpc endpoint
  362. */
  363. function wp_getUsersBlogs( $args ) {
  364. global $current_site;
  365. // If this isn't on WPMU then just use blogger_getUsersBlogs
  366. if ( !is_multisite() ) {
  367. array_unshift( $args, 1 );
  368. return $this->blogger_getUsersBlogs( $args );
  369. }
  370. $this->escape( $args );
  371. $username = $args[0];
  372. $password = $args[1];
  373. if ( !$user = $this->login($username, $password) )
  374. return $this->error;
  375. do_action( 'xmlrpc_call', 'wp.getUsersBlogs' );
  376. $blogs = (array) get_blogs_of_user( $user->ID );
  377. $struct = array( );
  378. foreach ( $blogs as $blog ) {
  379. // Don't include blogs that aren't hosted at this site
  380. if ( $blog->site_id != $current_site->id )
  381. continue;
  382. $blog_id = $blog->userblog_id;
  383. switch_to_blog($blog_id);
  384. $is_admin = current_user_can('manage_options');
  385. $struct[] = array(
  386. 'isAdmin' => $is_admin,
  387. 'url' => get_option( 'home' ) . '/',
  388. 'blogid' => (string) $blog_id,
  389. 'blogName' => get_option( 'blogname' ),
  390. 'xmlrpc' => site_url( 'xmlrpc.php' )
  391. );
  392. restore_current_blog( );
  393. }
  394. return $struct;
  395. }
  396. /**
  397. * Retrieve page.
  398. *
  399. * @since 2.2.0
  400. *
  401. * @param array $args Method parameters. Contains:
  402. * - blog_id
  403. * - page_id
  404. * - username
  405. * - password
  406. * @return array
  407. */
  408. function wp_getPage($args) {
  409. $this->escape($args);
  410. $blog_id = (int) $args[0];
  411. $page_id = (int) $args[1];
  412. $username = $args[2];
  413. $password = $args[3];
  414. if ( !$user = $this->login($username, $password) ) {
  415. return $this->error;
  416. }
  417. if ( !current_user_can( 'edit_page', $page_id ) )
  418. return new IXR_Error( 401, __( 'Sorry, you cannot edit this page.' ) );
  419. do_action('xmlrpc_call', 'wp.getPage');
  420. // Lookup page info.
  421. $page = get_page($page_id);
  422. // If we found the page then format the data.
  423. if ( $page->ID && ($page->post_type == 'page') ) {
  424. // Get all of the page content and link.
  425. $full_page = get_extended($page->post_content);
  426. $link = post_permalink($page->ID);
  427. // Get info the page parent if there is one.
  428. $parent_title = "";
  429. if ( !empty($page->post_parent) ) {
  430. $parent = get_page($page->post_parent);
  431. $parent_title = $parent->post_title;
  432. }
  433. // Determine comment and ping settings.
  434. $allow_comments = comments_open($page->ID) ? 1 : 0;
  435. $allow_pings = pings_open($page->ID) ? 1 : 0;
  436. // Format page date.
  437. $page_date = mysql2date('Ymd\TH:i:s', $page->post_date, false);
  438. $page_date_gmt = mysql2date('Ymd\TH:i:s', $page->post_date_gmt, false);
  439. // For drafts use the GMT version of the date
  440. if ( $page->post_status == 'draft' )
  441. $page_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page->post_date ), 'Ymd\TH:i:s' );
  442. // Pull the categories info together.
  443. $categories = array();
  444. foreach ( wp_get_post_categories($page->ID) as $cat_id ) {
  445. $categories[] = get_cat_name($cat_id);
  446. }
  447. // Get the author info.
  448. $author = get_userdata($page->post_author);
  449. $page_template = get_post_meta( $page->ID, '_wp_page_template', true );
  450. if ( empty( $page_template ) )
  451. $page_template = 'default';
  452. $page_struct = array(
  453. 'dateCreated' => new IXR_Date($page_date),
  454. 'userid' => $page->post_author,
  455. 'page_id' => $page->ID,
  456. 'page_status' => $page->post_status,
  457. 'description' => $full_page['main'],
  458. 'title' => $page->post_title,
  459. 'link' => $link,
  460. 'permaLink' => $link,
  461. 'categories' => $categories,
  462. 'excerpt' => $page->post_excerpt,
  463. 'text_more' => $full_page['extended'],
  464. 'mt_allow_comments' => $allow_comments,
  465. 'mt_allow_pings' => $allow_pings,
  466. 'wp_slug' => $page->post_name,
  467. 'wp_password' => $page->post_password,
  468. 'wp_author' => $author->display_name,
  469. 'wp_page_parent_id' => $page->post_parent,
  470. 'wp_page_parent_title' => $parent_title,
  471. 'wp_page_order' => $page->menu_order,
  472. 'wp_author_id' => (string) $author->ID,
  473. 'wp_author_display_name' => $author->display_name,
  474. 'date_created_gmt' => new IXR_Date($page_date_gmt),
  475. 'custom_fields' => $this->get_custom_fields($page_id),
  476. 'wp_page_template' => $page_template
  477. );
  478. return($page_struct);
  479. }
  480. // If the page doesn't exist indicate that.
  481. else {
  482. return(new IXR_Error(404, __('Sorry, no such page.')));
  483. }
  484. }
  485. /**
  486. * Retrieve Pages.
  487. *
  488. * @since 2.2.0
  489. *
  490. * @param array $args Method parameters. Contains:
  491. * - blog_id
  492. * - username
  493. * - password
  494. * - num_pages
  495. * @return array
  496. */
  497. function wp_getPages($args) {
  498. $this->escape($args);
  499. $blog_id = (int) $args[0];
  500. $username = $args[1];
  501. $password = $args[2];
  502. $num_pages = isset($args[3]) ? (int) $args[3] : 10;
  503. if ( !$user = $this->login($username, $password) )
  504. return $this->error;
  505. if ( !current_user_can( 'edit_pages' ) )
  506. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  507. do_action('xmlrpc_call', 'wp.getPages');
  508. $pages = get_posts( array('post_type' => 'page', 'post_status' => 'any', 'numberposts' => $num_pages) );
  509. $num_pages = count($pages);
  510. // If we have pages, put together their info.
  511. if ( $num_pages >= 1 ) {
  512. $pages_struct = array();
  513. for ( $i = 0; $i < $num_pages; $i++ ) {
  514. $page = wp_xmlrpc_server::wp_getPage(array(
  515. $blog_id, $pages[$i]->ID, $username, $password
  516. ));
  517. $pages_struct[] = $page;
  518. }
  519. return($pages_struct);
  520. }
  521. // If no pages were found return an error.
  522. else {
  523. return(array());
  524. }
  525. }
  526. /**
  527. * Create new page.
  528. *
  529. * @since 2.2.0
  530. *
  531. * @param array $args Method parameters. See {@link wp_xmlrpc_server::mw_newPost()}
  532. * @return unknown
  533. */
  534. function wp_newPage($args) {
  535. // Items not escaped here will be escaped in newPost.
  536. $username = $this->escape($args[1]);
  537. $password = $this->escape($args[2]);
  538. $page = $args[3];
  539. $publish = $args[4];
  540. if ( !$user = $this->login($username, $password) )
  541. return $this->error;
  542. do_action('xmlrpc_call', 'wp.newPage');
  543. // Make sure the user is allowed to add new pages.
  544. if ( !current_user_can('publish_pages') )
  545. return(new IXR_Error(401, __('Sorry, you cannot add new pages.')));
  546. // Mark this as content for a page.
  547. $args[3]["post_type"] = 'page';
  548. // Let mw_newPost do all of the heavy lifting.
  549. return($this->mw_newPost($args));
  550. }
  551. /**
  552. * Delete page.
  553. *
  554. * @since 2.2.0
  555. *
  556. * @param array $args Method parameters.
  557. * @return bool True, if success.
  558. */
  559. function wp_deletePage($args) {
  560. $this->escape($args);
  561. $blog_id = (int) $args[0];
  562. $username = $args[1];
  563. $password = $args[2];
  564. $page_id = (int) $args[3];
  565. if ( !$user = $this->login($username, $password) )
  566. return $this->error;
  567. do_action('xmlrpc_call', 'wp.deletePage');
  568. // Get the current page based on the page_id and
  569. // make sure it is a page and not a post.
  570. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  571. if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  572. return(new IXR_Error(404, __('Sorry, no such page.')));
  573. // Make sure the user can delete pages.
  574. if ( !current_user_can('delete_page', $page_id) )
  575. return(new IXR_Error(401, __('Sorry, you do not have the right to delete this page.')));
  576. // Attempt to delete the page.
  577. $result = wp_delete_post($page_id);
  578. if ( !$result )
  579. return(new IXR_Error(500, __('Failed to delete the page.')));
  580. return(true);
  581. }
  582. /**
  583. * Edit page.
  584. *
  585. * @since 2.2.0
  586. *
  587. * @param array $args Method parameters.
  588. * @return unknown
  589. */
  590. function wp_editPage($args) {
  591. // Items not escaped here will be escaped in editPost.
  592. $blog_id = (int) $args[0];
  593. $page_id = (int) $this->escape($args[1]);
  594. $username = $this->escape($args[2]);
  595. $password = $this->escape($args[3]);
  596. $content = $args[4];
  597. $publish = $args[5];
  598. if ( !$user = $this->login($username, $password) )
  599. return $this->error;
  600. do_action('xmlrpc_call', 'wp.editPage');
  601. // Get the page data and make sure it is a page.
  602. $actual_page = wp_get_single_post($page_id, ARRAY_A);
  603. if ( !$actual_page || ($actual_page['post_type'] != 'page') )
  604. return(new IXR_Error(404, __('Sorry, no such page.')));
  605. // Make sure the user is allowed to edit pages.
  606. if ( !current_user_can('edit_page', $page_id) )
  607. return(new IXR_Error(401, __('Sorry, you do not have the right to edit this page.')));
  608. // Mark this as content for a page.
  609. $content['post_type'] = 'page';
  610. // Arrange args in the way mw_editPost understands.
  611. $args = array(
  612. $page_id,
  613. $username,
  614. $password,
  615. $content,
  616. $publish
  617. );
  618. // Let mw_editPost do all of the heavy lifting.
  619. return($this->mw_editPost($args));
  620. }
  621. /**
  622. * Retrieve page list.
  623. *
  624. * @since 2.2.0
  625. *
  626. * @param array $args Method parameters.
  627. * @return unknown
  628. */
  629. function wp_getPageList($args) {
  630. global $wpdb;
  631. $this->escape($args);
  632. $blog_id = (int) $args[0];
  633. $username = $args[1];
  634. $password = $args[2];
  635. if ( !$user = $this->login($username, $password) )
  636. return $this->error;
  637. if ( !current_user_can( 'edit_pages' ) )
  638. return new IXR_Error( 401, __( 'Sorry, you cannot edit pages.' ) );
  639. do_action('xmlrpc_call', 'wp.getPageList');
  640. // Get list of pages ids and titles
  641. $page_list = $wpdb->get_results("
  642. SELECT ID page_id,
  643. post_title page_title,
  644. post_parent page_parent_id,
  645. post_date_gmt,
  646. post_date,
  647. post_status
  648. FROM {$wpdb->posts}
  649. WHERE post_type = 'page'
  650. ORDER BY ID
  651. ");
  652. // The date needs to be formatted properly.
  653. $num_pages = count($page_list);
  654. for ( $i = 0; $i < $num_pages; $i++ ) {
  655. $post_date = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date, false);
  656. $post_date_gmt = mysql2date('Ymd\TH:i:s', $page_list[$i]->post_date_gmt, false);
  657. $page_list[$i]->dateCreated = new IXR_Date($post_date);
  658. $page_list[$i]->date_created_gmt = new IXR_Date($post_date_gmt);
  659. // For drafts use the GMT version of the date
  660. if ( $page_list[$i]->post_status == 'draft' ) {
  661. $page_list[$i]->date_created_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $page_list[$i]->post_date ), 'Ymd\TH:i:s' );
  662. $page_list[$i]->date_created_gmt = new IXR_Date( $page_list[$i]->date_created_gmt );
  663. }
  664. unset($page_list[$i]->post_date_gmt);
  665. unset($page_list[$i]->post_date);
  666. unset($page_list[$i]->post_status);
  667. }
  668. return($page_list);
  669. }
  670. /**
  671. * Retrieve authors list.
  672. *
  673. * @since 2.2.0
  674. *
  675. * @param array $args Method parameters.
  676. * @return array
  677. */
  678. function wp_getAuthors($args) {
  679. $this->escape($args);
  680. $blog_id = (int) $args[0];
  681. $username = $args[1];
  682. $password = $args[2];
  683. if ( !$user = $this->login($username, $password) )
  684. return $this->error;
  685. if ( !current_user_can('edit_posts') )
  686. return(new IXR_Error(401, __('Sorry, you cannot edit posts on this site.')));
  687. do_action('xmlrpc_call', 'wp.getAuthors');
  688. $authors = array();
  689. foreach ( get_users( array( 'fields' => array('ID','user_login','display_name') ) ) as $user ) {
  690. $authors[] = array(
  691. 'user_id' => $user->ID,
  692. 'user_login' => $user->user_login,
  693. 'display_name' => $user->display_name
  694. );
  695. }
  696. return $authors;
  697. }
  698. /**
  699. * Get list of all tags
  700. *
  701. * @since 2.7
  702. *
  703. * @param array $args Method parameters.
  704. * @return array
  705. */
  706. function wp_getTags( $args ) {
  707. $this->escape( $args );
  708. $blog_id = (int) $args[0];
  709. $username = $args[1];
  710. $password = $args[2];
  711. if ( !$user = $this->login($username, $password) )
  712. return $this->error;
  713. if ( !current_user_can( 'edit_posts' ) )
  714. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
  715. do_action( 'xmlrpc_call', 'wp.getKeywords' );
  716. $tags = array( );
  717. if ( $all_tags = get_tags() ) {
  718. foreach( (array) $all_tags as $tag ) {
  719. $struct['tag_id'] = $tag->term_id;
  720. $struct['name'] = $tag->name;
  721. $struct['count'] = $tag->count;
  722. $struct['slug'] = $tag->slug;
  723. $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
  724. $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
  725. $tags[] = $struct;
  726. }
  727. }
  728. return $tags;
  729. }
  730. /**
  731. * Create new category.
  732. *
  733. * @since 2.2.0
  734. *
  735. * @param array $args Method parameters.
  736. * @return int Category ID.
  737. */
  738. function wp_newCategory($args) {
  739. $this->escape($args);
  740. $blog_id = (int) $args[0];
  741. $username = $args[1];
  742. $password = $args[2];
  743. $category = $args[3];
  744. if ( !$user = $this->login($username, $password) )
  745. return $this->error;
  746. do_action('xmlrpc_call', 'wp.newCategory');
  747. // Make sure the user is allowed to add a category.
  748. if ( !current_user_can('manage_categories') )
  749. return(new IXR_Error(401, __('Sorry, you do not have the right to add a category.')));
  750. // If no slug was provided make it empty so that
  751. // WordPress will generate one.
  752. if ( empty($category['slug']) )
  753. $category['slug'] = '';
  754. // If no parent_id was provided make it empty
  755. // so that it will be a top level page (no parent).
  756. if ( !isset($category['parent_id']) )
  757. $category['parent_id'] = '';
  758. // If no description was provided make it empty.
  759. if ( empty($category["description"]) )
  760. $category["description"] = "";
  761. $new_category = array(
  762. 'cat_name' => $category['name'],
  763. 'category_nicename' => $category['slug'],
  764. 'category_parent' => $category['parent_id'],
  765. 'category_description' => $category['description']
  766. );
  767. $cat_id = wp_insert_category($new_category, true);
  768. if ( is_wp_error( $cat_id ) ) {
  769. if ( 'term_exists' == $cat_id->get_error_code() )
  770. return (int) $cat_id->get_error_data();
  771. else
  772. return(new IXR_Error(500, __('Sorry, the new category failed.')));
  773. } elseif ( ! $cat_id ) {
  774. return(new IXR_Error(500, __('Sorry, the new category failed.')));
  775. }
  776. return($cat_id);
  777. }
  778. /**
  779. * Remove category.
  780. *
  781. * @since 2.5.0
  782. *
  783. * @param array $args Method parameters.
  784. * @return mixed See {@link wp_delete_term()} for return info.
  785. */
  786. function wp_deleteCategory($args) {
  787. $this->escape($args);
  788. $blog_id = (int) $args[0];
  789. $username = $args[1];
  790. $password = $args[2];
  791. $category_id = (int) $args[3];
  792. if ( !$user = $this->login($username, $password) )
  793. return $this->error;
  794. do_action('xmlrpc_call', 'wp.deleteCategory');
  795. if ( !current_user_can('manage_categories') )
  796. return new IXR_Error( 401, __( 'Sorry, you do not have the right to delete a category.' ) );
  797. return wp_delete_term( $category_id, 'category' );
  798. }
  799. /**
  800. * Retrieve category list.
  801. *
  802. * @since 2.2.0
  803. *
  804. * @param array $args Method parameters.
  805. * @return array
  806. */
  807. function wp_suggestCategories($args) {
  808. $this->escape($args);
  809. $blog_id = (int) $args[0];
  810. $username = $args[1];
  811. $password = $args[2];
  812. $category = $args[3];
  813. $max_results = (int) $args[4];
  814. if ( !$user = $this->login($username, $password) )
  815. return $this->error;
  816. if ( !current_user_can( 'edit_posts' ) )
  817. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts to this site in order to view categories.' ) );
  818. do_action('xmlrpc_call', 'wp.suggestCategories');
  819. $category_suggestions = array();
  820. $args = array('get' => 'all', 'number' => $max_results, 'name__like' => $category);
  821. foreach ( (array) get_categories($args) as $cat ) {
  822. $category_suggestions[] = array(
  823. 'category_id' => $cat->term_id,
  824. 'category_name' => $cat->name
  825. );
  826. }
  827. return($category_suggestions);
  828. }
  829. /**
  830. * Retrieve comment.
  831. *
  832. * @since 2.7.0
  833. *
  834. * @param array $args Method parameters.
  835. * @return array
  836. */
  837. function wp_getComment($args) {
  838. $this->escape($args);
  839. $blog_id = (int) $args[0];
  840. $username = $args[1];
  841. $password = $args[2];
  842. $comment_id = (int) $args[3];
  843. if ( !$user = $this->login($username, $password) )
  844. return $this->error;
  845. if ( !current_user_can( 'moderate_comments' ) )
  846. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  847. do_action('xmlrpc_call', 'wp.getComment');
  848. if ( ! $comment = get_comment($comment_id) )
  849. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  850. // Format page date.
  851. $comment_date = mysql2date('Ymd\TH:i:s', $comment->comment_date, false);
  852. $comment_date_gmt = mysql2date('Ymd\TH:i:s', $comment->comment_date_gmt, false);
  853. if ( '0' == $comment->comment_approved )
  854. $comment_status = 'hold';
  855. else if ( 'spam' == $comment->comment_approved )
  856. $comment_status = 'spam';
  857. else if ( '1' == $comment->comment_approved )
  858. $comment_status = 'approve';
  859. else
  860. $comment_status = $comment->comment_approved;
  861. $link = get_comment_link($comment);
  862. $comment_struct = array(
  863. 'date_created_gmt' => new IXR_Date($comment_date_gmt),
  864. 'user_id' => $comment->user_id,
  865. 'comment_id' => $comment->comment_ID,
  866. 'parent' => $comment->comment_parent,
  867. 'status' => $comment_status,
  868. 'content' => $comment->comment_content,
  869. 'link' => $link,
  870. 'post_id' => $comment->comment_post_ID,
  871. 'post_title' => get_the_title($comment->comment_post_ID),
  872. 'author' => $comment->comment_author,
  873. 'author_url' => $comment->comment_author_url,
  874. 'author_email' => $comment->comment_author_email,
  875. 'author_ip' => $comment->comment_author_IP,
  876. 'type' => $comment->comment_type,
  877. );
  878. return $comment_struct;
  879. }
  880. /**
  881. * Retrieve comments.
  882. *
  883. * Besides the common blog_id, username, and password arguments, it takes a filter
  884. * array as last argument.
  885. *
  886. * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
  887. *
  888. * The defaults are as follows:
  889. * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
  890. * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
  891. * - 'number' - Default is 10. Total number of media items to retrieve.
  892. * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  893. *
  894. * @since 2.7.0
  895. *
  896. * @param array $args Method parameters.
  897. * @return array. Contains a collection of comments. See {@link wp_xmlrpc_server::wp_getComment()} for a description of each item contents
  898. */
  899. function wp_getComments($args) {
  900. $raw_args = $args;
  901. $this->escape($args);
  902. $blog_id = (int) $args[0];
  903. $username = $args[1];
  904. $password = $args[2];
  905. $struct = $args[3];
  906. if ( !$user = $this->login($username, $password) )
  907. return $this->error;
  908. if ( !current_user_can( 'moderate_comments' ) )
  909. return new IXR_Error( 401, __( 'Sorry, you cannot edit comments.' ) );
  910. do_action('xmlrpc_call', 'wp.getComments');
  911. if ( isset($struct['status']) )
  912. $status = $struct['status'];
  913. else
  914. $status = '';
  915. $post_id = '';
  916. if ( isset($struct['post_id']) )
  917. $post_id = absint($struct['post_id']);
  918. $offset = 0;
  919. if ( isset($struct['offset']) )
  920. $offset = absint($struct['offset']);
  921. $number = 10;
  922. if ( isset($struct['number']) )
  923. $number = absint($struct['number']);
  924. $comments = get_comments( array('status' => $status, 'post_id' => $post_id, 'offset' => $offset, 'number' => $number ) );
  925. $num_comments = count($comments);
  926. if ( ! $num_comments )
  927. return array();
  928. $comments_struct = array();
  929. // FIXME: we already have the comments, why query them again?
  930. for ( $i = 0; $i < $num_comments; $i++ ) {
  931. $comment = wp_xmlrpc_server::wp_getComment(array(
  932. $raw_args[0], $raw_args[1], $raw_args[2], $comments[$i]->comment_ID,
  933. ));
  934. $comments_struct[] = $comment;
  935. }
  936. return $comments_struct;
  937. }
  938. /**
  939. * Delete a comment.
  940. *
  941. * By default, the comment will be moved to the trash instead of deleted.
  942. * See {@link wp_delete_comment()} for more information on
  943. * this behavior.
  944. *
  945. * @since 2.7.0
  946. *
  947. * @param array $args Method parameters. Contains:
  948. * - blog_id
  949. * - username
  950. * - password
  951. * - comment_id
  952. * @return mixed {@link wp_delete_comment()}
  953. */
  954. function wp_deleteComment($args) {
  955. $this->escape($args);
  956. $blog_id = (int) $args[0];
  957. $username = $args[1];
  958. $password = $args[2];
  959. $comment_ID = (int) $args[3];
  960. if ( !$user = $this->login($username, $password) )
  961. return $this->error;
  962. if ( !current_user_can( 'moderate_comments' ) )
  963. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  964. if ( ! get_comment($comment_ID) )
  965. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  966. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  967. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  968. do_action('xmlrpc_call', 'wp.deleteComment');
  969. return wp_delete_comment($comment_ID);
  970. }
  971. /**
  972. * Edit comment.
  973. *
  974. * Besides the common blog_id, username, and password arguments, it takes a
  975. * comment_id integer and a content_struct array as last argument.
  976. *
  977. * The allowed keys in the content_struct array are:
  978. * - 'author'
  979. * - 'author_url'
  980. * - 'author_email'
  981. * - 'content'
  982. * - 'date_created_gmt'
  983. * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See {@link get_comment_statuses()} for more details
  984. *
  985. * @since 2.7.0
  986. *
  987. * @param array $args. Contains:
  988. * - blog_id
  989. * - username
  990. * - password
  991. * - comment_id
  992. * - content_struct
  993. * @return bool True, on success.
  994. */
  995. function wp_editComment($args) {
  996. $this->escape($args);
  997. $blog_id = (int) $args[0];
  998. $username = $args[1];
  999. $password = $args[2];
  1000. $comment_ID = (int) $args[3];
  1001. $content_struct = $args[4];
  1002. if ( !$user = $this->login($username, $password) )
  1003. return $this->error;
  1004. if ( !current_user_can( 'moderate_comments' ) )
  1005. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  1006. if ( ! get_comment($comment_ID) )
  1007. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  1008. if ( !current_user_can( 'edit_comment', $comment_ID ) )
  1009. return new IXR_Error( 403, __( 'You are not allowed to moderate comments on this site.' ) );
  1010. do_action('xmlrpc_call', 'wp.editComment');
  1011. if ( isset($content_struct['status']) ) {
  1012. $statuses = get_comment_statuses();
  1013. $statuses = array_keys($statuses);
  1014. if ( ! in_array($content_struct['status'], $statuses) )
  1015. return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  1016. $comment_approved = $content_struct['status'];
  1017. }
  1018. // Do some timestamp voodoo
  1019. if ( !empty( $content_struct['date_created_gmt'] ) ) {
  1020. $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  1021. $comment_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  1022. $comment_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  1023. }
  1024. if ( isset($content_struct['content']) )
  1025. $comment_content = $content_struct['content'];
  1026. if ( isset($content_struct['author']) )
  1027. $comment_author = $content_struct['author'];
  1028. if ( isset($content_struct['author_url']) )
  1029. $comment_author_url = $content_struct['author_url'];
  1030. if ( isset($content_struct['author_email']) )
  1031. $comment_author_email = $content_struct['author_email'];
  1032. // We've got all the data -- post it:
  1033. $comment = compact('comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url');
  1034. $result = wp_update_comment($comment);
  1035. if ( is_wp_error( $result ) )
  1036. return new IXR_Error(500, $result->get_error_message());
  1037. if ( !$result )
  1038. return new IXR_Error(500, __('Sorry, the comment could not be edited. Something wrong happened.'));
  1039. return true;
  1040. }
  1041. /**
  1042. * Create new comment.
  1043. *
  1044. * @since 2.7.0
  1045. *
  1046. * @param array $args Method parameters.
  1047. * @return mixed {@link wp_new_comment()}
  1048. */
  1049. function wp_newComment($args) {
  1050. global $wpdb;
  1051. $this->escape($args);
  1052. $blog_id = (int) $args[0];
  1053. $username = $args[1];
  1054. $password = $args[2];
  1055. $post = $args[3];
  1056. $content_struct = $args[4];
  1057. $allow_anon = apply_filters('xmlrpc_allow_anonymous_comments', false);
  1058. $user = $this->login($username, $password);
  1059. if ( !$user ) {
  1060. $logged_in = false;
  1061. if ( $allow_anon && get_option('comment_registration') )
  1062. return new IXR_Error( 403, __( 'You must be registered to comment' ) );
  1063. else if ( !$allow_anon )
  1064. return $this->error;
  1065. } else {
  1066. $logged_in = true;
  1067. }
  1068. if ( is_numeric($post) )
  1069. $post_id = absint($post);
  1070. else
  1071. $post_id = url_to_postid($post);
  1072. if ( ! $post_id )
  1073. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1074. if ( ! get_post($post_id) )
  1075. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1076. $comment['comment_post_ID'] = $post_id;
  1077. if ( $logged_in ) {
  1078. $comment['comment_author'] = $wpdb->escape( $user->display_name );
  1079. $comment['comment_author_email'] = $wpdb->escape( $user->user_email );
  1080. $comment['comment_author_url'] = $wpdb->escape( $user->user_url );
  1081. $comment['user_ID'] = $user->ID;
  1082. } else {
  1083. $comment['comment_author'] = '';
  1084. if ( isset($content_struct['author']) )
  1085. $comment['comment_author'] = $content_struct['author'];
  1086. $comment['comment_author_email'] = '';
  1087. if ( isset($content_struct['author_email']) )
  1088. $comment['comment_author_email'] = $content_struct['author_email'];
  1089. $comment['comment_author_url'] = '';
  1090. if ( isset($content_struct['author_url']) )
  1091. $comment['comment_author_url'] = $content_struct['author_url'];
  1092. $comment['user_ID'] = 0;
  1093. if ( get_option('require_name_email') ) {
  1094. if ( 6 > strlen($comment['comment_author_email']) || '' == $comment['comment_author'] )
  1095. return new IXR_Error( 403, __( 'Comment author name and email are required' ) );
  1096. elseif ( !is_email($comment['comment_author_email']) )
  1097. return new IXR_Error( 403, __( 'A valid email address is required' ) );
  1098. }
  1099. }
  1100. $comment['comment_parent'] = isset($content_struct['comment_parent']) ? absint($content_struct['comment_parent']) : 0;
  1101. $comment['comment_content'] = isset($content_struct['content']) ? $content_struct['content'] : null;
  1102. do_action('xmlrpc_call', 'wp.newComment');
  1103. return wp_new_comment($comment);
  1104. }
  1105. /**
  1106. * Retrieve all of the comment status.
  1107. *
  1108. * @since 2.7.0
  1109. *
  1110. * @param array $args Method parameters.
  1111. * @return array
  1112. */
  1113. function wp_getCommentStatusList($args) {
  1114. $this->escape( $args );
  1115. $blog_id = (int) $args[0];
  1116. $username = $args[1];
  1117. $password = $args[2];
  1118. if ( !$user = $this->login($username, $password) )
  1119. return $this->error;
  1120. if ( !current_user_can( 'moderate_comments' ) )
  1121. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1122. do_action('xmlrpc_call', 'wp.getCommentStatusList');
  1123. return get_comment_statuses( );
  1124. }
  1125. /**
  1126. * Retrieve comment count.
  1127. *
  1128. * @since 2.5.0
  1129. *
  1130. * @param array $args Method parameters.
  1131. * @return array
  1132. */
  1133. function wp_getCommentCount( $args ) {
  1134. $this->escape($args);
  1135. $blog_id = (int) $args[0];
  1136. $username = $args[1];
  1137. $password = $args[2];
  1138. $post_id = (int) $args[3];
  1139. if ( !$user = $this->login($username, $password) )
  1140. return $this->error;
  1141. if ( !current_user_can( 'edit_posts' ) )
  1142. return new IXR_Error( 403, __( 'You are not allowed access to details about comments.' ) );
  1143. do_action('xmlrpc_call', 'wp.getCommentCount');
  1144. $count = wp_count_comments( $post_id );
  1145. return array(
  1146. 'approved' => $count->approved,
  1147. 'awaiting_moderation' => $count->moderated,
  1148. 'spam' => $count->spam,
  1149. 'total_comments' => $count->total_comments
  1150. );
  1151. }
  1152. /**
  1153. * Retrieve post statuses.
  1154. *
  1155. * @since 2.5.0
  1156. *
  1157. * @param array $args Method parameters.
  1158. * @return array
  1159. */
  1160. function wp_getPostStatusList( $args ) {
  1161. $this->escape( $args );
  1162. $blog_id = (int) $args[0];
  1163. $username = $args[1];
  1164. $password = $args[2];
  1165. if ( !$user = $this->login($username, $password) )
  1166. return $this->error;
  1167. if ( !current_user_can( 'edit_posts' ) )
  1168. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1169. do_action('xmlrpc_call', 'wp.getPostStatusList');
  1170. return get_post_statuses( );
  1171. }
  1172. /**
  1173. * Retrieve page statuses.
  1174. *
  1175. * @since 2.5.0
  1176. *
  1177. * @param array $args Method parameters.
  1178. * @return array
  1179. */
  1180. function wp_getPageStatusList( $args ) {
  1181. $this->escape( $args );
  1182. $blog_id = (int) $args[0];
  1183. $username = $args[1];
  1184. $password = $args[2];
  1185. if ( !$user = $this->login($username, $password) )
  1186. return $this->error;
  1187. if ( !current_user_can( 'edit_pages' ) )
  1188. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1189. do_action('xmlrpc_call', 'wp.getPageStatusList');
  1190. return get_page_statuses( );
  1191. }
  1192. /**
  1193. * Retrieve page templates.
  1194. *
  1195. * @since 2.6.0
  1196. *
  1197. * @param array $args Method parameters.
  1198. * @return array
  1199. */
  1200. function wp_getPageTemplates( $args ) {
  1201. $this->escape( $args );
  1202. $blog_id = (int) $args[0];
  1203. $username = $args[1];
  1204. $password = $args[2];
  1205. if ( !$user = $this->login($username, $password) )
  1206. return $this->error;
  1207. if ( !current_user_can( 'edit_pages' ) )
  1208. return new IXR_Error( 403, __( 'You are not allowed access to details about this site.' ) );
  1209. $templates = get_page_templates( );
  1210. $templates['Default'] = 'default';
  1211. return $templates;
  1212. }
  1213. /**
  1214. * Retrieve blog options.
  1215. *
  1216. * @since 2.6.0
  1217. *
  1218. * @param array $args Method parameters.
  1219. * @return array
  1220. */
  1221. function wp_getOptions( $args ) {
  1222. $this->escape( $args );
  1223. $blog_id = (int) $args[0];
  1224. $username = $args[1];
  1225. $password = $args[2];
  1226. $options = isset( $args[3] ) ? (array) $args[3] : array();
  1227. if ( !$user = $this->login($username, $password) )
  1228. return $this->error;
  1229. // If no specific options where asked for, return all of them
  1230. if ( count( $options ) == 0 )
  1231. $options = array_keys($this->blog_options);
  1232. return $this->_getOptions($options);
  1233. }
  1234. /**
  1235. * Retrieve blog options value from list.
  1236. *
  1237. * @since 2.6.0
  1238. *
  1239. * @param array $options Options to retrieve.
  1240. * @return array
  1241. */
  1242. function _getOptions($options) {
  1243. $data = array( );
  1244. foreach ( $options as $option ) {
  1245. if ( array_key_exists( $option, $this->blog_options ) ) {
  1246. $data[$option] = $this->blog_options[$option];
  1247. //Is the value static or dynamic?
  1248. if ( isset( $data[$option]['option'] ) ) {
  1249. $data[$option]['value'] = get_option( $data[$option]['option'] );
  1250. unset($data[$option]['option']);
  1251. }
  1252. }
  1253. }
  1254. return $data;
  1255. }
  1256. /**
  1257. * Update blog options.
  1258. *
  1259. * @since 2.6.0
  1260. *
  1261. * @param array $args Method parameters.
  1262. * @return unknown
  1263. */
  1264. function wp_setOptions( $args ) {
  1265. $this->escape( $args );
  1266. $blog_id = (int) $args[0];
  1267. $username = $args[1];
  1268. $password = $args[2];
  1269. $options = (array) $args[3];
  1270. if ( !$user = $this->login($username, $password) )
  1271. return $this->error;
  1272. if ( !current_user_can( 'manage_options' ) )
  1273. return new IXR_Error( 403, __( 'You are not allowed to update options.' ) );
  1274. foreach ( $options as $o_name => $o_value ) {
  1275. $option_names[] = $o_name;
  1276. if ( !array_key_exists( $o_name, $this->blog_options ) )
  1277. continue;
  1278. if ( $this->blog_options[$o_name]['readonly'] == true )
  1279. continue;
  1280. update_option( $this->blog_options[$o_name]['option'], $o_value );
  1281. }
  1282. //Now return the updated values
  1283. return $this->_getOptions($option_names);
  1284. }
  1285. /**
  1286. * Retrieve a media item by ID
  1287. *
  1288. * @since 3.1.0
  1289. *
  1290. * @param array $args Method parameters. Contains:
  1291. * - blog_id
  1292. * - username
  1293. * - password
  1294. * - attachment_id
  1295. * @return array. Associative array containing:
  1296. * - 'date_created_gmt'
  1297. * - 'parent'
  1298. * - 'link'
  1299. * - 'thumbnail'
  1300. * - 'title'
  1301. * - 'caption'
  1302. * - 'description'
  1303. * - 'metadata'
  1304. */
  1305. function wp_getMediaItem($args) {
  1306. $this->escape($args);
  1307. $blog_id = (int) $args[0];
  1308. $username = $args[1];
  1309. $password = $args[2];
  1310. $attachment_id = (int) $args[3];
  1311. if ( !$user = $this->login($username, $password) )
  1312. return $this->error;
  1313. if ( !current_user_can( 'upload_files' ) )
  1314. return new IXR_Error( 403, __( 'You are not allowed to upload files to this site.' ) );
  1315. do_action('xmlrpc_call', 'wp.getMediaItem');
  1316. if ( ! $attachment = get_post($attachment_id) )
  1317. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  1318. // Format page date.
  1319. $attachment_date = mysql2date('Ymd\TH:i:s', $attachment->post_date, false);
  1320. $attachment_date_gmt = mysql2date('Ymd\TH:i:s', $attachment->post_date_gmt, false);
  1321. $link = wp_get_attachment_url($attachment->ID);
  1322. $thumbnail_link = wp_get_attachment_thumb_url($attachment->ID);
  1323. $attachment_struct = array(
  1324. 'date_created_gmt' => new IXR_Date($attachment_date_gmt),
  1325. 'parent' => $attachment->post_parent,
  1326. 'link' => $link,
  1327. 'thumbnail' => $thumbnail_link,
  1328. 'title' => $attachment->post_title,
  1329. 'caption' => $attachment->post_excerpt,
  1330. 'description' => $attachment->post_content,
  1331. 'metadata' => wp_get_attachment_metadata($attachment->ID),
  1332. );
  1333. return $attachment_struct;
  1334. }
  1335. /**
  1336. * Retrieves a collection of media library items (or attachments)
  1337. *
  1338. * Besides the common blog_id, username, and password arguments, it takes a filter
  1339. * array as last argument.
  1340. *
  1341. * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
  1342. *
  1343. * The defaults are as follows:
  1344. * - 'number' - Default is 5. Total number of media items to retrieve.
  1345. * - 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  1346. * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  1347. * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
  1348. *
  1349. * @since 3.1.0
  1350. *
  1351. * @param array $args Method parameters. Contains:
  1352. * - blog_id
  1353. * - username
  1354. * - password
  1355. * - filter
  1356. * @return array. Contains a collection of media items. See {@link wp_xmlrpc_server::wp_getMediaItem()} for a description of each item contents
  1357. */
  1358. function wp_getMediaLibrary($args) {
  1359. $raw_args = $args;
  1360. $this->escape($args);
  1361. $blog_id = (int) $args[0];
  1362. $username = $args[1];
  1363. $password = $args[2];
  1364. $struct = isset( $args[3] ) ? $args[3] : array() ;
  1365. if ( !$user = $this->login($username, $password) )
  1366. return $this->error;
  1367. if ( !current_user_can( 'upload_files' ) )
  1368. return new IXR_Error( 401, __( 'Sorry, you cannot upload files.' ) );
  1369. do_action('xmlrpc_call', 'wp.getMediaLibrary');
  1370. $parent_id = ( isset($struct['parent_id']) ) ? absint($struct['parent_id']) : '' ;
  1371. $mime_type = ( isset($struct['mime_type']) ) ? $struct['mime_type'] : '' ;
  1372. $offset = ( isset($struct['offset']) ) ? absint($struct['offset']) : 0 ;
  1373. $number = ( isset($struct['number']) ) ? absint($struct['number']) : -1 ;
  1374. $attachments = get_posts( array('post_type' => 'attachment', 'post_parent' => $parent_id, 'offset' => $offset, 'numberposts' => $number, 'post_mime_type' => $mime_type ) );
  1375. $num_attachments = count($attachments);
  1376. if ( ! $num_attachments )
  1377. return array();
  1378. $attachments_struct = array();
  1379. foreach ($attachments as $attachment )
  1380. $attachments_struct[] = $this->wp_getMediaItem( array( $raw_args[0], $raw_args[1], $raw_args[2], $attachment->ID ) );
  1381. return $attachments_struct;
  1382. }
  1383. /**
  1384. * Retrieves a list of post formats used by the site
  1385. *
  1386. * @since 3.1
  1387. *
  1388. * @param array $args Method parameters. Contains:
  1389. * - blog_id
  1390. * - username
  1391. * - password
  1392. * @return array
  1393. */
  1394. function wp_getPostFormats( $args ) {
  1395. $this->escape( $args );
  1396. $blog_id = (int) $args[0];
  1397. $username = $args[1];
  1398. $password = $args[2];
  1399. if ( !$user = $this->login( $username, $password ) )
  1400. return $this->error;
  1401. do_action( 'xmlrpc_call', 'wp.getPostFormats' );
  1402. $formats = get_post_format_strings();
  1403. # find out if they want a list of currently supports formats
  1404. if ( isset( $args[3] ) && is_array( $args[3] ) ) {
  1405. if ( $args[3]['show-supported'] ) {
  1406. if ( current_theme_supports( 'post-formats' ) ) {
  1407. $supported = get_theme_support( 'post-formats' );
  1408. $data['all'] = $formats;
  1409. $data['supported'] = $supported[0];
  1410. $formats = $data;
  1411. }
  1412. }
  1413. }
  1414. return $formats;
  1415. }
  1416. /* Blogger API functions.
  1417. * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
  1418. */
  1419. /**
  1420. * Retrieve blogs that user owns.
  1421. *
  1422. * Will make more sense once we support multiple blogs.
  1423. *
  1424. * @since 1.5.0
  1425. *
  1426. * @param array $args Method parameters.
  1427. * @return array
  1428. */
  1429. function blogger_getUsersBlogs($args) {
  1430. if ( is_multisite() )
  1431. return $this->_multisite_getUsersBlogs($args);
  1432. $this->escape($args);
  1433. $username = $args[1];
  1434. $password = $args[2];
  1435. if ( !$user = $this->login($username, $password) )
  1436. return $this->error;
  1437. do_action('xmlrpc_call', 'blogger.getUsersBlogs');
  1438. $is_admin = current_user_can('manage_options');
  1439. $struct = array(
  1440. 'isAdmin' => $is_admin,
  1441. 'url' => get_option('home') . '/',
  1442. 'blogid' => '1',
  1443. 'blogName' => get_option('blogname'),
  1444. 'xmlrpc' => site_url( 'xmlrpc.php' )
  1445. );
  1446. return array($struct);
  1447. }
  1448. /**
  1449. * Private function for retrieving a users blogs for multisite setups
  1450. *
  1451. * @access protected
  1452. */
  1453. function _multisite_getUsersBlogs($args) {
  1454. global $current_blog;
  1455. $domain = $current_blog->domain;
  1456. $path = $current_blog->path . 'xmlrpc.php';
  1457. $protocol = is_ssl() ? 'https' : 'http';
  1458. $rpc = new IXR_Client("$protocol://{$domain}{$path}");
  1459. $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
  1460. $blogs = $rpc->getResponse();
  1461. if ( isset($blogs['faultCode']) )
  1462. return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
  1463. if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  1464. return $blogs;
  1465. } else {
  1466. foreach ( (array) $blogs as $blog ) {
  1467. if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
  1468. return array($blog);
  1469. }
  1470. return array();
  1471. }
  1472. }
  1473. /**
  1474. * Retrieve user's data.
  1475. *
  1476. * Gives your client some info about you, so you don't have to.
  1477. *
  1478. * @since 1.5.0
  1479. *
  1480. * @param array $args Method parameters.
  1481. * @return array
  1482. */
  1483. function blogger_getUserInfo($args) {
  1484. $this->escape($args);
  1485. $username = $args[1];
  1486. $password = $args[2];
  1487. if ( !$user = $this->login($username, $password) )
  1488. return $this->error;
  1489. if ( !current_user_can( 'edit_posts' ) )
  1490. return new IXR_Error( 401, __( 'Sorry, you do not have access to user data on this site.' ) );
  1491. do_action('xmlrpc_call', 'blogger.getUserInfo');
  1492. $struct = array(
  1493. 'nickname' => $user->nickname,
  1494. 'userid' => $user->ID,
  1495. 'url' => $user->user_url,
  1496. 'lastname' => $user->last_name,
  1497. 'firstname' => $user->first_name
  1498. );
  1499. return $struct;
  1500. }
  1501. /**
  1502. * Retrieve post.
  1503. *
  1504. * @since 1.5.0
  1505. *
  1506. * @param array $args Method parameters.
  1507. * @return array
  1508. */
  1509. function blogger_getPost($args) {
  1510. $this->escape($args);
  1511. $post_ID = (int) $args[1];
  1512. $username = $args[2];
  1513. $password = $args[3];
  1514. if ( !$user = $this->login($username, $password) )
  1515. return $this->error;
  1516. if ( !current_user_can( 'edit_post', $post_ID ) )
  1517. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  1518. do_action('xmlrpc_call', 'blogger.getPost');
  1519. $post_data = wp_get_single_post($post_ID, ARRAY_A);
  1520. $categories = implode(',', wp_get_post_categories($post_ID));
  1521. $content = '<title>'.stripslashes($post_data['post_title']).'</title>';
  1522. $content .= '<category>'.$categories.'</category>';
  1523. $content .= stripslashes($post_data['post_content']);
  1524. $struct = array(
  1525. 'userid' => $post_data['post_author'],
  1526. 'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'], false)),
  1527. 'content' => $content,
  1528. 'postid' => (string) $post_data['ID']
  1529. );
  1530. return $struct;
  1531. }
  1532. /**
  1533. * Retrieve list of recent posts.
  1534. *
  1535. * @since 1.5.0
  1536. *
  1537. * @param array $args Method parameters.
  1538. * @return array
  1539. */
  1540. function blogger_getRecentPosts($args) {
  1541. $this->escape($args);
  1542. // $args[0] = appkey - ignored
  1543. $blog_ID = (int) $args[1]; /* though we don't use it yet */
  1544. $username = $args[2];
  1545. $password = $args[3];
  1546. if ( isset( $args[4] ) )
  1547. $query = array( 'numberposts' => absint( $args[4] ) );
  1548. else
  1549. $query = array();
  1550. if ( !$user = $this->login($username, $password) )
  1551. return $this->error;
  1552. do_action('xmlrpc_call', 'blogger.getRecentPosts');
  1553. $posts_list = wp_get_recent_posts( $query );
  1554. if ( !$posts_list ) {
  1555. $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  1556. return $this->error;
  1557. }
  1558. foreach ($posts_list as $entry) {
  1559. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  1560. continue;
  1561. $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
  1562. $categories = implode(',', wp_get_post_categories($entry['ID']));
  1563. $content = '<title>'.stripslashes($entry['post_title']).'</title>';
  1564. $content .= '<category>'.$categories.'</category>';
  1565. $content .= stripslashes($entry['post_content']);
  1566. $struct[] = array(
  1567. 'userid' => $entry['post_author'],
  1568. 'dateCreated' => new IXR_Date($post_date),
  1569. 'content' => $content,
  1570. 'postid' => (string) $entry['ID'],
  1571. );
  1572. }
  1573. $recent_posts = array();
  1574. for ( $j=0; $j<count($struct); $j++ ) {
  1575. array_push($recent_posts, $struct[$j]);
  1576. }
  1577. return $recent_posts;
  1578. }
  1579. /**
  1580. * Retrieve blog_filename content.
  1581. *
  1582. * @since 1.5.0
  1583. *
  1584. * @param array $args Method parameters.
  1585. * @return string
  1586. */
  1587. function blogger_getTemplate($args) {
  1588. $this->escape($args);
  1589. $blog_ID = (int) $args[1];
  1590. $username = $args[2];
  1591. $password = $args[3];
  1592. $template = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
  1593. if ( !$user = $this->login($username, $password) )
  1594. return $this->error;
  1595. do_action('xmlrpc_call', 'blogger.getTemplate');
  1596. if ( !current_user_can('edit_themes') )
  1597. return new IXR_Error(401, __('Sorry, this user can not edit the template.'));
  1598. /* warning: here we make the assumption that the blog's URL is on the same server */
  1599. $filename = get_option('home') . '/';
  1600. $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
  1601. $f = fopen($filename, 'r');
  1602. $content = fread($f, filesize($filename));
  1603. fclose($f);
  1604. /* so it is actually editable with a windows/mac client */
  1605. // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented. $content = str_replace("\n", "\r\n", $content);
  1606. return $content;
  1607. }
  1608. /**
  1609. * Updates the content of blog_filename.
  1610. *
  1611. * @since 1.5.0
  1612. *
  1613. * @param array $args Method parameters.
  1614. * @return bool True when done.
  1615. */
  1616. function blogger_setTemplate($args) {
  1617. $this->escape($args);
  1618. $blog_ID = (int) $args[1];
  1619. $username = $args[2];
  1620. $password = $args[3];
  1621. $content = $args[4];
  1622. $template = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
  1623. if ( !$user = $this->login($username, $password) )
  1624. return $this->error;
  1625. do_action('xmlrpc_call', 'blogger.setTemplate');
  1626. if ( !current_user_can('edit_themes') )
  1627. return new IXR_Error(401, __('Sorry, this user cannot edit the template.'));
  1628. /* warning: here we make the assumption that the blog's URL is on the same server */
  1629. $filename = get_option('home') . '/';
  1630. $filename = preg_replace('#https?://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
  1631. if ($f = fopen($filename, 'w+')) {
  1632. fwrite($f, $content);
  1633. fclose($f);
  1634. } else {
  1635. return new IXR_Error(500, __('Either the file is not writable, or something wrong happened. The file has not been updated.'));
  1636. }
  1637. return true;
  1638. }
  1639. /**
  1640. * Create new post.
  1641. *
  1642. * @since 1.5.0
  1643. *
  1644. * @param array $args Method parameters.
  1645. * @return int
  1646. */
  1647. function blogger_newPost($args) {
  1648. $this->escape($args);
  1649. $blog_ID = (int) $args[1]; /* though we don't use it yet */
  1650. $username = $args[2];
  1651. $password = $args[3];
  1652. $content = $args[4];
  1653. $publish = $args[5];
  1654. if ( !$user = $this->login($username, $password) )
  1655. return $this->error;
  1656. do_action('xmlrpc_call', 'blogger.newPost');
  1657. $cap = ($publish) ? 'publish_posts' : 'edit_posts';
  1658. if ( !current_user_can($cap) )
  1659. return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
  1660. $post_status = ($publish) ? 'publish' : 'draft';
  1661. $post_author = $user->ID;
  1662. $post_title = xmlrpc_getposttitle($content);
  1663. $post_category = xmlrpc_getpostcategory($content);
  1664. $post_content = xmlrpc_removepostdata($content);
  1665. $post_date = current_time('mysql');
  1666. $post_date_gmt = current_time('mysql', 1);
  1667. $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
  1668. $post_ID = wp_insert_post($post_data);
  1669. if ( is_wp_error( $post_ID ) )
  1670. return new IXR_Error(500, $post_ID->get_error_message());
  1671. if ( !$post_ID )
  1672. return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
  1673. $this->attach_uploads( $post_ID, $post_content );
  1674. logIO('O', "Posted ! ID: $post_ID");
  1675. return $post_ID;
  1676. }
  1677. /**
  1678. * Edit a post.
  1679. *
  1680. * @since 1.5.0
  1681. *
  1682. * @param array $args Method parameters.
  1683. * @return bool true when done.
  1684. */
  1685. function blogger_editPost($args) {
  1686. $this->escape($args);
  1687. $post_ID = (int) $args[1];
  1688. $username = $args[2];
  1689. $password = $args[3];
  1690. $content = $args[4];
  1691. $publish = $args[5];
  1692. if ( !$user = $this->login($username, $password) )
  1693. return $this->error;
  1694. do_action('xmlrpc_call', 'blogger.editPost');
  1695. $actual_post = wp_get_single_post($post_ID,ARRAY_A);
  1696. if ( !$actual_post || $actual_post['post_type'] != 'post' )
  1697. return new IXR_Error(404, __('Sorry, no such post.'));
  1698. $this->escape($actual_post);
  1699. if ( !current_user_can('edit_post', $post_ID) )
  1700. return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
  1701. extract($actual_post, EXTR_SKIP);
  1702. if ( ('publish' == $post_status) && !current_user_can('publish_posts') )
  1703. return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
  1704. $post_title = xmlrpc_getposttitle($content);
  1705. $post_category = xmlrpc_getpostcategory($content);
  1706. $post_content = xmlrpc_removepostdata($content);
  1707. $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
  1708. $result = wp_update_post($postdata);
  1709. if ( !$result )
  1710. return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be edited.'));
  1711. $this->attach_uploads( $ID, $post_content );
  1712. return true;
  1713. }
  1714. /**
  1715. * Remove a post.
  1716. *
  1717. * @since 1.5.0
  1718. *
  1719. * @param array $args Method parameters.
  1720. * @return bool True when post is deleted.
  1721. */
  1722. function blogger_deletePost($args) {
  1723. $this->escape($args);
  1724. $post_ID = (int) $args[1];
  1725. $username = $args[2];
  1726. $password = $args[3];
  1727. $publish = $args[4];
  1728. if ( !$user = $this->login($username, $password) )
  1729. return $this->error;
  1730. do_action('xmlrpc_call', 'blogger.deletePost');
  1731. $actual_post = wp_get_single_post($post_ID,ARRAY_A);
  1732. if ( !$actual_post || $actual_post['post_type'] != 'post' )
  1733. return new IXR_Error(404, __('Sorry, no such post.'));
  1734. if ( !current_user_can('delete_post', $post_ID) )
  1735. return new IXR_Error(401, __('Sorry, you do not have the right to delete this post.'));
  1736. $result = wp_delete_post($post_ID);
  1737. if ( !$result )
  1738. return new IXR_Error(500, __('For some strange yet very annoying reason, this post could not be deleted.'));
  1739. return true;
  1740. }
  1741. /* MetaWeblog API functions
  1742. * specs on wherever Dave Winer wants them to be
  1743. */
  1744. /**
  1745. * Create a new post.
  1746. *
  1747. * The 'content_struct' argument must contain:
  1748. * - title
  1749. * - description
  1750. * - mt_excerpt
  1751. * - mt_text_more
  1752. * - mt_keywords
  1753. * - mt_tb_ping_urls
  1754. * - categories
  1755. *
  1756. * Also, it can optionally contain:
  1757. * - wp_slug
  1758. * - wp_password
  1759. * - wp_page_parent_id
  1760. * - wp_page_order
  1761. * - wp_author_id
  1762. * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
  1763. * - mt_allow_comments - can be 'open' or 'closed'
  1764. * - mt_allow_pings - can be 'open' or 'closed'
  1765. * - date_created_gmt
  1766. * - dateCreated
  1767. *
  1768. * @since 1.5.0
  1769. *
  1770. * @param array $args Method parameters. Contains:
  1771. * - blog_id
  1772. * - username
  1773. * - password
  1774. * - content_struct
  1775. * - publish
  1776. * @return int
  1777. */
  1778. function mw_newPost($args) {
  1779. $this->escape($args);
  1780. $blog_ID = (int) $args[0]; // we will support this in the near future
  1781. $username = $args[1];
  1782. $password = $args[2];
  1783. $content_struct = $args[3];
  1784. $publish = isset( $args[4] ) ? $args[4] : 0;
  1785. if ( !$user = $this->login($username, $password) )
  1786. return $this->error;
  1787. do_action('xmlrpc_call', 'metaWeblog.newPost');
  1788. $page_template = '';
  1789. if ( !empty( $content_struct['post_type'] ) ) {
  1790. if ( $content_struct['post_type'] == 'page' ) {
  1791. if ( $publish )
  1792. $cap = 'publish_pages';
  1793. elseif ('publish' == $content_struct['page_status'])
  1794. $cap = 'publish_pages';
  1795. else
  1796. $cap = 'edit_pages';
  1797. $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  1798. $post_type = 'page';
  1799. if ( !empty( $content_struct['wp_page_template'] ) )
  1800. $page_template = $content_struct['wp_page_template'];
  1801. } elseif ( $content_struct['post_type'] == 'post' ) {
  1802. if ( $publish )
  1803. $cap = 'publish_posts';
  1804. elseif ('publish' == $content_struct['post_status'])
  1805. $cap = 'publish_posts';
  1806. else
  1807. $cap = 'edit_posts';
  1808. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  1809. $post_type = 'post';
  1810. } else {
  1811. // No other post_type values are allowed here
  1812. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  1813. }
  1814. } else {
  1815. if ( $publish )
  1816. $cap = 'publish_posts';
  1817. elseif ('publish' == $content_struct['post_status'])
  1818. $cap = 'publish_posts';
  1819. else
  1820. $cap = 'edit_posts';
  1821. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  1822. $post_type = 'post';
  1823. }
  1824. if ( !current_user_can( $cap ) )
  1825. return new IXR_Error( 401, $error_message );
  1826. // Check for a valid post format if one was given
  1827. if ( isset( $content_struct['wp_post_format'] ) ) {
  1828. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  1829. if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  1830. return new IXR_Error( 404, __( 'Invalid post format' ) );
  1831. }
  1832. }
  1833. // Let WordPress generate the post_name (slug) unless
  1834. // one has been provided.
  1835. $post_name = "";
  1836. if ( isset($content_struct['wp_slug']) )
  1837. $post_name = $content_struct['wp_slug'];
  1838. // Only use a password if one was given.
  1839. if ( isset($content_struct['wp_password']) )
  1840. $post_password = $content_struct['wp_password'];
  1841. // Only set a post parent if one was provided.
  1842. if ( isset($content_struct['wp_page_parent_id']) )
  1843. $post_parent = $content_struct['wp_page_parent_id'];
  1844. // Only set the menu_order if it was provided.
  1845. if ( isset($content_struct['wp_page_order']) )
  1846. $menu_order = $content_struct['wp_page_order'];
  1847. $post_author = $user->ID;
  1848. // If an author id was provided then use it instead.
  1849. if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
  1850. switch ( $post_type ) {
  1851. case "post":
  1852. if ( !current_user_can('edit_others_posts') )
  1853. return(new IXR_Error(401, __('You are not allowed to post as this user')));
  1854. break;
  1855. case "page":
  1856. if ( !current_user_can('edit_others_pages') )
  1857. return(new IXR_Error(401, __('You are not allowed to create pages as this user')));
  1858. break;
  1859. default:
  1860. return(new IXR_Error(401, __('Invalid post type.')));
  1861. break;
  1862. }
  1863. $post_author = $content_struct['wp_author_id'];
  1864. }
  1865. $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
  1866. $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
  1867. $post_status = $publish ? 'publish' : 'draft';
  1868. if ( isset( $content_struct["{$post_type}_status"] ) ) {
  1869. switch ( $content_struct["{$post_type}_status"] ) {
  1870. case 'draft':
  1871. case 'pending':
  1872. case 'private':
  1873. case 'publish':
  1874. $post_status = $content_struct["{$post_type}_status"];
  1875. break;
  1876. default:
  1877. $post_status = $publish ? 'publish' : 'draft';
  1878. break;
  1879. }
  1880. }
  1881. $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
  1882. $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
  1883. $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
  1884. if ( isset($content_struct['mt_allow_comments']) ) {
  1885. if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  1886. switch ( $content_struct['mt_allow_comments'] ) {
  1887. case 'closed':
  1888. $comment_status = 'closed';
  1889. break;
  1890. case 'open':
  1891. $comment_status = 'open';
  1892. break;
  1893. default:
  1894. $comment_status = get_option('default_comment_status');
  1895. break;
  1896. }
  1897. } else {
  1898. switch ( (int) $content_struct['mt_allow_comments'] ) {
  1899. case 0:
  1900. case 2:
  1901. $comment_status = 'closed';
  1902. break;
  1903. case 1:
  1904. $comment_status = 'open';
  1905. break;
  1906. default:
  1907. $comment_status = get_option('default_comment_status');
  1908. break;
  1909. }
  1910. }
  1911. } else {
  1912. $comment_status = get_option('default_comment_status');
  1913. }
  1914. if ( isset($content_struct['mt_allow_pings']) ) {
  1915. if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  1916. switch ( $content_struct['mt_allow_pings'] ) {
  1917. case 'closed':
  1918. $ping_status = 'closed';
  1919. break;
  1920. case 'open':
  1921. $ping_status = 'open';
  1922. break;
  1923. default:
  1924. $ping_status = get_option('default_ping_status');
  1925. break;
  1926. }
  1927. } else {
  1928. switch ( (int) $content_struct['mt_allow_pings'] ) {
  1929. case 0:
  1930. $ping_status = 'closed';
  1931. break;
  1932. case 1:
  1933. $ping_status = 'open';
  1934. break;
  1935. default:
  1936. $ping_status = get_option('default_ping_status');
  1937. break;
  1938. }
  1939. }
  1940. } else {
  1941. $ping_status = get_option('default_ping_status');
  1942. }
  1943. if ( $post_more )
  1944. $post_content = $post_content . '<!--more-->' . $post_more;
  1945. $to_ping = null;
  1946. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  1947. $to_ping = $content_struct['mt_tb_ping_urls'];
  1948. if ( is_array($to_ping) )
  1949. $to_ping = implode(' ', $to_ping);
  1950. }
  1951. // Do some timestamp voodoo
  1952. if ( !empty( $content_struct['date_created_gmt'] ) )
  1953. $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  1954. elseif ( !empty( $content_struct['dateCreated']) )
  1955. $dateCreated = $content_struct['dateCreated']->getIso();
  1956. if ( !empty( $dateCreated ) ) {
  1957. $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  1958. $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  1959. } else {
  1960. $post_date = current_time('mysql');
  1961. $post_date_gmt = current_time('mysql', 1);
  1962. }
  1963. $post_category = array();
  1964. if ( isset( $content_struct['categories'] ) ) {
  1965. $catnames = $content_struct['categories'];
  1966. logIO('O', 'Post cats: ' . var_export($catnames,true));
  1967. if ( is_array($catnames) ) {
  1968. foreach ($catnames as $cat) {
  1969. $post_category[] = get_cat_ID($cat);
  1970. }
  1971. }
  1972. }
  1973. $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');
  1974. $post_ID = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;
  1975. // Only posts can be sticky
  1976. if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  1977. if ( $content_struct['sticky'] == true )
  1978. stick_post( $post_ID );
  1979. elseif ( $content_struct['sticky'] == false )
  1980. unstick_post( $post_ID );
  1981. }
  1982. if ( isset($content_struct['custom_fields']) )
  1983. $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  1984. // Handle enclosures
  1985. $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  1986. $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  1987. $this->attach_uploads( $post_ID, $post_content );
  1988. // Handle post formats if assigned, value is validated earlier
  1989. // in this function
  1990. if ( isset( $content_struct['wp_post_format'] ) )
  1991. wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
  1992. $post_ID = wp_insert_post( $postdata, true );
  1993. if ( is_wp_error( $post_ID ) )
  1994. return new IXR_Error(500, $post_ID->get_error_message());
  1995. if ( !$post_ID )
  1996. return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
  1997. logIO('O', "Posted ! ID: $post_ID");
  1998. return strval($post_ID);
  1999. }
  2000. function add_enclosure_if_new($post_ID, $enclosure) {
  2001. if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
  2002. $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'];
  2003. $found = false;
  2004. foreach ( (array) get_post_custom($post_ID) as $key => $val) {
  2005. if ($key == 'enclosure') {
  2006. foreach ( (array) $val as $enc ) {
  2007. if ($enc == $encstring) {
  2008. $found = true;
  2009. break 2;
  2010. }
  2011. }
  2012. }
  2013. }
  2014. if (!$found)
  2015. add_post_meta( $post_ID, 'enclosure', $encstring );
  2016. }
  2017. }
  2018. /**
  2019. * Attach upload to a post.
  2020. *
  2021. * @since 2.1.0
  2022. *
  2023. * @param int $post_ID Post ID.
  2024. * @param string $post_content Post Content for attachment.
  2025. */
  2026. function attach_uploads( $post_ID, $post_content ) {
  2027. global $wpdb;
  2028. // find any unattached files
  2029. $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
  2030. if ( is_array( $attachments ) ) {
  2031. foreach ( $attachments as $file ) {
  2032. if ( strpos( $post_content, $file->guid ) !== false )
  2033. $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
  2034. }
  2035. }
  2036. }
  2037. /**
  2038. * Edit a post.
  2039. *
  2040. * @since 1.5.0
  2041. *
  2042. * @param array $args Method parameters.
  2043. * @return bool True on success.
  2044. */
  2045. function mw_editPost($args) {
  2046. $this->escape($args);
  2047. $post_ID = (int) $args[0];
  2048. $username = $args[1];
  2049. $password = $args[2];
  2050. $content_struct = $args[3];
  2051. $publish = $args[4];
  2052. if ( !$user = $this->login($username, $password) )
  2053. return $this->error;
  2054. do_action('xmlrpc_call', 'metaWeblog.editPost');
  2055. $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
  2056. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  2057. $post_type = 'post';
  2058. $page_template = '';
  2059. if ( !empty( $content_struct['post_type'] ) ) {
  2060. if ( $content_struct['post_type'] == 'page' ) {
  2061. if ( $publish || 'publish' == $content_struct['page_status'] )
  2062. $cap = 'publish_pages';
  2063. else
  2064. $cap = 'edit_pages';
  2065. $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  2066. $post_type = 'page';
  2067. if ( !empty( $content_struct['wp_page_template'] ) )
  2068. $page_template = $content_struct['wp_page_template'];
  2069. } elseif ( $content_struct['post_type'] == 'post' ) {
  2070. if ( $publish || 'publish' == $content_struct['post_status'] )
  2071. $cap = 'publish_posts';
  2072. else
  2073. $cap = 'edit_posts';
  2074. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  2075. $post_type = 'post';
  2076. } else {
  2077. // No other post_type values are allowed here
  2078. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  2079. }
  2080. } else {
  2081. if ( $publish || 'publish' == $content_struct['post_status'] )
  2082. $cap = 'publish_posts';
  2083. else
  2084. $cap = 'edit_posts';
  2085. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  2086. $post_type = 'post';
  2087. }
  2088. if ( !current_user_can( $cap ) )
  2089. return new IXR_Error( 401, $error_message );
  2090. // Check for a valid post format if one was given
  2091. if ( isset( $content_struct['wp_post_format'] ) ) {
  2092. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  2093. if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  2094. return new IXR_Error( 404, __( 'Invalid post format' ) );
  2095. }
  2096. }
  2097. $postdata = wp_get_single_post($post_ID, ARRAY_A);
  2098. // If there is no post data for the give post id, stop
  2099. // now and return an error. Other wise a new post will be
  2100. // created (which was the old behavior).
  2101. if ( empty($postdata["ID"]) )
  2102. return(new IXR_Error(404, __('Invalid post ID.')));
  2103. $this->escape($postdata);
  2104. extract($postdata, EXTR_SKIP);
  2105. // Let WordPress manage slug if none was provided.
  2106. $post_name = "";
  2107. $post_name = $postdata['post_name'];
  2108. if ( isset($content_struct['wp_slug']) )
  2109. $post_name = $content_struct['wp_slug'];
  2110. // Only use a password if one was given.
  2111. if ( isset($content_struct['wp_password']) )
  2112. $post_password = $content_struct['wp_password'];
  2113. // Only set a post parent if one was given.
  2114. if ( isset($content_struct['wp_page_parent_id']) )
  2115. $post_parent = $content_struct['wp_page_parent_id'];
  2116. // Only set the menu_order if it was given.
  2117. if ( isset($content_struct['wp_page_order']) )
  2118. $menu_order = $content_struct['wp_page_order'];
  2119. $post_author = $postdata['post_author'];
  2120. // Only set the post_author if one is set.
  2121. if ( isset($content_struct['wp_author_id']) && ($user->ID != $content_struct['wp_author_id']) ) {
  2122. switch ( $post_type ) {
  2123. case 'post':
  2124. if ( !current_user_can('edit_others_posts') )
  2125. return(new IXR_Error(401, __('You are not allowed to change the post author as this user.')));
  2126. break;
  2127. case 'page':
  2128. if ( !current_user_can('edit_others_pages') )
  2129. return(new IXR_Error(401, __('You are not allowed to change the page author as this user.')));
  2130. break;
  2131. default:
  2132. return(new IXR_Error(401, __('Invalid post type.')));
  2133. break;
  2134. }
  2135. $post_author = $content_struct['wp_author_id'];
  2136. }
  2137. if ( isset($content_struct['mt_allow_comments']) ) {
  2138. if ( !is_numeric($content_struct['mt_allow_comments']) ) {
  2139. switch ( $content_struct['mt_allow_comments'] ) {
  2140. case 'closed':
  2141. $comment_status = 'closed';
  2142. break;
  2143. case 'open':
  2144. $comment_status = 'open';
  2145. break;
  2146. default:
  2147. $comment_status = get_option('default_comment_status');
  2148. break;
  2149. }
  2150. } else {
  2151. switch ( (int) $content_struct['mt_allow_comments'] ) {
  2152. case 0:
  2153. case 2:
  2154. $comment_status = 'closed';
  2155. break;
  2156. case 1:
  2157. $comment_status = 'open';
  2158. break;
  2159. default:
  2160. $comment_status = get_option('default_comment_status');
  2161. break;
  2162. }
  2163. }
  2164. }
  2165. if ( isset($content_struct['mt_allow_pings']) ) {
  2166. if ( !is_numeric($content_struct['mt_allow_pings']) ) {
  2167. switch ( $content_struct['mt_allow_pings'] ) {
  2168. case 'closed':
  2169. $ping_status = 'closed';
  2170. break;
  2171. case 'open':
  2172. $ping_status = 'open';
  2173. break;
  2174. default:
  2175. $ping_status = get_option('default_ping_status');
  2176. break;
  2177. }
  2178. } else {
  2179. switch ( (int) $content_struct["mt_allow_pings"] ) {
  2180. case 0:
  2181. $ping_status = 'closed';
  2182. break;
  2183. case 1:
  2184. $ping_status = 'open';
  2185. break;
  2186. default:
  2187. $ping_status = get_option('default_ping_status');
  2188. break;
  2189. }
  2190. }
  2191. }
  2192. $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
  2193. $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
  2194. $post_category = array();
  2195. if ( isset( $content_struct['categories'] ) ) {
  2196. $catnames = $content_struct['categories'];
  2197. if ( is_array($catnames) ) {
  2198. foreach ($catnames as $cat) {
  2199. $post_category[] = get_cat_ID($cat);
  2200. }
  2201. }
  2202. }
  2203. $post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
  2204. $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
  2205. $post_status = $publish ? 'publish' : 'draft';
  2206. if ( isset( $content_struct["{$post_type}_status"] ) ) {
  2207. switch( $content_struct["{$post_type}_status"] ) {
  2208. case 'draft':
  2209. case 'pending':
  2210. case 'private':
  2211. case 'publish':
  2212. $post_status = $content_struct["{$post_type}_status"];
  2213. break;
  2214. default:
  2215. $post_status = $publish ? 'publish' : 'draft';
  2216. break;
  2217. }
  2218. }
  2219. $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
  2220. if ( ('publish' == $post_status) ) {
  2221. if ( ( 'page' == $post_type ) && !current_user_can('publish_pages') )
  2222. return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
  2223. else if ( !current_user_can('publish_posts') )
  2224. return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
  2225. }
  2226. if ( $post_more )
  2227. $post_content = $post_content . "<!--more-->" . $post_more;
  2228. $to_ping = null;
  2229. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  2230. $to_ping = $content_struct['mt_tb_ping_urls'];
  2231. if ( is_array($to_ping) )
  2232. $to_ping = implode(' ', $to_ping);
  2233. }
  2234. // Do some timestamp voodoo
  2235. if ( !empty( $content_struct['date_created_gmt'] ) )
  2236. $dateCreated = str_replace( 'Z', '', $content_struct['date_created_gmt']->getIso() ) . 'Z'; // We know this is supposed to be GMT, so we're going to slap that Z on there by force
  2237. elseif ( !empty( $content_struct['dateCreated']) )
  2238. $dateCreated = $content_struct['dateCreated']->getIso();
  2239. if ( !empty( $dateCreated ) ) {
  2240. $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  2241. $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
  2242. } else {
  2243. $post_date = $postdata['post_date'];
  2244. $post_date_gmt = $postdata['post_date_gmt'];
  2245. }
  2246. // We've got all the data -- post it:
  2247. $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');
  2248. $result = wp_update_post($newpost, true);
  2249. if ( is_wp_error( $result ) )
  2250. return new IXR_Error(500, $result->get_error_message());
  2251. if ( !$result )
  2252. return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
  2253. // Only posts can be sticky
  2254. if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  2255. if ( $content_struct['sticky'] == true )
  2256. stick_post( $post_ID );
  2257. elseif ( $content_struct['sticky'] == false )
  2258. unstick_post( $post_ID );
  2259. }
  2260. if ( isset($content_struct['custom_fields']) )
  2261. $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  2262. // Handle enclosures
  2263. $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
  2264. $this->add_enclosure_if_new($post_ID, $thisEnclosure);
  2265. $this->attach_uploads( $ID, $post_content );
  2266. // Handle post formats if assigned, validation is handled
  2267. // earlier in this function
  2268. if ( isset( $content_struct['wp_post_format'] ) )
  2269. wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );
  2270. logIO('O',"(MW) Edited ! ID: $post_ID");
  2271. return true;
  2272. }
  2273. /**
  2274. * Retrieve post.
  2275. *
  2276. * @since 1.5.0
  2277. *
  2278. * @param array $args Method parameters.
  2279. * @return array
  2280. */
  2281. function mw_getPost($args) {
  2282. $this->escape($args);
  2283. $post_ID = (int) $args[0];
  2284. $username = $args[1];
  2285. $password = $args[2];
  2286. if ( !$user = $this->login($username, $password) )
  2287. return $this->error;
  2288. if ( !current_user_can( 'edit_post', $post_ID ) )
  2289. return new IXR_Error( 401, __( 'Sorry, you cannot edit this post.' ) );
  2290. do_action('xmlrpc_call', 'metaWeblog.getPost');
  2291. $postdata = wp_get_single_post($post_ID, ARRAY_A);
  2292. if ($postdata['post_date'] != '') {
  2293. $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date'], false);
  2294. $post_date_gmt = mysql2date('Ymd\TH:i:s', $postdata['post_date_gmt'], false);
  2295. // For drafts use the GMT version of the post date
  2296. if ( $postdata['post_status'] == 'draft' )
  2297. $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $postdata['post_date'] ), 'Ymd\TH:i:s' );
  2298. $categories = array();
  2299. $catids = wp_get_post_categories($post_ID);
  2300. foreach($catids as $catid)
  2301. $categories[] = get_cat_name($catid);
  2302. $tagnames = array();
  2303. $tags = wp_get_post_tags( $post_ID );
  2304. if ( !empty( $tags ) ) {
  2305. foreach ( $tags as $tag )
  2306. $tagnames[] = $tag->name;
  2307. $tagnames = implode( ', ', $tagnames );
  2308. } else {
  2309. $tagnames = '';
  2310. }
  2311. $post = get_extended($postdata['post_content']);
  2312. $link = post_permalink($postdata['ID']);
  2313. // Get the author info.
  2314. $author = get_userdata($postdata['post_author']);
  2315. $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
  2316. $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
  2317. // Consider future posts as published
  2318. if ( $postdata['post_status'] === 'future' )
  2319. $postdata['post_status'] = 'publish';
  2320. // Get post format
  2321. $post_format = get_post_format( $post_ID );
  2322. if ( empty( $post_format ) )
  2323. $post_format = 'standard';
  2324. $sticky = false;
  2325. if ( is_sticky( $post_ID ) )
  2326. $sticky = true;
  2327. $enclosure = array();
  2328. foreach ( (array) get_post_custom($post_ID) as $key => $val) {
  2329. if ($key == 'enclosure') {
  2330. foreach ( (array) $val as $enc ) {
  2331. $encdata = split("\n", $enc);
  2332. $enclosure['url'] = trim(htmlspecialchars($encdata[0]));
  2333. $enclosure['length'] = (int) trim($encdata[1]);
  2334. $enclosure['type'] = trim($encdata[2]);
  2335. break 2;
  2336. }
  2337. }
  2338. }
  2339. $resp = array(
  2340. 'dateCreated' => new IXR_Date($post_date),
  2341. 'userid' => $postdata['post_author'],
  2342. 'postid' => $postdata['ID'],
  2343. 'description' => $post['main'],
  2344. 'title' => $postdata['post_title'],
  2345. 'link' => $link,
  2346. 'permaLink' => $link,
  2347. // commented out because no other tool seems to use this
  2348. // 'content' => $entry['post_content'],
  2349. 'categories' => $categories,
  2350. 'mt_excerpt' => $postdata['post_excerpt'],
  2351. 'mt_text_more' => $post['extended'],
  2352. 'mt_allow_comments' => $allow_comments,
  2353. 'mt_allow_pings' => $allow_pings,
  2354. 'mt_keywords' => $tagnames,
  2355. 'wp_slug' => $postdata['post_name'],
  2356. 'wp_password' => $postdata['post_password'],
  2357. 'wp_author_id' => (string) $author->ID,
  2358. 'wp_author_display_name' => $author->display_name,
  2359. 'date_created_gmt' => new IXR_Date($post_date_gmt),
  2360. 'post_status' => $postdata['post_status'],
  2361. 'custom_fields' => $this->get_custom_fields($post_ID),
  2362. 'wp_post_format' => $post_format,
  2363. 'sticky' => $sticky
  2364. );
  2365. if ( !empty($enclosure) ) $resp['enclosure'] = $enclosure;
  2366. return $resp;
  2367. } else {
  2368. return new IXR_Error(404, __('Sorry, no such post.'));
  2369. }
  2370. }
  2371. /**
  2372. * Retrieve list of recent posts.
  2373. *
  2374. * @since 1.5.0
  2375. *
  2376. * @param array $args Method parameters.
  2377. * @return array
  2378. */
  2379. function mw_getRecentPosts($args) {
  2380. $this->escape($args);
  2381. $blog_ID = (int) $args[0];
  2382. $username = $args[1];
  2383. $password = $args[2];
  2384. if ( isset( $args[3] ) )
  2385. $query = array( 'numberposts' => absint( $args[3] ) );
  2386. else
  2387. $query = array();
  2388. if ( !$user = $this->login($username, $password) )
  2389. return $this->error;
  2390. do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');
  2391. $posts_list = wp_get_recent_posts( $query );
  2392. if ( !$posts_list )
  2393. return array( );
  2394. foreach ($posts_list as $entry) {
  2395. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  2396. continue;
  2397. $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
  2398. $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
  2399. // For drafts use the GMT version of the date
  2400. if ( $entry['post_status'] == 'draft' )
  2401. $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
  2402. $categories = array();
  2403. $catids = wp_get_post_categories($entry['ID']);
  2404. foreach( $catids as $catid )
  2405. $categories[] = get_cat_name($catid);
  2406. $tagnames = array();
  2407. $tags = wp_get_post_tags( $entry['ID'] );
  2408. if ( !empty( $tags ) ) {
  2409. foreach ( $tags as $tag ) {
  2410. $tagnames[] = $tag->name;
  2411. }
  2412. $tagnames = implode( ', ', $tagnames );
  2413. } else {
  2414. $tagnames = '';
  2415. }
  2416. $post = get_extended($entry['post_content']);
  2417. $link = post_permalink($entry['ID']);
  2418. // Get the post author info.
  2419. $author = get_userdata($entry['post_author']);
  2420. $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
  2421. $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
  2422. // Consider future posts as published
  2423. if ( $entry['post_status'] === 'future' )
  2424. $entry['post_status'] = 'publish';
  2425. // Get post format
  2426. $post_format = get_post_format( $entry['ID'] );
  2427. if ( empty( $post_format ) )
  2428. $post_format = 'standard';
  2429. $struct[] = array(
  2430. 'dateCreated' => new IXR_Date($post_date),
  2431. 'userid' => $entry['post_author'],
  2432. 'postid' => (string) $entry['ID'],
  2433. 'description' => $post['main'],
  2434. 'title' => $entry['post_title'],
  2435. 'link' => $link,
  2436. 'permaLink' => $link,
  2437. // commented out because no other tool seems to use this
  2438. // 'content' => $entry['post_content'],
  2439. 'categories' => $categories,
  2440. 'mt_excerpt' => $entry['post_excerpt'],
  2441. 'mt_text_more' => $post['extended'],
  2442. 'mt_allow_comments' => $allow_comments,
  2443. 'mt_allow_pings' => $allow_pings,
  2444. 'mt_keywords' => $tagnames,
  2445. 'wp_slug' => $entry['post_name'],
  2446. 'wp_password' => $entry['post_password'],
  2447. 'wp_author_id' => (string) $author->ID,
  2448. 'wp_author_display_name' => $author->display_name,
  2449. 'date_created_gmt' => new IXR_Date($post_date_gmt),
  2450. 'post_status' => $entry['post_status'],
  2451. 'custom_fields' => $this->get_custom_fields($entry['ID']),
  2452. 'wp_post_format' => $post_format
  2453. );
  2454. }
  2455. $recent_posts = array();
  2456. for ( $j=0; $j<count($struct); $j++ ) {
  2457. array_push($recent_posts, $struct[$j]);
  2458. }
  2459. return $recent_posts;
  2460. }
  2461. /**
  2462. * Retrieve the list of categories on a given blog.
  2463. *
  2464. * @since 1.5.0
  2465. *
  2466. * @param array $args Method parameters.
  2467. * @return array
  2468. */
  2469. function mw_getCategories($args) {
  2470. $this->escape($args);
  2471. $blog_ID = (int) $args[0];
  2472. $username = $args[1];
  2473. $password = $args[2];
  2474. if ( !$user = $this->login($username, $password) )
  2475. return $this->error;
  2476. if ( !current_user_can( 'edit_posts' ) )
  2477. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  2478. do_action('xmlrpc_call', 'metaWeblog.getCategories');
  2479. $categories_struct = array();
  2480. if ( $cats = get_categories(array('get' => 'all')) ) {
  2481. foreach ( $cats as $cat ) {
  2482. $struct['categoryId'] = $cat->term_id;
  2483. $struct['parentId'] = $cat->parent;
  2484. $struct['description'] = $cat->name;
  2485. $struct['categoryDescription'] = $cat->description;
  2486. $struct['categoryName'] = $cat->name;
  2487. $struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
  2488. $struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
  2489. $categories_struct[] = $struct;
  2490. }
  2491. }
  2492. return $categories_struct;
  2493. }
  2494. /**
  2495. * Uploads a file, following your settings.
  2496. *
  2497. * Adapted from a patch by Johann Richard.
  2498. *
  2499. * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
  2500. *
  2501. * @since 1.5.0
  2502. *
  2503. * @param array $args Method parameters.
  2504. * @return array
  2505. */
  2506. function mw_newMediaObject($args) {
  2507. global $wpdb;
  2508. $blog_ID = (int) $args[0];
  2509. $username = $wpdb->escape($args[1]);
  2510. $password = $wpdb->escape($args[2]);
  2511. $data = $args[3];
  2512. $name = sanitize_file_name( $data['name'] );
  2513. $type = $data['type'];
  2514. $bits = $data['bits'];
  2515. logIO('O', '(MW) Received '.strlen($bits).' bytes');
  2516. if ( !$user = $this->login($username, $password) )
  2517. return $this->error;
  2518. do_action('xmlrpc_call', 'metaWeblog.newMediaObject');
  2519. if ( !current_user_can('upload_files') ) {
  2520. logIO('O', '(MW) User does not have upload_files capability');
  2521. $this->error = new IXR_Error(401, __('You are not allowed to upload files to this site.'));
  2522. return $this->error;
  2523. }
  2524. if ( $upload_err = apply_filters( 'pre_upload_error', false ) )
  2525. return new IXR_Error(500, $upload_err);
  2526. if ( !empty($data['overwrite']) && ($data['overwrite'] == true) ) {
  2527. // Get postmeta info on the object.
  2528. $old_file = $wpdb->get_row("
  2529. SELECT ID
  2530. FROM {$wpdb->posts}
  2531. WHERE post_title = '{$name}'
  2532. AND post_type = 'attachment'
  2533. ");
  2534. // Delete previous file.
  2535. wp_delete_attachment($old_file->ID);
  2536. // Make sure the new name is different by pre-pending the
  2537. // previous post id.
  2538. $filename = preg_replace('/^wpid\d+-/', '', $name);
  2539. $name = "wpid{$old_file->ID}-{$filename}";
  2540. }
  2541. $upload = wp_upload_bits($name, NULL, $bits);
  2542. if ( ! empty($upload['error']) ) {
  2543. $errorString = sprintf(__('Could not write file %1$s (%2$s)'), $name, $upload['error']);
  2544. logIO('O', '(MW) ' . $errorString);
  2545. return new IXR_Error(500, $errorString);
  2546. }
  2547. // Construct the attachment array
  2548. // attach to post_id 0
  2549. $post_id = 0;
  2550. $attachment = array(
  2551. 'post_title' => $name,
  2552. 'post_content' => '',
  2553. 'post_type' => 'attachment',
  2554. 'post_parent' => $post_id,
  2555. 'post_mime_type' => $type,
  2556. 'guid' => $upload[ 'url' ]
  2557. );
  2558. // Save the data
  2559. $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
  2560. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  2561. return apply_filters( 'wp_handle_upload', array( 'file' => $name, 'url' => $upload[ 'url' ], 'type' => $type ), 'upload' );
  2562. }
  2563. /* MovableType API functions
  2564. * specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
  2565. */
  2566. /**
  2567. * Retrieve the post titles of recent posts.
  2568. *
  2569. * @since 1.5.0
  2570. *
  2571. * @param array $args Method parameters.
  2572. * @return array
  2573. */
  2574. function mt_getRecentPostTitles($args) {
  2575. $this->escape($args);
  2576. $blog_ID = (int) $args[0];
  2577. $username = $args[1];
  2578. $password = $args[2];
  2579. if ( isset( $args[3] ) )
  2580. $query = array( 'numberposts' => absint( $args[3] ) );
  2581. else
  2582. $query = array();
  2583. if ( !$user = $this->login($username, $password) )
  2584. return $this->error;
  2585. do_action('xmlrpc_call', 'mt.getRecentPostTitles');
  2586. $posts_list = wp_get_recent_posts( $query );
  2587. if ( !$posts_list ) {
  2588. $this->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
  2589. return $this->error;
  2590. }
  2591. foreach ($posts_list as $entry) {
  2592. if ( !current_user_can( 'edit_post', $entry['ID'] ) )
  2593. continue;
  2594. $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date'], false);
  2595. $post_date_gmt = mysql2date('Ymd\TH:i:s', $entry['post_date_gmt'], false);
  2596. // For drafts use the GMT version of the date
  2597. if ( $entry['post_status'] == 'draft' )
  2598. $post_date_gmt = get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $entry['post_date'] ), 'Ymd\TH:i:s' );
  2599. $struct[] = array(
  2600. 'dateCreated' => new IXR_Date($post_date),
  2601. 'userid' => $entry['post_author'],
  2602. 'postid' => (string) $entry['ID'],
  2603. 'title' => $entry['post_title'],
  2604. 'post_status' => $entry['post_status'],
  2605. 'date_created_gmt' => new IXR_Date($post_date_gmt)
  2606. );
  2607. }
  2608. $recent_posts = array();
  2609. for ( $j=0; $j<count($struct); $j++ ) {
  2610. array_push($recent_posts, $struct[$j]);
  2611. }
  2612. return $recent_posts;
  2613. }
  2614. /**
  2615. * Retrieve list of all categories on blog.
  2616. *
  2617. * @since 1.5.0
  2618. *
  2619. * @param array $args Method parameters.
  2620. * @return array
  2621. */
  2622. function mt_getCategoryList($args) {
  2623. $this->escape($args);
  2624. $blog_ID = (int) $args[0];
  2625. $username = $args[1];
  2626. $password = $args[2];
  2627. if ( !$user = $this->login($username, $password) )
  2628. return $this->error;
  2629. if ( !current_user_can( 'edit_posts' ) )
  2630. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  2631. do_action('xmlrpc_call', 'mt.getCategoryList');
  2632. $categories_struct = array();
  2633. if ( $cats = get_categories(array('hide_empty' => 0, 'hierarchical' => 0)) ) {
  2634. foreach ( $cats as $cat ) {
  2635. $struct['categoryId'] = $cat->term_id;
  2636. $struct['categoryName'] = $cat->name;
  2637. $categories_struct[] = $struct;
  2638. }
  2639. }
  2640. return $categories_struct;
  2641. }
  2642. /**
  2643. * Retrieve post categories.
  2644. *
  2645. * @since 1.5.0
  2646. *
  2647. * @param array $args Method parameters.
  2648. * @return array
  2649. */
  2650. function mt_getPostCategories($args) {
  2651. $this->escape($args);
  2652. $post_ID = (int) $args[0];
  2653. $username = $args[1];
  2654. $password = $args[2];
  2655. if ( !$user = $this->login($username, $password) )
  2656. return $this->error;
  2657. if ( !current_user_can( 'edit_post', $post_ID ) )
  2658. return new IXR_Error( 401, __( 'Sorry, you can not edit this post.' ) );
  2659. do_action('xmlrpc_call', 'mt.getPostCategories');
  2660. $categories = array();
  2661. $catids = wp_get_post_categories(intval($post_ID));
  2662. // first listed category will be the primary category
  2663. $isPrimary = true;
  2664. foreach ( $catids as $catid ) {
  2665. $categories[] = array(
  2666. 'categoryName' => get_cat_name($catid),
  2667. 'categoryId' => (string) $catid,
  2668. 'isPrimary' => $isPrimary
  2669. );
  2670. $isPrimary = false;
  2671. }
  2672. return $categories;
  2673. }
  2674. /**
  2675. * Sets categories for a post.
  2676. *
  2677. * @since 1.5.0
  2678. *
  2679. * @param array $args Method parameters.
  2680. * @return bool True on success.
  2681. */
  2682. function mt_setPostCategories($args) {
  2683. $this->escape($args);
  2684. $post_ID = (int) $args[0];
  2685. $username = $args[1];
  2686. $password = $args[2];
  2687. $categories = $args[3];
  2688. if ( !$user = $this->login($username, $password) )
  2689. return $this->error;
  2690. do_action('xmlrpc_call', 'mt.setPostCategories');
  2691. if ( !current_user_can('edit_post', $post_ID) )
  2692. return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
  2693. foreach ( $categories as $cat ) {
  2694. $catids[] = $cat['categoryId'];
  2695. }
  2696. wp_set_post_categories($post_ID, $catids);
  2697. return true;
  2698. }
  2699. /**
  2700. * Retrieve an array of methods supported by this server.
  2701. *
  2702. * @since 1.5.0
  2703. *
  2704. * @param array $args Method parameters.
  2705. * @return array
  2706. */
  2707. function mt_supportedMethods($args) {
  2708. do_action('xmlrpc_call', 'mt.supportedMethods');
  2709. $supported_methods = array();
  2710. foreach ( $this->methods as $key => $value ) {
  2711. $supported_methods[] = $key;
  2712. }
  2713. return $supported_methods;
  2714. }
  2715. /**
  2716. * Retrieve an empty array because we don't support per-post text filters.
  2717. *
  2718. * @since 1.5.0
  2719. *
  2720. * @param array $args Method parameters.
  2721. */
  2722. function mt_supportedTextFilters($args) {
  2723. do_action('xmlrpc_call', 'mt.supportedTextFilters');
  2724. return apply_filters('xmlrpc_text_filters', array());
  2725. }
  2726. /**
  2727. * Retrieve trackbacks sent to a given post.
  2728. *
  2729. * @since 1.5.0
  2730. *
  2731. * @param array $args Method parameters.
  2732. * @return mixed
  2733. */
  2734. function mt_getTrackbackPings($args) {
  2735. global $wpdb;
  2736. $post_ID = intval($args);
  2737. do_action('xmlrpc_call', 'mt.getTrackbackPings');
  2738. $actual_post = wp_get_single_post($post_ID, ARRAY_A);
  2739. if ( !$actual_post )
  2740. return new IXR_Error(404, __('Sorry, no such post.'));
  2741. $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) );
  2742. if ( !$comments )
  2743. return array();
  2744. $trackback_pings = array();
  2745. foreach ( $comments as $comment ) {
  2746. if ( 'trackback' == $comment->comment_type ) {
  2747. $content = $comment->comment_content;
  2748. $title = substr($content, 8, (strpos($content, '</strong>') - 8));
  2749. $trackback_pings[] = array(
  2750. 'pingTitle' => $title,
  2751. 'pingURL' => $comment->comment_author_url,
  2752. 'pingIP' => $comment->comment_author_IP
  2753. );
  2754. }
  2755. }
  2756. return $trackback_pings;
  2757. }
  2758. /**
  2759. * Sets a post's publish status to 'publish'.
  2760. *
  2761. * @since 1.5.0
  2762. *
  2763. * @param array $args Method parameters.
  2764. * @return int
  2765. */
  2766. function mt_publishPost($args) {
  2767. $this->escape($args);
  2768. $post_ID = (int) $args[0];
  2769. $username = $args[1];
  2770. $password = $args[2];
  2771. if ( !$user = $this->login($username, $password) )
  2772. return $this->error;
  2773. do_action('xmlrpc_call', 'mt.publishPost');
  2774. if ( !current_user_can('publish_posts') || !current_user_can('edit_post', $post_ID) )
  2775. return new IXR_Error(401, __('Sorry, you cannot publish this post.'));
  2776. $postdata = wp_get_single_post($post_ID,ARRAY_A);
  2777. $postdata['post_status'] = 'publish';
  2778. // retain old cats
  2779. $cats = wp_get_post_categories($post_ID);
  2780. $postdata['post_category'] = $cats;
  2781. $this->escape($postdata);
  2782. $result = wp_update_post($postdata);
  2783. return $result;
  2784. }
  2785. /* PingBack functions
  2786. * specs on www.hixie.ch/specs/pingback/pingback
  2787. */
  2788. /**
  2789. * Retrieves a pingback and registers it.
  2790. *
  2791. * @since 1.5.0
  2792. *
  2793. * @param array $args Method parameters.
  2794. * @return array
  2795. */
  2796. function pingback_ping($args) {
  2797. global $wpdb;
  2798. do_action('xmlrpc_call', 'pingback.ping');
  2799. $this->escape($args);
  2800. $pagelinkedfrom = $args[0];
  2801. $pagelinkedto = $args[1];
  2802. $title = '';
  2803. $pagelinkedfrom = str_replace('&amp;', '&', $pagelinkedfrom);
  2804. $pagelinkedto = str_replace('&amp;', '&', $pagelinkedto);
  2805. $pagelinkedto = str_replace('&', '&amp;', $pagelinkedto);
  2806. // Check if the page linked to is in our site
  2807. $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
  2808. if ( !$pos1 )
  2809. return new IXR_Error(0, __('Is there no link to us?'));
  2810. // let's find which post is linked to
  2811. // FIXME: does url_to_postid() cover all these cases already?
  2812. // if so, then let's use it and drop the old code.
  2813. $urltest = parse_url($pagelinkedto);
  2814. if ( $post_ID = url_to_postid($pagelinkedto) ) {
  2815. $way = 'url_to_postid()';
  2816. } elseif ( preg_match('#p/[0-9]{1,}#', $urltest['path'], $match) ) {
  2817. // the path defines the post_ID (archives/p/XXXX)
  2818. $blah = explode('/', $match[0]);
  2819. $post_ID = (int) $blah[1];
  2820. $way = 'from the path';
  2821. } elseif ( preg_match('#p=[0-9]{1,}#', $urltest['query'], $match) ) {
  2822. // the querystring defines the post_ID (?p=XXXX)
  2823. $blah = explode('=', $match[0]);
  2824. $post_ID = (int) $blah[1];
  2825. $way = 'from the querystring';
  2826. } elseif ( isset($urltest['fragment']) ) {
  2827. // an #anchor is there, it's either...
  2828. if ( intval($urltest['fragment']) ) {
  2829. // ...an integer #XXXX (simplest case)
  2830. $post_ID = (int) $urltest['fragment'];
  2831. $way = 'from the fragment (numeric)';
  2832. } elseif ( preg_match('/post-[0-9]+/',$urltest['fragment']) ) {
  2833. // ...a post id in the form 'post-###'
  2834. $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
  2835. $way = 'from the fragment (post-###)';
  2836. } elseif ( is_string($urltest['fragment']) ) {
  2837. // ...or a string #title, a little more complicated
  2838. $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
  2839. $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
  2840. if (! ($post_ID = $wpdb->get_var($sql)) ) {
  2841. // returning unknown error '0' is better than die()ing
  2842. return new IXR_Error(0, '');
  2843. }
  2844. $way = 'from the fragment (title)';
  2845. }
  2846. } else {
  2847. // TODO: Attempt to extract a post ID from the given URL
  2848. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  2849. }
  2850. $post_ID = (int) $post_ID;
  2851. logIO("O","(PB) URL='$pagelinkedto' ID='$post_ID' Found='$way'");
  2852. $post = get_post($post_ID);
  2853. if ( !$post ) // Post_ID not found
  2854. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  2855. if ( $post_ID == url_to_postid($pagelinkedfrom) )
  2856. return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
  2857. // Check if pings are on
  2858. if ( !pings_open($post) )
  2859. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  2860. // Let's check that the remote site didn't already pingback this entry
  2861. if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
  2862. return new IXR_Error( 48, __( 'The pingback has already been registered.' ) );
  2863. // very stupid, but gives time to the 'from' server to publish !
  2864. sleep(1);
  2865. // Let's check the remote site
  2866. $linea = wp_remote_fopen( $pagelinkedfrom );
  2867. if ( !$linea )
  2868. return new IXR_Error(16, __('The source URL does not exist.'));
  2869. $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
  2870. // Work around bug in strip_tags():
  2871. $linea = str_replace('<!DOC', '<DOC', $linea);
  2872. $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
  2873. $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
  2874. preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
  2875. $title = $matchtitle[1];
  2876. if ( empty( $title ) )
  2877. return new IXR_Error(32, __('We cannot find a title on that page.'));
  2878. $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
  2879. $p = explode( "\n\n", $linea );
  2880. $preg_target = preg_quote($pagelinkedto, '|');
  2881. foreach ( $p as $para ) {
  2882. if ( strpos($para, $pagelinkedto) !== false ) { // it exists, but is it a link?
  2883. preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
  2884. // If the URL isn't in a link context, keep looking
  2885. if ( empty($context) )
  2886. continue;
  2887. // We're going to use this fake tag to mark the context in a bit
  2888. // the marker is needed in case the link text appears more than once in the paragraph
  2889. $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
  2890. // prevent really long link text
  2891. if ( strlen($context[1]) > 100 )
  2892. $context[1] = substr($context[1], 0, 100) . '...';
  2893. $marker = '<wpcontext>'.$context[1].'</wpcontext>'; // set up our marker
  2894. $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
  2895. $excerpt = strip_tags($excerpt, '<wpcontext>'); // strip all tags but our context marker
  2896. $excerpt = trim($excerpt);
  2897. $preg_marker = preg_quote($marker, '|');
  2898. $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
  2899. $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
  2900. break;
  2901. }
  2902. }
  2903. if ( empty($context) ) // Link to target not found
  2904. return new IXR_Error(17, __('The source URL does not contain a link to the target URL, and so cannot be used as a source.'));
  2905. $pagelinkedfrom = str_replace('&', '&amp;', $pagelinkedfrom);
  2906. $context = '[...] ' . esc_html( $excerpt ) . ' [...]';
  2907. $pagelinkedfrom = $wpdb->escape( $pagelinkedfrom );
  2908. $comment_post_ID = (int) $post_ID;
  2909. $comment_author = $title;
  2910. $comment_author_email = '';
  2911. $this->escape($comment_author);
  2912. $comment_author_url = $pagelinkedfrom;
  2913. $comment_content = $context;
  2914. $this->escape($comment_content);
  2915. $comment_type = 'pingback';
  2916. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_content', 'comment_type');
  2917. $comment_ID = wp_new_comment($commentdata);
  2918. do_action('pingback_post', $comment_ID);
  2919. return sprintf(__('Pingback from %1$s to %2$s registered. Keep the web talking! :-)'), $pagelinkedfrom, $pagelinkedto);
  2920. }
  2921. /**
  2922. * Retrieve array of URLs that pingbacked the given URL.
  2923. *
  2924. * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
  2925. *
  2926. * @since 1.5.0
  2927. *
  2928. * @param array $args Method parameters.
  2929. * @return array
  2930. */
  2931. function pingback_extensions_getPingbacks($args) {
  2932. global $wpdb;
  2933. do_action('xmlrpc_call', 'pingback.extensions.getPingbacks');
  2934. $this->escape($args);
  2935. $url = $args;
  2936. $post_ID = url_to_postid($url);
  2937. if ( !$post_ID ) {
  2938. // We aren't sure that the resource is available and/or pingback enabled
  2939. return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
  2940. }
  2941. $actual_post = wp_get_single_post($post_ID, ARRAY_A);
  2942. if ( !$actual_post ) {
  2943. // No such post = resource not found
  2944. return new IXR_Error(32, __('The specified target URL does not exist.'));
  2945. }
  2946. $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) );
  2947. if ( !$comments )
  2948. return array();
  2949. $pingbacks = array();
  2950. foreach ( $comments as $comment ) {
  2951. if ( 'pingback' == $comment->comment_type )
  2952. $pingbacks[] = $comment->comment_author_url;
  2953. }
  2954. return $pingbacks;
  2955. }
  2956. }
  2957. ?>