PageRenderTime 88ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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