| | 710 | class WP_Object_Factory { |
| | 711 | /* static */ function create($object = null) { |
| | 712 | if ( is_a($object, 'WP_Object') ) |
| | 713 | return $object; |
| | 714 | |
| | 715 | if ( is_array($object) ) |
| | 716 | $object = (object) $object; |
| | 717 | |
| | 718 | if ( |
| | 719 | isset($object->post_type) && |
| | 720 | class_exists('WP_' . $object->post_type) |
| | 721 | ) { |
| | 722 | $object_type = 'WP_' . $object->post_type; |
| | 723 | } elseif ( |
| | 724 | isset($object->comment_type) && |
| | 725 | class_exists('WP_' . $object->comment_type) |
| | 726 | ) { |
| | 727 | $object_type = 'WP_' . $object->comment_type; |
| | 728 | } else { |
| | 729 | $object_type = false; |
| | 730 | } |
| | 731 | |
| | 732 | $object_type = apply_filters('wp_object_class', $object_type, $object); |
| | 733 | |
| | 734 | if ( false != $object_type ) { |
| | 735 | |
| | 736 | $_object = new $object_type($object); |
| | 737 | |
| | 738 | if ( is_subclass_of( $_object, 'WP_Object' ) ) { |
| | 739 | return $_object; |
| | 740 | } |
| | 741 | |
| | 742 | } |
| | 743 | |
| | 744 | return $object; |
| | 745 | } |
| | 746 | } |
| | 747 | |
| | 748 | /* abstract */ class WP_Object { |
| | 749 | var $_id; |
| | 750 | var $_title; |
| | 751 | var $_content; |
| | 752 | var $_object_type; |
| | 753 | |
| | 754 | function WP_Object($object = null) { |
| | 755 | return $this->__construct($object); |
| | 756 | } |
| | 757 | |
| | 758 | function __construct($object = null) { |
| | 759 | foreach( (array) $object as $n => $v ) |
| | 760 | $this->$n = $v; |
| | 761 | |
| | 762 | $mapped_properties = array( |
| | 763 | 'comment_content' => 'content', |
| | 764 | 'comment_date' => 'date', |
| | 765 | 'comment_date_gmt' => 'date_gmt', |
| | 766 | 'comment_ID' => 'id', |
| | 767 | 'ID' => 'id', |
| | 768 | 'post_content' => 'content', |
| | 769 | 'post_date' => 'date', |
| | 770 | 'post_date_gmt' => 'date_gmt', |
| | 771 | 'post_title' => 'title', |
| | 772 | ); |
| | 773 | |
| | 774 | $mapped_properties = apply_filters('wp_object_mapped_properties', $mapped_properties, $this->_object_type, $object); |
| | 775 | |
| | 776 | foreach( $mapped_properties as $db_prop => $obj_prop ) { |
| | 777 | if ( isset( $object->$db_prop ) ) { |
| | 778 | $this->set_property($obj_prop, $object->$db_prop); |
| | 779 | } |
| | 780 | } |
| | 781 | |
| | 782 | } |
| | 783 | |
| | 784 | function get_id() { |
| | 785 | return (int) $this->_id; |
| | 786 | } |
| | 787 | |
| | 788 | function get_title() { |
| | 789 | return $this->_title; |
| | 790 | } |
| | 791 | |
| | 792 | function get_content() { |
| | 793 | return $this->_content; |
| | 794 | } |
| | 795 | |
| | 796 | function get_object_type() { |
| | 797 | return $this->_object_type; |
| | 798 | } |
| | 799 | |
| | 800 | function set_property($name = '', $value = null) { |
| | 801 | $property_name = '_' . preg_replace('/[^a-z0-9_]/', '', strtolower($name)); |
| | 802 | $this->$property_name = $value; |
| | 803 | } |
| | 804 | } |
| | 805 | |
| | 806 | class WP_Page extends WP_Post { |
| | 807 | var $_object_type = 'page'; |
| | 808 | } |
| | 809 | |
| | 810 | class WP_Post extends WP_Object { |
| | 811 | var $_object_type = 'post'; |
| | 812 | |
| | 813 | function WP_Post($object = null) { |
| | 814 | return $this->__construct($object); |
| | 815 | } |
| | 816 | |
| | 817 | function __construct($object = null) { |
| | 818 | if ( is_numeric($object) ) { |
| | 819 | $object = get_post($object); |
| | 820 | } |
| | 821 | |
| | 822 | return parent::__construct($object); |
| | 823 | } |
| | 824 | } |
| | 825 | |
| | 826 | class WP_Comment extends WP_Object { |
| | 827 | var $_object_type = 'comment'; |
| | 828 | |
| | 829 | function WP_Comment($object = null) { |
| | 830 | return $this->__construct($object); |
| | 831 | } |
| | 832 | |
| | 833 | function __construct($object = null) { |
| | 834 | if ( is_numeric($object) ) { |
| | 835 | $object = get_comment($object); |
| | 836 | } |
| | 837 | |
| | 838 | return parent::__construct($object); |
| | 839 | } |
| | 840 | } |
| | 841 | |
| | 842 | |
| 1699 | | // A factory and constructor that upgrades stdClass "rows" to WordPress classes. |
| 1700 | | class 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 | | |
| 1744 | | class 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 | | |
| 1766 | | class 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 | | |
| 1789 | | function is_post($object) { |
| 1790 | | return is_a('wp_post', $object); |
| 1791 | | } |
| 1792 | | |
| 1793 | | function is_comment($object) { |
| 1794 | | return is_a('wp_comment', $object); |
| 1795 | | } |
| 1796 | | |