Commit a82bfe10 authored by Bogdan Mucenica's avatar Bogdan Mucenica

added post type model functionality

parent 3d58eadc
......@@ -44,9 +44,9 @@
static public function init() {
ob_start();
require_once(__DIR__ .'/autoload.php');
require_once(__DIR__ . '/autoload.php');
//Read config
self::$config = require_once(THEME_FRAMEWORK_APP_PATH . '/config.php');
......@@ -71,6 +71,7 @@
}
}
self::registerModels();
self::registerFiltersAndActions();
......@@ -127,6 +128,24 @@
}
}
static private function registerModels() {
$basePath = dirname(__FILE__);
$dir = THEME_FRAMEWORK_APP_PATH . '/models';
if (file_exists($dir)) {
foreach (scandir($dir) as $file) {
$path = $dir . '/' . $file;
if ($file != '.' && $file != '..' && is_file($path)) {
//$className = THEME_FRAMEWORK_APP_NAMESPACE . '\\Models\\' . substr($file, 0, -4);
//new $className();
require_once($path);
}
}
}
}
/**
*
* @param string $className
......@@ -660,9 +679,8 @@
$iframe = preg_replace($toReplace, $match[0] . $queryStr, $iframe);
}
print($iframe);
print($iframe);
}
}
<?php
namespace WordpressTheme\Model;
abstract class Base {
/**
*
* @var array
*/
private $cachedValues = array();
/**
*
* @var \WP_Post
*/
private $post;
/**
*
* @param int|\WP_Post $postIdOrPostObj
*/
public function __construct($postIdOrPostObj) {
if(is_numeric($postIdOrPostObj)) {
$this->post = get_post($postIdOrPostObj);
} elseif(is_object($postIdOrPostObj)) {
$this->post = $postIdOrPostObj;
}
}
/**
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments) {
if(empty($this->post)) {
return null;
}
$fieldName = lcfirst(substr($name, 3));
$postProperty = 'post_' . $fieldName;
if(isset($this->post->$postProperty)) {
return $this->post->$postProperty;
}
if(!isset($this->cachedValues[$fieldName])) {
$this->cachedValues[$fieldName] = get_field($fieldName, $this->post->ID);
}
return $this->cachedValues[$fieldName];
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPosts($arguments = array()) {
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
$posts = array();
foreach(get_posts($arguments) as $post) {
$posts []= new static($post);
}
return $posts;
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPost($arguments = array()) {
if(is_array($arguments)) {
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
}
$post = get_post($arguments);
return $post ? new static($post) : false;
}
static abstract protected function getPostType();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment