PageRenderTime 65ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

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