Make WordPress Core

Ticket #12267: wp-classes.diff

File wp-classes.diff, 4.1 KB (added by andy, 15 years ago)

First draft. This works.

  • wp-includes/query.php

     
    23022302                if ( !$q['suppress_filters'] )
    23032303                        $this->posts = apply_filters('posts_results', $this->posts);
    23042304
     2305                // Turn each row into a classed object, e.g. wp_post, wp_comment.
     2306                if ( is_array($this->posts) )
     2307                        $this->posts = array_map(array('wp_row', 'get'), $this->posts);
     2308
    23052309                if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
    23062310                        $cjoin = apply_filters('comment_feed_join', '');
    23072311                        $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
  • wp-includes/classes.php

     
    16961696
    16971697}
    16981698
     1699// A factory and constructor that upgrades stdClass "rows" to WordPress classes.
     1700class wp_row {
     1701        // Factory. Call statically to upgrade a stdClass object to its specialized class: $o = wp_row::get($row).
     1702        function get($row) {
     1703                if ( is_a($row, 'wp_row') || is_subclass_of($row, 'wp_row') )
     1704                        return $row;
     1705
     1706                if ( is_array($row) )
     1707                        $row = (object) $row;
     1708
     1709                $class = 'wp_row';
     1710                if ( isset($row->post_type) ) {
     1711                        if ( class_exists("wp_" . $row->post_type) )
     1712                                $class = "wp_" . $row->post_type;
     1713                        else
     1714                                $class = "wp_post";
     1715                } elseif ( isset($row->comment_type) ) {
     1716                        if ( class_exists("wp_" . $row->comment_type) )
     1717                                $class = "wp_" . $row->comment_type;
     1718                        else
     1719                                $class = "wp_comment";
     1720                }
     1721
     1722                if ( function_exists("apply_filters") ) {
     1723                        $filtered_class = apply_filters("wp_row_class", $class, $row);
     1724                        if ( class_exists($filtered_class) )
     1725                                $class = $filtered_class;
     1726                }
     1727
     1728                return call_user_func(array($class, 'get'), $row);
     1729        }
     1730
     1731        function wp_row(&$row) {
     1732                return $this->__construct($row);
     1733        }
     1734
     1735        function __construct($row) {
     1736                if ( is_array($row) )
     1737                        $row = (object) $row;
     1738
     1739                foreach ( (array) $row as $k => $v )
     1740                        $this->$k = $row->$k;
     1741        }
     1742}
     1743
     1744class wp_post extends wp_row {
     1745        // Factory
     1746        function get($post) {
     1747                if ( $post = get_post($post) )
     1748                        return new wp_post($post);
     1749                else
     1750                        return new WP_Error(404, "Post not found.");
     1751        }
     1752
     1753        function id() { return $this->ID; }
     1754        function post_id() { return $this->ID; }
     1755        function type_id() { return 'post-' . $this->ID; }
     1756        function classes($class='') { return join( ' ', get_post_class( $class, $this->id() ) ); }
     1757
     1758        function permalink() { return get_permalink($this); }
     1759        function title() { return get_the_title($this); }
     1760        function date($format='') { return; }
     1761        function time($format='') { return get_the_time($format, $this); }
     1762        function author() { $authordata = get_userdata($this->post_author); return $authordata->display_name; }
     1763        function content() { return get_content($this); }
     1764}
     1765
     1766class wp_comment extends wp_row {
     1767        // Factory
     1768        function get($comment) {
     1769                if ( $comment = get_comment($comment) )
     1770                        return new wp_comment($comment);
     1771                else
     1772                        return new WP_Error(404, "Comment not found.");
     1773        }
     1774
     1775        function id() { return $this->comment_ID; }
     1776        function post_id() { return $this->comment_post_ID; }
     1777        function type_id() { return 'comment-' . $this->comment_ID; }
     1778        function classes($class='') { return join( ' ', get_comment_class( $class, $this->id() ) ); }
     1779
     1780        function permalink() { return get_comment_link($this); }
     1781        function title() { return sprintf(__("Comment on %s"), get_the_title($this->post_id())); }
     1782        function time($format='') { return mysql2date($format?$format:get_option('time_format'), $this->comment_date); }
     1783        function date($format='') { return date($format?$format:get_option('date_format'), $this->time('U')); }
     1784        function author() { return $this->comment_author; }
     1785        function excerpt() { return $this->comment_content; }
     1786        function content() { return get_comment_content($this); }
     1787}
     1788
     1789function is_post($object) {
     1790        return is_a('wp_post', $object);
     1791}
     1792
     1793function is_comment($object) {
     1794        return is_a('wp_comment', $object);
     1795}
     1796
    16991797?>