Last active
August 29, 2015 14:00
-
-
Save ScreamingDev/11138455 to your computer and use it in GitHub Desktop.
Make WordPress feel like Magento - Post vs. VarienObject
This file contains hidden or 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
<?php | |
namespace Oowp; | |
/** | |
* Class Oowp_AbstractPost | |
* | |
* @method getPostContent() | |
* @method getPostTitle() | |
* @method setPostContent($string) | |
* @method setPostTitle($string) | |
*/ | |
abstract class AbstractPost | |
{ | |
const POST_TYPE = 'post'; | |
protected $_postFacade; | |
protected $_metaData; | |
public function __construct() | |
{ | |
$this->reset(); | |
} | |
protected static $_underscoreCache; | |
protected function _underscore($name) | |
{ | |
if (!isset(self::$_underscoreCache[$name])) | |
{ | |
self::$_underscoreCache[$name] = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)); | |
} | |
return self::$_underscoreCache[$name]; | |
} | |
protected function _camelize($name) | |
{ | |
return str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); | |
} | |
public function __call($func, $args) | |
{ | |
$target = $this->_underscore(substr($func, 3)); | |
switch (substr($func, 0, 3)) | |
{ | |
case 'get': | |
return $this->getData($target); | |
break; | |
case 'set': | |
$this->setData($target, $args[0]); | |
break; | |
} | |
} | |
public function getData($fieldName) | |
{ | |
if (isset($this->getFacade()->$fieldName)) | |
{ | |
return $this->$fieldName; | |
} | |
return $this->_getMeta($fieldName); | |
} | |
public function setData($fieldName, $fieldValue) | |
{ | |
if (isset($this->getFacade()->$fieldName)) | |
{ | |
$this->$fieldName = $fieldValue; | |
} | |
else | |
{ | |
return $this->_metaData[$fieldName] = $fieldValue; | |
} | |
} | |
public function getField($fieldName) | |
{ | |
return $this->getFacade()->$fieldName; | |
} | |
public function setField($fieldName, $fieldValue) | |
{ | |
$this->getFacade()->$fieldName = $fieldValue; | |
} | |
public function getId() | |
{ | |
return $this->getFacade()->ID; | |
} | |
/** | |
* Load a single post. | |
* | |
* @param $id | |
* | |
* @return bool | |
*/ | |
public function load($id) | |
{ | |
if (null !== $id) | |
{ | |
$this->_postFacade = get_post($id); | |
return (bool) $this->_postFacade->ID; | |
} | |
return false; | |
} | |
public function loadByArgs($args) | |
{ | |
$post = current( | |
get_posts( | |
array( | |
'post_type' => static::POST_TYPE, | |
'posts_per_page' => 1, | |
'post_status' => 'any', | |
) + $args | |
) | |
); | |
if ($post) | |
{ | |
$this->load($post->ID); | |
} | |
return $this->getId(); | |
} | |
public function loadByAttribute($key, $value) | |
{ | |
if (!isset($this->getFacade()->$key)) | |
{ | |
return $this->loadByArgs( | |
array( | |
'meta_key' => $key, | |
'meta_value' => $value, | |
) | |
); | |
} | |
return $this->loadByArgs(array($key => $value)); | |
} | |
public function loadByField($fieldName, $fieldValue) | |
{ | |
return $this->loadByArgs( | |
array( | |
$fieldName => $fieldValue | |
) | |
); | |
} | |
public function reset() | |
{ | |
$this->_metaData = array(); | |
$this->_postFacade = new \WP_Post(new \StdClass()); | |
} | |
public function save() | |
{ | |
if (!$this->getId()) | |
{ | |
$id = wp_insert_post( | |
array( | |
'post_type' => static::POST_TYPE, | |
) | |
); | |
$this->load($id); | |
} | |
if ($this->getFacade() instanceof \WP_Post) | |
{ | |
wp_update_post($this->getFacade()->to_array()); | |
} | |
foreach ($this->_metaData as $index => $value) | |
{ | |
update_post_meta($this->getId(), $index, $value); | |
} | |
} | |
public function truncate() | |
{ | |
foreach ($this->getMetaSet() as $key) | |
{ | |
delete_post_meta($this->getId(), $key); | |
} | |
$this->reset(); | |
} | |
/** | |
* @param $index | |
* @param $newValue | |
*/ | |
public function setMeta($index, $newValue) | |
{ | |
$this->_metaData[$index] = $newValue; | |
} | |
protected function _getMeta($index) | |
{ | |
if (!isset($this->_metaData[$index])) | |
{ | |
$this->_metaData[$index] = get_post_meta($this->getId(), $index, true); | |
} | |
return $this->_metaData[$index]; | |
} | |
/** | |
* @return WP_Post | |
*/ | |
public function getFacade() | |
{ | |
return $this->_postFacade; | |
} | |
public function getMetaSet() | |
{ | |
if ($this->getId()) | |
{ | |
$this->_metaData = get_post_meta($this->getId()) + $this->_metaData; | |
} | |
return $this->_metaData; | |
} | |
public function applyTemplate($fileName = null) | |
{ | |
if (null === $fileName) | |
{ | |
$obj = new \ReflectionClass($this); | |
$classFile = $obj->getFileName(); | |
$fileName = str_replace('.php', '.phtml', $classFile); | |
} | |
ob_start(); | |
include $fileName; | |
$this->setPostContent(ob_get_clean()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment