Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tried installing this file as plugin and it give me XML-server parse errors, so had to de-activate.
- What is wrong with my code?
- <?php
- /***************************************************************************/
- /* Plugin Name: post_decode.php */
- /* Plugin URI: http://nyledavis.com/code/wordpress/plugin-post_decode/ */
- /* Description: Code to allow the extension of XML-RPC processing to work */
- /* with the content/description string that contains HTML */
- /* tags which cause XML parser errors and thus must be */
- /* encoded with either "htmlentities" or "htmlspecialchars" */
- /* and thus need decoding, so the HTML is in native format or */
- /* code and thus the HTML formatting is maintained. */
- /* Version: 0.1.0 */
- /* Author: Nyle E. Davis */
- /* Create Date: 11/06/2010 */
- /***************************************************************************/
- function post_decode ($text, $type, $charset = 'Windows-1252') {
- if ($type=='entities') {
- return html_entity_decode ($text, ENT_COMPAT, $charset, false);
- }
- if ($type=='chars') {
- return htmlspecialchars_decode ($text, ENT_COMPAT, $charset, false);
- }
- } // end function
- add_filter( 'xmlrpc_methods', 'post_decode' );
- function mw_newPost($args) {
- $this->escape($args);
- $blog_ID = (int) $args[0]; // we will support this in the near future
- $username = $args[1];
- $password = $args[2];
- $content_struct = $args[3];
- $publish = $args[4];
- if ( !$user = $this->login($username, $password) )
- return $this->error;
- do_action('xmlrpc_call', 'metaWeblog.newPost');
- $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
- $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
- $post_type = 'post';
- $page_template = '';
- if ( !empty( $content_struct['post_type'] ) ) {
- if ( $content_struct['post_type'] == 'page' ) {
- $cap = ( $publish ) ? 'publish_pages' : 'edit_pages';
- $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
- $post_type = 'page';
- if ( !empty( $content_struct['wp_page_template'] ) )
- $page_template = $content_struct['wp_page_template'];
- } elseif ( $content_struct['post_type'] == 'post' ) {
- // This is the default, no changes needed
- } else {
- // No other post_type values are allowed here
- return new IXR_Error( 401, __( 'Invalid post type.' ) );
- }
- }
- if ( !current_user_can( $cap ) )
- return new IXR_Error( 401, $error_message );
- // Let WordPress generate the post_name (slug) unless
- // one has been provided.
- $post_name = "";
- if ( isset($content_struct["wp_slug"]) )
- $post_name = $content_struct["wp_slug"];
- // Only use a password if one was given.
- if ( isset($content_struct["wp_password"]) )
- $post_password = $content_struct["wp_password"];
- // Only set a post parent if one was provided.
- if ( isset($content_struct["wp_page_parent_id"]) )
- $post_parent = $content_struct["wp_page_parent_id"];
- // Only set the menu_order if it was provided.
- if ( isset($content_struct["wp_page_order"]) )
- $menu_order = $content_struct["wp_page_order"];
- $post_author = $user->ID;
- // If an author id was provided then use it instead.
- if ( isset($content_struct["wp_author_id"]) && ($user->ID != $content_struct["wp_author_id"]) ) {
- switch ( $post_type ) {
- case "post":
- if ( !current_user_can("edit_others_posts") )
- return(new IXR_Error(401, __("You are not allowed to post as this user")));
- break;
- case "page":
- if ( !current_user_can("edit_others_pages") )
- return(new IXR_Error(401, __("You are not allowed to create pages as this user")));
- break;
- default:
- return(new IXR_Error(401, __("Invalid post type.")));
- break;
- }
- $post_author = $content_struct["wp_author_id"];
- }
- $post_title = $content_struct['title'];
- // Following 5 lines added/modified by Nyle Davis on 11/06/10
- // to decode encoded HTML content
- $post_decode = $content_struct['post_decode'];
- $post_dec_type = $content_struct['post_dec_type'];
- $post_dec_char = $content_struct['post_dec_char'];
- $post_desc = $content_struct['description'];
- $post_content = post_decode($post_desc,$post_dec_type,$post_dec_char);
- $post_status = $publish ? 'publish' : 'draft';
- if ( isset( $content_struct["{$post_type}_status"] ) ) {
- switch ( $content_struct["{$post_type}_status"] ) {
- case 'draft':
- case 'private':
- case 'publish':
- $post_status = $content_struct["{$post_type}_status"];
- break;
- case 'pending':
- // Pending is only valid for posts, not pages.
- if ( $post_type === 'post' )
- $post_status = $content_struct["{$post_type}_status"];
- break;
- default:
- $post_status = $publish ? 'publish' : 'draft';
- break;
- }
- }
- $post_excerpt = $content_struct['mt_excerpt'];
- $post_more = $content_struct['mt_text_more'];
- $tags_input = $content_struct['mt_keywords'];
- if ( isset($content_struct["mt_allow_comments"]) ) {
- if ( !is_numeric($content_struct["mt_allow_comments"]) ) {
- switch ( $content_struct["mt_allow_comments"] ) {
- case "closed":
- $comment_status = "closed";
- break;
- case "open":
- $comment_status = "open";
- break;
- default:
- $comment_status = get_option("default_comment_status");
- break;
- }
- } else {
- switch ( (int) $content_struct["mt_allow_comments"] ) {
- case 0:
- case 2:
- $comment_status = "closed";
- break;
- case 1:
- $comment_status = "open";
- break;
- default:
- $comment_status = get_option("default_comment_status");
- break;
- }
- }
- } else {
- $comment_status = get_option("default_comment_status");
- }
- if ( isset($content_struct["mt_allow_pings"]) ) {
- if ( !is_numeric($content_struct["mt_allow_pings"]) ) {
- switch ( $content_struct['mt_allow_pings'] ) {
- case "closed":
- $ping_status = "closed";
- break;
- case "open":
- $ping_status = "open";
- break;
- default:
- $ping_status = get_option("default_ping_status");
- break;
- }
- } else {
- switch ( (int) $content_struct["mt_allow_pings"] ) {
- case 0:
- $ping_status = "closed";
- break;
- case 1:
- $ping_status = "open";
- break;
- default:
- $ping_status = get_option("default_ping_status");
- break;
- }
- }
- } else {
- $ping_status = get_option("default_ping_status");
- }
- if ( $post_more )
- $post_content = $post_content . "<!--more-->" . $post_more;
- $to_ping = $content_struct['mt_tb_ping_urls'];
- if ( is_array($to_ping) )
- $to_ping = implode(' ', $to_ping);
- // Do some timestamp voodoo
- if ( !empty( $content_struct['date_created_gmt'] ) )
- $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
- elseif ( !empty( $content_struct['dateCreated']) )
- $dateCreated = $content_struct['dateCreated']->getIso();
- if ( !empty( $dateCreated ) ) {
- $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
- $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
- } else {
- $post_date = current_time('mysql');
- $post_date_gmt = current_time('mysql', 1);
- }
- $catnames = $content_struct['categories'];
- logIO('O', 'Post cats: ' . var_export($catnames,true));
- $post_category = array();
- if ( is_array($catnames) ) {
- foreach ($catnames as $cat) {
- $post_category[] = get_cat_ID($cat);
- }
- }
- // We've got all the data -- post it:
- $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');
- $post_ID = wp_insert_post($postdata, true);
- if ( is_wp_error( $post_ID ) )
- return new IXR_Error(500, $post_ID->get_error_message());
- if ( !$post_ID )
- return new IXR_Error(500, __('Sorry, your entry could not be posted. Something wrong happened.'));
- // Only posts can be sticky
- if ( $post_type == 'post' && isset( $content_struct['sticky'] ) ) {
- if ( $content_struct['sticky'] == true )
- stick_post( $post_ID );
- elseif ( $content_struct['sticky'] == false )
- unstick_post( $post_ID );
- }
- if ( isset($content_struct['custom_fields']) )
- $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
- // Handle enclosures
- $this->add_enclosure_if_new($post_ID, $content_struct['enclosure']);
- $this->attach_uploads( $post_ID, $post_content );
- logIO('O', "Posted ! ID: $post_ID");
- return strval($post_ID);
- }
- add_filter( 'xmlrpc_methods', 'mw_newPost' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement