Make WordPress Core

Changeset 1565


Ignore:
Timestamp:
08/26/2004 03:42:43 PM (20 years ago)
Author:
michelvaldrighi
Message:

added mw.getRecentPosts, mw.getCategories, refactored the living shit out of the rest of the file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xmlrpc.php

    r1563 r1565  
    66include('../wp-config.php');
    77include_once(ABSPATH . WPINC . '/class-IXR.php');
     8include_once(ABSPATH . WPINC . '/functions-post.php');
    89
    910// Turn off all warnings and errors.
     
    1314$post_default_category = 1; // posts submitted via the xmlrpc interface go into that category
    1415
    15 $xmlrpc_logging = 0;
     16$xmlrpc_logging = 1;
    1617
    1718function logIO($io,$msg) {
     
    3435logIO("I", $HTTP_RAW_POST_DATA);
    3536
    36 
     37function printr ( $var, $do_not_echo = false )
     38{
     39   ob_start();
     40   print_r($var);
     41   $code =  htmlentities(ob_get_contents());
     42   ob_clean();
     43   if ( !$do_not_echo )
     44   {
     45       echo "<pre>$code</pre>";
     46   }
     47   return $code;
     48}
    3749
    3850class wp_xmlrpc_server extends IXR_Server {
     
    5365          'metaWeblog.editPost' => 'this:mw_editPost',
    5466          'metaWeblog.getPost' => 'this:mw_getPost',
     67          'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
     68          'metaWeblog.getCategories' => 'this:mw_getCategories',
    5569
    5670          'demo.sayHello' => 'this:sayHello',
     
    7185    function login_pass_ok($user_login, $user_pass) {
    7286      if (!user_pass_ok($user_login, $user_pass)) {
    73         $this->error = new IXR_Error(403, 'Bad login/pass combination.');
     87        $this->error = new IXR_Error(403, 'Bad login/pass combination.'.$user_login);
    7488        return false;
    7589      }
     
    146160
    147161      $user_data = get_userdatabylogin($user_login);
    148       $post_data = get_postdata($post_ID);
    149 
    150       $post_date = mysql2date('Ymd\TH:i:s', $post_data['Date']);
     162      $post_data = wp_get_single_post($post_ID, ARRAY_A);
    151163
    152164      $categories = implode(',', wp_get_post_cats(1, $post_ID));
    153165
    154       $content  = '<title>'.stripslashes($post_data['Title']).'</title>';
     166      $content  = '<title>'.stripslashes($post_data['post_title']).'</title>';
    155167      $content .= '<category>'.$categories.'</category>';
    156       $content .= stripslashes($post_data['Content']);
     168      $content .= stripslashes($post_data['post_content']);
    157169
    158170      $struct = array(
    159         'userid'    => $post_data['Author_ID'],
    160         'dateCreateed' => mysql2date('Ymd\TH:i:s', $post_data['Date']),
     171        'userid'    => $post_data['post_author'],
     172        'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s\Z', $post_data['post_date_gmt'])),
    161173        'content'     => $content,
    162174        'postid'  => $post_data['ID']
     
    177189      $num_posts  = $args[4];
    178190
    179       if ($num_posts > 0) {
    180         $limit = " LIMIT $num_posts";
    181       } else {
    182         $limit = '';
    183       }
    184 
    185       if (!$this->login_pass_ok($user_login, $user_pass)) {
    186         return $this->error;
    187       }
    188 
    189       $sql = "SELECT * FROM $wpdb->posts ORDER BY post_date DESC".$limit;
    190       $result = $wpdb->get_results($sql);
    191 
    192       if (!$result) {
     191      if (!$this->login_pass_ok($user_login, $user_pass)) {
     192        return $this->error;
     193      }
     194
     195      $posts_list = wp_get_recent_posts($num_posts);
     196
     197      if (!$posts_list) {
    193198        $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.');
    194199        return $this->error;
    195200      }
    196201
    197       $i = 0;
    198       foreach ($result as $row) {
    199         $post_data = array(
    200           'ID' => $row->ID,
    201           'Author_ID' => $row->post_author,
    202           'Date' => $row->post_date,
    203           'Content' => $row->post_content,
    204           'Title' => $row->post_title,
    205           'Category' => $row->post_category
     202      foreach ($posts_list as $entry) {
     203     
     204        $post_date = mysql2date('Ymd\TH:i:s\Z', $entry['post_date_gmt']);
     205        $categories = implode(',', wp_get_post_cats(1, $entry['ID']));
     206
     207        $content  = '<title>'.stripslashes($entry['post_itle']).'</title>';
     208        $content .= '<category>'.$categories.'</category>';
     209        $content .= stripslashes($entry['post_content']);
     210
     211        $struct[] = array(
     212          'userid' => $entry['post_author'],
     213          'dateCreated' => new IXR_Date($post_date),
     214          'content' => $content,
     215          'postid' => $entry['ID'],
    206216        );
    207217
    208         $categories = implode(',', wp_get_post_cats(1, $post_data['ID']));
    209         $post_date = mysql2date("Ymd\TH:i:s", $post_data['Date']);
    210 
    211         $content  = '<title>'.stripslashes($post_data['Title']).'</title>';
    212         $content .= '<category>'.$categories.'</category>';
    213         $content .= stripslashes($post_data['Content']);
    214 
    215         $author_data = get_userdata($post_data['Author_ID']);
    216 
    217         switch($author_data['user_idmode']) {
    218         case 'nickname':
    219           $author_name = $author_data['user_nickname'];
    220         case 'login':
    221           $author_name = $author_data['user_login'];
    222           break;
    223         case 'firstname':
    224           $author_name = $author_data['user_firstname'];
    225           break;
    226         case 'lastname':
    227           $author_name = $author_data['user_lastname'];
    228           break;
    229         case 'namefl':
    230           $author_name = $author_data['user_firstname']." ".$author_data['user_lastname'];
    231           break;
    232         case 'namelf':
    233           $author_name = $author_data['user_lastname']." ".$author_data['user_firstname'];
    234           break;
    235         default:
    236           $author_name = $author_data['user_nickname'];
    237           break;
    238         }
    239 
    240         $struct[$i] = array(
    241           'authorName' => $author_name,
    242           'userid' => $post_data['Author_ID'],
    243           'dateCreated' => $post_date,
    244           'content' => $content,
    245           'postid' => $post_data['ID'],
    246           'category' => $categories
    247         );
    248 
    249         $i++;
    250218      }
    251219
     
    286254
    287255      /* so it is actually editable with a windows/mac client */
    288       $content = str_replace("\n", "\r\n", $content);
     256      // 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);
    289257
    290258      return $content;
     
    511479      $catnames = $content_struct['categories'];
    512480      // FIXME: commented until a fix to print_r is found: logio('O', 'Post cats: ' . print_r($catnames,true));
     481      //logio('O', 'Post cats: ' . printr($catnames,true));
    513482      $post_category = array();
    514483
     
    627596
    628597        $post_date = mysql2date('Ymd\TH:i:s\Z', $postdata['post_date_gmt']);
    629            
     598
     599        $categories = array();
    630600        $catids = wp_get_post_cats('', $post_ID);
    631601        foreach($catids as $catid) {
    632           $catname = get_cat_name($catid);
    633           $catnameenc = new xmlrpcval($catname);
    634           $catlist[] = $catnameenc;
     602          $categories[] = get_cat_name($catid);
    635603        }
     604
    636605        $post = get_extended($postdata['post_content']);
     606        $link = post_permalink($entry['ID']);
     607
    637608        $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
    638609        $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
    639610
    640611        $resp = array(
    641                 'link' => post_permalink($post_ID),
     612                'link' => $link,
    642613                'title' => $postdata['post_title'],
    643614                'description' => $post['main'],
     
    646617                'postid' => $postdata['ID'],
    647618                'content' => $postdata['post_content'],
    648                 'permalink' => post_permalink($post_ID),
    649                 'categories' => $catlist,
     619                'permalink' => $link,
     620                'categories' => $categories,
    650621                'mt_excerpt' => $postdata['post_excerpt'],
    651622                'mt_allow_comments' => $allow_comments,
     
    659630      }
    660631    }
    661    
     632
     633
     634    /* metaweblog.getRecentPosts ...returns recent posts */
     635    function mw_getRecentPosts($args) {
     636
     637      $blog_ID     = $args[0];
     638      $user_login  = $args[1];
     639      $user_pass   = $args[2];
     640      $num_posts  = $args[4];
     641
     642      if (!$this->login_pass_ok($user_login, $user_pass)) {
     643        return $this->error;
     644      }
     645
     646      $posts_list = wp_get_recent_posts($num_posts);
     647
     648      if (!$posts_list) {
     649        $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.');
     650        return $this->error;
     651      }
     652
     653      foreach ($posts_list as $entry) {
     654     
     655        $post_date = mysql2date('Ymd\TH:i:s\Z', $entry['post_date_gmt']);
     656        $categories = array();
     657        $catids = wp_get_post_cats('', $entry['ID']);
     658        foreach($catids as $catid) {
     659          $categories[] = get_cat_name($catid);
     660        }
     661
     662        $post = get_extended($entry['post_content']);
     663        $link = post_permalink($entry['ID']);
     664
     665        $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
     666        $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
     667
     668        $struct[] = array(
     669          'link' => $link,
     670          'title' => $entry['post_title'],
     671          'description' => $post['main'],
     672          'dateCreated' => new IXR_Date($post_date),
     673          'userid' => $entry['post_author'],
     674          'postid' => $entry['ID'],
     675          'content' => $entry['post_content'],
     676          'permalink' => $link,
     677          'categories' => $categories,
     678          'mt_excerpt' => $entry['post_excerpt'],
     679          'mt_allow_comments' => $allow_comments,
     680          'mt_allow_pings' => $allow_pings,
     681          'mt_text_more' => $post['extended']
     682        );
     683
     684      }
     685
     686      $recent_posts = array();
     687      for ($j=0; $j<count($struct); $j++) {
     688        array_push($recent_posts, $struct[$j]);
     689      }
     690     
     691      return $recent_posts;
     692    }
     693
     694
     695    /* metaweblog.getCategories ...returns the list of categories on a given weblog */
     696    function mw_getCategories($args) {
     697
     698      global $wpdb;
     699
     700      $blog_ID     = $args[0];
     701      $user_login  = $args[1];
     702      $user_pass   = $args[2];
     703
     704      if (!$this->login_pass_ok($user_login, $user_pass)) {
     705        return $this->error;
     706      }
     707
     708      $categories_struct = array();
     709
     710      // FIXME: can we avoid using direct SQL there?
     711      if ($cats = $wpdb->get_results("SELECT cat_ID,cat_name FROM $wpdb->categories", ARRAY_A)) {
     712        foreach ($cats as $cat) {
     713          $struct['categoryId'] = $cat['cat_ID'];
     714          $struct['description'] = $cat['cat_name'];
     715          $struct['categoryName'] = $cat['cat_name'];
     716          $struct['htmlUrl'] = htmlspecialchars(get_category_link(false, $cat['cat_ID'], $cat['cat_name']));
     717          $struct['rssUrl'] = htmlspecialchars(get_category_rss_link(false, $cat['cat_ID'], $cat['cat_name']));
     718
     719          $categories_struct[] = $struct;
     720        }
     721      }
     722
     723      return $categories_struct;
     724    }
     725
    662726}
    663727
Note: See TracChangeset for help on using the changeset viewer.