Skip to content

Commit

Permalink
title and content virtual properties, code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonybudd committed Jun 9, 2017
1 parent 15d25b6 commit ab6de22
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 44 deletions.
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ Class Product extends WP_Model

### Helper Properties

The $new property will return true if the model has not been saved in the Database yet.
The $new property will return true if the model has not been saved in the database yet.

The $dirty property will return true if the data in the model is different from what's currently stored in the database.

Expand All @@ -456,6 +456,8 @@ $product->content; // Post content

```

***

### Helper Methods

```php
Expand Down Expand Up @@ -495,6 +497,8 @@ Product::asList('post_title')
]
```

***

## Taxonomies

If you would like to have any taxonomies loaded into the model, add the optional public property $taxonomies (array of taxonomy slugs) to the class.
Expand Down
103 changes: 60 additions & 43 deletions src/WP_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* record, eloquent-esque models of WordPress Posts.
*
* @author AnthonyBudd <anthonybudd94@gmail.com>
* @todo date format,
*/
Abstract Class WP_Model implements JsonSerializable
{
Expand All @@ -15,8 +16,6 @@
public $ID;
public $_post;
public $_where;
public $title;
public $content;

public $attributes = [];
public $prefix = '';
Expand All @@ -31,7 +30,6 @@
public $booted = FALSE;



/**
* Create a new instace with data
*
Expand Down Expand Up @@ -61,15 +59,9 @@ public function __construct(Array $insert = [])
}
}
}

if(!empty($insert['title'])){
$this->title = $insert['title'];
}

if(!empty($insert['content'])){
$this->content = $insert['content'];
}


$this->set('title', isset($insert['title'])? $insert['title'] : '');
$this->set('content', isset($insert['content'])? $insert['content'] : '');
$this->boot();
}

Expand All @@ -85,8 +77,8 @@ protected function boot()
if(!empty($this->ID)){
$this->new = FALSE;
$this->_post = get_post($this->ID);
$this->title = $this->_post->post_title;
$this->content = $this->_post->post_content;
$this->set('title', $this->_post->post_title);
$this->set('content', $this->_post->post_content);

foreach($this->attributes as $attribute){
$meta = $this->getMeta($attribute);
Expand Down Expand Up @@ -137,7 +129,8 @@ public static function register($args = [])
*
* @param array $insert
*/
public static function insert(Array $insert = []){
public static function insert(Array $insert = [])
{
return Self::newInstance($insert)->save();
}

Expand Down Expand Up @@ -187,14 +180,15 @@ public static function removeHooks()

/**
* save_post hook: Triggers save method for a given post
*
* Note: Self::exists() checks if the post is of the correct post type
*
* @param int $ID
* @return void
*/
public static function onSave($ID)
{
if(get_post_status($ID) == 'publish' &&
Self::exists($ID)){ // If post is the right post type
if(get_post_status($ID) === 'publish' && Self::exists($ID)){
$post = Self::find($ID);
$post->save();
}
Expand All @@ -209,13 +203,15 @@ public static function onSave($ID)
*
* @return object
*/
protected static function newWithoutConstructor(){
protected static function newWithoutConstructor()
{
$class = get_called_class();
$reflection = new ReflectionClass($class);
return $reflection->newInstanceWithoutConstructor();
}

public static function extract($array, $column){
public static function extract($array, $column)
{
$return = [];

if(is_array($array)){
Expand All @@ -231,6 +227,11 @@ public static function extract($array, $column){
return $return;
}

private function getAttributes()
{
return array_merge($this->attributes, ['title', 'content']);
}

/**
* Returns the post type
*
Expand Down Expand Up @@ -355,7 +356,8 @@ function_exists($this->filter[$attribute])) {
* @param string meta_key
* @return string
*/
public function getMeta($key){
public function getMeta($key)
{
return get_post_meta($this->ID, ($this->prefix.$key), TRUE);
}

Expand All @@ -366,7 +368,8 @@ public function getMeta($key){
* @param string meta_value
* @return void
*/
public function setMeta($key, $value){
public function setMeta($key, $value)
{
update_post_meta($this->ID, ($this->prefix.$key), $value);
}

Expand All @@ -376,7 +379,8 @@ public function setMeta($key, $value){
* @param string meta_key
* @return void
*/
public function deleteMeta($key){
public function deleteMeta($key)
{
delete_post_meta($this->ID, ($this->prefix.$key));
}

Expand Down Expand Up @@ -411,7 +415,7 @@ public function get($attribute, $default = NULL)
*/
public function set($attribute, $value)
{
if(in_array($attribute, $this->attributes)){
if(in_array($attribute, $this->getAttributes())){
$this->data[$attribute] = $value;
}
}
Expand All @@ -428,7 +432,7 @@ public function __set($attribute, $value)
$this->dirty = true;
}

if(in_array($attribute, $this->attributes)){
if(in_array($attribute, $this->getAttributes())){
$this->set($attribute, $value);
}else if(isset($this->taxonomies) && in_array($attribute, $this->taxonomies)){
$this->setTaxonomy($attribute, $value);
Expand All @@ -440,10 +444,11 @@ public function __set($attribute, $value)
*/
public function __get($attribute)
{
if(in_array($attribute, $this->attributes)){
if(in_array($attribute, $this->getAttributes())){
if($this->isFilterProperty($attribute)){
return $this->getFilterProperty($attribute);
}

return $this->get($attribute);
}else if($this->isVirtualProperty($attribute)){
return $this->getVirtualProperty($attribute);
Expand Down Expand Up @@ -548,7 +553,8 @@ public function removeTaxonomies($attribute, Array $taxonomies)
/**
* @return void
*/
public function clearTaxonomy($taxonomy){
public function clearTaxonomy($taxonomy)
{
$this->addTaxonomies($taxonomy, []);
}

Expand All @@ -567,7 +573,7 @@ public static function exists($ID, $postTypeSafe = TRUE)
if($postTypeSafe){
if(
(get_post_status($ID) !== FALSE) &&
(get_post_type($ID) == Self::getPostType())){
(get_post_type($ID) === Self::getPostType())){
return TRUE;
}
}else{
Expand Down Expand Up @@ -718,12 +724,14 @@ public static function findBypassBoot($ID)
* @param integer $limit
* @return Array
*/
public static function mostRecent($limit = 10){
public static function mostRecent($limit = 10)
{
$class = get_called_class();
return $class::finder('MostRecent__', ['limit' => $limit]);
}

public static function _finderMostRecent__($args){
public static function _finderMostRecent__($args)
{
return [
'posts_per_page' => (isset($args['limit'])? $args['limit'] : 3),
];
Expand Down Expand Up @@ -840,13 +848,18 @@ public static function where($key, $value = FALSE)
$params = [
'post_type' => Self::getPostType()
];
if ( is_array( $value ) ) {
$params = array_merge( $params, $value );

if(is_array($value)){
$params = array_merge($params, $value);
}

if(is_array($key)){
if (!$params['meta_query']) { $params['meta_query'] = []; }
if (!$params['tax_query']) { $params['tax_query'] = []; }
if(!isset($params['meta_query'])){
$params['meta_query'] = [];
}
if(!isset($params['tax_query'])){
$params['tax_query'] = [];
}

foreach($key as $key_ => $meta){
if($key_ === 'meta_relation'){
Expand All @@ -869,7 +882,6 @@ public static function where($key, $value = FALSE)
];
}
}

}else{
$params['meta_query'] = [
[
Expand All @@ -886,7 +898,6 @@ public static function where($key, $value = FALSE)
foreach($query->get_posts() as $key => $post){
$arr[] = Self::find($post->ID);
}

return $arr;
}

Expand All @@ -912,19 +923,21 @@ public static function in($ids = [])
// -----------------------------------------------------
// Query
// -----------------------------------------------------
public static function query(){
public static function query()
{
$model = Self::newWithoutConstructor();
$model->_where = [];
return $model;
}

public function params( $extra = array() ) {
public function params($extra = [])
{
$this->_params = $extra;

return $this;
}

public function meta($key, $x, $y = NULL, $z = NULL){
public function meta($key, $x, $y = NULL, $z = NULL)
{
if(!is_null($z)){
$this->_where[] = [
'key' => $key,
Expand All @@ -948,7 +961,8 @@ public function meta($key, $x, $y = NULL, $z = NULL){
return $this;
}

public function tax($taxonomy, $x, $y = NULL, $z = NULL){
public function tax($taxonomy, $x, $y = NULL, $z = NULL)
{
if(!is_null($z)){
$this->_where[] = [
'taxonomy' => $taxonomy,
Expand All @@ -975,11 +989,13 @@ public function tax($taxonomy, $x, $y = NULL, $z = NULL){
return $this;
}

public function execute(){
public function execute()
{
return Self::where($this->_where, $this->_params);
}

public function executeAsoc($key){
public function executeAsoc($key)
{
$models = Self::where($this->_where, $this->_params);
return Self::asList($key, $models);
}
Expand Down Expand Up @@ -1086,7 +1102,8 @@ public function hardDelete()
/**
* @return void
*/
public static function restore($ID){
public static function restore($ID)
{
wp_untrash_post($ID);
return Self::find($ID);
}
Expand Down
25 changes: 25 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,32 @@ function test(){
error(__LINE__ .' $default');
}

// -----------------------------------------------------
// VIRTUAL PROPERTIES
// -----------------------------------------------------
$product = Product::insert([
'title' => 'title',
'content' => 'content'
]);

if(! ($product->title === 'title') ){
error(__LINE__ .' VIRTUAL PROPERTIES');
}

if(! ($product->content === 'content') ){
error(__LINE__ .' VIRTUAL PROPERTIES');
}

$product->save();
$product = Product::find($product->ID);

if(! ($product->title === 'title') ){
error(__LINE__ .' VIRTUAL PROPERTIES');
}

if(! ($product->content === 'content') ){
error(__LINE__ .' VIRTUAL PROPERTIES');
}

// -----------------------------------------------------
// EVENTS
Expand Down

0 comments on commit ab6de22

Please sign in to comment.