forked from ithinkdancan/SBXWP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.php
130 lines (102 loc) · 2.25 KB
/
Template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace SBX;
use \WP_Query;
use SBX\MetaBox;
abstract class Template
{
protected static $templates = array();
/**
* Dynamically load a directory of templates
*
* @param string $dir
* @param string $namespace
* @return void
*/
public static function load($dir, $namespace)
{
$files = scandir($dir);
foreach($files as $file)
{
if(strpos($file, '.php') !== false)
{
$classname = $namespace . '\\' . substr($file, 0, strpos($file, '.'));
static::register(new $classname);
}
}
}
static public function register(Template $template)
{
if(isset(static::$templates[$template->path]))
throw new \Exception("Template already exists for path [{$template->path}]");
static::$templates[$template->path] = $template;
}
static public function get($path)
{
if(!isset(static::$templates[$path]))
throw new \Exception("No template exists for path [$path]");
return static::$templates[$path];
}
static public function has($path)
{
return isset(static::$templates[$path]);
}
static public function current()
{
global $post;
if($post)
{
$slug = get_page_template_slug($post->ID);
if(static::has($slug))
return static::get($slug);
if(is_single() && static::has('single.php'))
return static::get('single.php');
}
return null;
}
public $path;
public function __construct($path)
{
$this->path = $path;
add_action('init', array($this, 'metaboxes_init'));
add_action('widgets_init', array($this, 'widgets_init'), 11);
}
public function pages()
{
$query = new WP_Query(array(
'meta_key' => '_wp_page_template',
'meta_value' => $this->path,
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => 'page'
));
return $query->posts;
}
final public function metaboxes_init()
{
$this->metaboxes();
}
final public function widgets_init()
{
$this->sidebars();
}
public function sidebar($id, $page = null)
{
if(!$page)
{
global $post;
$page = $post;
}
dynamic_sidebar("{$page->post_name}-$id");
}
public function metabox($id, $page = null)
{
if(!$page)
{
global $post;
$page = $post;
}
return MetaBox::value($id, $page->ID);
}
abstract protected function metaboxes();
abstract protected function sidebars();
}