Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonybudd committed Feb 15, 2017
1 parent 06f4fc4 commit 60c23ce
Show file tree
Hide file tree
Showing 3 changed files with 872 additions and 33 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $book->save();

Introduction: [Medium Post](https://medium.com/@AnthonyBudd/wp-model-6887e1a24d3c)

Advanced functionality: [Medium Post](https://medium.com/@AnthonyBudd/wp-model-advanced-b44f117617a7)
Advanced Functionality: [Medium Post](https://medium.com/@AnthonyBudd/wp-model-advanced-b44f117617a7)

***

Expand Down
121 changes: 89 additions & 32 deletions src/WP_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct(Array $insert = [])
$this->boot();
}


/**
* Load data into the model
*/
Expand Down Expand Up @@ -230,14 +231,56 @@ public function getMeta($key){
return get_post_meta($this->ID, ($this->prefix.$key), TRUE);
}

/**
* @return void
*/
public function setMeta($key, $value){
update_post_meta($this->ID, ($this->prefix.$key), $value);
}

/**
* @return void
*/
public function deleteMeta($key){
delete_post_meta($this->ID, ($this->prefix.$key));
}

/**
* @return void
*/
public function isVirtualProperty($attribute)
{
return (isset($this->virtual) &&
in_array($attribute, $this->virtual) &&
method_exists($this, ('_get'. ucfirst($attribute))));
}

/**
* @return void
*/
public function getVirtualProperty($attribute)
{
return call_user_func([$this, ('_get'. ucfirst($attribute))]);
}

/**
* @return void
*/
public function isFilterProperty($attribute)
{
return (isset($this->filter) &&
in_array($attribute, $this->filter) &&
method_exists($this, ('_filter'. ucfirst($attribute))));
}

/**
* @return void
*/
public function getFilterProperty($attribute)
{
return call_user_func_array([$this, ('_filter'. ucfirst($attribute))], [$this->get($attribute)]);
}


// -----------------------------------------------------
// GETTERS & SETTERS
Expand Down Expand Up @@ -266,6 +309,9 @@ public function set($attribute, $value)
$this->data[$attribute] = $value;
}

/**
* @return void
*/
public function getTaxonomy($attribute, $param = NULL)
{
if(isset($this->taxonomies) && isset($this->tax_data[$attribute])){
Expand All @@ -282,8 +328,7 @@ public function getTaxonomy($attribute, $param = NULL)
}

/**
* NOT DONE
* $value can be array of id's (ints) or array of slugs
* @return void
*/
public function addTaxonomy($taxonomy, $value)
{
Expand All @@ -306,8 +351,7 @@ public function addTaxonomy($taxonomy, $value)
}

/**
* NOT DONE
* $value can be array of id's (ints) or array of slugs
* @return void
*/
public function addTaxonomies($attribute, Array $taxonomies)
{
Expand All @@ -318,7 +362,7 @@ public function addTaxonomies($attribute, Array $taxonomies)
}

/**
* NOT DONE
* @return void
*/
public function removeTaxonomy($attribute, $value)
{
Expand All @@ -340,41 +384,23 @@ public function removeTaxonomy($attribute, $value)
}
}

/**
* @return void
*/
public function removeTaxonomies($attribute, Array $taxonomies)
{
foreach($taxonomies as $taxonomy){
$this->removeTaxonomy($attribute, $taxonomy);
}
}

/**
* @return void
*/
public function clearTaxonomy($taxonomy){
$this->addTaxonomies($taxonomy, []);
}

public function isVirtualProperty($attribute)
{
return (isset($this->virtual) &&
in_array($attribute, $this->virtual) &&
method_exists($this, ('_get'. ucfirst($attribute))));
}

public function getVirtualProperty($attribute)
{
return call_user_func([$this, ('_get'. ucfirst($attribute))]);
}

public function isFilterProperty($attribute)
{
return (isset($this->filter) &&
in_array($attribute, $this->filter) &&
method_exists($this, ('_filter'. ucfirst($attribute))));
}

public function getFilterProperty($attribute)
{
return call_user_func_array([$this, ('_filter'. ucfirst($attribute))], [$this->get($attribute)]);
}

// -----------------------------------------------------
// HELPER METHODS
// -----------------------------------------------------
Expand Down Expand Up @@ -408,6 +434,9 @@ public function post()
return $this->_post;
}

/**
* @return void
*/
public function hasFeaturedImage()
{
return (get_the_post_thumbnail_url($this->ID) !== FALSE)? TRUE : FALSE;
Expand Down Expand Up @@ -466,6 +495,9 @@ public static function single()
return Self::find(get_the_ID());
}

/**
* @return void
*/
public function permalink()
{
return get_permalink($this->ID);
Expand All @@ -474,6 +506,9 @@ public function permalink()
// -----------------------------------------------------
// MAGIC METHODS
// -----------------------------------------------------
/**
* @return void
*/
public function __set($attribute, $value)
{
if($this->booted){
Expand All @@ -487,6 +522,9 @@ public function __set($attribute, $value)
}
}

/**
* @return void
*/
public function __get($attribute)
{
if(in_array($attribute, $this->attributes)){
Expand Down Expand Up @@ -604,9 +642,7 @@ public static function asList($value = 'title', $models = FALSE)
}

/**
* [finder description]
* @param [type] $finder [description]
* @return [type] [description]
* @return void
*/
public static function finder($finder, Array $arguments = [])
{
Expand Down Expand Up @@ -635,6 +671,9 @@ public static function finder($finder, Array $arguments = [])
return $return;
}

/**
* @return void
*/
public static function where($key, $value = FALSE)
{
if(is_array($key)){
Expand Down Expand Up @@ -688,6 +727,9 @@ public static function where($key, $value = FALSE)
return $arr;
}

/**
* @return void
*/
public static function in($ids = [])
{
$results = [];
Expand Down Expand Up @@ -764,13 +806,19 @@ public function save()
// -----------------------------------------------------
// DELETE
// -----------------------------------------------------
/**
* @return void
*/
public function delete()
{
$this->triggerEvent('deleting');
wp_trash_post($this->ID);
$this->triggerEvent('deleted');
}

/**
* @return void
*/
public function hardDelete()
{
$this->triggerEvent('hardDeleting');
Expand All @@ -794,6 +842,9 @@ public function hardDelete()
$this->triggerEvent('hardDeleted');
}

/**
* @return void
*/
public static function restore($ID){
wp_untrash_post($ID);
return Self::find($ID);
Expand All @@ -802,6 +853,9 @@ public static function restore($ID){
// -----------------------------------------------------
// PATCHING
// -----------------------------------------------------
/**
* @return void
*/
public static function patchable($method = FALSE)
{
if(isset($_REQUEST['_model']) && $_REQUEST['_model'] === Self::getPostType()){
Expand All @@ -817,6 +871,9 @@ public static function patchable($method = FALSE)
}
}

/**
* @return void
*/
public function patch($method = FALSE)
{
$this->triggerEvent('patching');
Expand Down
Loading

0 comments on commit 60c23ce

Please sign in to comment.