Skip to content

Commit

Permalink
Expanded use cases for Mvied_Post and Mvied_Model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Ems committed Feb 13, 2014
1 parent 063228d commit 55454bc
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 24 deletions.
56 changes: 37 additions & 19 deletions Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,9 @@ class Mvied_Model {
* @param int $id
* @return void
*/
public function __construct( $id ) {
$this->setPost(new Mvied_Post($id));
$this->ID = $this->getPost()->ID;
$this->name = $this->getPost()->post_title;

$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $property) {
$property = $property->getName();
if ( !isset($this->$property) ) {
$this->$property = $this->getPost()->getPostMeta($property);
}
public function __construct( $id = null ) {
if ($id > 0) {
$this->setPost(new Mvied_Post($id));
}
}

Expand All @@ -59,13 +50,34 @@ public function __get( $property ) {
return $this->getPost()->getPostMeta($property);
}

/**
* Get Columns (Meta Data)
* @return array $columns
*/
public function getColumns() {
$columns = array();
$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $property) {
if (strpos($property, '_') !== 0) {
$columns[] = $property->getName();
}
}
return $columns;
}

/**
* Get Post
*
* @param none
* @return Mvied_Post
*/
public function getPost() {
if (!$this->_post && !$this->ID) {
$this->setPost(new Mvied_Post);
} else if ($this->ID) {
$this->setPost(new Mvied_Post($this->ID));
}
return $this->_post;
}

Expand All @@ -77,6 +89,13 @@ public function getPost() {
*/
public function setPost( Mvied_Post $post ) {
$this->_post = $post;
$this->ID = $post->ID;
$this->name = $post->post_title;
foreach($this->getColumns() as $column) {
if ($this->$column == null) {
$this->$column = $this->getPost()->getPostMeta($column);
}
}
return $this;
}

Expand All @@ -88,7 +107,8 @@ public function setPost( Mvied_Post $post ) {
*/
public function load( $array = array() ) {
foreach($array as $key => $value) {
if ( property_exists($this, $key) ) {
//if model has this column
if ( in_array($key, $this->getColumns()) ) {
$this->$key = $value;
}
}
Expand All @@ -101,12 +121,10 @@ public function load( $array = array() ) {
* @return void
*/
public function save() {
$reflect = new ReflectionClass($this);
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach($properties as $property) {
$property = $property->getName();
if ( !in_array($property, array('ID','name')) && strpos($property, '_') !== 0 ) {
$this->getPost()->updatePostMeta($property, $this->$property);
$this->getPost()->save();
foreach($this->getColumns() as $column) {
if ( !in_array($column, array('ID','name')) ) {
$this->getPost()->updatePostMeta($column, $this->$column);
}
}
}
Expand Down
119 changes: 114 additions & 5 deletions Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,122 @@
*/
class Mvied_Post {

/**
* @var int $ID Are you updating an existing post?
*/
public $ID;

/**
* @var string $post_title The title of your post.
*/
public $post_title;

/**
* @var string $post_content The full text of the post.
*/
public $post_content;

/**
* @var string $post_name The name (slug) for your post
*/
public $post_name;

/**
* @var string $post_status [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] Default 'draft'.
*/
public $post_status;

/**
* @var string $post_type [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] Default 'post'
*/
public $post_type;

/**
* @var int $post_author The user ID number of the author. Default is the current user ID.
*/
public $post_author;

/**
* @var string $ping_status [ 'closed' | 'open' ] Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
*/
public $ping_status;

/**
* @var int $post_parent Sets the parent of the new post, if any. Default 0.
*/
public $post_parent;

/**
* @var int $menu_order If new post is a page, sets the order in which it should appear in supported menus. Default 0.
*/
public $menu_order;

/**
* @var string $to_ping Space or carriage return-separated list of URLs to ping. Default empty string.
*/
public $to_ping;

/**
* @var string $pinged Space or carriage return-separated list of URLs that have been pinged. Default empty string.
*/
public $pinged;

/**
* @var string $post_password Password for post, if any. Default empty string.
*/
public $post_password;

/**
* @var string $post_excerpt For all your post excerpt needs.
*/
public $post_excerpt;

/**
* @var string $post_date [ Y-m-d H:i:s ] The time post was made.
*/
public $post_date;

/**
* @var string $post_date_gmt [ Y-m-d H:i:s ] The time post was made, in GMT.
*/
public $post_date_gmt;

/**
* @var string $comment_status [ 'closed' | 'open' ] Default is the option 'default_comment_status', or 'closed'.
*/
public $comment_status;

/**
* @var array $post_category [ array(<category id>, ...) ] Default empty.
*/
public $post_category;

/**
* @var string|array $tags_input [ '<tag>, <tag>, ...' | array ] Default empty.
*/
public $tags_input;

/**
* @var string|array $tax_input [ array( <taxonomy> => <array | string> ) ] For custom taxonomies. Default empty.
*/
public $tax_input;

/**
* @var string $page_template Default empty.
*/
public $page_template;

/**
* Instantiate Post from ID
*
* @param int $id
* @throws Exception
* @return void
*/
public function __construct( $id ) {
if ( ! isset($id) || ! get_post($id) ) {
throw new Exception("Invalid Post ID '" . $id . "'.");
public function __construct( $id = null) {
if ( $id ) {
$this->load( get_post($id, ARRAY_A) );
}
$this->load( get_post($id, ARRAY_A) );
}

/**
Expand Down Expand Up @@ -71,7 +176,11 @@ public function load( $array = array() ) {
* @return void
*/
public function save() {
return wp_update_post( $this->toArray() );
if ($this->ID) {
return wp_update_post( $this->toArray() );
} else {
return $this->ID = wp_insert_post( $this->toArray() );
}
}

}

0 comments on commit 55454bc

Please sign in to comment.