XMLRPCでWordPressのカスタム投稿タイプに投稿する

「XMLRPC カスタム投稿」などで検索エンジンからこられる方がいらっしゃるので、投稿の仕方をちょっと・・・
「metaWeblog.newPost」のPostTypeはPostかPageしかないので、カスタムPostTypeを投稿するとエラーが返ってきます。
「/wp-includes/class-wp-xmlrpc-server.php」を参照。
なので、XMLRPCを拡張する必要があります。

「metaWeblog.newPost」をコピーして、カスタム投稿タイプが「movies」の場合は、

// function.php
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');

function wpex_xmlrpc_server($class_name) {
    return 'wpex_xmlrpc_server';
}
add_filter('wp_xmlrpc_server_class', 'wpex_xmlrpc_server');

class wpex_xmlrpc_server extends wp_xmlrpc_server {
    function __construct() {
        parent::__construct();
        $this->methods['wpex.newPost'] = 'this:wpex_newPost';
    }

    // metaWeblog.newPostをコピーしてリネーム
    function wpex_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     = isset( $args[4] ) ? $args[4] : 0;

        if ( !$user = $this->login($username, $password) )
            return $this->error;

        do_action('xmlrpc_call', 'metaWeblog.newPost');

        $page_template = '';
        if ( !empty( $content_struct['post_type'] ) ) {
            if ( $content_struct['post_type'] == 'page' ) {
                if ( $publish )
                    $cap  = 'publish_pages';
                elseif ('publish' == $content_struct['page_status'])
                    $cap  = 'publish_pages';
                else
                    $cap = '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' ) {
                if ( $publish )
                    $cap  = 'publish_posts';
                elseif ('publish' == $content_struct['post_status'])
                    $cap  = 'publish_posts';
                else
                    $cap = 'edit_posts';
                $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
                $post_type = 'post';
            } elseif ( $content_struct['post_type'] == 'movies' ) {
                if ( $publish )
                    $cap  = 'publish_posts';
                elseif ('publish' == $content_struct['post_status'])
                    $cap  = 'publish_posts';
                else
                    $cap = 'edit_posts';
                $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
                $post_type = 'movies';
            } else {
                // No other post_type values are allowed here
                return new IXR_Error( 401, __( 'Invalid post type.' ) );
            }
        } else {
            if ( $publish )
                $cap  = 'publish_posts';
            elseif ('publish' == $content_struct['post_status'])
                $cap  = 'publish_posts';
            else
                $cap = 'edit_posts';
            $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
            $post_type = 'post';
        }

        if ( !current_user_can( $cap ) )
            return new IXR_Error( 401, $error_message );

        // Check for a valid post format if one was given
        if ( isset( $content_struct['wp_post_format'] ) ) {
            $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
            if ( !array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
                return new IXR_Error( 404, __( 'Invalid post format' ) );
            }
        }

        // 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 "movies":
                    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 = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
        $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;

        $post_status = $publish ? 'publish' : 'draft';

        if ( isset( $content_struct["{$post_type}_status"] ) ) {
            switch ( $content_struct["{$post_type}_status"] ) {
                case 'draft':
                case 'pending':
                case 'private':
                case 'publish':
                    $post_status = $content_struct["{$post_type}_status"];
                    break;
                default:
                    $post_status = $publish ? 'publish' : 'draft';
                    break;
            }
        }

        $post_excerpt = isset($content_struct['mt_excerpt']) ? $content_struct['mt_excerpt'] : null;
        $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;

        $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;

        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 . '<span id="more-509"></span>' . $post_more;

        $to_ping = null;
        if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
            $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);
        }

        $post_category = array();
        if ( isset( $content_struct['categories'] ) ) {
            $catnames = $content_struct['categories'];
            logIO('O', 'Post cats: ' . var_export($catnames,true));

            if ( is_array($catnames) ) {
                foreach ($catnames as $cat) {
                    $post_category[] = get_cat_ID($cat);
                }
            }
        }

        $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 = $postdata['ID'] = get_default_post_to_edit( $post_type, true )->ID;

        // 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
        $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
        $this->add_enclosure_if_new($post_ID, $thisEnclosure);

        $this->attach_uploads( $post_ID, $post_content );

        // Handle post formats if assigned, value is validated earlier
        // in this function
        if ( isset( $content_struct['wp_post_format'] ) )
            wp_set_post_terms( $post_ID, array( 'post-format-' . $content_struct['wp_post_format'] ), 'post_format' );

        $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.'));

        logIO('O', "Posted ! ID: $post_ID");

        return strval($post_ID);
    }
}

「movies」でコードを検索すると2カ所あります。

投稿する側のプログラムは

$post_type = 'movies';
$content = new XML_RPC_Value(
                    array(
                        'title' => new XML_RPC_Value($title, 'string'),
                        'post_type' => new XML_RPC_Value($post_type, 'string'),
                        'categories' => new XML_RPC_Value($categories, 'array'),
                        'description' => new XML_RPC_Value($description, 'string'),
                        'custom_fields' => new XML_RPC_Value($custom_fields, 'struct'),
                        'mt_keywords' => new XML_RPC_Value($mt_keywords, 'string'),
                        'dateCreated' => new XML_RPC_Value($hatudaidate, 'dateTime.iso8601')
                    ),
                    'struct'
);
$publish = new XML_RPC_Value(1, 'boolean');
$message = new XML_RPC_Message(
                        'wpex.newPost',
                        array($blog_id, $username, $passwd, $content, $publish)
);
$result = $c->send($message);

こんな感じで投稿すると、カスタム投稿タイプに投稿されます。

「XMLRPCでWordPressのカスタム投稿タイプに投稿する」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)