Ticket #12267: wp-classes.diff

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

First draft. This works.

Line 
1Index: wp-includes/query.php
2===================================================================
3--- wp-includes/query.php       (revision 13186)
4+++ wp-includes/query.php       (working copy)
5@@ -2302,6 +2302,10 @@
6                if ( !$q['suppress_filters'] )
7                        $this->posts = apply_filters('posts_results', $this->posts);
8 
9+               // Turn each row into a classed object, e.g. wp_post, wp_comment.
10+               if ( is_array($this->posts) )
11+                       $this->posts = array_map(array('wp_row', 'get'), $this->posts);
12+
13                if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
14                        $cjoin = apply_filters('comment_feed_join', '');
15                        $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
16Index: wp-includes/classes.php
17===================================================================
18--- wp-includes/classes.php     (revision 13185)
19+++ wp-includes/classes.php     (working copy)
20@@ -1696,4 +1696,102 @@
21 
22 }
23 
24+// A factory and constructor that upgrades stdClass "rows" to WordPress classes.
25+class wp_row {
26+       // Factory. Call statically to upgrade a stdClass object to its specialized class: $o = wp_row::get($row).
27+       function get($row) {
28+               if ( is_a($row, 'wp_row') || is_subclass_of($row, 'wp_row') )
29+                       return $row;
30+
31+               if ( is_array($row) )
32+                       $row = (object) $row;
33+
34+               $class = 'wp_row';
35+               if ( isset($row->post_type) ) {
36+                       if ( class_exists("wp_" . $row->post_type) )
37+                               $class = "wp_" . $row->post_type;
38+                       else
39+                               $class = "wp_post";
40+               } elseif ( isset($row->comment_type) ) {
41+                       if ( class_exists("wp_" . $row->comment_type) )
42+                               $class = "wp_" . $row->comment_type;
43+                       else
44+                               $class = "wp_comment";
45+               }
46+
47+               if ( function_exists("apply_filters") ) {
48+                       $filtered_class = apply_filters("wp_row_class", $class, $row);
49+                       if ( class_exists($filtered_class) )
50+                               $class = $filtered_class;
51+               }
52+
53+               return call_user_func(array($class, 'get'), $row);
54+       }
55+
56+       function wp_row(&$row) {
57+               return $this->__construct($row);
58+       }
59+
60+       function __construct($row) {
61+               if ( is_array($row) )
62+                       $row = (object) $row;
63+
64+               foreach ( (array) $row as $k => $v )
65+                       $this->$k = $row->$k;
66+       }
67+}
68+
69+class wp_post extends wp_row {
70+       // Factory
71+       function get($post) {
72+               if ( $post = get_post($post) )
73+                       return new wp_post($post);
74+               else
75+                       return new WP_Error(404, "Post not found.");
76+       }
77+
78+       function id() { return $this->ID; }
79+       function post_id() { return $this->ID; }
80+       function type_id() { return 'post-' . $this->ID; }
81+       function classes($class='') { return join( ' ', get_post_class( $class, $this->id() ) ); }
82+
83+       function permalink() { return get_permalink($this); }
84+       function title() { return get_the_title($this); }
85+       function date($format='') { return; }
86+       function time($format='') { return get_the_time($format, $this); }
87+       function author() { $authordata = get_userdata($this->post_author); return $authordata->display_name; }
88+       function content() { return get_content($this); }
89+}
90+
91+class wp_comment extends wp_row {
92+       // Factory
93+       function get($comment) {
94+               if ( $comment = get_comment($comment) )
95+                       return new wp_comment($comment);
96+               else
97+                       return new WP_Error(404, "Comment not found.");
98+       }
99+
100+       function id() { return $this->comment_ID; }
101+       function post_id() { return $this->comment_post_ID; }
102+       function type_id() { return 'comment-' . $this->comment_ID; }
103+       function classes($class='') { return join( ' ', get_comment_class( $class, $this->id() ) ); }
104+
105+       function permalink() { return get_comment_link($this); }
106+       function title() { return sprintf(__("Comment on %s"), get_the_title($this->post_id())); }
107+       function time($format='') { return mysql2date($format?$format:get_option('time_format'), $this->comment_date); }
108+       function date($format='') { return date($format?$format:get_option('date_format'), $this->time('U')); }
109+       function author() { return $this->comment_author; }
110+       function excerpt() { return $this->comment_content; }
111+       function content() { return get_comment_content($this); }
112+}
113+
114+function is_post($object) {
115+       return is_a('wp_post', $object);
116+}
117+
118+function is_comment($object) {
119+       return is_a('wp_comment', $object);
120+}
121+
122 ?>