Make WordPress Core

Changeset 4793


Ignore:
Timestamp:
01/24/2007 01:16:08 AM (20 years ago)
Author:
ryan
Message:

WP XML-RPC API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xmlrpc.php

    r4710 r4793  
    6868        function wp_xmlrpc_server() {
    6969                $this->methods = array(
     70                        // WordPress API
     71                        'wp.getPage'                    => 'this:wp_getPage',
     72                        'wp.getPages'                   => 'this:wp_getPages',
     73                        'wp.newPage'                    => 'this:wp_newPage',
     74                        'wp.deletePage'                 => 'this:wp_deletePage',
     75                        'wp.editPage'                   => 'this:wp_editPage',
     76                        'wp.getPageList'                => 'this:wp_getPageList',
     77                        'wp.getAuthors'                 => 'this:wp_getAuthors',
     78                        'wp.getCategories'              => 'this:mw_getCategories',             // Alias
     79                        'wp.newCategory'                => 'this:wp_newCategory',
     80                        'wp.suggestCategories'  => 'this:wp_suggestCategories',
     81
    7082                        // Blogger API
    7183                        'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
     
    147159        }
    148160
     161        /**
     162         * WordPress XML-RPC API
     163         * wp_getPage
     164         */
     165        function wp_getPage($args) {
     166                $this->escape($args);
     167
     168                $blog_id        = $args[0];
     169                $page_id        = $args[1];
     170                $username       = $args[2];
     171                $password       = $args[3];
     172
     173                if(!$this->login_pass_ok($username, $password)) {
     174                        return($this->error);
     175                }
     176
     177                // Lookup page info.
     178                $page = get_page($page_id);
     179
     180                // If we found the page then format the data.
     181                if($page->ID && ($page->post_type == "page")) {
     182                        // Get all of the page content and link.
     183                        $full_page = get_extended($page->post_content);
     184                        $link = post_permalink($page->ID);
     185
     186                        // Determine comment and ping settings.
     187                        $allow_comments = ("open" == $page->comment_status) ? 1 : 0;
     188                        $allow_pings = ("open" == $page->ping_status) ? 1 : 0;
     189
     190                        // Format page date.
     191                        $page_date = mysql2date("Ymd\TH:i:s", $page->post_date);
     192
     193                        // Pull the categories info together.
     194                        $categories = array();
     195                        foreach(wp_get_post_categories($page->ID) as $cat_id) {
     196                                $categories[] = get_cat_name($cat_id);
     197                        }
     198
     199                        // Get the users nicename.
     200                        $nicename = get_profile("user_nicename", $username);
     201
     202                        $page_struct = array(
     203                                "dateCreated"           => new IXR_Date($page_date),
     204                                "userid"                        => $page->post_author,
     205                                "page_id"                       => $page->ID,
     206                                "description"           => $full_page["main"],
     207                                "title"                         => $page->post_title,
     208                                "link"                          => $link,
     209                                "permaLink"                     => $link,
     210                                "categories"            => $categories,
     211                                "excerpt"                       => $page->post_excerpt,
     212                                "text_more"                     => $full_page["extended"],
     213                                "mt_allow_comments"     => $allow_comments,
     214                                "mt_allow_pings"        => $allow_pings,
     215                                "wp_slug"                       => $page->post_name,
     216                                "wp_password"           => $page->post_password,
     217                                "wp_author"                     => $nicename,
     218                                "wp_page_parent_id"     => $page->post_parent,
     219                                "wp_page_order"         => $page->menu_order
     220                        );
     221
     222                        return($page_struct);
     223                }
     224                // If the page doesn't exist indicate that.
     225                else {
     226                        return(new IXR_Error(404, "Sorry, no such page."));
     227                }
     228        }
     229
     230        /**
     231         * WordPress XML-RPC API
     232         * wp_getPages
     233         */
     234        function wp_getPages($args) {
     235                $this->escape($args);
     236
     237                $blog_id        = $args[0];
     238                $username       = $args[1];
     239                $password       = $args[2];
     240
     241                if(!$this->login_pass_ok($username, $password)) {
     242                        return($this->error);
     243                }
     244
     245                // Lookup info on pages.
     246                $pages = array();
     247                $pages = get_pages();
     248                $num_pages = count($pages);
     249
     250                // If we have pages, put together their info.
     251                if($num_pages >= 1) {
     252                        $pages_struct = array();
     253
     254                        for($i = 0; $i < $num_pages; $i++) {
     255                                $page = wp_xmlrpc_server::wp_getPage(array(
     256                                        $blog_id, $pages[$i]->ID, $username, $password
     257                                ));
     258                                $pages_struct[] = $page;
     259                        }
     260
     261                        return($pages_struct);
     262                }
     263                // If no pages were found return an error.
     264                else {
     265                        return(new IXR_Error(404, "Sorry, no pages were found."));
     266                }
     267        }
     268
     269        /**
     270         * WordPress XML-RPC API
     271         * wp_newPage
     272         */
     273        function wp_newPage($args) {
     274                $this->escape($args);
     275
     276                $blog_id        = $args[0];
     277                $username       = $args[1];
     278                $password       = $args[2];
     279                $page           = $args[3];
     280                $publish        = $args[4];
     281
     282                if(!$this->login_pass_ok($username, $password)) {
     283                        return($this->error);
     284                }
     285
     286                // Set the user context and check if they are allowed
     287                // to add new pages.
     288                $user = set_current_user(0, $username);
     289                if(!current_user_can("publish_pages")) {
     290                        return(new IXR_Error(401, "Sorry, you can not add new pages."));
     291                }
     292
     293                // Mark this as content for a page.
     294                $args[3]["post_type"] = "page";
     295
     296                // Let mw_newPost do all of the heavy lifting.
     297                return($this->mw_newPost($args));
     298        }
     299
     300        /**
     301         * WordPress XML-RPC API
     302         * wp_deletePage
     303         */
     304        function wp_deletePage($args) {
     305                $this->escape($args);
     306
     307                $blog_id        = $args[0];
     308                $username       = $args[1];
     309                $password       = $args[2];
     310                $page_id        = $args[3];
     311
     312                if(!$this->login_pass_ok($username, $password)) {
     313                        return($this->error);
     314                }
     315
     316                // Get the current page based on the page_id and
     317                // make sure it is a page and not a post.
     318                $actual_page = wp_get_single_post($page_id, ARRAY_A);
     319                if(
     320                        !$actual_page
     321                        || ($actual_page["post_type"] != "page")
     322                ) {
     323                        return(new IXR_Error(404, "Sorry, no such page."));
     324                }
     325
     326                // Set the user context and make sure they can delete pages.
     327                set_current_user(0, $username);
     328                if(!current_user_can("delete_page", $page_id)) {
     329                        return(new IXR_Error(401, "Sorry, you do not have the right to delete this page."));
     330                }
     331
     332                // Attempt to delete the page.
     333                $result = wp_delete_post($page_id);
     334                if(!$result) {
     335                        return(new IXR_Error(500, "Failed to delete the page."));
     336                }
     337
     338                return(true);
     339        }
     340
     341        /**
     342         * WordPress XML-RPC API
     343         * wp_editPage
     344         */
     345        function wp_editPage($args) {
     346                $this->escape($args);
     347
     348                $blog_id        = $args[0];
     349                $page_id        = $args[1];
     350                $username       = $args[2];
     351                $password       = $args[3];
     352                $content        = $args[4];
     353                $publish        = $args[5];
     354
     355                if(!$this->login_pass_ok($username, $password)) {
     356                        return($this->error);
     357                }
     358
     359                // Get the page data and make sure it is a page.
     360                $actual_page = wp_get_single_post($page_id, ARRAY_A);
     361                if(
     362                        !$actual_page
     363                        || ($actual_page["post_type"] != "page")
     364                ) {
     365                        return(new IXR_Error(404, "Sorry, no such page."));
     366                }
     367
     368                // Set the user context and make sure they are allowed to edit pages.
     369                set_current_user(0, $username);
     370                if(!current_user_can("edit_page", $page_id)) {
     371                        return(new IXR_Error(401, "Sorry, you do not have the right to edit this page."));
     372                }
     373
     374                // Mark this as content for a page.
     375                $content["post_type"] = "page";
     376
     377                // Arrange args in the way mw_editPost understands.
     378                $args = array(
     379                        $page_id,
     380                        $username,
     381                        $password,
     382                        $content,
     383                        $publish
     384                );
     385
     386                // Let mw_editPost do all of the heavy lifting.
     387                return($this->mw_editPost($args));
     388        }
     389
     390        /**
     391         * WordPress XML-RPC API
     392         * wp_getPageList
     393         */
     394        function wp_getPageList($args) {
     395                global $wpdb;
     396
     397                $this->escape($args);
     398
     399                $blog_id                                = $args[0];
     400                $username                               = $args[1];
     401                $password                               = $args[2];
     402
     403                if(!$this->login_pass_ok($username, $password)) {
     404                        return($this->error);
     405                }
     406
     407                // Get list of pages ids and titles
     408                $page_list = $wpdb->get_results("
     409                        SELECT ID page_id,
     410                                post_title page_title
     411                        FROM {$wpdb->posts}
     412                        WHERE post_type = 'page'
     413                        ORDER BY ID
     414                ");
     415
     416                return($page_list);
     417        }
     418
     419        /**
     420         * WordPress XML-RPC API
     421         * wp_getAuthors
     422         */
     423        function wp_getAuthors($args) {
     424                global $wpdb;
     425
     426                $this->escape($args);
     427
     428                $blog_id        = $args[0];
     429                $username       = $args[1];
     430                $password       = $args[2];
     431
     432                if(!$this->login_pass_ok($username, $password)) {
     433                        return($this->error);
     434                }
     435
     436                // Get basic info on all users.
     437                $all_users = $wpdb->get_results("
     438                        SELECT u.ID id,
     439                                u.user_login username
     440                        FROM {$wpdb->users} u
     441                        ORDER BY u.user_login
     442                ");
     443
     444                return($all_users);
     445        }
     446
     447        /**
     448         * WordPress XML-RPC API
     449         * wp_newCategory
     450         */
     451        function wp_newCategory($args) {
     452                $this->escape($args);
     453
     454                $blog_id                                = $args[0];
     455                $username                               = $args[1];
     456                $password                               = $args[2];
     457                $category                               = $args[3];
     458
     459                if(!$this->login_pass_ok($username, $password)) {
     460                        return($this->error);
     461                }
     462
     463                // Set the user context and make sure they are
     464                // allowed to add a category.
     465                set_current_user(0, $username);
     466                if(!current_user_can("manage_categories", $page_id)) {
     467                        return(new IXR_Error(401, "Sorry, you do not have the right to add a category."));
     468                }
     469
     470                // We need this to make use of the wp_insert_category()
     471                // funciton.
     472                require_once(ABSPATH . "wp-admin/admin-db.php");
     473
     474                // If no slug was provided make it empty so that
     475                // WordPress will generate one.
     476                if(empty($category["slug"])) {
     477                        $category["slug"] = "";
     478                }
     479
     480                // If no parent_id was provided make it empty
     481                // so that it will be a top level page (no parent).
     482                if(empty($category["parent_id"])) {
     483                        $category["parent_id"] = "";
     484                }
     485
     486                // If no description was provided make it empty.
     487                if(empty($category["description"])) {
     488                        $category["description"] = "";
     489                }
     490               
     491                $new_category = array(
     492                        "cat_name"                              => $category["name"],
     493                        "category_nicename"             => $category["slug"],
     494                        "category_parent"               => $category["parent_id"],
     495                        "category_description"  => $category["description"]
     496                );
     497
     498                $cat_id = wp_insert_category($new_category);
     499                if(!$cat_id) {
     500                        return(new IXR_Error(500, "Sorry, the new category failed."));
     501                }
     502
     503                return($cat_id);
     504        }
     505
     506        /**
     507         * WordPress XML-RPC API
     508         * wp_suggestCategories
     509         */
     510        function wp_suggestCategories($args) {
     511                global $wpdb;
     512
     513                $this->escape($args);
     514
     515                $blog_id                                = $args[0];
     516                $username                               = $args[1];
     517                $password                               = $args[2];
     518                $category                               = $args[3];
     519                $max_results                    = $args[4];
     520
     521                if(!$this->login_pass_ok($username, $password)) {
     522                        return($this->error);
     523                }
     524
     525                // Only set a limit if one was provided.
     526                $limit = "";
     527                if(!empty($max_results)) {
     528                        $limit = "LIMIT {$max_results}";
     529                }
     530
     531                $category_suggestions = $wpdb->get_results("
     532                        SELECT cat_ID category_id,
     533                                cat_name category_name
     534                        FROM {$wpdb->categories}
     535                        WHERE cat_name LIKE '{$category}%'
     536                        {$limit}
     537                ");
     538
     539                return($category_suggestions);
     540        }
     541
     542
    149543        /* Blogger API functions
    150544         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
     
    514908          if ( !current_user_can('publish_posts') )
    515909            return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.');
     910
     911                // The post_type defaults to post, but could also be page.
     912                $post_type = "post";
     913                if(
     914                        !empty($content_struct["post_type"])
     915                        && ($content_struct["post_type"] == "page")
     916                ) {
     917                        $post_type = "page";
     918                }
     919
     920                // Let WordPress generate the post_name (slug) unless
     921                // one has been provided.
     922                $post_name = "";
     923                if(!empty($content_struct["wp_slug"])) {
     924                        $post_name = $content_struct["wp_slug"];
     925                }
     926
     927                // Only use a password if one was given.
     928                if(!empty($content_struct["wp_password"])) {
     929                        $post_password = $content_struct["wp_password"];
     930                }
     931
     932                // Only set a post parent if one was provided.
     933                if(!empty($content_struct["wp_page_parent_id"])) {
     934                        $post_parent = $content_struct["wp_page_parent_id"];
     935                }
     936
     937                // Only set the menu_order if it was provided.
     938                if(!empty($content_struct["wp_page_order"])) {
     939                        $menu_order = $content_struct["wp_page_order"];
     940                }
    516941
    517942          $post_author = $user->ID;
     
    560985
    561986          // We've got all the data -- post it:
    562           $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');
     987          $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');
    563988
    564989          $post_ID = wp_insert_post($postdata);
     
    6131038          extract($postdata);
    6141039                $this->escape($postdata);
     1040
     1041                // The post_type defaults to post, but could also be page.
     1042                $post_type = "post";
     1043                if(
     1044                        !empty($content_struct["post_type"])
     1045                        && ($content_struct["post_type"] == "page")
     1046                ) {
     1047                        $post_type = "page";
     1048                }
     1049
     1050                // Let WordPress manage slug if none was provided.
     1051                $post_name = "";
     1052                if(!empty($content_struct["wp_slug"])) {
     1053                        $post_name = $content_struct["wp_slug"];
     1054                }
     1055
     1056                // Only use a password if one was given.
     1057                if(!empty($content_struct["wp_password"])) {
     1058                        $post_password = $content_struct["wp_password"];
     1059                }
     1060
     1061                // Only set a post parent if one was given.
     1062                if(!empty($content_struct["wp_page_parent_id"])) {
     1063                        $post_parent = $content_struct["wp_page_parent_id"];
     1064                }
     1065
     1066                // Only ste the menu_order if it was given.
     1067                if(!empty($content_struct["wp_page_order"])) {
     1068                        $menu_order = $content_struct["wp_page_order"];
     1069                }
    6151070
    6161071          $post_title = $content_struct['title'];
     
    6561111
    6571112          // We've got all the data -- post it:
    658           $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping');
     1113          $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');
    6591114
    6601115          $result = wp_update_post($newpost);
Note: See TracChangeset for help on using the changeset viewer.