| 1 | <?php |
| 2 | |
| 3 | // A base interface class with factory methods. |
| 4 | // Not to become an actual interface, nor an abstract class, |
| 5 | // because it can be instantiated in a last-ditch effort by the |
| 6 | // factory method to return something with the necessary methods. |
| 7 | class WP_Presentable { |
| 8 | // A factory method that upgrades stdClass "rows" to their corresponding Presentable classes. |
| 9 | // Learn about factories and other design patterns: http://www.ibm.com/developerworks/library/os-php-designptrns/ |
| 10 | /* public static */ function create($row) { |
| 11 | if ( is_a($row, __CLASS__) ) |
| 12 | return $row; |
| 13 | |
| 14 | if ( is_array($row) ) |
| 15 | $row = (object) $row; |
| 16 | |
| 17 | if ( isset($row->post_type) ) { |
| 18 | if ( empty($row->post_type) ) |
| 19 | $row->post_type = 'post'; |
| 20 | $class = __CLASS__ . "_$row->post_type"; |
| 21 | if ( !class_exists($class) ) |
| 22 | $class = __CLASS__ . '_post'; |
| 23 | } elseif ( isset($row->comment_type) ) { |
| 24 | if ( empty($row->comment_type) ) |
| 25 | $row->comment_type = 'comment'; |
| 26 | $class = __CLASS__ . "_$row->comment_type"; |
| 27 | if ( !class_exists($class) ) |
| 28 | $class = __CLASS__ . "_comment"; |
| 29 | } |
| 30 | |
| 31 | if ( function_exists("apply_filters") ) { |
| 32 | $filtered_class = apply_filters("WP_Presentable_class", $class, $row); |
| 33 | if ( class_exists($filtered_class) ) |
| 34 | $class = $filtered_class; |
| 35 | } |
| 36 | |
| 37 | return new $class($row); |
| 38 | } |
| 39 | |
| 40 | function WP_Presentable($row) { |
| 41 | return $this->__construct($row); |
| 42 | } |
| 43 | |
| 44 | // Takes an array or object and transfers its named properties to the new instance. |
| 45 | function __construct($row) { |
| 46 | if ( is_array($row) ) |
| 47 | $row = (object) $row; |
| 48 | |
| 49 | foreach ( (array) $row as $k => $v ) |
| 50 | $this->$k = $row->$k; |
| 51 | } |
| 52 | |
| 53 | // Interface |
| 54 | |
| 55 | function get_id() { } // post->ID, comment->comment_ID |
| 56 | function get_type() { } // post, page, comment, pingback |
| 57 | function get_supertype() { } // post, comment |
| 58 | |
| 59 | function get_date($format='') { } |
| 60 | function get_time($format='') { } |
| 61 | |
| 62 | function get_url() { } // permalink |
| 63 | function get_edit_url() { } |
| 64 | |
| 65 | function get_title() { } // post_title, "Comment on post_title" |
| 66 | function get_content() { } |
| 67 | function get_excerpt() { } |
| 68 | |
| 69 | function get_author_user_id() { } // int or false |
| 70 | function get_author_name() { } |
| 71 | function get_author_url() { } // from comment form or user profile |
| 72 | function get_author_link() { } |
| 73 | function get_author_archive_url() { } |
| 74 | function get_author_archive_link() { } |
| 75 | |
| 76 | function get_categories() { } |
| 77 | function get_tags() { } |
| 78 | function get_terms() { } |
| 79 | function get_classes($classes='') { } |
| 80 | } |
| 81 | |
| 82 | class WP_Presentable_post extends WP_Presentable { |
| 83 | function get_id() { return $this->ID; } |
| 84 | function get_type() { return $this->post_type; } |
| 85 | function get_supertype() { return 'post'; } |
| 86 | |
| 87 | function get_date($format='') { } |
| 88 | function get_time($format='') { } |
| 89 | |
| 90 | function get_url() { } // permalink |
| 91 | function get_edit_url() { } |
| 92 | |
| 93 | function get_title() { return get_the_title($this); } // post_title, "Comment on post_title" |
| 94 | function get_content() { } |
| 95 | function get_excerpt() { } |
| 96 | |
| 97 | function get_author_user_id() { } // int or false |
| 98 | function get_author_name() { } |
| 99 | function get_author_url() { } // from comment form or user profile |
| 100 | function get_author_link() { } |
| 101 | function get_author_archive_url() { } |
| 102 | function get_author_archive_link() { } |
| 103 | |
| 104 | function get_categories() { } |
| 105 | function get_tags() { } |
| 106 | function get_terms() { } |
| 107 | function get_classes($classes='') { } |
| 108 | } |
| 109 | |
| 110 | class WP_Presentable_comment extends WP_Presentable { |
| 111 | function get_id() { return $this->comment_ID; } |
| 112 | function get_type() { return $this->comment_type; } |
| 113 | function get_supertype() { return 'comment'; } |
| 114 | |
| 115 | function get_date($format='') { return date($format?$format:get_option('date_format'), $this->get_time('U')); } |
| 116 | function get_time($format='') { return mysql2date($format?$format:get_option('time_format'), $this->comment_date); } |
| 117 | |
| 118 | function get_url() { return get_comment_link($this); } // permalink |
| 119 | function get_edit_url() { } |
| 120 | |
| 121 | function get_title() { return sprintf(__("Comment on %s"), get_the_title($this->comment_post_ID)); } |
| 122 | function get_content() { return get_comment_text($this); } |
| 123 | function get_excerpt() { return get_comment_excerpt($this); } |
| 124 | |
| 125 | function get_author_user_id() { } // int or false |
| 126 | function get_author_name() { } |
| 127 | function get_author_url() { } // from comment form or user profile |
| 128 | function get_author_link() { } |
| 129 | function get_author_archive_url() { } |
| 130 | function get_author_archive_link() { } |
| 131 | |
| 132 | function get_categories() { } |
| 133 | function get_tags() { } |
| 134 | function get_terms() { } |
| 135 | function get_classes($classes='') { } |
| 136 | |
| 137 | function author() { return $this->comment_author; } |
| 138 | function excerpt() { return $this->comment_content; } |
| 139 | } |
| 140 | |
| 141 | function is_post($object) { |
| 142 | return is_a($object, 'WP_Presentable_post'); |
| 143 | } |
| 144 | |
| 145 | function is_comment($object) { |
| 146 | return is_a($object, 'WP_Presentable_comment'); |
| 147 | } |
| 148 | |