Make WordPress Core

Ticket #7652: 7652-separate.diff

File 7652-separate.diff, 9.2 KB (added by rmccue, 14 years ago)

Separate WP_SimplePieAtomPub_Item into class-feed.php

  • wp-app.php

     
    1616require_once('./wp-load.php');
    1717
    1818/** Atom Publishing Protocol Class */
    19 require_once(ABSPATH . WPINC . '/atomlib.php');
     19require_once(ABSPATH . WPINC . '/class-feed.php');
    2020
    2121/** Admin Image API for metadata updating */
    2222require_once(ABSPATH . '/wp-admin/includes/image.php');
     
    258258         * @since 2.2.0
    259259         */
    260260        function handle_request() {
    261                 global $always_authenticate;
    262 
    263261                if ( !empty( $_SERVER['ORIG_PATH_INFO'] ) )
    264262                        $path = $_SERVER['ORIG_PATH_INFO'];
    265263                else
     
    294292                                        // authenticate regardless of the operation and set the current
    295293                                        // user. each handler will decide if auth is required or not.
    296294                                        if ( !$this->authenticate() ) {
    297                                                 if ( $always_authenticate )
    298                                                         $this->auth_required('Credentials required.');
     295                                                $this->auth_required('Credentials required.');
    299296                                        }
    300297
    301298                                        array_shift($matches);
     
    390387                global $user_ID;
    391388                $this->get_accepted_content_type($this->atom_content_types);
    392389
    393                 $parser = new AtomParser();
    394                 if ( !$parser->parse() )
    395                         $this->client_error();
     390                $feed = $this->get_parser();
     391                $entry = $feed->get_item(0);
    396392
    397                 $entry = array_pop($parser->feed->entries);
     393                log_app('Received entry:', $entry->get_title());
    398394
    399                 log_app('Received entry:', print_r($entry,true));
    400 
    401395                $catnames = array();
    402                 foreach ( $entry->categories as $cat ) {
    403                         array_push($catnames, $cat["term"]);
     396                foreach ( (array) $entry->get_categories() as $category ) {
     397                        array_push($catnames, $category->get_term());
    404398                }
    405399
    406400                $wp_cats = get_categories(array('hide_empty' => false));
     
    412406                                array_push($post_category, $cat->term_id);
    413407                }
    414408
    415                 $publish = ! ( isset( $entry->draft ) && 'yes' == trim( $entry->draft ) );
     409                $publish = !$entry->get_draft_status();
    416410
    417411                $cap = ($publish) ? 'publish_posts' : 'edit_posts';
    418412
     
    422416                $blog_ID = get_current_blog_id();
    423417                $post_status = ($publish) ? 'publish' : 'draft';
    424418                $post_author = (int) $user_ID;
    425                 $post_title = $entry->title[1];
    426                 $post_content = $entry->content[1];
    427                 $post_excerpt = $entry->summary[1];
    428                 $pubtimes = $this->get_publish_time($entry->published);
    429                 $post_date = $pubtimes[0];
    430                 $post_date_gmt = $pubtimes[1];
     419                $post_title = $entry->get_title();
     420                $post_content = $entry->get_content();
     421                $post_excerpt = $entry->get_description();
     422                $post_date = $entry->get_date('Y-m-d H:i:s');
     423                $post_date_gmt = $entry->get_gmdate('Y-m-d H:i:s');
    431424
    432425                if ( isset( $_SERVER['HTTP_SLUG'] ) )
    433426                        $post_name = $_SERVER['HTTP_SLUG'];
     
    490483                // quick check and exit
    491484                $this->get_accepted_content_type($this->atom_content_types);
    492485
    493                 $parser = new AtomParser();
    494                 if ( !$parser->parse() )
    495                         $this->bad_request();
     486                $feed = $this->get_parser();
     487                $parsed = $feed->get_item(0);
    496488
    497                 $parsed = array_pop($parser->feed->entries);
     489                log_app('Received UPDATED entry:', $parsed->get_title());
    498490
    499                 log_app('Received UPDATED entry:', print_r($parsed,true));
    500 
    501491                // check for not found
    502492                global $entry;
    503493                $this->set_current_entry($postID);
     
    505495                if ( !current_user_can('edit_post', $entry['ID']) )
    506496                        $this->auth_required(__('Sorry, you do not have the right to edit this post.'));
    507497
    508                 $publish = ! ( isset($parsed->draft) && 'yes' == trim($parsed->draft) );
     498                $publish = !$parsed->get_draft_status();
    509499                $post_status = ($publish) ? 'publish' : 'draft';
    510500
    511501                extract($entry);
    512502
    513                 $post_title = $parsed->title[1];
    514                 $post_content = $parsed->content[1];
    515                 $post_excerpt = $parsed->summary[1];
    516                 $pubtimes = $this->get_publish_time($entry->published);
    517                 $post_date = $pubtimes[0];
    518                 $post_date_gmt = $pubtimes[1];
    519                 $pubtimes = $this->get_publish_time($parsed->updated);
    520                 $post_modified = $pubtimes[0];
    521                 $post_modified_gmt = $pubtimes[1];
     503                $post_title = $parsed->get_title();
     504                $post_content = $parsed->get_content();
     505                $post_excerpt = $parsed->get_description();
    522506
     507                $post_date = $parsed->get_date('Y-m-d H:i:s');
     508                $post_date_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
     509
     510                $post_modified =  $parsed->get_updated('Y-m-d H:i:s');
     511                $post_modified_gmt =  $parsed->get_gmupdated('Y-m-d H:i:s');
     512
    523513                $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt');
    524514                $this->escape($postdata);
    525515
     
    657647                // quick check and exit
    658648                $this->get_accepted_content_type($this->atom_content_types);
    659649
    660                 $parser = new AtomParser();
    661                 if (!$parser->parse()) {
    662                         $this->bad_request();
    663                 }
     650                $feed = $this->get_parser();
     651                $parsed = $feed->get_item(0);
    664652
    665                 $parsed = array_pop($parser->feed->entries);
    666 
    667653                // check for not found
    668654                global $entry;
    669655                $this->set_current_entry($postID);
     
    673659
    674660                extract($entry);
    675661
    676                 $post_title = $parsed->title[1];
    677                 $post_content = $parsed->summary[1];
    678                 $pubtimes = $this->get_publish_time($parsed->updated);
    679                 $post_modified = $pubtimes[0];
    680                 $post_modified_gmt = $pubtimes[1];
     662                $post_title = $parsed->get_title();
     663                $post_content = $parsed->get_description();
     664                $post_modified = $parsed->get_date('Y-m-d H:i:s');
     665                $post_modified_gmt = $parsed->get_gmdate('Y-m-d H:i:s');
    681666
    682667                $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'post_modified', 'post_modified_gmt');
    683668                $this->escape($postdata);
     
    12321217                log_app('Status','400: Bad Request');
    12331218                header('Content-Type: text/plain');
    12341219                status_header('400');
     1220                echo $msg;
    12351221                exit;
    12361222        }
    12371223
     
    13361322                log_app('Status','400: Client Error');
    13371323                header('Content-Type: text/plain');
    13381324                status_header('400');
     1325                echo $msg;
    13391326                exit;
    13401327        }
    13411328
     
    14481435         * @return bool
    14491436         */
    14501437        function authenticate() {
     1438                global $always_authenticate;
     1439
    14511440                log_app("authenticate()",print_r($_ENV, true));
    14521441
    14531442                // if using mod_rewrite/ENV hack
     
    14741463                        }
    14751464                }
    14761465
     1466                // If we're forcing admin abilities
     1467                if (!$always_authenticate) {
     1468                        wp_set_current_user(1);
     1469                        return true;
     1470                }
     1471
    14771472                return false;
    14781473        }
    14791474
     
    15981593                }
    15991594        }
    16001595
     1596        /**
     1597         * Create a SimplePie parser with POST data
     1598         *
     1599         * @return SimplePie
     1600         */
     1601        function &get_parser() {
     1602                $data = file_get_contents('php://input');
     1603                // SimplePie expects the feed element to be the top element
     1604                // This could probably be improved
     1605                if (strpos($data, '<feed') === false) {
     1606                        $data = str_replace('<entry', '<feed xmlns="' . SIMPLEPIE_NAMESPACE_ATOM_10 . '"><entry', $data);
     1607                        $data = str_replace('</entry>', '</entry></feed>', $data);
     1608                }
     1609                $feed = new SimplePie();
     1610                $feed->set_item_class('WP_SimplePieAtomPub_Item');
     1611                $feed->set_raw_data($data);
     1612                $feed->init();
     1613                if ( $feed->error() )
     1614                        $this->bad_request($feed->error());
     1615                return $feed;
     1616        }
    16011617}
    16021618
    16031619/**
  • wp-includes/class-feed.php

     
    100100                        }
    101101                }
    102102        }
     103}
     104
     105define('SIMPLEPIE_NAMESPACE_ATOMPUB', 'http://www.w3.org/2007/app');
     106
     107/**
     108 * SimplePie Helper for AtomPub
     109 *
     110 * @package WordPress
     111 * @subpackage Publishing
     112 * @since 3.1
     113 */
     114class WP_SimplePieAtomPub_Item extends SimplePie_Item {
     115        /**
     116         * Constructor
     117         */
     118        function WP_SimplePieAtomPub_Item($feed, $data) {
     119                parent::SimplePie_Item($feed, $data);
     120        }
     121
     122        /**
     123         * Get the status of the entry
     124         *
     125         * @return bool True if the item is a draft, false otherwise
     126         */
     127        function get_draft_status() {
     128                $draft = false;
     129                if (($control = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOMPUB, 'control')) && !empty($control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data'])) {
     130                        $draft = ('yes' == $control[0]['child'][SIMPLEPIE_NAMESPACE_ATOMPUB]['draft'][0]['data']);
     131                }
     132                return $draft;
     133        }
     134
     135        /**
     136         * Get the GMT timestamp of the entry
     137         *
     138         * @param string $format date() format
     139         * @return int|string|null
     140         */
     141        function get_gmdate($format = 'j F Y, g:i a') {
     142                return gmdate($format, $this->get_date('U'));
     143        }
     144
     145        /**
     146         * Get the updated timestamp of the entry
     147         *
     148         * AtomPub needs the distinction between "created" and "updated".
     149         * @param string $format date() format
     150         * @return string|int|null
     151         */
     152        function get_updated($format = 'j F Y, g:i a') {
     153                if ($updated = $this->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated')) {
     154                        $return = date('Y-m-d H:i:s', $updated[0]['data']);
     155                }
     156                else {
     157                        return null;
     158                }
     159
     160                if ($return) {
     161                        $parser = SimplePie_Parse_Date::get();
     162                        $parsed = $parser->parse($return);
     163                        $date_format = (string) $date_format;
     164                        switch ($date_format) {
     165                                case '':
     166                                        return $this->sanitize($return, SIMPLEPIE_CONSTRUCT_TEXT);
     167
     168                                case 'U':
     169                                        return $parsed;
     170
     171                                default:
     172                                        return date($date_format, $parsed);
     173                        }
     174                }
     175                else {
     176                        return null;
     177                }
     178        }
     179
     180        /**
     181         * Get the updated GMT timestamp of the entry
     182         *
     183         * @param string $format date() format
     184         * @return int|string|null
     185         */
     186        function get_gmupdated($format = 'j F Y, g:i a') {
     187                return gmdate($format, $this->get_updated('U'));
     188        }
    103189}
     190 No newline at end of file