-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mike Ems
committed
Aug 11, 2012
1 parent
55bb2fe
commit d230488
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Model class for a WordPress. | ||
* | ||
* @author Mike Ems | ||
* @package Mvied | ||
*/ | ||
class Mvied_Model { | ||
|
||
protected $_post; | ||
|
||
public $ID; | ||
|
||
public $name; | ||
|
||
public function __construct( $id ) { | ||
if ( ! isset($id) ) { | ||
return $this; | ||
} | ||
|
||
$this->_post = get_post($id); | ||
$this->ID = $this->_post->ID; | ||
$this->name = $this->_post->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 = get_post_meta($this->ID, $property, true); | ||
} | ||
} | ||
} | ||
|
||
public function __get( $property ) { | ||
return get_post_meta($this->ID, $property, true); | ||
} | ||
|
||
public function getPost() { | ||
return $this->_post; | ||
} | ||
|
||
public function load( $array = array() ) { | ||
foreach($array as $key => $value) { | ||
if ( property_exists($this, $key) ) { | ||
$this->$key = $value; | ||
} | ||
} | ||
} | ||
|
||
public function save() { | ||
$reflect = new ReflectionClass($this); | ||
$properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); | ||
foreach($properties as $property) { | ||
$property = $property->getName(); | ||
if ( strpos($property, '_') !== 0 ) { | ||
update_post_meta($this->_post->ID, $property, $this->$property); | ||
} | ||
} | ||
} | ||
|
||
} |