Advertisement
Guest User

Untitled

a guest
Nov 11th, 2010
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. Tried installing this file as plugin and it give me XML-server parse errors, so had to de-activate.
  2.  
  3. What is wrong with my code?
  4.  
  5. <?php
  6. /***************************************************************************/
  7. /* Plugin Name: post_decode.php */
  8. /* Plugin URI: http://nyledavis.com/code/wordpress/plugin-post_decode/ */
  9. /* Description: Code to allow the extension of XML-RPC processing to work */
  10. /* with the content/description string that contains HTML */
  11. /* tags which cause XML parser errors and thus must be */
  12. /* encoded with either "htmlentities" or "htmlspecialchars" */
  13. /* and thus need decoding, so the HTML is in native format or */
  14. /* code and thus the HTML formatting is maintained. */
  15. /* Version: 0.1.0 */
  16. /* Author: Nyle E. Davis */
  17. /* Create Date: 11/06/2010 */
  18. /***************************************************************************/
  19.  
  20. function post_decode ($text, $type, $charset = 'Windows-1252') {
  21. if ($type=='entities') {
  22. return html_entity_decode ($text, ENT_COMPAT, $charset, false);
  23. }
  24. if ($type=='chars') {
  25. return htmlspecialchars_decode ($text, ENT_COMPAT, $charset, false);
  26. }
  27. } // end function
  28.  
  29. add_filter( 'xmlrpc_methods', 'post_decode' );
  30.  
  31. function mw_newPost($args) {
  32. $this->escape($args);
  33.  
  34. $blog_ID = (int) $args[0]; // we will support this in the near future
  35. $username = $args[1];
  36. $password = $args[2];
  37. $content_struct = $args[3];
  38. $publish = $args[4];
  39.  
  40. if ( !$user = $this->login($username, $password) )
  41. return $this->error;
  42.  
  43. do_action('xmlrpc_call', 'metaWeblog.newPost');
  44.  
  45. $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
  46. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  47. $post_type = 'post';
  48. $page_template = '';
  49. if ( !empty( $content_struct['post_type'] ) ) {
  50. if ( $content_struct['post_type'] == 'page' ) {
  51. $cap = ( $publish ) ? 'publish_pages' : 'edit_pages';
  52. $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  53. $post_type = 'page';
  54. if ( !empty( $content_struct['wp_page_template'] ) )
  55. $page_template = $content_struct['wp_page_template'];
  56. } elseif ( $content_struct['post_type'] == 'post' ) {
  57. // This is the default, no changes needed
  58. } else {
  59. // No other post_type values are allowed here
  60. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  61. }
  62. }
  63.  
  64. if ( !current_user_can( $cap ) )
  65. return new IXR_Error( 401, $error_message );
  66.  
  67. // Let WordPress generate the post_name (slug) unless
  68. // one has been provided.
  69. $post_name = "";
  70. if ( isset($content_struct["wp_slug"]) )
  71. $post_name = $content_struct["wp_slug"];
  72.  
  73. // Only use a password if one was given.
  74. if ( isset($content_struct["wp_password"]) )
  75. $post_password = $content_struct["wp_password"];
  76.  
  77. // Only set a post parent if one was provided.
  78. if ( isset($content_struct["wp_page_parent_id"]) )
  79. $post_parent = $content_struct["wp_page_parent_id"];
  80.  
  81. // Only set the menu_order if it was provided.
  82. if ( isset($content_struct["wp_page_order"]) )
  83. $menu_order = $content_struct["wp_page_order"];
  84.  
  85. $post_author = $user->ID;
  86.  
  87. // If an author id was provided then use it instead.
  88. if ( isset($content_struct["wp_author_id"]) && ($user->ID != $content_struct["wp_author_id"]) ) {
  89. switch ( $post_type ) {
  90. case "post":
  91. if ( !current_user_can("edit_others_posts") )
  92. return(new IXR_Error(401, __("You are not allowed to post as this user")));
  93. break;
  94. case "page":
  95. if ( !current_user_can("edit_others_pages") )
  96. return(new IXR_Error(401, __("You are not allowed to create pages as this user")));
  97. break;
  98. default:
  99. return(new IXR_Error(401, __("Invalid post type.")));
  100. break;
  101. }
  102. $post_author = $content_struct["wp_author_id"];
  103. }
  104.  
  105. $post_title = $content_struct['title'];
  106. // Following 5 lines added/modified by Nyle Davis on 11/06/10
  107. // to decode encoded HTML content
  108. $post_decode = $content_struct['post_decode'];
  109. $post_dec_type = $content_struct['post_dec_type'];
  110. $post_dec_char = $content_struct['post_dec_char'];
  111. $post_desc = $content_struct['description'];
  112. $post_content = post_decode($post_desc,$post_dec_type,$post_dec_char);
  113.  
  114. $post_status = $publish ? 'publish' : 'draft';
  115.  
  116. if ( isset( $content_struct["{$post_type}_status"] ) ) {
  117. switch ( $content_struct["{$post_type}_status"] ) {
  118. case 'draft':
  119. case 'private':
  120. case 'publish':
  121. $post_status = $content_struct["{$post_type}_status"];
  122. break;
  123. case 'pending':
  124. // Pending is only valid for posts, not pages.
  125. if ( $post_type === 'post' )
  126. $post_status = $content_struct["{$post_type}_status"];
  127. break;
  128. default:
  129. $post_status = $publish ? 'publish' : 'draft';
  130. break;
  131. }
  132. }
  133.  
  134. $post_excerpt = $content_struct['mt_excerpt'];
  135. $post_more = $content_struct['mt_text_more'];
  136.  
  137. $tags_input = $content_struct['mt_keywords'];
  138.  
  139. if ( isset($content_struct["mt_allow_comments"]) ) {
  140. if ( !is_numeric($content_struct["mt_allow_comments"]) ) {
  141. switch ( $content_struct["mt_allow_comments"] ) {
  142. case "closed":
  143. $comment_status = "closed";
  144. break;
  145. case "open":
  146. $comment_status = "open";
  147. break;
  148. default:
  149. $comment_status = get_option("default_comment_status");
  150. break;
  151. }
  152. } else {
  153. switch ( (int) $content_struct["mt_allow_comments"] ) {
  154. case 0:
  155. case 2:
  156. $comment_status = "closed";
  157. break;
  158. case 1:
  159. $comment_status = "open";
  160. break;
  161. default:
  162. $comment_status = get_option("default_comment_status");
  163. break;
  164. }
  165. }
  166. } else {
  167. $comment_status = get_option("default_comment_status");
  168. }
  169.  
  170. if ( isset($content_struct["mt_allow_pings"]) ) {
  171. if ( !is_numeric($content_struct["mt_allow_pings"]) ) {
  172. switch ( $content_struct['mt_allow_pings'] ) {
  173. case "closed":
  174. $ping_status = "closed";
  175. break;
  176. case "open":
  177. $ping_status = "open";
  178. break;
  179. default:
  180. $ping_status = get_option("default_ping_status");
  181. break;
  182. }
  183. } else {
  184. switch ( (int) $content_struct["mt_allow_pings"] ) {
  185. case 0:
  186. $ping_status = "closed";
  187. break;
  188. case 1:
  189. $ping_status = "open";
  190. break;
  191. default:
  192. $ping_status = get_option("default_ping_status");
  193. break;
  194. }
  195. }
  196. } else {
  197. $ping_status = get_option("default_ping_status");
  198. }
  199.  
  200. if ( $post_more )
  201. $post_content = $post_content . "<!--more-->" . $post_more;
  202.  
  203. $to_ping = $content_struct['mt_tb_ping_urls'];
  204. if ( is_array($to_ping) )
  205. $to_ping = implode(' ', $to_ping);
  206.  
  207. // Do some timestamp voodoo
  208. if ( !empty( $content_struct['date_created_gmt'] ) )
  209. $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
  210. elseif ( !empty( $content_struct['dateCreated']) )
  211. $dateCreated = $content_struct['dateCreated']->getIso();
  212.  
  213. if ( !empty( $dateCreated ) ) {
  214. $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
  215. $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
  216. } else {
  217. $post_date = current_time('mysql');
  218. $post_date_gmt = current_time('mysql', 1);
  219. }
  220.  
  221. $catnames = $content_struct['categories'];
  222. logIO('O', 'Post cats: ' . var_export($catnames,true));
  223. $post_category = array();
  224.  
  225. if ( is_array($catnames) ) {
  226. foreach ($catnames as $cat) {
  227. $post_category[] = get_cat_ID($cat);
  228. }
  229. }
  230.  
  231. // We've got all the data -- post it:
  232. $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');
  233.  
  234. $post_ID = wp_insert_post($postdata, true);
  235. if ( is_wp_error( $post_ID ) )
  236. return new IXR_Error(500, $post_ID->get_error_message());
  237.  
  238. if ( !$post_ID )
  239. return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
  240.  
  241. // Only posts can be sticky
  242. if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
  243. if ( $content_struct['sticky'] == true )
  244. stick_post( $post_ID );
  245. elseif ( $content_struct['sticky'] == false )
  246. unstick_post( $post_ID );
  247. }
  248.  
  249. if ( isset($content_struct['custom_fields']) )
  250. $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
  251.  
  252. // Handle enclosures
  253. $this->add_enclosure_if_new($post_ID, $content_struct['enclosure']);
  254.  
  255. $this->attach_uploads( $post_ID, $post_content );
  256.  
  257. logIO('O', "Posted ! ID: $post_ID");
  258.  
  259. return strval($post_ID);
  260. }
  261. add_filter( 'xmlrpc_methods', 'mw_newPost' );
  262.  
  263. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement